Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Simple 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 simple linear regression
    ' by fitting a linear equation to two 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()

            ' Create a calculator.
            Dim calculator As New StatsCalculator()

            ' Use the calculator to load the sheet.
            Dim sheet As DataSheet = calculator.Load( _
                "..\..\..\..\..\Data\Temperatures.csv")
            Console.WriteLine(sheet)

            ' The above data sheet holds data for the pressure of a chemical
            ' substance at different temperatures. Suppose we conjecture that
            ' there is a linear relationship between the reciprocal of the
            ' temperature in Kelvin and the logarithm of the pressure. We can
            ' use linear regression to verify this conjecture.
            '
            ' The first step is to create the new variables (i.e. the
            ' reciprocal of the temperature in Kelvin and the logarithm of the
            ' pressure) by transforming the above data sheet. 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. 
            '
            ' We can create the new variables by creating columns for Log(P),
            ' T then 1/T. To do this, the three expressions we are looking for
            ' are: "Log(P, 10)", "[T (Celsius)] + 273.15" and "1/T".
            '
            ' The first expression is the logarithm of the P column to base 10.
            ' The second expression uses square brackets to escape a column
            ' name since the column name contains spaces. The third expression
            ' is the reciprocal of the new T column, i.e. the reciprocal of the
            ' temparture in Kelvin.

            sheet.Columns.Create("Log(P)", "Log(P, 10)")
            sheet.Columns.Create("T", "[T (Celsius)] + 273.15")
            sheet.Columns.Create("1/T", "1/T")
            Console.WriteLine(sheet)

            ' Perform linear regression with an intercept (true value)
            ' between the Log(P) and 1/T columns.
            Dim regression _
                As New LinearRegression(sheet, True, "Log(P)", "1/T")

            ' 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