package com.imsl.test.example.stat;
import java.text.*;
import com.imsl.stat.*;
import com.imsl.math.*;
/**
*
* Finds the best regressions using Mallow's \(C_p\) criterion.
*
*
*
* This example uses the same data set as example
* {@link SelectionRegressionEx1}, but Mallow's \(C_p\) statistic is used as the
* criterion rather than \(R^2\). Note that when Mallow's \(C_p\) statistic (or
* adjusted \(R^2\)) is specified, the method setMaximumBestFound
* is used to indicate the total number of "best" regressions (rather than
* indicating the number of best regressions per subset size, as in the case of
* the \(R^2\) criterion). In this example, the three best regressions are found
* to be \((1, 2), (1, 2, 4)\), and \((1, 2, 3)\).
*
*
* @see Code
* @see Output
*/
public class SelectionRegressionEx2 {
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
};
String criterionOption;
MessageFormat critMsg
= new MessageFormat("Regressions with {0} variable(s) ({1})");
MessageFormat critLabel
= new MessageFormat(" Criterion Variables");
MessageFormat coefMsg = new MessageFormat("Best Regressions with"
+ " {0} variable(s) ({1})");
MessageFormat coefLabel = new MessageFormat("Variable Coefficient"
+ " Standard Error t-statistic p-value");
SelectionRegression sr = new SelectionRegression(4);
sr.setCriterionOption(SelectionRegression.MALLOWS_CP_CRITERION);
sr.setMaximumBestFound(3);
sr.compute(x, y);
SelectionRegression.Statistics stats = sr.getStatistics();
criterionOption = "Mallows Cp";
for (int i = 1; i <= 4; i++) {
double[] tmpCrit = stats.getCriterionValues(i);
int[][] indvar = stats.getIndependentVariables(i);
Object p[] = {new Integer(i), criterionOption};
System.out.println(critMsg.format(p));
Object p1[] = {null};
System.out.println(critLabel.format(p1));
for (int j = 0; j < tmpCrit.length; j++) {
System.out.print(" " + tmpCrit[j] + " ");
for (int k = 0; k < indvar[j].length; k++) {
System.out.print(indvar[j][k] + " ");
}
System.out.println("");
}
System.out.println("");
}
for (int i = 0; i < 3; i++) {
System.out.println("");
double[][] tmpCoef = stats.getCoefficientStatistics(i);
Object p[] = {new Integer(tmpCoef.length), criterionOption};
System.out.println(coefMsg.format(p));
Object p2[] = {null};
System.out.println(coefLabel.format(p2));
PrintMatrix pm = new PrintMatrix();
pm.setColumnSpacing(10);
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(4);
PrintMatrixFormat tst = new PrintMatrixFormat();
tst.setNoColumnLabels();
tst.setNoRowLabels();
tst.setNumberFormat(nf);
pm.print(tst, tmpCoef);
System.out.println();
System.out.println();
}
}
}