package com.imsl.test.example.stat; import com.imsl.stat.*; import com.imsl.math.*; /** *

Fits a regression to a polynomial with user defined basis functions. *

*

* In this example, we fit the polynomial \(y = c_0 + c_1 x + c_2 x^2 \cdots + * c_4 x^4 + \varepsilon\). For this model, the user basis function is \(f_i (x) * = x^{i + 1}\) with \(i = 0,1, \cdots ,nBasis = 4\) and * hasIntercept=true. *

*

* Data are generated using the model \(y = 10 + 2x + 5x^3\) with \(x = 0,1, * \cdots 9\).

* * * * @see Code * @see Output */ public class UserBasisRegressionEx2 { public static void main(String args[]) { class Basis implements RegressionBasis { @Override public double basis(int index, double x) { // Notice zero-based indexing requires index be incremented return Math.pow(x, index + 1); } } UserBasisRegression ubr = new UserBasisRegression(new Basis(), 4, true); for (double x = 0; x < 10; x++) { double y = 10.0 + x + 3 * Math.pow(x, 3); ubr.update(x, y, 1.0); } // Print the regression coefficients double coef[] = ubr.getCoefficients(); new PrintMatrix("The regression coefficients are:").print(coef); // Print the Anova Table ANOVA anovaTable = ubr.getANOVA(); System.out.println("Degrees Of Freedom For Model: " + anovaTable.getDegreesOfFreedomForModel()); System.out.println("Degrees Of Freedom For Error: " + anovaTable.getDegreesOfFreedomForError()); System.out.println("Total (Corrected) Degrees Of Freedom: " + anovaTable.getTotalDegreesOfFreedom()); System.out.println("Sum Of Squares For Model: " + anovaTable.getSumOfSquaresForModel()); System.out.println("Sum Of Squares For Error: " + anovaTable.getSumOfSquaresForError()); System.out.println("Total (Corrected) Sum Of Squares: " + anovaTable.getTotalSumOfSquares()); System.out.println("Model Mean Square: " + anovaTable.getModelMeanSquare()); System.out.println("Error Mean Square: " + anovaTable.getErrorMeanSquare()); System.out.println("F statistic: " + anovaTable.getF()); System.out.println("P value: " + anovaTable.getP()); System.out.println("R Squared (in percent): " + anovaTable.getRSquared()); System.out.println("Adjusted R Squared (in percent): " + anovaTable.getAdjustedRSquared()); System.out.println("Model Error Standard deviation: " + anovaTable.getModelErrorStdev()); System.out.println("Mean Of Y: " + anovaTable.getMeanOfY()); System.out.println("Coefficient Of Variation (in percent): " + anovaTable.getCoefficientOfVariation()); } }