将回归线拟合到 R 中的时间序列数据

Fitting regression line to timeseries data in R

我有一个月度数据集 test,绘制时看起来像这样:

我的 objective 是为数据拟合回归线并创建未来月份的预测(首先是 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")
test2 <- ts(test$Freq, start= c(2014, 11), frequency = 12)
fit <- tslm(test2 ~ trend + season)
plot(forecast(fit, h=20))