-
Notifications
You must be signed in to change notification settings - Fork 17
/
crossval_spls.m
187 lines (155 loc) · 6.44 KB
/
crossval_spls.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function [cumpress,press,nze] = crossval_spls(x,y,lvs,keepXs,blocks_r,prepx,prepy,opt)
% Row-wise k-fold (rkf) cross-validation for square-prediction-errors computing in SPLS.
%
% cumpress = crossval_spls(x,y) % minimum call
% [cumpress,press,nze] =
% crossval_spls(x,y,lvs,keepXs,blocks_r,prepx,prepy,opt) % complete call
%
%
% INPUTS:
%
% x: [NxM] billinear data set for model fitting
%
% y: [NxO] billinear data set of predicted variables
%
% lvs: [1xA] Latent Variables considered (e.g. lvs = 1:2 selects the
% first two LVs). By default, lvs = 0:rank(x)
%
% keepXs: [1xK] Numbers of x-block variables kept per latent variable modeled. By default, keepXs = 1:M
%
% blocks_r: [1x1] maximum number of blocks of samples (N by default)
%
% prepx: [1x1] preprocesing of the x-block
% 0: no preprocessing
% 1: mean centering
% 2: autoscaling (default)
%
% prepy: [1x1] preprocesing of the y-block
% 0: no preprocessing
% 1: mean centering
% 2: autoscaling (default)
%
% opt: (str or num) options for data plotting.
% 0: no plots.
% 1: plot (default)
%
%
% OUTPUTS:
%
% cumpress: [AxK] Cumulative PRESS
%
% press: [AxKxO] PRESS per variable
%
% nze: [AxK] Non-zero elements in the regression coefficient matrix.
%
%
% EXAMPLE OF USE: Random data with structural relationship
%
% X = simuleMV(20,10,8);
% Y = 0.1*randn(20,2) + X(:,1:2);
% lvs = 0:10;
% keepXs = 1:10;
% [cumpress,press,nze] = crossval_spls(X,Y,lvs);
%
%
% coded by: Jose Camacho Paez (josecamacho@ugr.es)
% last modification: 24/Aug/16.
%
% Copyright (C) 2016 University of Granada, Granada
% Copyright (C) 2016 Jose Camacho Paez
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%% Arguments checking
% Set default values
routine=dbstack;
assert (nargin >= 2, 'Error in the number of arguments. Type ''help %s'' for more info.', routine(1).name);
N = size(x, 1);
M = size(x, 2);
O = size(y, 2);
if nargin < 3 || isempty(lvs), lvs = 0:rank(x); end;
A = length(lvs);
if nargin < 4 || isempty(keepXs), keepXs = 1:M; end;
J = length(keepXs);
if nargin < 5 || isempty(blocks_r), blocks_r = N; end;
if nargin < 6 || isempty(prepx), prepx = 2; end;
if nargin < 7 || isempty(prepy), prepy = 2; end;
if nargin < 8 || isempty(opt), opt = 1; end;
% Convert column arrays to row arrays
if size(lvs,2) == 1, lvs = lvs'; end;
if size(keepXs,2) == 1, keepXs = keepXs'; end;
% Validate dimensions of input data
assert (isequal(size(y), [N O]), 'Dimension Error: 2nd argument must be N-by-O. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(size(lvs), [1 A]), 'Dimension Error: 3rd argument must be 1-by-A. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(size(keepXs), [1 J]), 'Dimension Error: 4th argument must be 1-by-J. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(size(blocks_r), [1 1]), 'Dimension Error: 5th argument must be 1-by-1. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(size(prepx), [1 1]), 'Dimension Error: 6th argument must be 1-by-1. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(size(prepy), [1 1]), 'Dimension Error: 7th argument must be 1-by-1. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(size(opt), [1 1]), 'Dimension Error: 8th argument must be 1-by-1. Type ''help %s'' for more info.', routine(1).name);
% Preprocessing
lvs = unique(lvs);
keepXs = unique(keepXs);
% Validate values of input data
assert (isempty(find(lvs<0)), 'Value Error: 3rd argument must not contain negative values. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(fix(lvs), lvs), 'Value Error: 3rd argument must contain integers. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(fix(keepXs), keepXs), 'Value Error: 4th argument must contain integers. Type ''help %s'' for more info.', routine(1).name);
assert (isequal(fix(blocks_r), blocks_r), 'Value Error: 5th argument must be an integer. Type ''help %s'' for more info.', routine(1).name);
assert (blocks_r>2, 'Value Error: 5th argument must be above 2. Type ''help %s'' for more info.', routine(1).name);
assert (blocks_r<=N, 'Value Error: 5th argument must be at most N. Type ''help %s'' for more info.', routine(1).name);
%% Main code
% Initialization
press = zeros(length(lvs),length(keepXs),O);
nze = zeros(length(lvs),length(keepXs));
rows = rand(1,N);
[a,r_ind]=sort(rows);
elem_r=N/blocks_r;
% Cross-validation
for i=1:blocks_r,
ind_i = r_ind(round((i-1)*elem_r+1):round(i*elem_r)); % Sample selection
i2 = ones(N,1);
i2(ind_i)=0;
sample = x(ind_i,:);
calibr = x(find(i2),:);
sample_y = y(ind_i,:);
calibr_y = y(find(i2),:);
[ccs,av,st] = preprocess2D(calibr,prepx);
[ccs_y,av_y,st_y] = preprocess2D(calibr_y,prepy);
scs = preprocess2Dapp(sample,av,st);
scs_y = preprocess2Dapp(sample_y,av_y,st_y);
if ~isempty(find(lvs)),
for lv=1:length(lvs),
for keepX=1:length(keepXs),
if lvs(lv),
model = sparsepls2(ccs, ccs_y, lvs(lv), keepXs(keepX)*ones(size(1:lvs(lv))), O*ones(size(1:lvs(lv))), 500, 1e-10, 1, 0);
beta = model.R*model.Q';
srec = scs*beta;
pem = scs_y-srec;
press(lv,keepX,:) = squeeze(press(lv,keepX,:))' + sum(pem.^2,1);
nze(lv,keepX) = nze(lv,keepX) + length(find(beta));
else
press(lv,keepX,:) = squeeze(press(lv,keepX,:))' + sum(scs_y.^2,1);
nze(lv,keepX) = nze(lv,keepX) + M*O;
end
end
end
else
pem = scs_y;
press = press + ones(length(keepXs),1)*sum(pem.^2,1);
nze = nze + ones(length(keepXs),1)*M*O;
end
end
cumpress = sum(press,3);
%% Show results
if opt == 1,
fig_h = plot_vec(cumpress',keepXs,[],{'#NZV','PRESS'},[],0,lvs);
end