Documentation

## Conduct a Lagrange Multiplier Test

This example shows how to calculate the required inputs for conducting a Lagrange multiplier (LM) test with `lmtest`. The LM test compares the fit of a restricted model against an unrestricted model by testing whether the gradient of the loglikelihood function of the unrestricted model, evaluated at the restricted maximum likelihood estimates (MLEs), is significantly different from zero.

The required inputs for `lmtest` are the score function and an estimate of the unrestricted variance-covariance matrix evaluated at the restricted MLEs. This example compares the fit of an AR(1) model against an AR(2) model.

### Step 1. Compute the restricted MLE.

Obtain the restricted MLE by fitting an AR(1) model (with a Gaussian innovation distribution) to the given data. Assume you have presample observations (${y}_{-1}$, ${y}_{0}$) = (9.6249,9.6396).

```Y = [10.1591; 10.1675; 10.1957; 10.6558; 10.2243; 10.4429; 10.5965; 10.3848; 10.3972; 9.9478; 9.6402; 9.7761; 10.0357; 10.8202; 10.3668; 10.3980; 10.2892; 9.6310; 9.6318; 9.1378; 9.6318; 9.1378]; Y0 = [9.6249; 9.6396]; model = arima(1,0,0); fit = estimate(model,Y,'Y0',Y0);```
``` ARIMA(1,0,0) Model (Gaussian Distribution): Value StandardError TStatistic PValue _______ _____________ __________ _________ Constant 3.2999 2.4606 1.3411 0.17988 AR{1} 0.67097 0.24635 2.7237 0.0064564 Variance 0.12506 0.043015 2.9074 0.0036441 ```

When conducting an LM test, only the restricted model needs to be fit.

### Step 2. Compute the gradient matrix.

Estimate the variance-covariance matrix for the unrestricted AR(2) model using the outer product of gradients (OPG) method.

For an AR(2) model with Gaussian innovations, the contribution to the loglikelihood function at time $t$ is given by

`$\mathrm{log}{L}_{t}=-0.5\mathrm{log}\left(2\pi {\sigma }_{\epsilon }^{2}\right)-\frac{\left({y}_{t}-c-{\varphi }_{1}{y}_{t-1}-{\varphi }_{2}{y}_{t-2}{\right)}^{2}}{2{\sigma }_{\epsilon }^{2}}$`

where ${\sigma }_{\epsilon }^{2}$ is the variance of the innovation distribution.

The contribution to the gradient at time $t$ is

`$\left[\begin{array}{cccc}\frac{\partial \mathrm{log}{L}_{t}}{\partial c}& \frac{\partial \mathrm{log}{L}_{t}}{\partial {\varphi }_{1}}& \frac{\partial \mathrm{log}{L}_{t}}{\partial {\varphi }_{2}}& \frac{\partial \mathrm{log}{L}_{t}}{\partial {\sigma }_{\epsilon }^{2}}\end{array}\right],$`

where

`$\begin{array}{ccc}\frac{\partial \mathrm{log}{L}_{t}}{\partial c}& =& \frac{{y}_{t}-c-{\varphi }_{1}{y}_{t-1}-{\varphi }_{2}{y}_{t-2}}{{\sigma }_{\epsilon }^{2}}\\ \frac{\partial \mathrm{log}{L}_{t}}{\partial {\varphi }_{1}}& =& \frac{{y}_{t-1}\left({y}_{t}-c-{\varphi }_{1}{y}_{t-1}-{\varphi }_{2}{y}_{t-2}\right)}{{\sigma }_{\epsilon }^{2}}\\ \frac{\partial \mathrm{log}{L}_{t}}{\partial {\varphi }_{2}}& =& \frac{{y}_{t-2}\left({y}_{t}-c-{\varphi }_{1}{y}_{t-1}-{\varphi }_{2}{y}_{t-2}\right)}{{\sigma }_{\epsilon }^{2}}\\ \frac{\partial \mathrm{log}{L}_{t}}{\partial {\sigma }_{\epsilon }^{2}}& =& -\frac{1}{2{\sigma }_{\epsilon }^{2}}+\frac{\left({y}_{t}-c-{\varphi }_{1}{y}_{t-1}-{\varphi }_{2}{y}_{t-2}{\right)}^{2}}{2{\sigma }_{\epsilon }^{4}}\end{array}$`

Evaluate the gradient matrix, $G$, at the restricted MLEs (using ${\underset{}{\overset{ˆ}{\varphi }}}_{2}=0$ ).

```c = fit.Constant; phi1 = fit.AR{1}; phi2 = 0; sig2 = fit.Variance; Yt = Y; Yt1 = [9.6396; Y(1:end-1)]; Yt2 = [9.6249; Yt1(1:end-1)]; N = length(Y); G = zeros(N,4); G(:,1) = (Yt-c-phi1*Yt1-phi2*Yt2)/sig2; G(:,2) = Yt1.*(Yt-c-phi1*Yt1-phi2*Yt2)/sig2; G(:,3) = Yt2.*(Yt-c-phi1*Yt1-phi2*Yt2)/sig2; G(:,4) = -0.5/sig2 + 0.5*(Yt-c-phi1*Yt1-phi2*Yt2).^2/sig2^2;```

### Step 3. Estimate the variance-covariance matrix.

Compute the OPG variance-covariance matrix estimate.

`V = inv(G'*G)`
```V = 4×4 6.1431 -0.6966 0.0827 0.0367 -0.6966 0.1535 -0.0846 -0.0061 0.0827 -0.0846 0.0771 0.0024 0.0367 -0.0061 0.0024 0.0019 ```

Numerical inaccuracies can occur due to computer precision. To make the variance-covariance matrix symmetric, combine half of its value with half of its transpose.

`V = V/2 + V'/2;`

### Step 4. Calculate the score function.

Evaluate the score function (the sum of the individual contributions to the gradient).

`score = sum(G);`

### Step 5. Conduct the Lagrange multiplier test.

Conduct the Lagrange multiplier test to compare the restricted AR(1) model against the unrestricted AR(2) model. The number of restrictions (the degree of freedom) is one.

`[h,p,LMstat,crit] = lmtest(score,V,1)`
```h = logical 0 ```
```p = 0.5787 ```
```LMstat = 0.3084 ```
```crit = 3.8415 ```

The restricted AR(1) model is not rejected in favor of the AR(2) model (`h = 0`).