[TOC]
Imports System
Imports System.IO
Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats
Namespace CenterSpace.NMath.Stats.Examples.VisualBasic
' A .NET example in VB.NET showing Partial Least Squares (PLS) with scores
' and loadings.
' In chemometrics one often wishes to know the chemical composition of a
' sample of, say, a gas or liquid. One common technique is to study the
' absorption spectrum of light passing through the sample. Partial Least
' Squares (PLS) is often used to construct a predictive model in this
' situation. Suppose we wish to measure the concentration of m different
' constituents in the substance and that we look at n different
' absorption spectra (an absorption spectra measures the amount of light
' absorbed at each wavelength). Pick p different wavelengths in the
' absorption spectra and let A = {aij} be the be the nxp matrix where aij
' is the absorption measurement at the jth wavelength in the ith sample.
' And let C = {cij} be the nxm matrix where cij is the concentration of
' the jth constituent in the ith sample. This example constructs a
' predictive model for C given A Imports PLS. In particular the scores and
' loadings for the response variable, C, and the predictor variable, A,
' are examined.
Module PLS2ScoresAndLoadingsExample
' The main entry point for the application.
Sub Main()
' Read in some chemometric data.
Dim yDatafilename As String = "..\\..\\chemometricY.dat"
Dim xDatafilename As String = "..\\..\\chemometricX.dat"
Dim x1Datafilename As String = "..\\..\\chemometricX1.dat"
Dim xDataStream As StreamReader
Dim x1DataStream As StreamReader
Dim yDataStream As StreamReader
Try
xDataStream = New StreamReader(xDatafilename)
x1DataStream = New StreamReader(x1Datafilename)
yDataStream = New StreamReader(yDatafilename)
Catch E As FileNotFoundException
Dim msg As String = String.Format("Could not find data file {0}, ", xDatafilename)
msg += ", "
msg += x1Datafilename
msg += ", "
msg += yDatafilename
msg += "."
msg += Environment.NewLine
msg += E.Message
Console.WriteLine(msg)
Return
End Try
' Read in absorption data matrices. We'll use A to calculate the
' PLS model and predict concentrations Imports the absorption data
' in A1
Dim A As DoubleMatrix = New DoubleMatrix(xDataStream)
Dim A1 As DoubleMatrix = New DoubleMatrix(x1DataStream)
Dim C As DoubleMatrix = New DoubleMatrix(yDataStream)
Dim numComponents As Integer = 3
Dim pls As PLS2 = New PLS2()
Console.WriteLine()
Console.WriteLine("Calculating...")
pls.Calculate(A, C, numComponents)
' Check that the PLS computation succeeded.
Console.WriteLine("Is it good? " & pls.IsGood)
' The scores and loadings availability and access are specific
' the particular PLS algorithm used. We used the NIPALS algorithm,
' which is the default, so we must retrieve the algorithm object
' from the PLS object and extract the scores and loadings from it.
Dim Alg As PLS2NipalsAlgorithm = DirectCast(pls.Calculator, PLS2NipalsAlgorithm)
' Get the spectral scores
Dim S As DoubleMatrix = Alg.PredictorScores
Console.WriteLine()
Console.WriteLine("Spectral Scores ------------------------------")
Console.WriteLine()
For I As Integer = 0 To (numComponents - 1)
Console.WriteLine("spectral score " & I)
Console.WriteLine(S.Col(I))
Console.WriteLine()
Next
Console.WriteLine()
' Get spectral loadings
Dim Bx As DoubleMatrix = Alg.PredictorLoadings
Console.WriteLine()
Console.WriteLine("Spectral Loadings ------------------------------")
Console.WriteLine()
For I As Integer = 0 To (numComponents - 1)
Console.WriteLine("spectral loading " & I)
Console.WriteLine(Bx.Col(I))
Console.WriteLine()
Next
Console.WriteLine()
' Get concentration weighted scores
Dim U As DoubleMatrix = Alg.ResponseScores
Console.WriteLine()
Console.WriteLine("Concentration Weighted Scores ------------------")
Console.WriteLine()
For I As Integer = 0 To (numComponents - 1)
Console.WriteLine("concentration score " & I)
Console.WriteLine(U.Col(I))
Console.WriteLine()
Next
Console.WriteLine()
' Get concentration loadings
Dim By As DoubleMatrix = Alg.ResponseLoadings
Console.WriteLine()
Console.WriteLine("Concentration Loadings ------------------")
Console.WriteLine()
For I As Integer = 0 To (numComponents - 1)
Console.WriteLine("concentration loading " & I)
Console.WriteLine(By.Col(I))
Console.WriteLine()
Next
Console.WriteLine()
' Prediction Imports the coefficients matrix. Predict the constituent
' concentrations from a vector of spectral responses.
' The coeffient matrix, B, can be used for prediction as follows:
' Let Cu be the predicted concentrations for the spectral response
' vector Au, let Abar the mean of the spectral data and Cbar be the mean
' of the concentrations data used to create the model. Then
' Cu = Cbar + (Au - Abar)B.
Dim CBar As DoubleVector = Alg.ResponseMean
Dim ABar As DoubleVector = Alg.PredictorMean
Dim B As DoubleMatrix = Alg.Coefficients
Dim Au As DoubleVector = A1.Row(0)
Dim CU As DoubleVector = CBar + NMathFunctions.TransposeProduct(B, (Au - ABar))
Console.WriteLine("Prediction Using Coefficient Matrix ------------------")
Console.WriteLine()
Console.WriteLine("Predicted concentrations for spectral response")
Console.WriteLine()
Console.WriteLine()
Console.WriteLine(Au)
Console.WriteLine()
Console.WriteLine("is")
Console.WriteLine()
Console.WriteLine(CU)
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]