简体   繁体   English

R中的时间序列预测误差

[英]Time Series Forecasting Error in R

I'm attempting to use the forecast function from the forecast package v4.06 on a time series object I create. 我试图在我创建的时间序列对象上使用预报包v4.06中的预报函数。 Depending on the size of the vector I pass into the time series function to generate a time series object, I will get an exception thrown from the subsequent call to forecast. 根据传递给时间序列函数以生成时间序列对象的向量的大小,我将在随后的预测调用中引发异常。

For example, creating a time series object with a vector of length 6 like so : 例如,创建一个长度为6的向量的时间序列对象,如下所示:

tsObj <- ts(tsVector[1:6], frequency=12, start=c(2013,4))

and then calling the forecast function on that time series object: 然后在该时间序列对象上调用预报函数:

pred <- forecast(tsObj, 1)

generates the following Exception: 生成以下异常:

Error in lsfit(1:maxn, y.sa[1:maxn]) : 0 responses, but only 2 variables
In addition: Warning message:
In lsfit(1:maxn, y.sa[1:maxn]) : 6 missing values deleted

In fact, vector lengths of 4 thru 11 generates this error, but lengths of 1,2,3, or >=12 succeed in generating an appropriate prediction. 实际上,矢量长度4到11会产生此错误,但是1,2,3或> = 12的长度会成功生成适当的预测。 Any insights into the exception would be immensely appreciated. 对于异常的任何见解将不胜感激。 Thanks for the time! 感谢您的时间!

================================================================================ ================================================== ==============================

I believe I found the source for lsfit and found the place where this exception is generated: 我相信我找到了lsfit的来源,并找到了产生此异常的地方:

## check for compatible lengths
nrx <- NROW(x)
ncx <- NCOL(x)
nry <- NROW(y)
ncy <- NCOL(y)
nwts <- length(wt)
if(nry != nrx) stop(paste("X matrix has", nrx, "responses, Y",
   "has", nry, "responses."))
if(nry < ncx) stop(paste(nry, "responses, but only", ncx,     "variables"))

It looks like a mismatch in number of rows between the x and y vectors. 它看起来像x和y向量之间的行数不匹配。 However, I can't find where lsfit is called within the forecast package to figure out what "y" is in this case. 但是,在这种情况下,我找不到在预测包中调用lsfit的位置来确定“ y”是什么的情况。 Thanks for any/all help! 感谢您提供的所有帮助!

The issue is that you're setting a frequency of 12, but this isn't possible to estimate with less than the same number of tsVector inputs. 问题是您将频率设置为12,但是用少于相同数量的tsVector输入进行估计是不可能的。 So the minimum frequency you can set is the number of objects in your vector. 因此,可以设置的最小频率是矢量中对象的数量。

Both of these code blocks work for me: 这两个代码块都对我有用:

tsVector <- c(1,2,3,4,5, 6)
tsObj <- ts(tsVector, frequency = 5, start = c(2013, 4))
pred <- forecast(tsObj, 1)

and

tsVector <- c(1,2,3,4,5, 6, 7, 8, 9 , 10, 11, 12)
tsObj <- ts(tsVector, frequency = 12, start = c(2013, 4))
pred <- forecast(tsObj, 1)

Hope this helps! 希望这可以帮助!

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

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