2

I would like to extract the AR coefficients from a few models i created with auto.arima. the problem is that I want to take the sum of the AR coefficients but without the intercept/mean.

bsp_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp_auto <- auto.arima(bsp_ts, max.p = 12, max.q = 0, seasonal = FALSE, d=0)
summary(bsp_auto)
sum(coef(bsp_auto))

how can I access only the AR coefficients? I know the coefficients are saved in a list from auto.arima, thus I think the solutions is to access the coefficients with the corresponding "list language", but I'm still unexperienced with lists. Can anybody help? :) thanks in advance...

Edit: Multiple models

bsp1_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp2_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp3_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))

bsp_ts <- list(bsp1_ts, bsp2_ts, bsp3_ts) 

bsp_auto <- lapply(bsp_ts, function(x) auto.arima(x, max.p = 12, max.q = 0, seasonal = FALSE, d=0))

coefficient extraction for multiple models, something like this:

ARcoef_li <- lapply(ARpers_li, function(x) sum(ARpers_li$x$coef)
yaennu
  • 85
  • 1
  • 9

1 Answers1

0

Single Model

library(forecast)
bsp_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp_auto <- auto.arima(bsp_ts, max.p = 12, max.q = 0, seasonal = FALSE, d=0)

You can do:

my_coefficients = bsp_auto$coef
my_coefficients[!names(my_coefficients) == 'intercept']

Note that when you type bsp_auto$, RStudio will give you suggestions on the avavailable objects to extract from the model. That information can maybe help you in the future with similar tasks. bsp_auto$coef returns a named vector:

      ar1 intercept 
0.7877316 3.6909753 

so the second line in my answer is used to 'discard' the intercept from our vector.

Hope this helps!


Multiple models

bsp1_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp2_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp3_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))

bsp_ts <- list(bsp1_ts, bsp2_ts, bsp3_ts) 

bsp_auto <- lapply(bsp_ts, function(x) auto.arima(x, max.p = 12, max.q = 0, seasonal = FALSE, d=0))

lapply(bsp_auto,function(x){ x$coef[!names(x$coef)=='intercept']} )
Florian
  • 24,425
  • 4
  • 49
  • 80
  • perfect, thanks a lot! now i want to do this for all my models.. i edited my question for a cleaner presentation. I am searching now for sth like this: `ARcoef_li <- lapply(ARpers_li, function(x) sum(ARpers_li$x$coef))` – yaennu Jan 14 '18 at 09:59
  • 1
    Please do not change your question after it has been answered, since any answers already posted will be wrong all of a sudden. Instead consider framing it as a new question on Stack Overflow along with your new insights and attempts. Any way, I have added a line of code that should help you extract the coefficient from multiple models, hope this helps! – Florian Jan 14 '18 at 10:06
  • thanks! it worked and in the future i will post a new quesstion :) – yaennu Jan 16 '18 at 15:41
  • No problem, glad I could help! – Florian Jan 16 '18 at 16:27