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.
The regressors, generated using the dummy method LEAVE_OUT_LAST
, are the model whose mean function is
import java.text.*; import com.imsl.stat.*; import com.imsl.math.PrintMatrix; import com.imsl.math.PrintMatrixFormat; import java.text.MessageFormat; public class RegressorsForGLMEx2 { static MessageFormat mf = new MessageFormat("{0} {1,number,#0.0000}"); 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); } }
Mapping of Effects to Regressor Columns Effect {0} is in regressor column(s) {0} Effect {1} is in regressor column(s) {1, 2} Effect {0, 1} is in regressor column(s) {3, 4} Effect {2} is in regressor column(s) {5} Effect {0, 2} is in regressor column(s) {6} Effect {1, 2} is in regressor column(s) {7, 8} Effect {0, 1, 2} is in regressor column(s) {9, 10} Regressors Alpha1 Beta1 Beta2 Gamma11 Gamma12 Delta Zeta1 Eta1 Eta2 Theta11 Theta12 0 1 1 0 1 0 1.11 1.11 1.11 0 1.11 0 1 1 1 0 1 0 2.22 2.22 2.22 0 2.22 0 2 1 1 0 1 0 3.33 3.33 3.33 0 3.33 0 3 1 0 1 0 1 1.11 1.11 0 1.11 0 1.11 4 1 0 1 0 1 2.22 2.22 0 2.22 0 2.22 5 1 0 1 0 1 3.33 3.33 0 3.33 0 3.33 6 1 0 0 0 0 1.11 1.11 0 0 0 0 7 1 0 0 0 0 2.22 2.22 0 0 0 0 8 1 0 0 0 0 3.33 3.33 0 0 0 0 9 0 1 0 0 0 1.11 0 1.11 0 0 0 10 0 1 0 0 0 2.22 0 2.22 0 0 0 11 0 1 0 0 0 3.33 0 3.33 0 0 0 12 0 0 1 0 0 1.11 0 0 1.11 0 0 13 0 0 1 0 0 2.22 0 0 2.22 0 0 14 0 0 1 0 0 3.33 0 0 3.33 0 0 15 0 0 0 0 0 1.11 0 0 0 0 0 16 0 0 0 0 0 2.22 0 0 0 0 0 17 0 0 0 0 0 3.33 0 0 0 0 0 * * * Analysis of Variance * * * Degrees of freedom for the model 11.0000 Degrees of freedom for the error 6.0000 Total degrees of freedom 17.0000 Sum of squares for the model 43.9028 Sum of squares for error 0.8333 Total sum of squares 44.7361 Model mean square 3.9912 Error mean square 0.1389 F-statistic 28.7364 p-value 0.0003 R-squared 98.1372 Adjusted R-squared 94.7221 Standard deviation for the model error 0.3727 Overall mean of y 3.9722 Coefficient of variation 9.3821Link to Java source.