Example: StepwiseRegression

This example uses a data set from Draper and Smith (1981, pp. 629-630). Method compute is invoked to find the best regression for each subset size using the R^2 criterion. By default, stepwise regression is performed.

using System;
using Imsl;
using Imsl.Math;
using Imsl.Stat;

public class StepwiseRegressionEx1
{
    
    public static void  Main(System.String[] args)
    {
        double[,] x = {{7.0, 26.0, 6.0, 60.0},
                       {1.0, 29.0, 15.0, 52.0},
                       {11.0, 56.0, 8.0, 20.0},
                       {11.0, 31.0, 8.0, 47.0},
                       {7.0, 52.0, 6.0, 33.0},
                       {11.0, 55.0, 9.0, 22.0},
                       {3.0, 71.0, 17.0, 6.0},
                       {1.0, 31.0, 22.0, 44.0},
                       {2.0, 54.0, 18.0, 22.0},
                       {21.0, 47.0, 4.0, 26},
                       {1.0, 40.0, 23.0, 34.0},
                       {11.0, 66.0, 9.0, 12.0},
                       {10.0, 68.0, 8.0, 12.0}};
        
        double[] y = new double[]{78.5, 74.3, 104.3, 87.6,
                                  95.9, 109.2, 102.7, 72.5,
                                  93.1, 115.9, 83.8, 113.3, 109.4};
        
        StepwiseRegression sr = new StepwiseRegression(x, y);
        sr.Compute();
        
        PrintMatrix pm = new PrintMatrix();
        PrintMatrixFormat pmf = new PrintMatrixFormat();
        pmf.NumberFormat = "0.000";
        pm.SetTitle("*** ANOVA *** "); pm.Print(sr.ANOVA.GetArray());
        
        StepwiseRegression.CoefficientTTestsValue coefT = sr.CoefficientTTests;
        double[,] coef = new double[4, 4];
        for (int i = 0; i < 4; i++)
        {
            coef[i,0] = coefT.GetCoefficient(i);
            coef[i,1] = coefT.GetStandardError(i);
            coef[i,2] = coefT.GetTStatistic(i);
            coef[i,3] = coefT.GetPValue(i);
        }
        pm.SetTitle("*** Coef *** "); pm.Print(pmf, coef);
        pm.SetTitle("*** Swept *** "); pm.Print(sr.Swept);
        pm.SetTitle("*** History *** "); pm.Print(sr.History);
        pm.SetTitle("*** VIF *** "); pm.Print(sr.CoefficientVIF);
        pm.SetTitle("*** CovS *** "); pm.Print(pmf, sr.CovariancesSwept);
        Console.WriteLine("*** Intercept ***   " + sr.Intercept);
    }
}

Output

       *** ANOVA *** 
               0             
 0     2                     
 1    10                     
 2    12                     
 3  2641.00096476634         
 4    74.7621121567356       
 5  2715.76307692308         
 6  1320.50048238317         
 7     7.47621121567356      
 8   176.626963081888        
 9     1.58106023181439E-08  
10    97.2471047716931       
11    96.6965257260317       
12     2.73426612012685      
13     NaN                   
14     NaN                   

          *** Coef *** 
     0       1       2       3    
0   1.440  0.138   10.403  0.000  
1   0.416  0.186   2.242   0.052  
2  -0.410  0.199  -2.058   0.070  
3  -0.614  0.049  -12.621  0.000  

*** Swept *** 
   0   
0   1  
1  -1  
2  -1  
3   1  
4  -1  

*** History *** 
   0  
0  2  
1  0  
2  0  
3  1  
4  0  

     *** VIF *** 
           0          
0   1.0641052101769   
1  18.7803086409578   
2   3.45960147891528  
3   1.0641052101769   

                 *** CovS *** 
     0        1         2        3        4     
0   0.003  -0.029    -0.946     0.000   1.440   
1  -0.029   154.720  -142.800   0.907   64.381  
2  -0.946  -142.800   142.302   0.070  -58.350  
3   0.000   0.907     0.070     0.000  -0.614   
4   1.440   64.381   -58.350   -0.614   74.762  

*** Intercept ***   103.097381636675

Link to C# source.