This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
predict.m
executable file
·276 lines (265 loc) · 11.7 KB
/
predict.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
function result=predict(x,y,inmodel)
%PREDICT is a function which computes regression results for new data based on
% the output from a RPCR or RSIMPLS analysis.
%
% I/O: result=predict(x,y,inmodel)
%
% Required input arguments:
% x: Data matrix of the explanatory variables of the new data
% y: Data matrix of the response variables of the new data
% inmodel: output from a RPCR or RSIMPLS model
%
% The output of PREDICT is a structure containing:
%
% result.fitted : Fitted response values of the new data
% result.res : Residuals of the new data
% result.sd : Score distances of the new data
% result.od : Orthogonal distances of the new data
% result.resd : Residual distances of the new data
% result.cutoff : Cutoff values for the score (result.cutoff.sd), orthogonal
% (result.cutoff.od) and residual distances (result.cutoff.resd).
% We use 0.99 quantiles of the chi-squared distribution.
% result.flag : The observations whose score distance is larger than
% 'result.cutoff.sd' receive a flag 'result.flag.sd' equal
% to zero (good leverage points). Otherwise 'result.flag.sd'
% is equal to one.
% The components 'result.flag.od' and 'result.flag.resd' are
% defined analogously, and determine the orthogonal outliers,
% resp. the bad leverage points/vertical outliers.
% The observations with 'result.flag.od' and 'result.flag.resd'
% equal to zero, can be considered as calibration outliers and receive
% 'result.flag.all' equal to zero. The regular observations and the good leverage
% points have 'result.flag.all' equal to one.
% result.rmsep : the root mean squared error of the
% .outlfree : non-outlying data
% .trimh : trimmed according to 'h' smallest residuals (=h from the inmodel)
% .trim50 : trimmed at 50% of the smallest residuals
% .trim75 : trimmed at 75% of the smallest residuals
% .trim95 : trimmed at 95% of the smallest residuals
% result.class : the name of the method used: 'RPCR' or 'RSIMPLS'
% result.classic : if there was a classical output from rpcr or rsimpls,
% all computations based on these classical results are
% done as well. Note that the root mean squared error is
% then based on the non-outlying data from the robust
% analysis, in order to compare the rmsep values on the
% same data set.
%
% Example:
% out.pcr = rpcr(Xtrain,ytrain,'k',3,'plots',0);
% result = predict(Xtest,ytest,out.pcr);
%
% Written by S. Verboven, M. Hubert
% Last Revision: 20/05/2008
% Last Update: 30/05/2008
X=x;
Y=y;
[n,p]=size(X);
[n,q]=size(Y);
%Fitted values and residuals of the new data
result.fitted=X*inmodel.slope + repmat(inmodel.int,n,1);
result.res=Y-result.fitted;
%Computing distances
if strcmp(inmodel.class,'RPCR')
XRc=X-repmat(inmodel.robpca.M,n,1);
result.T=XRc*inmodel.robpca.P;
result.sd=sqrt(mahalanobis(result.T,zeros(size(result.T,2),1),'cov',inmodel.robpca.L))';
Xtilde=result.T*inmodel.robpca.P';
Rdiff=XRc-Xtilde;
for i=1:n
result.od(i,1)=norm(Rdiff(i,:));
end
if q >1
cen=zeros(q,1)';
result.resd=sqrt(mahalanobis(result.res,cen,'cov',inmodel.mcdreg.cov))';
else
result.resd=result.res/inmodel.lts.scale;
end
%cutoffs
quan=chi2inv(0.99,inmodel.k);
result.cutoff.sd=quan^0.5;
if inmodel.k~=rank(X)
[m,s]=unimcd(inmodel.od.^(2/3),inmodel.h);
result.cutoff.od = sqrt(norminv(0.99,m,s).^3);
else
result.cutoff.od=0;
end
result.cutoff.resd=sqrt(chi2inv(0.99,q));
elseif strcmp(inmodel.class,'RSIMPLS')
XRc=X-repmat(inmodel.robpca.M(1:p),n,1);
result.T=XRc*inmodel.weights.r;
result.sd=sqrt(mahalanobis(result.T,inmodel.Tcenter,'cov',inmodel.Tcov))';
Xtilde=result.T*inmodel.weights.p';
Rdiff=XRc-Xtilde;
for i=1:n
result.od(i,1)=norm(Rdiff(i,:));
end
if q >1
cen=zeros(q,1)';
result.resd=sqrt(mahalanobis(result.res,cen,'cov',inmodel.cov))';
else
result.resd=result.res/sqrt(inmodel.cov);
end
%cutoffs
quan=chi2inv(0.99,inmodel.k);
result.cutoff.sd=quan^0.5;
if inmodel.k~=rank(X)
[m,s]=unimcd(inmodel.od.^(2/3),inmodel.h);
result.cutoff.od = sqrt(norminv(0.99,m,s).^3);
else
result.cutoff.od=0;
end
result.cutoff.resd=sqrt(chi2inv(0.99,q));
end
% Defining flags
result.flag.od=(result.od<=result.cutoff.od);
result.flag.sd=(result.sd<=result.cutoff.sd);
result.flag.resd=(abs(result.resd)<=result.cutoff.resd);
result.flag.all=result.flag.od & result.flag.resd;
% Rmsep based on data with 'result.flag.resd = 1', and the trimmed RMSEPs
N=sum(result.flag.resd);
Nh=ceil(inmodel.h/size(inmodel.fitted,1)*n);
N50=ceil(0.5*n);
N75=ceil(0.75*n);
N95=ceil(0.95*n);
if q>1
result.rmsep.outlfree=sqrt(1/(N*q)*sum(sum(result.res(result.flag.resd==1).^2,2)));
sortsqres=sort(sum(result.res.^2,2));
trimh=sum(sortsqres(1:Nh,:));
trim50=sum(sortsqres(1:N50,:));
trim75=sum(sortsqres(1:N75,:));
trim95=sum(sortsqres(1:N95,:));
result.rmsep.trimh=sqrt(1/(Nh*q)*trimh);
result.rmsep.trim50=sqrt(1/(N50*q)*trim50);
result.rmsep.trim75=sqrt(1/(N75*q)*trim75);
result.rmsep.trim95=sqrt(1/(N95*q)*trim95);
else
result.rmsep.outlfree=sqrt(1/N*sum((result.res(result.flag.resd==1)).^2));
sortsqres=sort(result.res.^2);
trimh=sum(sortsqres(1:Nh));
trim50=sum(sortsqres(1:N50));
trim75=sum(sortsqres(1:N75));
trim95=sum(sortsqres(1:N95));
result.rmsep.trimh=sqrt(1/Nh*trimh);
result.rmsep.trim50=sqrt(1/N50*trim50);
result.rmsep.trim75=sqrt(1/N75*trim75);
result.rmsep.trim95=sqrt(1/N95*trim95);
end
result.class=inmodel.class;
%In case the classical output is also given
if isstruct(inmodel.classic) && strcmp(inmodel.class,'RPCR')
% fitted values, residuals, distances
result.classic.fitted=X*inmodel.classic.slope + repmat(inmodel.classic.int,n,1);
result.classic.res=Y-result.classic.fitted;
XRc=X-repmat(inmodel.classic.cpca.M,n,1);
result.classic.T=XRc*inmodel.classic.cpca.P;
result.classic.sd=sqrt(mahalanobis(result.classic.T,zeros(size(result.classic.T,2),1),'cov',inmodel.classic.cpca.L))';
Xtilde=result.classic.T*inmodel.classic.cpca.P';
Rdiff=XRc-Xtilde;
for i=1:n
result.classic.od(i,1)=norm(Rdiff(i,:));
end
if q >1
cen=zeros(q,1)';
result.classic.resd=sqrt(mahalanobis(result.classic.res,cen,'cov',inmodel.classic.cov))';
else
result.classic.resd=result.classic.res/sqrt(inmodel.classic.cov);
end
% cutoffs and flags
quan=chi2inv(0.99,inmodel.k);
result.classic.cutoff.sd=quan^0.5;
if inmodel.k~=rank(X)
[m,s]=unimcd(inmodel.classic.od.^(2/3),inmodel.h);
result.classic.cutoff.od = sqrt(norminv(0.99,m,s).^3);
else
result.classic.cutoff.od=0;
end
result.classic.cutoff.resd=sqrt(chi2inv(0.99,q));
result.classic.flag.od=(result.classic.od<=result.classic.cutoff.od);
result.classic.flag.sd=(result.classic.sd<=result.classic.cutoff.sd);
result.classic.flag.resd=(abs(result.classic.resd)<=result.classic.cutoff.resd);
result.classic.flag.all=result.classic.flag.od & result.classic.flag.resd;
% classical rmsep of the good observations (from the robust analysis)
N=sum(result.flag.resd);
if q>1
result.classic.rmsep.outlfree=sqrt(1/(N*q)*sum(sum(result.classic.res(result.flag.resd==1).^2,2)));
sortsqresc=sort(sum(result.classic.res.^2,2));
trimh=sum(sortsqresc(1:Nh,:));
trim50=sum(sortsqresc(1:N50,:));
trim75=sum(sortsqresc(1:N75,:));
trim95=sum(sortsqresc(1:N95,:));
result.classic.rmsep.trimh=sqrt(1/(Nh*q)*trimh);
result.classic.rmsep.trim50=sqrt(1/(N50*q)*trim50);
result.classic.rmsep.trim75=sqrt(1/(N75*q)*trim75);
result.classic.rmsep.trim95=sqrt(1/(N95*q)*trim95);
else
result.classic.rmsep.outlfree=sqrt(1/N*sum((result.classic.res(result.flag.resd==1)).^2));
sortsqresc=sort(result.classic.res.^2);
trimh=sum(sortsqresc(1:Nh));
trim50=sum(sortsqresc(1:N50));
trim75=sum(sortsqresc(1:N75));
trim95=sum(sortsqresc(1:N95));
result.classic.rmsep.trimh=sqrt(1/Nh*trimh);
result.classic.rmsep.trim50=sqrt(1/N50*trim50);
result.classic.rmsep.trim75=sqrt(1/N75*trim75);
result.classic.rmsep.trim95=sqrt(1/N95*trim95);
end
result.classic.class=inmodel.classic.class;
elseif isstruct(inmodel.classic) && strcmp(inmodel.class,'RSIMPLS')
% fitted values, residuals, distances
result.classic.fitted=X*inmodel.classic.slope + repmat(inmodel.classic.int,n,1);
result.classic.res=Y-result.classic.fitted;
XRc=X-repmat(inmodel.classic.M(1:p),size(X,1),1);
result.classic.T=XRc*inmodel.classic.weights.r;
result.classic.sd=sqrt(mahalanobis(result.classic.T,zeros(size(result.classic.T,2),1),'cov',inmodel.classic.Tcov))';
Xtilde=result.classic.T*inmodel.classic.weights.p';
Rdiff=XRc-Xtilde;
for i=1:n
result.classic.od(i,1)=norm(Rdiff(i,:));
end
if q >1
cen=zeros(q,1)';
result.classic.resd=sqrt(mahalanobis(result.classic.res,cen,'cov',inmodel.classic.cov))';
else
result.classic.resd=result.classic.res/sqrt(inmodel.classic.cov);
end
% cutoffs and flags
quan=chi2inv(0.99,inmodel.k);
result.classic.cutoff.sd=quan^0.5;
if inmodel.k~=rank(X)
[m,s]=unimcd(inmodel.classic.od.^(2/3),inmodel.h);
result.classic.cutoff.od = sqrt(norminv(0.99,m,s).^3);
else
result.classic.cutoff.od=0;
end
result.classic.cutoff.resd=sqrt(chi2inv(0.99,q));
result.classic.flag.od=(result.classic.od<=result.classic.cutoff.od);
result.classic.flag.sd=(result.classic.sd<=result.classic.cutoff.sd);
result.classic.flag.resd=(abs(result.classic.resd)<=result.classic.cutoff.resd);
result.classic.flag.all=result.classic.flag.od & result.classic.flag.resd;
% classical rmsep of the good observations (from the robust analysis)
N=sum(result.flag.resd);
if q>1
result.classic.rmsep.outlfree=sqrt(1/(N*q)*sum(sum(result.classic.res(result.flag.resd==1).^2,2)));
sortsqresc=sort(sum(result.classic.res.^2,2));
trimh=sum(sortsqresc(1:Nh,:));
trim50=sum(sortsqresc(1:N50,:));
trim75=sum(sortsqresc(1:N75,:));
trim95=sum(sortsqresc(1:N95,:));
result.classic.rmsep.trimh=sqrt(1/(Nh*q)*trimh);
result.classic.rmsep.trim50=sqrt(1/(N50*q)*trim50);
result.classic.rmsep.trim75=sqrt(1/(N75*q)*trim75);
result.classic.rmsep.trim95=sqrt(1/(N95*q)*trim95);
else
result.classic.rmsep.outlfree=sqrt(1/N*sum((result.classic.res(result.flag.resd==1)).^2));
sortsqresc=sort(result.classic.res.^2);
trimh=sum(sortsqresc(1:Nh));
trim50=sum(sortsqresc(1:N50));
trim75=sum(sortsqresc(1:N75));
trim95=sum(sortsqresc(1:N95));
result.classic.rmsep.trimh=sqrt(1/Nh*trimh);
result.classic.rmsep.trim50=sqrt(1/N50*trim50);
result.classic.rmsep.trim75=sqrt(1/N75*trim75);
result.classic.rmsep.trim95=sqrt(1/N95*trim95);
end
result.classic.class=inmodel.classic.class;
end