Updated Version 5.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Multiple Linear Regression QuickStart Sample (Visual Basic)

Illustrates how to use the LinearRegressionModel class to perform a multiple linear regression in Visual Basic.

C# code F# code IronPython code Back to QuickStart Samples

Imports System.IO
Imports Extreme.Mathematics
Imports Extreme.Statistics
Imports Extreme.Statistics.Tests

Namespace Extreme.Numerics.QuickStart.VB
    ' Illustrates the use of the LinearRegressionModel class 
    ' to perform multiple linear regression.
    Module MultipleRegression

        Sub Main()
            ' Multiple linear regression can be performed using 
            ' the LinearRegressionModel class.
            ' This QuickStart sample uses old economic data about 50 countries
            ' from Belsley, Kuh and Welsch. The fields are as follows:
            '   DispInc: Per capita disposable income.
            '   Growth:  Percent rate of change of DispInc.
            '   Pop15:   Percentage of population under 15.
            '   Pop75:   Percentage of population over 75.
            '   Savings: Aggregate savings divided by disposable income.
            ' We want to investigate the effect of the first four variables
            ' on the savings ratio.

            ' First, read the data from a file into an ADO.NET DataTable. 
            ' For the sake of clarity, we put this code in its own method.
            Dim table As DataTable = ReadData()
            If table Is Nothing Then Exit Sub

            ' Next, create a VariableCollection from the data table:
            Dim Data As VariableCollection = New VariableCollection(table)

            ' Now create the regression model. Parameters are the name 
            ' of the dependent variable, a string array containing 
            ' the names of the independent variables, and the VariableCollection
            ' containing all variables.
            Dim model As LinearRegressionModel = New LinearRegressionModel(Data, _
                "Savings", New String() {"Pop15", "Pop75", "DispInc", "Growth"})

            ' We can set model options now, such as whether to include a constant:
            model.NoIntercept = False

            ' The Compute method performs the actual regression analysis.

            ' The Parameters collection contains information about the regression 
            ' parameters.
            Console.WriteLine("Variable              Value    Std.Error  t-stat  p-Value")
            For Each param As Parameter In model.Parameters
                ' Parameter objects have the following properties, in order:
                '   - Name, usually the name of the variable:
                '   - Estimated value of the parameter:
                '   - Standard error:
                '   - The value of the t statistic for the hypothesis that the parameter is zero.
                '   - Probability corresponding to the t statistic.
                Console.WriteLine("{0,-20}{1,10:F5}{2,10:F5}{3,8:F2} {4,7:F4}", _
                    param.Name, param.Value, param.StandardError, param.Statistic, param.PValue)

            ' In addition to these properties, Parameter objects have a GetConfidenceInterval
            ' method that returns a confidence interval at a specified confidence level.
            ' Notice that individual parameters can be accessed using their numeric index.
            ' Parameter 0 is the intercept, if it was included.
            Dim confidenceInterval As Interval = model.Parameters(0).GetConfidenceInterval(0.95)
            Console.WriteLine("95% confidence interval for Growth: {0:F4} - {1:F4}", _
                confidenceInterval.LowerBound, confidenceInterval.UpperBound)

            ' Parameters can also be accessed by name:
            confidenceInterval = model.Parameters("DispInc").GetConfidenceInterval(0.95)
            Console.WriteLine("95% confidence interval for Growth: {0:F4} - {1:F4}", _
                confidenceInterval.LowerBound, confidenceInterval.UpperBound)

            ' There is also a wealth of information about the analysis available
            ' through various properties of the LinearRegressionModel object:
            Console.WriteLine("Residual standard error: {0:F3}", model.StandardError)
            Console.WriteLine("R-Squared:               {0:F4}", model.RSquared)
            Console.WriteLine("Adjusted R-Squared:      {0:F4}", model.AdjustedRSquared)
            Console.WriteLine("F-statistic:             {0:F4}", model.FStatistic)
            Console.WriteLine("Corresponding p-value:   {0:F5}", model.PValue)

            ' Much of this data can be summarized in the form of an ANOVA table:

            Console.WriteLine("Press Enter key to continue.")
        End Sub

        ' Reads the data from a text file into a <see cref="DataTable"/>.
        ' Returns a DataTable.
        Public Function ReadData() As DataTable
            Dim data As DataTable = New DataTable("savings")

            data.Columns.Add("Key", GetType(String))
            Dim whitespace As Char() = New Char() {" "c, Chr(9)}
            Dim sr As StreamReader
                sr = New StreamReader("..\..\..\Data\savings.dat")
            Catch ex As FileNotFoundException
                Console.WriteLine("The data file could not be found. Please verify that the path is correct.")
                Return Nothing
            End Try

            ' Read the header and extract the field names.
            Dim line As String = sr.ReadLine()
            Dim pos As Integer = 0
            Dim pos2 As Integer
                Do While (Char.IsWhiteSpace(line.Chars(pos)))
                    pos = pos + 1
                pos2 = line.IndexOfAny(whitespace, pos)
                If (pos2 < 0) Then
                    data.Columns.Add(line.Substring(pos), GetType(Double))
                    Exit Do
                    data.Columns.Add(line.Substring(pos, pos2 - pos), GetType(Double))
                End If
                pos = pos2
            Loop While (pos >= 0)

            ' Now read the data and add them to the table.
            ' Assumes all columns except the first are numerical.
            Dim rowData As Object() = New Object(data.Columns.Count - 1) {}
            line = sr.ReadLine()
            Do While (Not (line Is Nothing) AndAlso line.Length > 0)

                Dim column As Integer = 0
                pos = 0
                    Dim field As String
                    Do While (Char.IsWhiteSpace(line.Chars(pos)))
                        pos = pos + 1
                    pos2 = line.IndexOfAny(whitespace, pos)
                    If (pos2 < 0) Then
                        field = line.Substring(pos)
                        field = line.Substring(pos, pos2 - pos)
                    End If
                    If (column = 0) Then
                        rowData(column) = field
                        rowData(column) = Double.Parse(field)
                    End If
                    column = column + 1
                    pos = pos2
                Loop While (pos >= 0 And column < data.Columns.Count)

                line = sr.ReadLine()
            Return data
        End Function
    End Module

End Namespace