[TOC]
Imports System
Imports System.IO
Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats
Namespace CenterSpace.NMath.Stats.Examples.CSharp
' A .NET example in VB.NET showing basic use of the PLS1 and PLS2 classes for solving
' partial least squares (PLS) problems.
Module PLSExample
Sub Main()
' Read in some chemometric data. The response, or Y, variable
' is a measure of the concetration of substances in a sample and
' the predictor, or x, variable is the absorption spectra of the sample
' sampled at discrete wavellengths. We'll read in two sets of predictor
' values. One to construct the model with and the other for making
' predictions.
Dim x1Datafilename As String = "..\\..\\chemometricX1.dat"
Dim x2Datafilename As String = "..\\..\\chemometricX2.dat"
Dim yDatafilename As String = "..\\..\\chemometricY.dat"
Dim x1DataStream As StreamReader
Dim x2DataStream As StreamReader
Dim yDataStream As StreamReader
Try
x1DataStream = New StreamReader(x1Datafilename)
x2DataStream = New StreamReader(x2Datafilename)
yDataStream = New StreamReader(yDatafilename)
Catch E As FileNotFoundException
Dim msg As String = String.Format("Could not find data file " & x1Datafilename)
msg += ", "
msg += x2Datafilename
msg += ", "
msg += yDatafilename
msg += "."
msg += Environment.NewLine
msg += E.Message
Console.WriteLine(msg)
Return
End Try
' Construct the absorption matrices A and A1, and the concentration
' matrix C
Dim A As DoubleMatrix = New DoubleMatrix(x1DataStream)
Dim A1 As DoubleMatrix = New DoubleMatrix(x2DataStream)
Dim C As DoubleMatrix = New DoubleMatrix(yDataStream)
Dim NumComponents As Integer = 3
'**********************************************************************
' PLS1
' PLS1 is used when the response variable is univariate, or one dimensional.
' Pick out the first column of our multivariate response variables as our
' univariate response variable y.
Dim Y As DoubleVector = C.Col(0)
' Construct a PLS1 object and perform the calculation.
Dim PlsOne As PLS1 = New PLS1()
Console.WriteLine()
Console.WriteLine("Calculating PLS1...")
PlsOne.Calculate(A, Y, NumComponents)
' Check that the calculation succeeded. If it did not, prInteger out
' a diagnostic error message and exit.
If (PlsOne.IsGood) Then
Console.WriteLine("Success")
Else
Console.WriteLine("PLS1 calculation failed: " & PlsOne.Message)
Return
End If
' Pull out a sample from our predictor matrix A1 and make a prediction
' for the corresponding concentration value.
Dim X As DoubleVector = A1.Row(0)
Dim pls1Yhat As Double = PlsOne.Predict(X)
Console.WriteLine()
Console.WriteLine("Predicted value for x = " & pls1Yhat)
' Predicit the concentration for all the samples in A1.
Dim pls1YhatVec As DoubleVector = PlsOne.Predict(A1)
Console.WriteLine()
Console.Write("Predicted value for A1 = ")
Console.WriteLine(pls1YhatVec)
' Construct an Analysis of Variance (ANOVA) object for PLS1 model and
' prInteger the results.
Dim PlsOneAnova As PLS1Anova = New PLS1Anova(PlsOne)
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("PLS1 ANOVA results ------------------------")
Console.WriteLine(" Sum of squares Total: " & PlsOneAnova.SumOfSquaresTotal)
Console.WriteLine(" Sum of squares residuals: " & PlsOneAnova.SumOfSquaresResiduals)
Console.WriteLine(" Standard Error: " & PlsOneAnova.StandardError)
Console.WriteLine(" Root means square error prediction: " & PlsOneAnova.RootMeanSqrErrorPrediction)
Console.WriteLine(" Coefficient of determination (R^2): " & PlsOneAnova.CoefficientOfDetermination)
' Perform the the PLS2 calculation on the multivarite response variable
' C and check that the calculation succeeded. If it did not, prInteger out
' a diagnostic error message and exit.
Dim PlsTwo As PLS2 = New PLS2()
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("Calculating PLS2...")
PlsTwo.Calculate(A, C, NumComponents)
' Check that the PLS computation succeeded.
If (PlsTwo.IsGood) Then
Console.WriteLine("Success")
Else
Console.WriteLine("PLS2 calculation failed: " & PlsTwo.Message)
Return
End If
' Grab a sample from the other absorption matrix and make a prediction
' of the concentrations of substances in the sample.
X = A1.Row(0)
Dim YHat As DoubleVector = PlsTwo.Predict(X)
Console.WriteLine()
Console.Write("Predicted value for x = ")
Console.WriteLine(YHat)
' Make predictions for all the samples in the absorption matrix A1
Dim YHatMat As DoubleMatrix = PlsTwo.Predict(A1)
Console.WriteLine()
Console.Write("Predicted value for A1 = ")
Console.WriteLine(YHatMat)
' Construct an Analysis of Variance (ANOVA) object for PLS2 model and
' prInteger the results.
Dim PlsTwoAnova As PLS2Anova = New PLS2Anova(PlsTwo)
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("PLS2 ANOVA results ------------------------")
Console.WriteLine()
Console.WriteLine("Sum of squares total")
Console.WriteLine(PlsTwoAnova.SumOfSquaresTotal)
Console.WriteLine()
Console.WriteLine("Sum of squares residuals")
Console.WriteLine(PlsTwoAnova.SumOfSquaresResiduals)
Console.WriteLine()
Console.WriteLine("Standard error")
Console.WriteLine(PlsTwoAnova.StandardError)
Console.WriteLine()
Console.WriteLine("Root means square error prediction")
Console.WriteLine(PlsTwoAnova.RootMeanSqrErrorPrediction)
Console.WriteLine()
Console.WriteLine("Coefficient of determination (R^2)")
Console.WriteLine(PlsTwoAnova.CoefficientOfDetermination)
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]