function [R,RMSE,rmse,r,rr,MRE,RSD] = pls_cv (traindata, trainlab,k)
for j=1:50
index = crossvalind( 'Kfold', length(trainlab) , k );
Yexp = [];
Ypred = [];
for i = 1 : k
test= (index == i);
train = ~test;
x= traindata( train, :);
y = trainlab( train );
xt = traindata( test, :);
yt = trainlab( test );
model=pls(x,y,7,0);
Yt1=xt*model(7,:)';
Yexp = vertcat(Yexp, yt );
Ypred = vertcat(Ypred, Yt1);
end
R = corrcoef(Ypred, Yexp);
r(j) = R(1,2);
RMSE(j)=sqrt(mse(Ypred - Yexp));
rmse=mean(RMSE);
rr=mean(r);
MRE=mean(abs(Ypred - Yexp)./Yexp);
RSD=std(Ypred);
end
想添加一个循环,优化模型model=pls(x,y,7,0)中的潜在变量(第三个参数,目前取7,可以优化),想做到给定一个区间,如[1 20],从该区间内挨个取值并计算,最后看rr和rmse,rr越大,rmse越小,则参数越优,在哪添加一个for循环做到在区间内取值挨个试,还有怎么添加一个网格搜索或者随机搜索找到最优的潜在变量参数值。