← All NMath Code Examples
Imports System
Imports System.IO
Imports System.Threading
Imports CenterSpace.NMath.Core
Namespace CenterSpace.NMath.Examples.VisualBasic
A .NET example in Visual Basic 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()
Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
Read in absorption data matrices. Well use A to calculate the
PLS model and predict concentrations Imports the absorption data
in A1
Dim A As DoubleMatrix = New DoubleMatrix(New StreamReader("chemometricX.dat"))
Dim A1 As DoubleMatrix = New DoubleMatrix(New StreamReader("chemometricX1.dat"))
Dim C As DoubleMatrix = New DoubleMatrix(New StreamReader("chemometricY.dat"))
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).ToString("G5"))
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).ToString("G5"))
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).ToString("G5"))
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).ToString("G5"))
Console.WriteLine()
Next
Console.WriteLine()
Prediction Imports the coefficients matrix. Predict the constituent
concentrations from a vector of spectral responses.
The coefficient 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.ToString("G5"))
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
← All NMath Code Examples