Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Multiple Linear Regression Example (VB.NET)

[VB.NET]
' 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 multiple linear regression
    ' by fitting a linear equation to multiple variables.
    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()

            ' Multiple linear regression is similar to simple linear
            ' regression with the exception that multiple predictor
            ' vectors are used to generate a response. Consider the
            ' following two predictor vectors:

            ' First predictor.
            Dim x1 As New Vector( _
                -1.039, -1.018, -0.986, -0.001, -0.043, _
                0.021, 1.012, 0.998, 1.033, 1.977)
            Console.WriteLine("X1:")
            Console.WriteLine(x1)
            Console.WriteLine()

            ' Second predictor.
            Dim x2 As New Vector( _
                -0.953, -0.017, 0.959, -1.02, -0.044, _
                0.957, -1.014, 0.035, 0.983, 1.999)
            Console.WriteLine("X2:")
            Console.WriteLine(x2)
            Console.WriteLine()

            ' We also require a response vector:
            Dim y As New Vector( _
                0.999, -0.46, -1.97, 1.448, 0.048, _
                -1.342, 1.956, 0.521, -0.892, -2.016)
            Console.WriteLine("Y:")
            Console.WriteLine(y)
            Console.WriteLine()

            ' Perform linear regression with no intercept (false value)
            ' between Y, X1 and X2.
            Dim regression _
                As New LinearRegression(False, y, x1, x2)

            ' Display model.
            Console.WriteLine(regression.ModelText)

            ' Access parameters.
            Console.WriteLine( _
                "Parameter 0: {0}", regression.Parameters(0))
            Console.WriteLine( _
                "Parameter 1: {0}", regression.Parameters(1))

            ' 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)

            ' Prediction.
            Dim predictor As Double = 0.0034
            Dim response As Double = regression.GetPrediction(predictor)
            Console.WriteLine( _
                "Predictor = {0}, Response = {1}", _
                predictor, response)

            ' Prediction interval (5%) for predictor.
            Console.WriteLine( _
                "Prediction interval (5%) for predictor: {0}", _
                regression.GetPredictionInterval(alpha, predictor))

            ' Covariance matrix.
            Console.WriteLine("Covariance matrix:")
            Console.WriteLine(regression.CovarianceMatrix)

        End Sub

    End Module

End Namespace