使用合适的“tslm”模型进行预测时出现“找不到对象”错误

时间:2016-11-23 15:40:44

标签: r time-series linear-regression forecasting lm

我正在尝试为我的时间序列实施一次性交叉验证,但却遇到了预测错误。

library('forecast')

data_aero <- c(579, 624, 651, 687,  745,  753,  844,  965, 1076, 1078, 1107)
data_railway <-c(1417, 1507, 1696, 1831, 1985, 1506, 1854, 2059, 2104, 1932, 1778)


data.ts <- ts(data=data.frame(aero = data_aero, railway = data_railway),  start = 2004)

st <- 2003
limit <- 2013
en <- 2014

data.ts.train <- window(data.ts, start=st, end=limit)
data.ts.test  <- window(data.ts, start=limit+1, end=en)


m <- tslm(aero~railway, data=data.ts.train)

直到这里一切都很好,但是如果我做的话

forecast(m, h=1)

我收到错误

Error in eval(expr, envir, enclos) : object 'railway' not found

1 个答案:

答案 0 :(得分:3)

您还需要指定railway

forecast(m, h = 1, newdata = data.frame(railway = 1))
#     Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
#2014      -244.3887 -690.9415 202.1641 -981.6033 492.8259

如果您提前{1}}预测,则需要为所有协变量指定k个新值。在上文中,我以k为例。

请注意,railway = 1是一个通用函数,有很多方法:

forecast

如果你适合&#34; tslm&#34;,你有:

methods(forecast)
# [1] forecast.ar          forecast.Arima       forecast.bats       
# [4] forecast.default*    forecast.ets         forecast.forecast*  
# [7] forecast.fracdiff    forecast.HoltWinters forecast.lm         
#[10] forecast.mlm*        forecast.mts*        forecast.nnetar     
#[13] forecast.stl         forecast.stlm        forecast.StructTS   
#[16] forecast.tbats       forecast.ts*         forecast.varest*
因此,class(m) # [1] "lm" 被调用。您似乎非常沉迷于forecast.lm,只需设置forecast.Arima。阅读h