VB.NET Multiple Linear Regression Example

[TOC]

Imports System
Imports System.IO
Imports System.Collections

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats

Namespace CenterSpace.NMath.Stats.Examples.VisualBasic

  ' A .NET example in VB.NET showing how to perform multiple linear regression.
  Module MultipleLinearRegressionExample

    Sub Main()

      Dim Filename As String = "..\\..\\MultipleLinearRegressionExample.dat"
      Dim DataStream As StreamReader

      Try
        DataStream = New StreamReader(Filename)
      Catch E As FileNotFoundException
        Dim Msg As String = String.Format("Could not find data file {0}.", Filename)
        Msg += Environment.NewLine
        Msg += E.Message
        Msg += Environment.NewLine
        Msg += "Data file must have the same name as the example source "
        Msg += Environment.NewLine
        Msg += "file and be located two directories up from where the "
        Msg += Environment.NewLine
        Msg += "executable is running."
        Console.WriteLine(Msg)
        Return
      End Try

      ' First read in the independent (or predictor) values. This is a matrix
      ' with one column and a row for each amounts measurement.
      Dim LongLatValues As DoubleMatrix = New DoubleMatrix(DataStream)

      ' Next, read in the resposnes. These are the readings of the gas 
      ' chromatograph
      Dim JanTemp As DoubleVector = New DoubleVector(DataStream)

      ' Print out the amounts and responses values.
      Console.WriteLine()
      Console.WriteLine("Longitudes and latitudes = " & LongLatValues.ToString())
      Console.WriteLine()
      Console.WriteLine("January temperatures = " & JanTemp.ToString())
      Console.WriteLine()

      Dim Regression As LinearRegression = New LinearRegression(LongLatValues, JanTemp, True)
      Dim RegressionAnova As LinearRegressionAnova = New LinearRegressionAnova(Regression)

      Dim RegressionStats As DataFrame = New DataFrame()
      RegressionStats.RowKeyHeader = "Source      "
      RegressionStats.AddColumn(New DFNumericColumn("Sum of Squares"))
      Dim DFCol As DFIntColumn = New DFIntColumn("df")
      RegressionStats.AddColumn(DFCol)
      RegressionStats.AddColumn(New DFNumericColumn("Mean Square"))
      RegressionStats.AddColumn(New DFNumericColumn("F-ratio"))

      RegressionStats.AddRow("Regression", RegressionAnova.RegressionSumOfSquares, RegressionAnova.ModelDegreesOfFreedom, RegressionAnova.MeanSquaredRegression, RegressionAnova.FStatistic)
      RegressionStats.AddRow("Residual", RegressionAnova.ResidualSumOfSquares, RegressionAnova.ErrorDegreesOfFreedom, RegressionAnova.MeanSquaredResidual, Double.NaN)

      Console.WriteLine("Dependent Variable is:       January Temp")
      Console.WriteLine(Regression.NumberOfObservations & " total cases")
      Console.WriteLine("R squared = " & (RegressionAnova.RSquared * 100) & "%,  R squared (adjusted) = " & (RegressionAnova.AdjustedRsquared * 100) & "%")

      Console.WriteLine("s = " & Math.Sqrt(Regression.Variance) & " with " & (Regression.NumberOfObservations - Regression.NumberOfParameters) & " degrees of freedom")
      Console.WriteLine()
      Console.WriteLine(RegressionStats)

      Console.WriteLine()
      Console.WriteLine("Press Enter Key")
      Console.ReadKey()

    End Sub

  End Module

End Namespace

[TOC]