使用"tslm"返回尺寸误差进行预测 [英] Forecasting with `tslm` returning dimension error
问题描述
对于线性模型预测函数,我和这里的提问者有类似的问题,但是我试图使用Rob Hyndman的预测软件包中的时间序列线性模型"函数.
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.
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.
但是,我然后按如下方式拟合tslm
对象(时间序列线性模型)-
However, I then fit a tslm
object (time series linear model) as follows -
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?
好的,这是一个可行的示例,我使用了Irsal对这个问题的回答中给出的数据(转换为时间序列对象),然后拟合了tslm.我收到相同的错误(显然是不同的尺寸):-
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. I get the same error (different dimensions obviously):-
我对自己做错的事情感到非常困惑,我的代码看起来与在此示例中使用的代码完全相同....
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.
nData.df <- data.frame(nData, nData2)
dat.ts <- tslm(nData~nData2, data = nData.df)
forecast(dat.ts)
推荐答案
tslm
拟合线性回归模型.如果要预测,则需要提供解释变量的将来值.这些应通过forecast.lm
的newdata
参数提供.
tslm
fits a linear regression model. 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
.
这篇关于使用"tslm"返回尺寸误差进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!