Example: Lack Of Fit

Consider the W�lfer Sunspot Data (Anderson 1971, page 660) consisting of the number of sunspots observed each year from 1749 through 1924. The data set for this example consists of the number of sunspots observed from 1770 through 1869. An ARMA(2,1) with nonzero mean is fitted using the ARMA class. The autocorrelations of the residuals are estimated using the AutoCorrelation class. Class LackOfFit is used to compute a portmanteau lack of fit test using 10 lags. The warning messages from ARMA in the output can be ignored. (See Example 2 in ARMA for a full explanation of the warning messages.)
using System;
using Imsl.Stat;

public class LackOfFitEx1
{

    public static void  Main(String[] args)
    {
        int lagmax = 10;
        int npfree = 4;
        double tolerance = 0.125;
        
        //  sunspot data for 1770 through 1869
        double[] x = { 100.8, 81.6, 66.5, 34.8, 30.6, 7, 19.8,
            92.5, 154.4, 125.9, 84.8, 68.1, 38.5, 22.8, 10.2, 24.1, 82.9,
            132, 130.9, 118.1, 89.9, 66.6, 60, 46.9, 41, 21.3, 16, 6.4,
            4.1, 6.8, 14.5, 34, 45, 43.1, 47.5, 42.2, 28.1, 10.1, 8.1,
            2.5, 0, 1.4, 5, 12.2, 13.9, 35.4, 45.8, 41.1, 30.4, 23.9,
            15.7, 6.6, 4, 1.8, 8.5, 16.6, 36.3, 49.7, 62.5, 67, 71, 47.8,
            27.5, 8.5, 13.2, 56.9, 121.5, 138.3, 103.2, 85.8, 63.2, 36.8,
            24.2, 10.7, 15, 40.1, 61.5, 98.5, 124.3, 95.9, 66.5, 64.5,
            54.2, 39, 20.6, 6.7, 4.3, 22.8, 54.8, 93.8, 95.7, 77.2, 59.1,
            44, 47, 30.5, 16.3, 7.3, 37.3, 73.9};
        
        // Get residuals from ARMA(2,1) for autocorrelation/lack of fit
        ARMA arma = new ARMA(2, 1, x);
        arma.Method = ARMA.ParamEstimation.LeastSquares;
        arma.ConvergenceTolerance = tolerance;
        arma.Compute();
        double[] residuals = arma.GetResidual();
        
        // Get autocorrelations from residuals for lack of fit test
        AutoCorrelation ac = new AutoCorrelation(residuals, lagmax);
        double[] correlations = ac.GetAutoCorrelations();
        
        // Get lack of fit test statistic and p-value
        double[] result = LackOfFit.Compute(x.Length, correlations,
            npfree, lagmax);
        
        // Print parameter estimates, test statistic, and p-value
        // NOTE: Test Statistic Q follows a Chi-squared dist.
        Console.WriteLine("Lack of Fit Statistic,  Q = " + result[0]);
        Console.WriteLine("P-value of Q = " + result[1]);
    }
}

Output

Lack of Fit Statistic,  Q = 14.6060180633065
P-value of Q = 0.97644740013821
Imsl.Stat.ARMA: Relative function convergence - Both the scaled actual and 
predicted reductions in the function are less than or equal to the relative 
function convergence tolerance "ConvergenceTolerance" = 0.0645856533065147.
Imsl.Stat.ARMA: Least squares estimation of the parameters has failed to 
converge.  Increase "length" and/or "tolerance" and/or "convergence_tolerance".  
The estimates of the parameters at the last iteration may be used as new 
starting values.

Link to C# source.