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.

using System;
using Imsl.Stat;
using Imsl.Math;

public class UserBasisRegressionEx2 : IRegressionBasis
{
	public double Basis(int index, double x)
	{
		// Notice zero-based indexing requires index be incremented
		return System.Math.Pow(x, index + 1);
	}

	public static void Main(string[] args)
	{
		double y = 0;
		double[] coef ;
		IRegressionBasis basis = new UserBasisRegressionEx2();
		UserBasisRegression ubr = 
			new UserBasisRegression(basis, 4, true);
		for (double x = 0; x < 10; x++)
		{
			y = 10.0 + x + 3 * System.Math.Pow(x, 3);
			ubr.Update(x, y, 1.0);
		}
		coef = ubr.GetCoefficients();
		PrintMatrix pm = new PrintMatrix("The regression coefficients are:");
		PrintMatrixFormat pmf = new PrintMatrixFormat();
		pmf.NumberFormat = "0.0";
		pm.Print(pmf, coef);
		
		// Print the Anova Table
		ANOVA anovaTable = ubr.ANOVA;
		
		System.Console.Out.WriteLine("Degrees Of Freedom For Model:          "
         + anovaTable.DegreesOfFreedomForModel);
		System.Console.Out.WriteLine("Degrees Of Freedom For Error:          "
         + anovaTable.DegreesOfFreedomForError);
		System.Console.Out.WriteLine("Total (Corrected) Degrees Of Freedom:  "
         + anovaTable.TotalDegreesOfFreedom);
		System.Console.Out.WriteLine("Sum Of Squares For Model:              "
         + anovaTable.SumOfSquaresForModel);
		System.Console.Out.WriteLine("Sum Of Squares For Error:              "
         + anovaTable.SumOfSquaresForError);
		System.Console.Out.WriteLine("Total (Corrected) Sum Of Squares:      "
         + anovaTable.TotalSumOfSquares);
		System.Console.Out.WriteLine("Model Mean Square:                     "
         + anovaTable.ModelMeanSquare);
		System.Console.Out.WriteLine("Error Mean Square:                     "
         + anovaTable.ErrorMeanSquare);
		System.Console.Out.WriteLine("F statistic:                           "
         + anovaTable.F);
		System.Console.Out.WriteLine("P value:                               "
         + anovaTable.P);
		System.Console.Out.WriteLine("R Squared (in percent):                "
         + anovaTable.RSquared);
		System.Console.Out.WriteLine("Adjusted R Squared (in percent):       "
         + anovaTable.AdjustedRSquared);
		System.Console.Out.WriteLine("Model Error Standard deviation:        "
         + anovaTable.ModelErrorStdev);
		System.Console.Out.WriteLine("Mean Of Y:                             "
         + anovaTable.MeanOfY);
		System.Console.Out.WriteLine("Coefficient Of Variation (in percent): "
         + anovaTable.CoefficientOfVariation);
	}
}

Output

The regression coefficients are:
    0    
0  10.0  
1  1.0   
2  0.0   
3  3.0   
4  0.0   

Degrees Of Freedom For Model:          4
Degrees Of Freedom For Error:          5
Total (Corrected) Degrees Of Freedom:  9
Sum Of Squares For Model:              5152488
Sum Of Squares For Error:              1.86264514923096E-09
Total (Corrected) Sum Of Squares:      5152488
Model Mean Square:                     1288122
Error Mean Square:                     3.72529029846191E-10
F statistic:                           3.45777616453632E+15
P value:                               8.69662785564134E-39
R Squared (in percent):                100
Adjusted R Squared (in percent):       99.9999999999999
Model Error Standard deviation:        1.93010111094261E-05
Mean Of Y:                             622
Coefficient Of Variation (in percent): 3.10305644846079E-06

Link to C# source.