Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting p,d,q values from a fitted ARIMA model in R?

I am running a time series forecast using forecast::auto.arima, and I was trying to see if there is a way to extract the values assigned to p, d, q (and seasonally as well if applicable) from the fitted time series object. Example:

fit <- auto.arima(mydata)

Say auto.arima() picked an ARIMA(1,1,0)(0,1,1)[12] model. Is there a way to extract the values of p,d, q (and P, D, Q) from fit? In the end I would like to have six variables automatically assigned as follows:

p=1, d=1, q=0, P=0, D=1, Q=1
like image 768
user1723699 Avatar asked Jan 06 '23 20:01

user1723699


1 Answers

If you look at ?auto.arima, you will know that it returns the same object as stats::arima. If you further look at ?arima, you see that the information you want can be found from $model of the returned value. The detail of $model can be read from ?KalmanLike:

phi, theta: numeric vectors of length >= 0 giving AR and MA parameters.

     Delta: vector of differencing coefficients, so an ARMA model is
            fitted to ‘y[t] - Delta[1]*y[t-1] - ...’.

So, you should do:

p <- length(fit$model$phi)
q <- length(fit$model$theta)
d <- fit$model$Delta

Example from ?auto.arima:

library(forecast)
fit <- auto.arima(WWWusage)

length(fit$model$phi)  ## 1
length(fit$model$theta)  ## 1
fit$model$Delta  ## 1

fit$coef
#       ar1       ma1 
# 0.6503760 0.5255959 

Alternatively (actually better), you can refer to the $arma value:

arma: A compact form of the specification, as a vector giving the
      number of AR, MA, seasonal AR and seasonal MA coefficients,
      plus the period and the number of non-seasonal and seasonal
      differences.

But you need to match them correctly and carefully. For the example above, there is:

fit$arma
# [1] 1 1 0 0 1 1 0

Using the notation ARIMA(p,d,q)(P,D,Q)[m], we can add name attribute for clear presentation:

setNames(fit$arma, c("p", "q", "P", "Q", "m", "d", "D"))
# p q P Q m d D 
# 1 1 0 0 1 1 0 
like image 111
Zheyuan Li Avatar answered Jan 24 '23 14:01

Zheyuan Li