Vespucci  1.0.0
plsdata.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2015 Wright State University - All Rights Reserved
3  Daniel P. Foose - Author
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
20 #include "Data/Analysis/plsdata.h"
21 
22 PLSData::PLSData(QString name):
23  AnalysisResults(name, "Partial Least Squares Results")
24 {
25 
26 }
27 
35 bool PLSData::Classify(const mat &spectra, const vec &wavelength, int components)
36 {
37  mat Y = repmat(wavelength, 1, components);
38  mat X_loadings, Y_loadings, X_scores, Y_scores, coefficients, percent_variance, fitted;
39  bool success = Vespucci::Math::DimensionReduction::plsregress(spectra, Y, components,
40  X_loadings, Y_loadings,
41  X_scores, Y_scores,
42  coefficients, percent_variance,
43  fitted);
44 
45  //mat residuals = fitted - spectra;
46  if (success){
47  AddMetadata("Type", "Classification (PCA)");
48  AddMetadata("Components calculated", QString::number(components));
49  AddMatrix("Percent Variance", percent_variance);
50  AddMatrix("Predictor Loadings", X_loadings);
51  AddMatrix("Response Loadings", Y_loadings);
52  AddMatrix("Predictor Scores", X_scores);
53  AddMatrix("Response Scores", Y_scores);
54  AddMatrix("Coefficients", coefficients);
55  AddMatrix("Fitted Data", fitted);
56  //AddMatrix("Residuals", residuals);
57  }
58 
59  return success;
60 
61 }
62 
63 bool PLSData::Calibrate(const mat &spectra, const mat &controls)
64 {
65  //spectra is y
66  //controls are X
67  mat X_loadings, Y_loadings, X_scores, Y_scores, coefficients, percent_variance, fitted;
68  bool success = Vespucci::Math::DimensionReduction::plsregress(controls, spectra, controls.n_cols,
69  X_loadings, Y_loadings,
70  X_scores, Y_scores,
71  coefficients, percent_variance,
72  fitted);
73 
74  inplace_trans(coefficients);
75  //mat residuals = fitted - spectra;
76  if (success){
77  AddMetadata("Type", "Calibration");
78  AddMetadata("Components calculated", QString::number(controls.n_cols));
79  AddMatrix("Percent Variance", percent_variance);
80  AddMatrix("Predictor Loadings", X_loadings);
81  AddMatrix("Response Loadings", Y_loadings);
82  AddMatrix("Predictor Scores", X_scores);
83  AddMatrix("Response Scores", Y_scores);
84  AddMatrix("Coefficients", coefficients);
85  AddMatrix("Fitted Data", fitted);
86  //AddMatrix("Residuals", residuals);
87  }
88 
89  return success;
90 }
91 
98 bool PLSData::Discriminate(const mat &data, const mat &labels)
99 {
100  //data (usually, but not necessarly spectra) is X
101  //labels are Y;
102  if (labels.n_rows != data.n_cols) return false;
103  mat X_loadings, Y_loadings, X_scores, Y_scores, coefficients, percent_variance, fitted;
104  bool success = Vespucci::Math::DimensionReduction::plsregress(data.t(), labels, labels.n_cols,
105  X_loadings, Y_loadings,
106  X_scores, Y_scores,
107  coefficients, percent_variance,
108  fitted);
109  mat residuals = fitted - labels;
110 
111  if (success){
112  AddMetadata("Type", "Calibration");
113  AddMetadata("Components calculated", QString::number(labels.n_cols));
114  AddMatrix("Percent Variance", percent_variance);
115  AddMatrix("Predictor Loadings", X_loadings);
116  AddMatrix("Response Loadings", Y_loadings);
117  AddMatrix("Predictor Scores", X_scores);
118  AddMatrix("Response Scores", Y_scores);
119  AddMatrix("Coefficients", coefficients);
120  AddMatrix("Fitted Data", fitted);
121  AddMatrix("Residuals", residuals);
122  }
123 
124  return success;
125 }
void AddMetadata(QString key, QString value)
bool Discriminate(const mat &data, const mat &labels)
PLSData::Discriminate.
Definition: plsdata.cpp:98
VESPUCCI_EXPORT bool plsregress(arma::mat X, arma::mat Y, int components, arma::mat &X_loadings, arma::mat &Y_loadings, arma::mat &X_scores, arma::mat &Y_scores, arma::mat &coefficients, arma::mat &percent_variance, arma::mat &fitted)
Vespucci::MathDimensionReduction::plsregress PLS Regression Using SIMPLS algorithm. This is essentially a line-for-line rewrite of plsregress from the Octave statistics package. Copyright (C) 2012 Fernando Damian Nieuwveldt fdnieuwveldt@gmail.com This is an implementation of the SIMPLS algorithm: Reference: SIMPLS: An alternative approach to partial least squares regression. Chemometrics and Intelligent Laboratory Systems (1993)
Definition: pls.cpp:43
void AddMatrix(const QString &key, const mat &value, QStringList column_headings=QStringList())
bool Classify(const mat &spectra, const vec &wavelength, int components)
PLSData::Apply.
Definition: plsdata.cpp:35
The AnalysisResults class A container for a mat object that allows a mat to be copied to a heap-alloc...
PLSData(QString name)
Definition: plsdata.cpp:22
bool Calibrate(const mat &spectra, const mat &controls)
Definition: plsdata.cpp:63