-
Notifications
You must be signed in to change notification settings - Fork 0
/
plscvfold.m
111 lines (93 loc) · 2.58 KB
/
plscvfold.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
function CV=plscvfold(X,y,A,K,method,PROCESS,order)
%+++ K-fold Cross-validation for PLS
%+++ Input: X: m x n (Sample matrix)
% y: m x 1 (measured property)
% A: The maximal number of latent variables for cross-validation
% K: fold. when K=m, it is leave-one-out CV
% method: pretreatment method. Contains: autoscaling,
% pareto,minmax,center or none.
% PROCESS: =1 : print process.
% =0 : don't print process.
%+++ Order: =0 sorted, default. For CV partition.
% =1 random.
% =2 original.
%+++ Output: Structural data: CV
%+++ Hongdong Li, Oct. 16, 2008.
%+++ Tutor: Prof. Yizeng Liang, [email protected].
%+++ Contact: [email protected].
if nargin<7;order=0;end
if nargin<6;PROCESS=0;end
if nargin<5;method='center';end
if nargin<4;K=10;end
if nargin<3;A=3;end
check=0;
if order==0
[y,indexyy]=sort(y);
X=X(indexyy,:);
elseif order==1
indexyy=randperm(length(y));
X=X(indexyy,:);
y=y(indexyy);
elseif order==2
indexyy=1:length(y);
X=X(indexyy,:);
y=y(indexyy);
end
[Mx,Nx]=size(X);
A=min([size(X) A]);
yytest=nan(Mx,1);
YR=nan(Mx,A);
groups = 1+rem(0:Mx-1,K);
for group=1:K
testk = find(groups==group); calk = find(groups~=group);
Xcal=X(calk,:);ycal=y(calk);
Xtest=X(testk,:);ytest=y(testk);
% data pretreatment
[Xs,xpara1,xpara2]=pretreat(Xcal,method);
[ys,ypara1,ypara2]=pretreat(ycal,method);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[B,W,T,P,Q]=pls_nipals(Xs,ys,A); % no pretreatment.
yp=[];
for j=1:A
B=W(:,1:j)*Q(1:j);
%+++ calculate the coefficient linking Xcal and ycal.
C=ypara2*B./xpara2';
coef=[C;ypara1-xpara1*C;];
%+++ predict
Xteste=[Xtest ones(size(Xtest,1),1)];
ypred=Xteste*coef;
yp=[yp ypred];
end
YR(testk,:)=[yp];yytest(testk,:)=ytest;
if PROCESS==1; fprintf('The %dth group finished.\n',group); end;
end
%+++ return the original order
YR(indexyy,:)=YR;
y(indexyy)=y;
if check==0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error=YR-repmat(y,1,A);
PRESS=sum(error.^2);
cv=sqrt(PRESS/Mx);
[RMSEP,index]=min(cv);index=index(1);
SST=sumsqr(yytest-mean(y));
for i=1:A
SSE=sumsqr(YR(:,i)-y);
Q2(i)=1-SSE/SST;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%+++ output %%%%%%%%%%%%%%%%
CV.method=method;
CV.check=0;
CV.residue=error;
CV.RMSECV=RMSEP;
CV.Q2_all=Q2;
CV.Q2_max=Q2(index);
CV.Ypred=YR;
CV.cv=cv;
CV.optPC=index;
elseif check==1
CV.method=method;
CV.check=1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%