private static PartialLeastSquaresAnalysis CreateWineExample(out double[,] inputs, out double[,] outputs)
{
// References: http://www.utdallas.edu/~herve/Abdi-PLSR2007-pretty.pdf
// Following the small example by Hervé Abdi (Hervé Abdi, Partial Least Square Regression),
// we will create a simple example where the goal is to predict the subjective evaluation of
// a set of 5 wines. The dependent variables that we want to predict for each wine are its
// likeability, and how well it goes with meat, or dessert (as rated by a panel of experts).
// The predictors are the price, the sugar, alcohol, and acidity content of each wine.
// Here we will list the inputs, or characteristics we would like to use in order to infer
// information from our wines. Each row denotes a different wine and lists its corresponding
// observable characteristics. The inputs are usually denoted by X in the literature.
inputs = new double[,]
{
// Wine | Price | Sugar | Alcohol | Acidity
{ 7, 7, 13, 7 },
{ 4, 3, 14, 7 },
{ 10, 5, 12, 5 },
{ 16, 7, 11, 3 },
{ 13, 3, 10, 3 },
};
// Here we will list our dependent variables. Dependent variables are the outputs, or what we
// would like to infer or predict from our available data, given a new observation. The outputs
// are usually denotes as Y in the literature.
outputs = new double[,]
{
// Wine | Hedonic | Goes with meat | Goes with dessert
{ 14, 7, 8 },
{ 10, 7, 6 },
{ 8, 5, 5 },
{ 2, 4, 7 },
{ 6, 2, 4 },
};
// Next, we will create our Partial Least Squares Analysis passing the inputs (values for
// predictor variables) and the associated outputs (values for dependent variables).
// We will also be using the using the Covariance Matrix/Center method (data will only
// be mean centered but not normalized) and the NIPALS algorithm.
PartialLeastSquaresAnalysis pls = new PartialLeastSquaresAnalysis(inputs, outputs,
AnalysisMethod.Center, PartialLeastSquaresAlgorithm.SIMPLS);
// Compute the analysis with all factors. The number of factors
// could also have been specified in a overload of this method.
pls.Compute();
// After computing the analysis, we can create a linear regression model in order
// to predict new variables. To do that, we may call the CreateRegression() method.
MultivariateLinearRegression regression = pls.CreateRegression();
// After the regression has been created, we will be able to classify new instances.
// For example, we will compute the outputs for the first input sample:
double[] y = regression.Compute(new double[] { 7, 7, 13, 7 });
// The y output will be very close to the corresponding output used as reference.
// In this case, y is a vector of length 3 with values { 14.00, 7.00, 7.75 }.
Assert.AreEqual(14.00, y[0], 1e-2);
Assert.AreEqual(+7.00, y[1], 1e-2);
Assert.AreEqual(+7.75, y[2], 1e-2);
return pls;
}