简体   繁体   English

使用“ tslm”返回尺寸误差进行预测

[英]Forecasting with `tslm` returning dimension error

I'm having a similar problem to the questioners here had with the linear model predict function, but I am trying to use the "time series linear model" function from Rob Hyndman's forecasting package. 我在这里与线性模型预测函数的提问者有类似的问题,但是我试图使用Rob Hyndman的预测软件包中的“时间序列线性模型”函数。

Predict.lm in R fails to recognize newdata R中的Predict.lm无法识别新数据

predict.lm with newdata 带newdata的predict.lm

totalConv <- ts(varData[,43])
metaSearch <- ts(varData[,45])
PPCBrand <- ts(varData[,38])
PPCGeneric <- ts(varData[,34])
PPCLocation <- ts(varData[,35])
brandDisplay <- ts(varData[,29])
standardDisplay <- ts(varData[,3])
TV <- ts(varData[,2])
richMedia <- ts(varData[,46])

df.HA <- data.frame(totalConv, metaSearch,  
            PPCBrand, PPCGeneric, PPCLocation,
            brandDisplay, standardDisplay, 
            TV, richMedia)

As you can see I've tried to avoid the names issues by creating a data frame of the time series objects. 如您所见,我试图通过创建时间序列对象的数据框来避免名称问题。

However, I then fit a tslm object (time series linear model) as follows - 但是,然后我按如下方式拟合tslm对象(时间序列线性模型)-

fit1 <- tslm(totalConv ~ metaSearch  
             + PPCBrand + PPCGeneric + PPCLocation 
             + brandDisplay + standardDisplay 
             + TV + richMedia data = df.HA
             )

Despite having created a data frame and named all the objects properly I get the same dimension error as these other users have experienced. 尽管已经创建了一个数据框并正确命名了所有对象,但我遇到了与其他用户相同的尺寸错误。

Error in forecast.lm(fit1) : Variables not found in newdata
In addition: Warning messages:
1: 'newdata' had 10 rows but variables found have 696 rows 
2: 'newdata' had 10 rows but variables found have 696 rows

the model frame seems to give sensible names to all of the variables, so I don't know what is up with the forecast function:- 模型框架似乎为所有变量都赋予了明智的名称,所以我不知道预测函数的作用:-

names(model.frame(fit1))
[1] "totalConv"       "metaSearch"      "PPCBrand"        "PPCGeneric"      "PPCLocation"     "brandDisplay"   
[7] "standardDisplay" "TV"              "richMedia" 

Can anyone suggest any other improvements to my model specification that might help the forecast function to run? 谁能对我的模型规格提出其他建议,以帮助运行预测功能?

EDIT 1: Ok, just so there's a working example, I've used the data given in Irsal's answer to this question (converting to time series objects) and then fitted the tslm. 编辑1:好的,这是一个可行的示例,我使用了Irsal对此问题的答案中给出的数据(转换为时间序列对象),然后拟合了tslm。 I get the same error (different dimensions obviously):- 我收到相同的错误(显然是不同的尺寸):-

Is there an easy way to revert a forecast back into a time series for plotting? 是否有一种简单的方法可以将预测恢复为时间序列进行绘图?

I'm really confused about what I'm doing wrong, my code looks identical to that used in all of the examples on this.... 我对自己做错的事情感到非常困惑,我的代码看起来与在所有示例中使用的代码相同。

data <- c(11,53,50,53,57,69,70,65,64,66,66,64,61,65,69,61,67,71,74,71,77,75,85,88,95,
           93,96,89,95,98,110,134,127,132,107,94,79,72,68,72,70,66,62,62,60,59,61,67,
           74,87,112,134,51,50,38,40,44,54,52,51,48,50,49,49,48,57,52,53,50,50,55,50,
           55,60,65,67,75,66,65,65,69,72,93,137,125,110,93,72,61,55,51,52,50,46,46,45,
           48,44,45,53,55,65,89,112,38,7,39,35,37,41,51,53,57,52,57,51,52,49,48,48,51,
           54,48,50,50,53,56,64,71,74,66,69,71,75,84,93,107,111,112,90,75,62,53,51,52,
           51,49,48,49,52,50,50,59,58,69,95,148,49,83,40,40,40,53,57,54,52,56,53,55,
           55,51,54,45,49,46,52,49,50,57,58,63,73,66,63,72,72,71,77,105,97,104,85,73,
           66,55,52,50,52,48,48,46,48,53,49,58,56,72,84,124,76,4,40,39,36,38,48,55,49,
           51,48,46,46,47,44,44,45,43,48,46,45,50,50,56,62,53,62,63)

 data2 <- c(rnorm(237))


library(forecast)



 nData <- ts(data)
 nData2 <- ts(data2)
 dat.ts <- tslm(nData~nData2)
 forecast(dat.ts)
Error in forecast.lm(dat.ts) : Variables not found in newdata
In addition: Warning messages:
1: 'newdata' had 10 rows but variables found have 237 rows 
2: 'newdata' had 10 rows but variables found have 237 rows 

EDIT 2: Same error even if I combine both series into a data frame. 编辑2:即使我将两个系列合并到一个数据框中,也会出现相同的错误。

nData.df <- data.frame(nData, nData2)
dat.ts <- tslm(nData~nData2, data = nData.df)
forecast(dat.ts)

tslm fits a linear regression model. tslm适合线性回归模型。 You need to provide the future values of the explanatory variables if you want to forecast. 如果要预测,则需要提供解释变量的将来值。 These should be provided via the newdata argument of forecast.lm . 这些应通过forecast.lmnewdata参数提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM