VB Simple Linear Regression Example

← All NMath Code Examples


Imports System
Imports System.IO
Imports System.Collections

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic showing how to use the linear regression class to perform a simple
  linear regression.

  A simple linear regression fits a straight line through a series of data
  points. Specifically, the points <c>(x,y)</c> are assumed to conform to 
  the equation <c>y = mx + b</c>. A simple linear regression uses a least 
  squares fit to compute the slope, <c>m</c>, and the y-intercept, <c>b</c>.

  Module SimpleLinearRegressionExample

    Sub Main()

      Read in data from the file. The data comes from The Data and Story 
      Library (http:lib.stat.cmu.edu/DASL) and is described below:
      Results of a study of gas chromatography, a technique which is used 
      to detect very small amounts of a substance. Five measurements were
      taken for each of four specimens containing different amounts of the
      substance. The amounts of the substance in each specimen was determined
      before the experiment. The responses variable is the output reading 
      from the gas chromatograph. The purpose of the study is to calibrate
      the chromatograph by relating the actual amounts of the substance to
      the chromatograph reading.

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

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

      Next, read in the responses. These are the readings of the gas 
      Dim Responses As DoubleVector = New DoubleVector(DataStream)

      Print out the amounts and responses values.
      Console.WriteLine("amounts = " & Amounts.ToString())
      Console.WriteLine("responses = " & Responses.ToString())

      Construct a linear regression. If we want our regression to calculate a
      y-intercept we must send in true for the "addIntercept" parameter (the
      third parameter in the constructor).
      Dim Regression As LinearRegression = New LinearRegression(Amounts, Responses, True)

      The y-intercept is the first element of the parameter array returned by 
      the regression, and the slope is the second.
      Console.WriteLine("y-intercept = " & Regression.Parameters(0))
      Console.WriteLine("Slope = " & Regression.Parameters(1))

      What would the model predict for the chromatograph reading for an
      input amounts of 0.75 and 1.25?
      Dim Amount As DoubleVector = New DoubleVector(0.75)
      Dim PredictedReading As Double = Regression.PredictedObservation(Amount)
      Console.WriteLine("Predicted reading for amount " & Amount(0) & " is " & PredictedReading)
      Amount(0) = 1.25
      PredictedReading = Regression.PredictedObservation(Amount)
      Console.WriteLine("Predicted reading for amount " & Amount(0) & " is " & PredictedReading)

      Lets look at the coefficient of determination for the model - Rsquared.
      To do this we will need to construct an Analysis Of VAriance object for 
      the regression.
      Dim RegressionAnova As New LinearRegressionAnova(Regression)
      Console.WriteLine("Rsquare = " & RegressionAnova.RSquared)

      Console.WriteLine("Press Enter Key")

    End Sub

  End Module

End Namespace
← All NMath Code Examples