Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fable: Extracting the p,d,q specification from an ARIMA model

Tags:

r

arima

fable-r

I've been using the tidy forecasting package fable (which has been so useful).

I was wondering if there was an easy way to extract that the p,d,q values from a mable.

Using the data on this guide as an example https://www.mitchelloharawild.com/blog/fable/

library(tidyverse)
library(tsibble)
library(fable)

tourism_state <- tourism %>% 
  group_by(State) %>% 
  summarise(Trips = sum(Trips))

fit <- tourism_state %>% 
  model(arima = ARIMA(Trips))
> fit
# A mable: 8 x 2
# Key:     State [8]
  State                                 arima
  <chr>                               <model>
1 ACT                          <ARIMA(0,1,1)>
2 New South Wales    <ARIMA(0,1,1)(0,1,1)[4]>
3 Northern Territory <ARIMA(1,0,1)(0,1,1)[4]>
4 Queensland                   <ARIMA(2,1,2)>
5 South Australia    <ARIMA(1,0,1)(0,1,1)[4]>
6 Tasmania           <ARIMA(0,0,3)(2,1,0)[4]>
7 Victoria           <ARIMA(0,1,1)(0,1,1)[4]>
8 Western Australia            <ARIMA(0,1,3)>

I know the specifications are stored under model[[1]]$fit$spec but I can't figure out a way to extract them if I have a large list of models

Ideally I'd like

  State                                 arima       p     d       q
  <chr>                               <model>
1 ACT                          <ARIMA(0,1,1)>       0     1       1
2 New South Wales    <ARIMA(0,1,1)(0,1,1)[4]>       0     1       1
3 Northern Territory <ARIMA(1,0,1)(0,1,1)[4]>       1     0       1
4 Queensland                   <ARIMA(2,1,2)>       
5 South Australia    <ARIMA(1,0,1)(0,1,1)[4]>       and so on....
6 Tasmania           <ARIMA(0,0,3)(2,1,0)[4]>
7 Victoria           <ARIMA(0,1,1)(0,1,1)[4]>
8 Western Australia            <ARIMA(0,1,3)>

Thanks!

like image 325
usually_confused Avatar asked Aug 13 '20 10:08

usually_confused


People also ask

How do I find my PDQ in Arima?

A nonseasonal ARIMA model is classified as an "ARIMA(p,d,q)" model, where: p is the number of autoregressive terms, d is the number of nonseasonal differences needed for stationarity, and. q is the number of lagged forecast errors in the prediction equation.

What is lambda in ARIMA model?

The lambda defines a Box-Cox transformation, as defined here: https://en.wikipedia.org/wiki/Power_transform. In particular, setting it to zero corresponds to first taking the log of the values, forecasting the resulting time series, and then taking the inverse operation on the forecasts (i.e. exp ).

What is seasonal ARIMA model?

A seasonal ARIMA model uses differencing at a lag equal to the number of seasons (s) to remove additive seasonal effects. As with lag 1 differencing to remove a trend, the lag s differencing introduces a moving average term. The seasonal ARIMA model includes autoregressive and moving average terms at lag s.


1 Answers

What about this?

# specificly needed libraries from tidyverse
library(dplyr)
library(purrr)

fit %>%
  mutate(map_dfr(arima, c("fit", "spec")))

#> # A mable: 8 x 10
#> # Key:     State [8]
#>   State                                 arima     p     d     q     P     D     Q constant period
#>   <chr>                               <model> <int> <int> <int> <int> <int> <int> <lgl>     <dbl>
#> 1 ACT                          <ARIMA(0,1,1)>     0     1     1     0     0     0 FALSE         4
#> 2 New South Wales    <ARIMA(0,1,1)(0,1,1)[4]>     0     1     1     0     1     1 FALSE         4
#> 3 Northern Territory <ARIMA(1,0,1)(0,1,1)[4]>     1     0     1     0     1     1 FALSE         4
#> 4 Queensland                   <ARIMA(2,1,2)>     2     1     2     0     0     0 FALSE         4
#> 5 South Australia    <ARIMA(1,0,1)(0,1,1)[4]>     1     0     1     0     1     1 FALSE         4
#> 6 Tasmania           <ARIMA(0,0,3)(2,1,0)[4]>     0     0     3     2     1     0 FALSE         4
#> 7 Victoria           <ARIMA(0,1,1)(0,1,1)[4]>     0     1     1     0     1     1 FALSE         4
#> 8 Western Australia            <ARIMA(0,1,3)>     0     1     3     0     0     0 FALSE         4

It works on R >= 4.0 and dplyr >= 1.0.

The arima column is a list. We can use map to extract data from lists.

map will return a list itself, but with map_dfr you can return a dataframe which mutate will interprete as a new set of columns to add to the original dataframe.

Note that with this code the output and the input keep the same class (mable).

like image 171
Edo Avatar answered Sep 30 '22 18:09

Edo