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

2

5

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
Herring answered 24/6, 2016 at 16:17 Comment(0)
N
10

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 
Nicias answered 24/6, 2016 at 16:32 Comment(2)
Thanks, the updated version is exactly what I was looking for!Herring
thanks I was looking for this definition! Initially I thought it was p,d,q,P,D,Q,m like in Statsmodel (Python)..Chrysalid
E
1

A better, less hands-on, solution exists using the forecast package and the function arimaorder(). Apply this to the model and it returns the elements in the order you would expect:

> arimaorder(model1)
        p         d         q         P         D         Q Frequency 
        0         0         1         0         1         0        12 
Equilibrant answered 4/7, 2023 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.