Extract models and AICc from auto.arima() as data frame

In the following example, how do I extract the models and corresponding AICcs as a data frame? For instance, first two rows would be:

model AICc
ARIMA(2,0,2)(1,0,1)[12] with non-zero mean 1003.3450
ARIMA(0,0,0) 996.8378
library(forecast)

# Data
set.seed(123)
z <- ts(rnorm(120, 30, 17), start=2008, frequency=12)

# Auto ARIMA
auto.arima(z, trace = TRUE)
#> 
#>  ARIMA(2,0,2)(1,0,1)[12] with non-zero mean : 1003.345
#>  ARIMA(0,0,0)            with non-zero mean : 996.8378
#>  ARIMA(1,0,0)(1,0,0)[12] with non-zero mean : 999.0416
#>  ARIMA(0,0,1)(0,0,1)[12] with non-zero mean : 999.1399
#>  ARIMA(0,0,0)            with zero mean     : 1187.768
#>  ARIMA(0,0,0)(1,0,0)[12] with non-zero mean : 996.902
#>  ARIMA(0,0,0)(0,0,1)[12] with non-zero mean : 997
#>  ARIMA(0,0,0)(1,0,1)[12] with non-zero mean : 999.0153
#>  ARIMA(1,0,0)            with non-zero mean : 998.9257
#>  ARIMA(0,0,1)            with non-zero mean : 998.9227
#>  ARIMA(1,0,1)            with non-zero mean : 996.8584
#> 
#>  Best model: ARIMA(0,0,0)            with non-zero mean
#> Series: z 
#> ARIMA(0,0,0) with non-zero mean 
#> 
#> Coefficients:
#>          mean
#>       30.2625
#> s.e.   1.3823
#> 
#> sigma^2 estimated as 231.2:  log likelihood=-496.37
#> AIC=996.74   AICc=996.84   BIC=1002.31

Created on 2020-11-22 by the reprex package (v0.3.0)

It looks like broom has an arima method for tidy(), did you try that?

Thanks for the try. Unfortunately, this is not what I want.
I want all the models with AICc as a data frame.

Right, I didn't realize the trace argument was behaving that way. I'm not sure it's totally good practice, but you could always capture the cat() output from trace and then process it into a data frame:

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(tidyverse)

set.seed(123)
z <- ts(rnorm(120, 30, 17), start=2008, frequency=12)

mod_capt <- capture.output(auto.arima(z, trace = TRUE))

mod_capt[seq(2,which(mod_capt == "")[2]-1)] %>%
  enframe(value = "raw_capture") %>%
  extract(raw_capture,
          into = c("model","mean","res"),
          regex = "^ (ARIMA[(),\\d\\[\\]]+)[[:blank:]]+with (non-zero|zero) mean +:[[:blank:]]+([\\d.]+)$")
#> # A tibble: 11 x 4
#>     name model                   mean     res     
#>    <int> <chr>                   <chr>    <chr>   
#>  1     1 ARIMA(2,0,2)(1,0,1)[12] non-zero 1003.345
#>  2     2 ARIMA(0,0,0)            non-zero 996.8378
#>  3     3 ARIMA(1,0,0)(1,0,0)[12] non-zero 999.0416
#>  4     4 ARIMA(0,0,1)(0,0,1)[12] non-zero 999.1399
#>  5     5 ARIMA(0,0,0)            zero     1187.768
#>  6     6 ARIMA(0,0,0)(1,0,0)[12] non-zero 996.902 
#>  7     7 ARIMA(0,0,0)(0,0,1)[12] non-zero 997     
#>  8     8 ARIMA(0,0,0)(1,0,1)[12] non-zero 999.0153
#>  9     9 ARIMA(1,0,0)            non-zero 998.9257
#> 10    10 ARIMA(0,0,1)            non-zero 998.9227
#> 11    11 ARIMA(1,0,1)            non-zero 996.8584

Created on 2020-11-25 by the reprex package (v0.3.0)

1 Like

Thanks! Just what I wanted :smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thanks for your response. I just tried tidy(); not working :frowning: Could you give me a hand?

I don't know anything about any of this, it just looks like this does a lot of what you want:

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(broom)
set.seed(123)
z <- ts(rnorm(120, 30, 17), start=2008, frequency=12)

mod <- auto.arima(z, trace = TRUE)
#> 
#>  ARIMA(2,0,2)(1,0,1)[12] with non-zero mean : 1003.345
#>  ARIMA(0,0,0)            with non-zero mean : 996.8378
#>  ARIMA(1,0,0)(1,0,0)[12] with non-zero mean : 999.0416
#>  ARIMA(0,0,1)(0,0,1)[12] with non-zero mean : 999.1399
#>  ARIMA(0,0,0)            with zero mean     : 1187.768
#>  ARIMA(0,0,0)(1,0,0)[12] with non-zero mean : 996.902
#>  ARIMA(0,0,0)(0,0,1)[12] with non-zero mean : 997
#>  ARIMA(0,0,0)(1,0,1)[12] with non-zero mean : 999.0153
#>  ARIMA(1,0,0)            with non-zero mean : 998.9257
#>  ARIMA(0,0,1)            with non-zero mean : 998.9227
#>  ARIMA(1,0,1)            with non-zero mean : 996.8584
#> 
#>  Best model: ARIMA(0,0,0)            with non-zero mean

tidy(mod)
#> # A tibble: 1 x 3
#>   term      estimate std.error
#>   <chr>        <dbl>     <dbl>
#> 1 intercept     30.3      1.38
glance(mod)
#> # A tibble: 1 x 5
#>   sigma logLik   AIC   BIC  nobs
#>   <dbl>  <dbl> <dbl> <dbl> <int>
#> 1  15.2  -496.  997. 1002.   120

Created on 2020-11-25 by the reprex package (v0.3.0)