VB PLS1 Scores And Loadings Example

← All NMath Stats Code Examples

 

Imports System

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats

Namespace CenterSpace.NMath.Stats.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 a constituent 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 = {ci} be the m vector where
  ' ci is the concentration of the constituent in the ith sample.
  ' This example constructs a predictive model for C given A using PLS. In
  ' particular the scores and loadings for the response variable, C, and the
  ' predictor variable, A, are examined.

  Module PLS1ScoresAndLoadingsExample

    Sub Main()

      Dim Rng As New RandGenUniform(124)
      Rng.LowerBound = 0.1
      Rng.UpperBound = 1.1
      Dim N As Integer = 7 ' number of samples (spectra)
      Dim P As Integer = 10 ' number of data points (wavelength)
      Dim F As Integer = 4  ' number of PLS eigenvectors

      Dim A As New DoubleMatrix(N, P, Rng) ' spectral absorbances
      Dim C As New DoubleVector(N, Rng)    ' constituent concentrations
      Dim PLS As New PLS1()

      Console.WriteLine()
      Console.WriteLine("Performing calculation...")
      PLS.Calculate(A, C, F)

      Console.WriteLine()
      Console.WriteLine("Is it good? " & PLS.IsGood)
      Console.WriteLine()

      Dim ALG = DirectCast(PLS.Calculator, PLS1NipalsAlgorithm)

      ' Get the spectral scores
      Dim S As DoubleMatrix = ALG.Scores
      Console.WriteLine()
      Console.WriteLine("Spectral Scores ------------------------------")
      Console.WriteLine()

      For I As Integer = 0 To (F - 1)
        Console.Write("spectral score ")
        Console.WriteLine(I)
        Console.WriteLine(S.Col(I).ToString("G5"))
        Console.WriteLine()
      Next
      Console.WriteLine()

      ' Get spectral loadings
      Dim Bx As DoubleMatrix = ALG.Loadings
      Console.WriteLine("Spectral Loadings ------------------------------")
      Console.WriteLine()

      For I As Integer = 0 To (F - 1)
        Console.Write("spectral loading ")
        Console.WriteLine(I)
        Console.WriteLine(Bx.Col(I).ToString("G5"))
        Console.WriteLine()
      Next
      Console.WriteLine()

      ' Predict the constituent concentrations from a vector of spectral responses.
      ' The regression vector, r, can be used for prediction as follows:
      ' Let Cu be the predicted concentration 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)'r.
      Dim Au As New DoubleVector(P, Rng) ' spectral responses
      Dim CBar As Double = ALG.ResponseMean
      Dim Abar As DoubleVector = ALG.PredictorMean
      Dim R As DoubleVector = ALG.RegressionVector

      Dim CU As Double = CBar + NMathFunctions.Dot((Au - Abar), R)
      Console.WriteLine()
      Console.WriteLine("Prediction Using Regression Vector ------------------")
      Console.WriteLine()
      Console.WriteLine("Predicted concentrations for spectral response ")
      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 Stats Code Examples
Top