C# PLS Example

[TOC]

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

      string x1Datafilename = "..\\..\\chemometricX1.dat";
      string x2Datafilename = "..\\..\\chemometricX2.dat";
      string yDatafilename = "..\\..\\chemometricY.dat";

      StreamReader x1DataStream;
      StreamReader x2DataStream;
      StreamReader yDataStream;

      try
      {
        x1DataStream = new StreamReader(x1Datafilename);
        x2DataStream = new StreamReader(x2Datafilename);
        yDataStream = new StreamReader(yDatafilename);
      }
      catch (FileNotFoundException e)
      {
        string msg = string.Format("Could not find data file {0}, {1}, {2}.", x1Datafilename,
          x2Datafilename, yDatafilename);
        msg += Environment.NewLine;
        msg += e.Message;
        Console.WriteLine(msg);
        return;
      }

      // Construct the absorption matrices A and A1, and the concentration
      // matrix C
      DoubleMatrix A = new DoubleMatrix(x1DataStream);
      DoubleMatrix A1 = new DoubleMatrix(x2DataStream);
      DoubleMatrix C = new DoubleMatrix(yDataStream);

      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.
      PLS1 plsOne = new PLS1();
      Console.WriteLine("\nCalculating 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("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("\nPredicted value for x = " + pls1Yhat);

      // Predicit the concentration for all the samples in A1.
      DoubleVector pls1YhatVec = plsOne.Predict(A1);
      Console.WriteLine("\nPredicted value for A1 = " + pls1YhatVec);

      // Construct an Analysis of Variance (ANOVA) object for PLS1 model and
      // print the results.
      PLS1Anova plsOneAnova = new PLS1Anova(plsOne);
      Console.WriteLine("\n\nPLS1 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 multivarite response variable
      // C and check that the calculation succeeded. If it did not, print out
      // a diagnostic error message and exit.

      PLS2 plsTwo = new PLS2();

      Console.WriteLine("\n\n\nCalculating PLS2...");
      plsTwo.Calculate(A, C, numComponents);

      // Check that the PLS computation succeeded.
      if (plsTwo.IsGood)
      {
        Console.WriteLine("Success");
      }
      else
      {
        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("\nPredicted value for x = " + yhat);

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

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

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

[TOC]