Example: Regression with User-supplied 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.

import com.imsl.stat.*;
import com.imsl.math.*;

public class UserBasisRegressionEx2 {

    public static void main(String args[]) {
        class Basis implements RegressionBasis {
            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());
    }
}

Output

The regression coefficients are:
   0   
0  10  
1   1  
2  -0  
3   3  
4  -0  

Degrees Of Freedom For Model:          4.0
Degrees Of Freedom For Error:          5.0
Total (Corrected) Degrees Of Freedom:  9.0
Sum Of Squares For Model:              5152487.999999998
Sum Of Squares For Error:              1.862645149230957E-9
Total (Corrected) Sum Of Squares:      5152488.0
Model Mean Square:                     1288121.9999999995
Error Mean Square:                     3.7252902984619143E-10
F statistic:                           3.4577761645363185E15
P value:                               8.696627855641336E-39
R Squared (in percent):                99.99999999999997
Adjusted R Squared (in percent):       99.99999999999993
Model Error Standard deviation:        1.9301011109426144E-5
Mean Of Y:                             622.0
Coefficient Of Variation (in percent): 3.103056448460795E-6
Link to Java source.