[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 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 Filename As String = "..\\..\\SimpleLinearRegressionExample.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 Amounts As DoubleMatrix = New DoubleMatrix(DataStream)
' Next, read in the resposnes. These are the readings of the gas
' chromatograph
Dim Responses As DoubleVector = New DoubleVector(DataStream)
' Print out the amounts and responses values.
Console.WriteLine()
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 paramameter 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)
' Let's 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()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]