package com.imsl.test.example.stat;
import com.imsl.stat.*;
import com.imsl.math.*;
/**
*
* Sets up data for a two-way analysis of covariance.
*
*
* In this example, a two-way analysis of covariance model containing all the
* interaction terms is fit. First, RegressorsForGLM
is used to
* produce a matrix of regressors, regressors
, from the data
* x
. Then, regressors
is used as the input matrix
* into Regression
to produce the final fit. The mapping from
* effects to columns in the matrix regressors
is given by the
* jagged array returned by getEffectsColumns
. Each row in this
* matrix gives the column number for the corresponding effect.
*
*
*
* @see Code
* @see Output
*/
public class RegressorsForGLMEx2 {
static public void main(String arg[]) {
double x[][] = {
{1.0, 1.0, 1.11},
{1.0, 1.0, 2.22},
{1.0, 1.0, 3.33},
{1.0, 2.0, 1.11},
{1.0, 2.0, 2.22},
{1.0, 2.0, 3.33},
{1.0, 3.0, 1.11},
{1.0, 3.0, 2.22},
{1.0, 3.0, 3.33},
{2.0, 1.0, 1.11},
{2.0, 1.0, 2.22},
{2.0, 1.0, 3.33},
{2.0, 2.0, 1.11},
{2.0, 2.0, 2.22},
{2.0, 2.0, 3.33},
{2.0, 3.0, 1.11},
{2.0, 3.0, 2.22},
{2.0, 3.0, 3.33}
};
double y[] = {
1.0, 2.0, 2.0, 4.0, 4.0, 6.0,
3.0, 3.5, 4.0, 4.5, 5.0, 5.5,
2.0, 3.0, 4.0, 5.0, 6.0, 7.0
};
int effects[][] = {
{0},
{1},
{0, 1},
{2},
{0, 2},
{1, 2},
{0, 1, 2}
};
int nClassVariables = 2;
RegressorsForGLM r = new RegressorsForGLM(x, nClassVariables);
r.setDummyMethod(RegressorsForGLM.LEAVE_OUT_LAST);
r.setEffects(effects);
System.out.println("Mapping of Effects to Regressor Columns");
int col[][] = r.getEffectsColumns();
for (int iEffect = 0; iEffect < effects.length; iEffect++) {
System.out.print("Effect {");
for (int j = 0; j < effects[iEffect].length; j++) {
if (j > 0) {
System.out.print(", ");
}
System.out.print(effects[iEffect][j]);
}
System.out.print("} is in regressor column(s) {");
for (int j = 0; j < col[iEffect].length; j++) {
if (j > 0) {
System.out.print(", ");
}
System.out.print(col[iEffect][j]);
}
System.out.println("}");
}
System.out.println();
double regressors[][] = r.getRegressors();
PrintMatrixFormat pmf = new PrintMatrixFormat();
pmf.setColumnLabels(new String[]{"Alpha1", "Beta1", "Beta2",
"Gamma11", "Gamma12", "Delta", "Zeta1", "Eta1", "Eta2",
"Theta11", "Theta12"});
new PrintMatrix("Regressors").print(pmf, regressors);
int nRegressors = r.getNumberOfRegressors();
LinearRegression regression = new LinearRegression(nRegressors, true);
regression.update(regressors, y);
System.out.println(" * * * Analysis of Variance * * *");
ANOVA anova = regression.getANOVA();
Object table[][] = new Object[15][2];
table[0][0] = "Degrees of freedom for the model ";
table[0][1] = anova.getDegreesOfFreedomForModel();
table[1][0] = "Degrees of freedom for the error ";
table[1][1] = anova.getDegreesOfFreedomForError();
table[2][0] = "Total degrees of freedom ";
table[2][1] = anova.getTotalDegreesOfFreedom();
table[3][0] = "Sum of squares for the model ";
table[3][1] = anova.getSumOfSquaresForModel();
table[4][0] = "Sum of squares for error ";
table[4][1] = anova.getSumOfSquaresForError();
table[5][0] = "Total sum of squares ";
table[5][1] = anova.getTotalSumOfSquares();
table[6][0] = "Model mean square ";
table[6][1] = anova.getModelMeanSquare();
table[7][0] = "Error mean square ";
table[7][1] = anova.getErrorMeanSquare();
table[8][0] = "F-statistic ";
table[8][1] = anova.getF();
table[9][0] = "p-value ";
table[9][1] = anova.getP();
table[10][0] = "R-squared ";
table[10][1] = anova.getRSquared();
table[11][0] = "Adjusted R-squared ";
table[11][1] = anova.getAdjustedRSquared();
table[12][0] = "Standard deviation for the model error";
table[12][1] = anova.getModelErrorStdev();
table[13][0] = "Overall mean of y ";
table[13][1] = anova.getMeanOfY();
table[14][0] = "Coefficient of variation ";
table[14][1] = anova.getCoefficientOfVariation();
pmf = new PrintMatrixFormat();
pmf.setNoColumnLabels();
pmf.setNoRowLabels();
pmf.setNumberFormat(new java.text.DecimalFormat("0.0000"));
new PrintMatrix().print(pmf, table);
}
}