IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
irootlab_pls.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %>@brief Partial Least Squares
4 %>
5 %> PLS according to [1]. <b>This PLS works with one response variable only</b> (which is typically the class - sometimes called PLSDA (PLS Discriminant Analysis)).
6 %>
7 %> <h3>Reference:</h3>
8 %> [1] Hastie, The elements of Statistical Learning, 2001, Algorithm 3.2, p.68
9 %
10 %> <b>Important: X-variables (columns of X) need to be standardized, otherwise the function will give an error.</b>
11 %
12 %> @param X "Predictor" variables
13 %> @param Y "Response" variable. <b>This PLS works with one variable only!</b>
14 %> @param no_factors Number of variables to be calculated.
15 %> @return <code>[loadings]</code> or <code>[loadings, scores]</code>
16 function varargout = irootlab_pls(X, Y, no_factors)
17 
18 if ~exist('no_factors', 'var')
19  no_factors = 1;
20 end;
21 
22 
23 % Y = data_normalize(Y, 's');
24 
25 % According to Hastie, the X-variables need to be standardized. This is not a general requirement of PLS, but only for
26 % the version applied here.
27 assert_standardized(X, 0.02);
28 
29 [no, nf] = size(X);
30 p = min(no_factors, nf);
31 
32 L = zeros(nf, p);
33 
34 
35 % penalty
36 if 1
37  M = eye(nf, nf);
38 else
39  % Kromer, Boulesteix, Tutz: Penalized Partial Least Squares Based on B-Splines Transformations
40  % http://epub.ub.uni-muenchen.de/1853/1/paper_485.pdf
41  % a nice and clear paper
42  dcoeff = [1, 10, 10]*no;
43  P = zeros(nf, nf);
44  for i = 1:length(dcoeff)
45  D = diff_operator(nf, i-1);
46  P = P+dcoeff(i)*(D'*D);
47  end;
48 
49  M = inv(P);
50 end;
51 
52 
53 
54 
55 for m = 1:p
56  % Coefficients are correlations between X and Y
57  L(:, m) = M*X'*Y;
58 
59  z = X*L(:, m);
60 
61  % Makes columns in X orthogonal to the newfound z.
62 
63  for j = 1:nf
64  X(:, j) = X(:, j)-z*(z'*X(:, j)/(z'*z));
65  end;
66 end;
67 
68 
69 % [XL, YL, XS, YS] = plsregress(X, Y, no_factors);
70 
71 L = adjust_unitnorm(L);
72 
73 if nargout == 1
74  varargout = {L};
75 else
76  varargout = {L, X*L};
77 end;
function adjust_unitnorm(in L)
function assert_standardized(in X, in tolerance)
function diff_operator(in nf, in order)
function irootlab_pls(in X, in Y, in no_factors)
Important: X-variables (columns of X) need to be standardized, otherwise the function will give an er...