VB PLS Example

← All NMath Core Code Examples

 

Imports System
Imports System.IO

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Core.Examples.CSharp

  ' A .NET example in Visual Basic 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 concentration of substances in a sample and
      ' the predictor, or x, variable is the absorption spectra of the sample
      ' sampled at discrete wavelengths. We'll read in two sets of predictor
      ' values. One to construct the model with and the other for making 
      ' predictions.

      ' Construct the absorption matrices A and A1, and the concentration
      ' matrix C
      Dim A As DoubleMatrix = New DoubleMatrix(New StreamReader("chemometricX1.dat"))
      Dim A1 As DoubleMatrix = New DoubleMatrix(New StreamReader("chemometricX2.dat"))
      Dim C As DoubleMatrix = New DoubleMatrix(New StreamReader("chemometricY.dat"))

      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.Write("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.ToString("G5"))

      ' Predict 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.ToString("G5"))

      ' 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 multivariate 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.Write("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.ToString("G5"))

      ' 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.ToString("G5"))

      ' 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

← All NMath Stats Code Examples
Top