Polynomial Regression Example (C#)
[C#]
' COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
' All rights reserved.
'
' Use of this copyright notice is precautionary only, and does not imply
' publication or disclosure. The content of this work contains confidential
' and proprietary information of Simplexar Software Limited. Any duplication,
' modification, distribution, or disclosure in any form, in whole, or in part,
' is strictly prohibited without express prior written permission.
' Get namespaces.
Imports System
Imports Simplexar.Statsar
Imports Simplexar.Statsar.LinearAlgebra
' Start namespace.
Namespace Simplexar.Examples.Statsar
' This example explains how to perform polynomial regression
' by fitting a polynomial curve to observed data.
Module Example
' The application entry point.
Public Sub Main()
Try
RunExample()
Catch exception As Exception
Console.WriteLine(exception)
End Try
Console.WriteLine("Press ENTER to terminate.")
Console.ReadLine()
End Sub
Private Sub RunExample()
' Create a calculator.
Dim calculator As New StatsCalculator()
' Use the calculator to load the sheet.
Dim sheet As DataSheet = calculator.Load( _
"..\..\..\..\..\Data\Polynomial.csv")
Console.WriteLine(sheet)
' Polynomial regression attempts to fit a polynomial curve
' to observed data. Note that this is still a special case
' of multiple linear regression since we try to fit a linear
' equation to additional high-order variables.
'
' The data sheet above stores data for two variables, X and Y.
' Suppose we conjecture that there is a quadratic relationship
' between X and Y. We can use linear regression to verify this
' conjecture. The first step is to create new higher-order
' variables. In this case, we need to create a new column to
' hold the square of X, since we are assuming a quadratic
' relationship.
'
' To do this, we can use the Create() method on the DataSheet
' columns collection. This method takes two parameters, the new
' column name and an expression to perform the transformation.
' The expression we are looking for is: "X * X".
sheet.Columns.Create("X^2", "X * X")
Console.WriteLine(sheet)
' Perform linear regression with an intercept (true value)
' between the Y, X and X^2 columns.
Dim regression _
As New LinearRegression(sheet, True, "Y", "X", "X^2")
' Display model.
Console.WriteLine(regression.ModelText)
' Access parameters.
Console.WriteLine( _
"Parameter 0: {0}", regression.Parameters(0))
Console.WriteLine( _
"Parameter 1: {0}", regression.Parameters(1))
Console.WriteLine( _
"Parameter 2: {0}", regression.Parameters(2))
' Standard deviation for parameter 0.
Console.WriteLine( _
"Standard deviation for parameter 0: {0}", _
regression.GetStandardDeviation(0))
' Confidence interval (5%) for parameter 0.
Dim alpha As Double = 0.05
Console.WriteLine( _
"Confidence interval (5%) for parameter 0: {0}", _
regression.GetConfidenceInterval(alpha, 0))
' Model statistics.
Console.WriteLine( _
"Model variance: {0}", regression.Variance)
Console.WriteLine( _
"Model standard deviation: {0}", _
regression.StandardDeviation)
Console.WriteLine( _
"Coefficient of determination: {0}", _
regression.CoefficientOfDetermination)
Console.WriteLine( _
"Adjusted coefficient of determination: {0}", _
regression.AdjustedCoefficientOfDetermination)
Console.WriteLine( _
"Mean response: {0}", _
regression.MeanResponse)
' Covariance matrix.
Console.WriteLine("Covariance matrix:")
Console.WriteLine(regression.CovarianceMatrix)
End Sub
End Module
End Namespace