Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
forecast.lm始终预测前面相同的时间段_R_Linear Regression_Forecasting_Predict - Fatal编程技术网

forecast.lm始终预测前面相同的时间段

forecast.lm始终预测前面相同的时间段,r,linear-regression,forecasting,predict,R,Linear Regression,Forecasting,Predict,我想预测一个线性模型,我用ols估计。但是,它总是预测前面相同的时间段,与我的数据集长度相同 favar <- ts.union(factors, data) colnames(favar) <- c(colnames(factors), "a", "b", "c", "d", "e", "f") forecast(model, newdata=favar, h=6, ts=T) 这就是我所做的 data <- ts(matrix(rnorm(144, mean=0, sd

我想预测一个线性模型,我用ols估计。但是,它总是预测前面相同的时间段,与我的数据集长度相同

favar <- ts.union(factors, data)
colnames(favar) <- c(colnames(factors), "a", "b", "c", "d", "e", "f")
forecast(model, newdata=favar, h=6, ts=T)
这就是我所做的

data <- ts(matrix(rnorm(144, mean=0, sd=1), ncol=6), start=c(2007,1), frequency=12)
无论我为
h设置了什么值
,结果总是提前24个月预测。我想,问题出现了,因为我必须提供
newdata
,我使用了原始数据集
favar
。但是,如果我尝试在没有它的情况下预测模型,我会得到以下错误:

eval(expr、envir、enclose)中出错:找不到对象“f1”

我已经尝试使用
predict.lm
进行预测,并且仅使用
lm
而不是
tslm
对模型进行估计。在任何情况下,我都面临同样的问题:预测期总是与提供的
新数据的长度相同

更新:我刚刚注意到,不仅我的预测长度与我的数据集相同,而且值也相同。基本上,我只有一份原始数据的副本

谢谢您的帮助。

forecast(model,newdata=favar,h=6,ts=T)
调用
forecast.lm
。 从
forecast.lm
的文档中:

新数据

一个可选的数据框,在其中查找要使用的变量 预测如果省略,则假定唯一变量为趋势 和季节,并生成h预测

h

预测的周期数。如果存在新数据,则忽略

这就是错误的原因

> forecast(model,h=6,ts=T)
Error in eval(expr, envir, enclos) : object 'f1' not found
> 
唯一已知的变量是
趋势
季节
,而不是
f1
f2
。等 因此
newdata
不能丢失,因此
h
被忽略

我害怕从
forecast
方法获得长度为6的预测。您需要一些长度为6的
新数据。然后在这6个点上计算由系数
coef(model)
确定的线性函数

当然你可以问系数

> coef(model)
       f1        f2         b         c         d         e         f 
 2.008211  1.344910 -0.532548 -1.375166  0.378199  2.169784 -1.971422 
并在不使用
forecast
方法的情况下使用它们

> myData <- X[1:6,-3] + matrix(sample(-100:100,6*7,,replace=TRUE)/100,6,7)
> myData
             f1         f2          b          c          d          e          f
[1,]  1.3901181  0.5794323  0.2638713  1.7911077 -1.9140976 -0.1632654  1.2130388
[2,] -0.5106604  1.0037957 -0.5357955  1.1981059 -0.3636334 -1.2746126 -0.1845794
[3,]  2.0191347 -0.8724608 -1.7707524  0.2779736  1.2814462 -0.4834006  0.1504435
[4,]  1.4574348  0.2173202 -1.1881501  0.7911197 -0.7332919 -1.0103667 -0.8201907
[5,] -1.8129340  0.2294362  0.7379416 -1.3893631  0.5011054  0.4321159  0.4026663
[6,]  1.9659584  1.8596798  0.7286796  1.9930237  0.6643413 -0.2609216 -0.2635644
> fcst <- myData %*% coef(model)
> fcst
          [,1]
[1,] -2.502231
[2,] -3.577032
[3,]  2.581397
[4,]  1.911273
[5,] -1.481277
[6,]  3.525071
> forecast(model,myData,ts=T)
         Point Forecast     Lo 80     Hi 80     Lo 95     Hi 95
Jan 2009      -2.502231 -2.502231 -2.502231 -2.502231 -2.502231
Feb 2009      -3.577032 -3.577032 -3.577032 -3.577032 -3.577032
Mar 2009       2.581397  2.581397  2.581397  2.581397  2.581397
Apr 2009       1.911273  1.911273  1.911273  1.911273  1.911273
May 2009      -1.481277 -1.481277 -1.481277 -1.481277 -1.481277
Jun 2009       3.525071  3.525071  3.525071  3.525071  3.525071
> 
>我的数据我的数据
f1 f2 b c d e f
[1,]  1.3901181  0.5794323  0.2638713  1.7911077 -1.9140976 -0.1632654  1.2130388
[2,] -0.5106604  1.0037957 -0.5357955  1.1981059 -0.3636334 -1.2746126 -0.1845794
[3,]  2.0191347 -0.8724608 -1.7707524  0.2779736  1.2814462 -0.4834006  0.1504435
[4,]  1.4574348  0.2173202 -1.1881501  0.7911197 -0.7332919 -1.0103667 -0.8201907
[5,] -1.8129340  0.2294362  0.7379416 -1.3893631  0.5011054  0.4321159  0.4026663
[6,]  1.9659584  1.8596798  0.7286796  1.9930237  0.6643413 -0.2609216 -0.2635644
>fcst fcst
[,1]
[1,] -2.502231
[2,] -3.577032
[3,]  2.581397
[4,]  1.911273
[5,] -1.481277
[6,]  3.525071
>预测(模型,myData,ts=T)
点预测Lo 80 Hi 80 Lo 95 Hi 95
2009年1月-2.502231-2.502231-2.502231-2.502231-2.502231-2.502231
2009年2月-3.577032-3.577032-3.577032-3.577032-3.577032
2009年3月2.581397 2.581397 2.581397 2.581397 2.581397 2.581397
2009年4月1.911273 1.911273 1.911273 1.911273 1.911273 1.911273
2009年5月-1.481277-1.481277-1.481277-1.481277-1.481277-1.481277
2009年6月3.525071 3.525071 3.525071 3.525071 3.525071 3.525071
> 

函数名
forecast
有点误导<如果
f1
f2
b
c
d
e
f
的预测值作为
newdata
newdata
给出,则代码>预测值
a
仅计算预测值。我知道我使用的数据与我在
newdata
中使用的数据相同。问题是,如果没有
newdata
,我就无法使用
forecast()
。问题是我为什么要这么做,以及我必须做些什么。也许其他变量的预测?但是我再次面临同样的问题,因为我首先需要对其他变量的预测来预测它们。
tslm
除了线性回归之外什么都不做,并且
forecast
在新的数据点对其进行评估。我不知道你想要实现什么,但也许
tslm
forecast
结合使用不是正确的工具。我编辑了我的答案。现在解释了
newdata
的必要性、
h
的无关性以及错误消息的原因。所以请不要让我远离寒冷。一个正确的答案是一个很好的答案,即使它不能帮助你立即继续你的申请。非常感谢你的帮助。现在我知道,至少,我必须改变我的方法,另外还要理解
tsml
> coef(model)
       f1        f2         b         c         d         e         f 
 2.008211  1.344910 -0.532548 -1.375166  0.378199  2.169784 -1.971422 
> myData <- X[1:6,-3] + matrix(sample(-100:100,6*7,,replace=TRUE)/100,6,7)
> myData
             f1         f2          b          c          d          e          f
[1,]  1.3901181  0.5794323  0.2638713  1.7911077 -1.9140976 -0.1632654  1.2130388
[2,] -0.5106604  1.0037957 -0.5357955  1.1981059 -0.3636334 -1.2746126 -0.1845794
[3,]  2.0191347 -0.8724608 -1.7707524  0.2779736  1.2814462 -0.4834006  0.1504435
[4,]  1.4574348  0.2173202 -1.1881501  0.7911197 -0.7332919 -1.0103667 -0.8201907
[5,] -1.8129340  0.2294362  0.7379416 -1.3893631  0.5011054  0.4321159  0.4026663
[6,]  1.9659584  1.8596798  0.7286796  1.9930237  0.6643413 -0.2609216 -0.2635644
> fcst <- myData %*% coef(model)
> fcst
          [,1]
[1,] -2.502231
[2,] -3.577032
[3,]  2.581397
[4,]  1.911273
[5,] -1.481277
[6,]  3.525071
> forecast(model,myData,ts=T)
         Point Forecast     Lo 80     Hi 80     Lo 95     Hi 95
Jan 2009      -2.502231 -2.502231 -2.502231 -2.502231 -2.502231
Feb 2009      -3.577032 -3.577032 -3.577032 -3.577032 -3.577032
Mar 2009       2.581397  2.581397  2.581397  2.581397  2.581397
Apr 2009       1.911273  1.911273  1.911273  1.911273  1.911273
May 2009      -1.481277 -1.481277 -1.481277 -1.481277 -1.481277
Jun 2009       3.525071  3.525071  3.525071  3.525071  3.525071
>