VB Multiple Linear Regression Example

← All NMath Core Code Examples

 

Imports System
Imports System.IO
Imports System.Collections

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

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

    Sub Main()

      Dim DataStream As New StreamReader("MultipleLinearRegressionExample.dat")

      ' 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 responses. 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 =")
      Console.WriteLine(LongLatValues.ToTabDelimited("G7"))
      Console.WriteLine()
      Console.WriteLine("January temperatures =")
      Console.WriteLine(JanTemp)
      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.ToString("P"))
      Console.WriteLine("R squared (adjusted) = " & RegressionAnova.AdjustedRsquared.ToString("P"))

      Console.WriteLine("s = {0} with {1} degrees of freedom",
        System.Math.Sqrt(Regression.Variance).ToString("G5"),
        Regression.NumberOfObservations - Regression.NumberOfParameters)
      Console.WriteLine()
      Console.WriteLine(RegressionStats)

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

    End Sub

  End Module

End Namespace

← All NMath Stats Code Examples
Top