# Understanding the Ljung-Box test

When I perform the Ljung-Box test with the checkresiduals() function from package forecast, I do not get why we would want to set the "lag" option. Suppose I have three simple AR-models on the same time serie:

1. AR(1)
2. AR(2)
3. AR(3)

Then for the first model I can use

checkresiduals(AR1$residuals, lag = 10, test = "LB") I can also use checkresiduals(AR1$residuals, lag = 30, test = "LB")

Depending on the number I choose for the argument "lag" the test-statistic and the p-value will differ. But WHY wouldn't I want to include the entire time series when performing the test? That is, what is the point in setting lag to a lower number than 50 if I have 50 observations?

Also, how do think when I choose this "lag" argument for my different models? Do I choose the same number for the AR(1) as for the AR(3) model? Should I choose the same number of lags as I have in the model, so that for an AR(3)-model i pick lag=3?

I have a hard time finding an answer to this. I have looked in the online book by Hyndman and Athanasopolous here: 5.4 Residual diagnostics | Forecasting: PrinciplesÂ andÂ Practice (3rdÂ ed) but it does not answer my question. They only set lag=10 in all examples.

Any explanation appreciated!

If you are using Hyndman and Athanasopolous 3rd ed, you should use the software suite that goes with it, which replaces {forecast} (recommended). If you will be staying with the older package, however, use the second edition text.

help(checkresiduals) gives

Number of lags to use in the Ljung-Box or Breusch-Godfrey test. If missing, it is set to min(10,n/5) for non-seasonal data, and min(2m, n/5) for seasonal data, where n is the length of the series, and m is the seasonal period of the data. It is further constrained to be at least df+3 where df is the degrees of freedom of the model. This ensures there are at least 3 degrees of freedom used in the chi-squared test.

How to read? Recall the purpose of the testing is to assess whether residuals are distinguishable from white noise, which in this case is simply random error. If we look at each residual separately, there is the risk that while it may appear as evidence against treating the residuals as white noise, the residual itself may actually be due to random variation. To overcome this, the portmanteau tests look at the first \ell residuals together to assess a test statistic

Q* = T(T + 2) \sum_{k=1}^\ell (T - k)^{-1}r_k^2

where

T is the number of observations
r_k is the residual for lag k
\ell is the number of autocorrelations to be tested

As \ell grows larger, the RHS of the equation grows larger (because of r^2 which insures that positive and negative residuals don't offset). The interpretation of Q* is that large values are evidence that the autocorrelations are not reflective of a white noise series by looking at the \chi^2 distribution for \ell degrees of freedom. Look what happens as the number of degrees of freedom increases

> pchisq(1,3:20)
[1] 1.987480e-01 9.020401e-02 3.743423e-02 1.438768e-02
[5] 5.171463e-03 1.751623e-03 5.624973e-04 1.721156e-04
[9] 5.038995e-05 1.416494e-05 3.834735e-06 1.002380e-06
[13] 2.535644e-07 6.219691e-08 1.481974e-08 3.435490e-09
[17] 7.759390e-10 1.709670e-10


Beyond 10 degrees of freedom, the values drop to less than 1-e6.

Thank you both for answering my question!
I see what you mean there Rob by using the proportion of p-values around the desired level. Overall, I get that the number of lags to choose is open to some debate, and that understanding will take me further. Great advice!

1 Like

This topic was automatically closed 21 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.