我正在尝试在一些月度时间序列数据上运行一些模型。时间序列数据的长度不相等,也不是从同一月开始/结束。我拥有的是一个数字月份列和一个数字年份列。我从这两个变量中创建了一个时间序列,并从中制作了一个tsibble
,以便我可以使用该fable
包。这就是我正在做的处理时间序列数据,
我在这里发布模拟数据。
# Packages
library(tidyverse)
library(tsibble)
library(fable)
library(fabletools)
# Simulated data
id <- c(rep (222, 28), rep(111, 36), rep(555, 16))
year <- c(rep(2014, 12), rep(2015, 12), rep(2016, 4),
rep(2014, 12), rep(2015, 12), rep(2016, 12),
rep(2015, 12), rep(2016, 4))
mnt <- c(seq(1, 12, by = 1), seq(1, 12, by = 1), seq(1, 4, by = 1),
seq(1, 12, by = 1), seq(1, 12, by = 1), seq(1, 12, by = 1),
seq(1, 12, by = 1), seq(1, 4, by = 1))
value <- rnorm(80, mean = 123, sd = 50)
dataf <- data.frame(id, mnt, year, value)
为了使它成为一个tsibble
我正在将我的月份变量mnt
转换为一个字符,
dataf$mnt[dataf$mnt == 1] <- "Jan"
dataf$mnt[dataf$mnt == 2] <- "Feb"
dataf$mnt[dataf$mnt == 3] <- "Mar"
dataf$mnt[dataf$mnt == 4] <- "Apr"
dataf$mnt[dataf$mnt == 5] <- "May"
dataf$mnt[dataf$mnt == 6] <- "Jun"
dataf$mnt[dataf$mnt == 7] <- "Jul"
dataf$mnt[dataf$mnt == 8] <- "Aug"
dataf$mnt[dataf$mnt == 9] <- "Sep"
dataf$mnt[dataf$mnt == 10] <- "Oct"
dataf$mnt[dataf$mnt == 11] <- "Nov"
dataf$mnt[dataf$mnt == 12] <- "Dec"
将月份和年份加在一起
dataf %>% unite("time", mnt:year, sep = " ")
做个小事
tsbl <- as_tsibble(dataf, index = time, key = id)
在这一点上,我有这个错误,
> tsbl <- as_tsibble(dataf, index = time, key = id)
Error: `var` must evaluate to a single number or a column name, not a function
Call `rlang::last_error()` to see a backtrace.
剩下的代码是这样的,
# Fitting arima
fit <- tsbl %>%
fill_gaps(b = 0) %>%
model(
arima = ARIMA(value),
)
fit
# One month ahead forecast
fc <- fit %>%
forecast(h = 1)
fc
# Accuracy measure
accuracy_table <- accuracy(fit)
知道如何预处理我的数据以从fable
包中运行预测模型吗?