# ARIMA: extract model degrees of freedom

Hi,

After estimating an ARIMA model in fable, how do I extract the model degrees of freedom (to match the number of parameters in the model) without inputting dof manually, as in?

``````cement <- aus_production |>
select(Cement) |>
filter_index("1988 Q1" ~ .)
train <- cement |> filter_index(. ~ "2007 Q4")

fit_arima <- train |> model(ARIMA(Cement, trace = TRUE))
report(fit_arima)

augment(fit_arima) |>
features(.innov, ljung_box, lag = 16, dof = ?)
``````

thanks,
Amarjit

An alternative to counting the number of of AR and MA terms, plus the constant if there is a drift, you can extract the number of estimated coefficients from fit_arima with:

``````length(fit_arima[][]\$fit\$par\$term)
``````

You can use `tidy()` here:

``````dof <- tidy(fit_arima) |>
filter(term != "constant") |>
NROW()
``````

Note that the df for the Ljung-Box model should not include the constant term.

1 Like

I am a bit confused. Amarjit's cement example is taken from the "Comparing ARIMA( ) and ETS( ) on seasonal data" section of fpp3 Chapter 9. The selected ARIMA model is (1, 0, 1)(2, 1, 1) w/ drift, and dof = 6 for ljung_box. I assumed this was 5 for the ARMA terms plus one for the constant for drift. An earlier example for Central African Republic exports selected ARIMA(3, 1, 0) with no drift and used dof = 3 for ljung_box.

I may need to change some lecture notes and an exercise!

I recently discovered that the original Ljung-Box paper did not count the constant in the degrees of freedom, and Achim Zeileis pointed out at Degrees-of-freedom adjustment in Ljung-Box test in checkresiduals() · Issue #908 · robjhyndman/forecast · GitHub that it should be excluded. In addition, when the LB test is applied to other models, it is not clear what dof should be used. So we updated the book to reflect this. I thought I had fixed all the examples, but I missed this one. It is now updated.

Thanks for the explanation! I probably would have noticed it earlier, but I am in my university's gradual retirement program and let a junior faculty member teach the forecasting class this year. I might teach it one last time next year and will need to go through the book more carefully.

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.