← 7. Time series
Time series diagnostics
Try this analysis →

Ljung–Box Test

Tests autocorrelation at several lags together to check whether unexplained patterns remain in the residuals of a time series model.

Residual diagnosticsAutocorrelationQ statisticLag selection

Residuals A · just noiseResiduals B · echo of past valuesCheck several lags together for leftover patterns in the residuals

You've built an ARIMA model to forecast monthly sales and even looked at the residual plot. Once the forecast is done, how can you check whether the model has adequately explained the temporal pattern in the data?

The residuals of a good forecasting model should not show clear, regular patterns that are left unexplained. If the residuals move in a similar direction over time, there may be information the model has not yet captured.

The Ljung–Box test tests jointly whether the autocorrelations at several chosen lags are all zero. Instead of checking a single lag, it evaluates the residual autocorrelation over a whole range at once.

In this article we compare two kinds of simulated residuals, read the test statistic and p-values, and look at what to watch for in lag selection and in diagnosing ARIMA residuals.

SECTION 1

What is the Ljung–Box test?

It checks autocorrelation at several lags at once.

The Ljung–Box test tests the null hypothesis that the autocorrelations of a time series up to a chosen lag are all zero. It is generally used to check the randomness of a time series itself or for residual diagnostics of forecasting models such as ARIMA.

The null hypothesis (H₀) is that the autocorrelations from lag 1 through lag h are all zero. A small p-value is interpreted as evidence that autocorrelation remains in that range. A large p-value does not prove that all the residuals are independent.

SECTION 2

Why check the autocorrelation of residuals?

To see whether temporal patterns the model failed to explain remain.

If a consistent, repeating pattern remains in the residuals, the differences between predicted and actual values, there may be room to explain more with lag terms or seasonality. This can affect the reliability of forecasts and inference.

No autocorrelation in the residuals is only one condition of model adequacy. Bias, changing variance, outliers, the shape of the distribution, and actual forecasting performance must be checked separately.

SECTION 3

How is the test computed?

It squares the autocorrelation at each lag and adds them up.

Test statistic

Q = n(n + 2) × Σ[k=1…h] rₖ² / (n − k)

n: number of residuals, h: maximum lag tested, rₖ: sample autocorrelation at lag k

A larger Q means the sample autocorrelations at the chosen lags are larger overall. For an ordinary time series, Q is compared with an approximately chi-square distribution with h degrees of freedom, but when parameters have been estimated, as with the residuals of an ARMA model, an appropriate degrees-of-freedom correction is needed.

For example, in diagnosing the residuals of a nonseasonal ARMA(p, q) model, h − p − q degrees of freedom are usually used. This depends on the model structure and software settings, and you should choose the lag so that the degrees of freedom remain positive after the correction.

SECTION 4

Real-world case: comparing two kinds of simulated residuals

Even residuals of the same length give different results if they are correlated over time.

To aid understanding, we generated two kinds of simulated residuals, each with 120 observations. Residuals A are independent normal noise, and residuals B were generated with an AR(1) structure so that the value at the previous time point affects the current value.

ItemResiduals AResiduals B
Observations120120
Generating structureIndependent normal noiseAR(1), coefficient 0.58
PurposeExample with almost no autocorrelationExample with leftover autocorrelation
Lags tested6·12·186·12·18

The statistics below were actually computed from these simulated data. Residuals B are a case where AR(1) structure remains; they are not assumed to be the output of any particular fitted model.

SECTION 5

What does the residual plot show?

Visible patterns are important clues to check before the test.

−403−403Residuals A · independent normal noiseResiduals B · AR(1) coefficient 0.58020406080100120
Residuals A and residuals B

Residuals A show no noticeable movement in a consistent direction. Residuals B, by contrast, have relatively many stretches where consecutive time points have values in the same direction. Don't conclude from the plot alone; judge it together with the test results.

SECTION 6

How are the ACF plot and the Ljung–Box test connected?

The ACF shows the pattern lag by lag; Ljung–Box evaluates the selected range as a whole.

−0.4−0.200.20.4161218LagResiduals AResiduals B±0.18
Autocorrelation of residuals (ACF)

The ACF shows how large the autocorrelation is at each lag. The Ljung–Box test accumulates these values to evaluate the overall evidence. Whether an individual ACF bar crosses the bounds and the p-value of the cumulative test are not the same question.

SECTION 7

How do you read the test results table?

Check Q and p together for each lag.

Maximum lagResiduals A QResiduals A pResiduals B QResiduals B p
61.810.93644.98< .001
1214.160.291101.04< .001
1821.650.248109.30< .001
02550751001.81p 0.93644.98p < .001Lag 614.16p 0.291101.04p < .001Lag 1221.65p 0.248109.30p < .001Lag 18Residuals AResiduals B
Ljung–Box Q statistic

These are results for simulated series without any degrees-of-freedom correction. For residuals A, there is not enough evidence to reject the null hypothesis at most of the chosen lags, while for residuals B the evidence of leftover autocorrelation is clear.

When checking the residuals of a fitted ARIMA model, don't copy the table above as is; recompute the degrees of freedom and p-values to reflect the number of parameters actually estimated.

SECTION 8

How do you choose the maximum lag?

The conclusion of the cumulative test can change when the lag changes.

SettingMeaningWatch out for
6 lagsChecks the 6 most recent past time pointsMay miss longer-cycle patterns
12 lagsChecks a one-year range of monthly dataAlso check seasonal autocorrelation
18 lagsChecks an even longer range as wellBeware of too many lags relative to sample size

Choose the lag considering the measurement interval of the data, known seasonal cycles, and the model structure. Trying several lags and reporting only the significant results can distort the conclusion.

If you have fitted a model, use a lag sufficiently larger than the degrees-of-freedom correction, and state which lags you tested.

SECTION 9

How does it differ from other time series diagnostics?

Stationarity, residual autocorrelation, and conditional heteroskedasticity are different questions.

MethodWhat does it check?
ACF/PACFThe shape and size of autocorrelation at each lag
ADF testWhether a unit root is present
Ljung–Box TestWhether the autocorrelations at several lags are zero
ARCH-LM testConditional heteroskedasticity left in squared residuals and the like
Forecast model evaluationHow small the actual forecast errors are

The Ljung–Box test is sometimes applied to squared residuals, but that asks a different question from the usual test of residual autocorrelation. When checking for volatility clustering, use the ARCH-LM test and similar tools as well.

SECTION 10

What should you check before the analysis?

Be clear about the time points, missing data, and whether a model was estimated.

Check whether the observation interval is regular, whether there are missing time points or outliers, and whether you have a raw time series or the residuals of an estimated model. Residuals from an estimated ARIMA model may require a degrees-of-freedom correction to the null distribution because of parameter estimation.

Setup caution

In residual diagnostics for ARMA(p, q), you generally check the relationship between the maximum lag h and p+q. When a seasonal model or regression terms are included, review how your software applies the degrees-of-freedom correction.

SECTION 11

Cautions when interpreting the results

Don't take a nonsignificant result as proof of complete independence.

A large p-value means no autocorrelation was found in the chosen range of lags. It does not mean there is no autocorrelation at all or that the model is perfect. Especially with a small sample, power may be low.

Even if a test over several lags is significant, it does not immediately tell you which lag is the problem. Check the ACF plot, and also review seasonality, trend, outliers, and the model specification.

SECTION 12

Reporting Ljung–Box test results in a sentence

Present the lags tested, the Q statistic, degrees of freedom, and the p-value together.

The following is an example sentence describing the lag-12 results for simulated residuals A and B created above. In real research, you should report values with the degrees-of-freedom correction for model estimation applied.

Example report sentence

“Checking autocorrelation up to lag 12, the Ljung–Box test was not significant for residuals A (Q(12) = 14.16, p = 0.291), but significant autocorrelation was found for residuals B (Q(12) = 101.04, p < .001).”

Also state clearly whether the test was applied to model residuals or to the original series, and when presenting several lags in the same table, do not report results selectively.

SECTION 13

Where can the Ljung–Box test be used?

It is used to check residuals after building a time series model.

It can be used for residual diagnostics in ARIMA and SARIMA forecasts of sales or demand, time series models of sensor measurements, mean models for financial returns, and more.

If the test is significant, look at which lags remain in the residual ACF and consider modifying the model with lag terms, seasonal terms, additional predictors, and so on. The revised model must also be checked with its own forecasting performance and diagnostics.

✓ Wrap-up

So you don't miss temporal patterns left in the residuals

Rather than a single p-value, check the ACF plot, the lag setting, and the model structure together.


The Ljung–Box test evaluates autocorrelation over several lags jointly to check whether patterns remain that the model has not yet explained. A significant result is a clue to revisit the model's lag or seasonal structure.

Conversely, a nonsignificant result does not confirm that the forecasting model is entirely valid. Additionally checking changes in residual variance, unusual values, and forecast errors lets you interpret the results more carefully.

KEY TAKEAWAY

The Ljung–Box test checks the null hypothesis that the autocorrelations at a set of lags are all zero. A significant test is evidence that temporal dependence remains in that range, while a nonsignificant one does not prove complete independence. In residual diagnostics, check the degrees-of-freedom correction for the lags and estimated parameters, along with the ACF plot.