How to get fitted values from ar() method model in

2019-08-06 13:21发布

问题:

I want to retrieve the fitted values from an ar() function output model in R. When using Arima() method, I get them using fitted(model.object) function, but I cannot find its equivalent for ar().

回答1:

It does not store a fitted vector but does have the residuals. An example of using the residuals from the ar-object to reconstruct the predictions from the original data:

 data(WWWusage)
 arf <- ar(WWWusage)
str(arf)
#====================
List of 14
 $ order       : int 3
 $ ar          : num [1:3] 1.175 -0.0788 -0.1544
 $ var.pred    : num 117
 $ x.mean      : num 137
 $ aic         : Named num [1:21] 258.822 5.787 0.413 0 0.545 ...
  ..- attr(*, "names")= chr [1:21] "0" "1" "2" "3" ...
 $ n.used      : int 100
 $ order.max   : num 20
 $ partialacf  : num [1:20, 1, 1] 0.9602 -0.2666 -0.1544 -0.1202 -0.0715 ...
 $ resid       : Time-Series [1:100] from 1 to 100: NA NA NA -2.65 -4.19 ...
 $ method      : chr "Yule-Walker"
 $ series      : chr "WWWusage"
 $ frequency   : num 1
 $ call        : language ar(x = WWWusage)
 $ asy.var.coef: num [1:3, 1:3] 0.01017 -0.01237 0.00271 -0.01237 0.02449 ...
 - attr(*, "class")= chr "ar"
#===================
 str(WWWusage)
# Time-Series [1:100] from 1 to 100: 88 84 85 85 84 85 83 85 88 89 ...
png(); plot(WWWusage)
lines(seq(WWWusage),WWWusage - arf$resid, col="red"); dev.off()



回答2:

The simplest way to get the fits from an AR(p) model would be to use auto.arima() from the forecast package, which does have a fitted() method. If you really want a pure AR model, you can constrain the differencing via the d parameter and the MA order via the max.q parameter.

> library(forecast)
> fitted(auto.arima(WWWusage,d=0,max.q=0))
Time Series:
Start = 1 
End = 100 
Frequency = 1 
  [1]  91.68778  86.20842  82.13922  87.60576  ...