使用 tslm 进行预测

Forecasting using a tslm

我使用 tslm 创建了一个时间序列线性模型。它是使用每周数据的 179 次观察构建的,我想预测接下来的 26 周,但不断出现错误:

tslm5<-tslm(tsorders~ trend +
          I(trend^2) + Month.number, data=ppc.order.forecasting[1:179,])

forecast(tslm5,newdata=ppc.order.forecasting[180:205,])

Error in `[[<-.data.frame`(`*tmp*`, length(tmpdata) + 1, value = c(1,  : 
  replacement has 179 rows, data has 26'

如何使用行 180:205 和 tslm5 中的数据来预测接下来的 26 周?

如果没有关于您的软件包版本和 ppc.order.forecasting 性质的更多信息,就不可能知道是什么导致了您的错误。

以下示例使用 forecast 包的最新版本 (8.4)。

library(forecast)
library(ggplot2)

ppc.order.forecasting <- cbind(
  trend = 1:205,
  Month.number = rep(1:12,18)[1:205],
  tsorders = rpois(205, abs((1:205)/50 + 2*sin(2*pi*(1:205)/12)))
) %>% ts(freq=12)
tslm5 <- tslm(tsorders~ trend + I(trend^2) + Month.number, 
              data=subset(ppc.order.forecasting, end=179))

forecast(tslm5, newdata=subset(ppc.order.forecasting, start=180)[,1:2]) %>%
  autoplot()