2021年3月21日星期日

NA for first value in residuals in forecast package in R

I am working through A Little Book of R for Time Series by Avril Coghlan. Some of the code in it seems a bit outdated but I am trying to use updated code as I go.

I am having trouble getting a correlogram of residuals from a forecast() object to work.

The data provided in the example is recorded annual rainfall in London from 1813-1912.

# read in the data  rain <- scan("http://robjhyndman.com/tsdldata/hurst/precip1.dat",skip=1)    # convert to time series object  rainseries <- ts(rain,start=c(1813))    # run through HoltWinters function  rainseriesforecasts <- HoltWinters(rainseries, beta=FALSE, gamma=FALSE)    # pass HoltWinters object into forecast() function  rainseriesforecasts2 <- forecast(rainseriesforecasts, h=8)  

Now when I pass the forecast object into the acf() function it throws an error

acf(rainseriesforecasts2$residuals, lag.max=20)    # Error in na.fail.default(as.ts(x)) : missing values in object  

When I examine the residuals it is the first value that is an NA

rainseriesforecasts2$residuals    Time Series:  Start = 1813   End = 1912   Frequency = 1     [1]         NA  2.5100000 -1.7605450  7.6619220 -0.1128951  0.1198281  2.6469377    [8] -1.1569105  7.8909960 -0.1293468  0.1237733  8.4407877 -0.9328169 -1.6003159  

However the forecasted values themselves do not contain a missing value so I am confused why the residual would be NA.

Am I doing something wrong? Is it this way for all models?

How do I obtain a numeric value for this first residual?

https://stackoverflow.com/questions/66737588/na-for-first-value-in-residuals-in-forecast-package-in-r March 22, 2021 at 05:11AM

没有评论:

发表评论