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 criterion. By default, stepwise regression is performed.
import java.text.*; import com.imsl.stat.*; import com.imsl.IMSLException.*; import com.imsl.math.PrintMatrix; import com.imsl.math.PrintMatrixFormat; public class StepwiseRegressionEx1 { public static void main(String[] args) throws Exception { double x[][] = { {7., 26., 6., 60.}, {1., 29., 15., 52.}, {11., 56., 8., 20.}, {11., 31., 8., 47.}, {7., 52., 6., 33.}, {11., 55., 9., 22.}, {3., 71., 17., 6.}, {1., 31., 22., 44.}, {2., 54., 18., 22.}, {21., 47., 4., 26}, {1., 40., 23., 34.}, {11., 66., 9., 12.}, {10.0, 68., 8., 12.}}; double y[] = { 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(); pm.setTitle("*** ANOVA *** "); pm.print(sr.getANOVA().getArray()); StepwiseRegression.CoefficientTTests coefT = sr.getCoefficientTTests(); 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(coef); pm.setTitle("*** Swept *** "); pm.print(sr.getSwept()); pm.setTitle("*** History *** "); pm.print(sr.getHistory()); pm.setTitle("*** VIF *** "); pm.print(sr.getCoefficientVIF()); pm.setTitle("*** CovS *** "); pm.print(sr.getCovariancesSwept()); System.out.println("*** Intercept *** " + sr.getIntercept()); } }
*** ANOVA *** 0 0 2 1 10 2 12 3 2,641.001 4 74.762 5 2,715.763 6 1,320.5 7 7.476 8 176.627 9 0 10 97.247 11 96.697 12 2.734 13 ? 14 ? *** Coef *** 0 1 2 3 0 1.44 0.138 10.403 0 1 0.416 0.186 2.242 0.052 2 -0.41 0.199 -2.058 0.07 3 -0.614 0.049 -12.621 0 *** 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.064 1 18.78 2 3.46 3 1.064 *** CovS *** 0 1 2 3 4 0 0.003 -0.029 -0.946 0 1.44 1 -0.029 154.72 -142.8 0.907 64.381 2 -0.946 -142.8 142.302 0.07 -58.35 3 0 0.907 0.07 0 -0.614 4 1.44 64.381 -58.35 -0.614 74.762 *** Intercept *** 103.09738163667471Link to Java source.