我有一个月度数据集test
,在绘制时,如下所示:
我的目标是为数据拟合回归线并创建未来月份的预测(从6个月开始)。我咨询了this reference,它实现了forecast
包。
但是,当我运行tslm()
时,我会收到以下错误:
tslm(test$Freq)
Error in formula.default(object, env = baseenv()) : invalid formula
而且当我尝试将test
转换为时间序列对象时,我又收到了另一个错误。
as.Date(as.character(test$Month))
Error in charToDate(x) :
character string is not in a standard unambiguous format
非常感谢有关如何在给定数据集上正确实现tslm()
函数的任何建议。以下是数据样本:
> dput(test)
structure(list(Month = structure(1:31, .Label = c("2014-11",
"2014-12", "2015-01", "2015-02", "2015-03", "2015-04", "2015-05",
"2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11",
"2015-12", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05",
"2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11",
"2016-12", "2017-01", "2017-02", "2017-03", "2017-04", "2017-05"
), class = "factor"), Freq = c(3L, 15L, 863L, 1031L, 4242L, 11610L,
20605L, 21105L, 29533L, 27354L, 30573L, 24423L, 20216L, 13616L,
9674L, 10037L, 13959L, 18760L, 28121L, 34346L, 36099L, 35841L,
39318L, 30736L, 27517L, 12588L, 13599L, 14465L, 17142L, 26229L,
29484L)), .Names = c("Month", "Freq"), row.names = c(NA, -31L
), class = "data.frame")
答案 0 :(得分:1)
test2 <- ts(test$Freq, start= c(2014, 11), frequency = 12)
fit <- tslm(test2 ~ trend + season)
plot(forecast(fit, h=20))