MATLAB Examples

# Conduct the Ljung-Box Q-Test

This example shows how to conduct the Ljung-Box Q-test for autocorrelation.

The time series is 57 consecutive days of overshorts from a gasoline tank in Colorado.

## Step 1. Load the data.

Load the time series of overshorts.

load(fullfile(matlabroot,'examples','econ','Data_Overshort.mat')) Y = Data; N = length(Y); figure plot(Y) xlim([0,N]) title('Overshorts for 57 Consecutive Days') 

The data appears to fluctuate around a constant mean, so no data transformations are needed before conducting the Ljung-Box Q-test.

## Step 2. Conduct the Ljung-Box Q-test.

Conduct the Ljung-Box Q-test for autocorrelation at lags 5, 10, and 15.

[h,p,Qstat,crit] = lbqtest(Y,'Lags',[5,10,15]) 
h = 1x3 logical array 1 1 1 p = 0.0016 0.0007 0.0013 Qstat = 19.3604 30.5986 36.9639 crit = 11.0705 18.3070 24.9958 

All outputs are vectors with three elements, corresponding to tests at each of the three lags. The first element of each output corresponds to the test at lag 5, the second element corresponds to the test at lag 10, and the third element corresponds to the test at lag 15.

The test decisions are stored in the vector h. The value h = 1 means reject the null hypothesis. Vector p contains the p-values for the three tests. At the significance level, the null hypothesis of no autocorrelation is rejected at all three lags. The conclusion is that there is significant autocorrelation in the series.

The test statistics and critical values are given in outputs Qstat and crit, respectively.