Search code examples
rtime-seriesarimaforecast

Checking residuals: Forecast -> checkresiduals and bgtest


I am using Rob Hyndman's forecast package, and I'd like to extract some of the values from the checkresiduals function. Is there a way to call checkresiduals(model) and then extract the p-value, lag, and df for the Ljung-Box Test that the function calls? The function simply prints the output to the console like so:

> checkresiduals(arima_model)

    Ljung-Box test

data:  Residuals from ARIMA(0,1,3)(2,0,1)[7]
Q* = 29.221, df = 8, p-value = 0.00029

If I were interested instead on running the Breusch-Godfrey Test using the parameters specified by the model that auto.arima discovered, would

bgtest(auto.arima(time_series))

be sufficient to specify the correct maximal order and regressors?

Thank you!


Solution

  • Both parts of your question relate to how the function forecast::checkresiduals actually works. This function is written in pure R, so I would suggest going over the code by just running the forecast::checkresiduals command in the console.

    For the LB case you can get the p-value like so:

    arima_model <- forecast::auto.arima(WWWusage)
    LBtest <- Box.test(zoo::na.approx(residuals(arima_model))
                       , fitdf = length(arima_model$coef)
                       , lag = max( length(arima_model$coef) + 3
                                    , min(10, length(residuals(arima_model))/5) )
                       , type = 'Ljung')
    
    LBtest$p.value
    

    The second part of your question has more intricacies and depends on your data and the context of your analysis, but you should be careful in using the auto.arima function.

    HTH