C# PLS Example

← All NMath Stats Code Examples

using System;
using System.IO;

using CenterSpace.NMath.Core;
using CenterSpace.NMath.Stats;

namespace CenterSpace.NMath.Stats.Examples.CSharp
{
/// <summary>
/// A .NET example in C# showing basic use of the PLS1 and PLS2 classes for solving
/// partial least squares (PLS) problems.
/// </summary>
class PLSExample
{

static void Main( string[] args )
{
// 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
var A = new DoubleMatrix( new StreamReader( "chemometricX1.dat" ));
var A1 = new DoubleMatrix( new StreamReader( "chemometricX2.dat" ));
var C = new DoubleMatrix( new StreamReader( "chemometricY.dat" ));

int numComponents = 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.
DoubleVector y = C.Col( 0 );

// Construct a PLS1 object and perform the calculation.
var plsOne = new PLS1();
Console.WriteLine();
Console.Write( "Calculating PLS1...  " );
plsOne.Calculate( A, y, numComponents );

// Check that the calculation succeeded. If it did not, print out
// a diagnostic error message and exit.
if ( plsOne.IsGood )
{
Console.WriteLine( "Success" );
}
else
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine( "PLS1 calculation failed: " + plsOne.Message );
return;
}

// Pull out a sample from our predictor matrix A1 and make a prediction
// for the corresponding concentration value.
DoubleVector x = A1.Row( 0 );
double pls1Yhat = plsOne.Predict( x );
Console.WriteLine();
Console.WriteLine( "Predicted value for x =" );
Console.WriteLine( pls1Yhat.ToString( "G5" ) );

// Predict the concentration for all the samples in A1.
DoubleVector pls1YhatVec = plsOne.Predict( A1 );
Console.WriteLine();
Console.WriteLine( "Predicted value for A1 =" );
Console.WriteLine( pls1YhatVec.ToString( "G5" ) );

// Construct an Analysis of Variance (ANOVA) object for PLS1 model and
// print the results.
var plsOneAnova = 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, print out
// a diagnostic error message and exit.

var plsTwo = new PLS2();

Console.WriteLine();
Console.WriteLine();
Console.Write( "Calculating PLS2...  " );
plsTwo.Calculate( A, C, numComponents );

// Check that the PLS computation succeeded.
if ( plsTwo.IsGood )
{
Console.WriteLine( "Success" );
}
else
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine( "PLS2 calculation failed: " + plsTwo.Message );
return;
}

// Grab a sample from the other absorption matrix and make a prediction
// of the concentrations of substances in the sample.
x = A1.Row( 0 );
DoubleVector yhat = plsTwo.Predict( x );
Console.WriteLine();
Console.WriteLine( "Predicted value for x =" );
Console.WriteLine( yhat.ToString( "G5" ) );

// Make predictions for all the samples in the absorption matrix A1
DoubleMatrix yhatMat = plsTwo.Predict( A1 );
Console.WriteLine();
Console.WriteLine( "Predicted value for A1 =" );
Console.WriteLine( yhatMat.ToTabDelimited( "G5" ) );

// Construct an Analysis of Variance (ANOVA) object for PLS2 model and
// print the results.
var plsTwoAnova = new PLS2Anova( plsTwo );
Console.WriteLine();
Console.WriteLine( "PLS2 ANOVA results ------------------------" );
Console.WriteLine();
Console.WriteLine( "Sum of squares total\n" + plsTwoAnova.SumOfSquaresTotal );
Console.WriteLine();
Console.WriteLine( "Sum of squares residuals\n" + plsTwoAnova.SumOfSquaresResiduals );
Console.WriteLine();
Console.WriteLine( "Standard error\n" + plsTwoAnova.StandardError );
Console.WriteLine();
Console.WriteLine( "Root means square error prediction\n" + plsTwoAnova.RootMeanSqrErrorPrediction );
Console.WriteLine();
Console.WriteLine( "Coefficient of determination (R^2)\n" + plsTwoAnova.CoefficientOfDetermination );

Console.WriteLine();
Console.WriteLine( "Press Enter Key" );