📄 plsrsgcv.m
字号:
function coeff = plsrsgcv(data,lv,cvit,cvnum)
%PLSRSGCV Generates PLS models for MSPC with cross-validation
% This function constructs a matrix of PLS models that
% can be used like a PCA model for multivariate statistical
% process control (MSPC) purposes. Given a data matrix (data)
% a PLS model is formed using a maximum of (lv) latent variables
% that relates each variable to all the others. The actual
% number of latent variables used is determined through cross
% validation using (cvit) test sets with (cvnum) number of
% samples chosen randomly. The PLS model regression
% vectors are collected in an output matrix (coeff) which
% can be used like the I=PP' matrix in PCA.
% I/O format is: coeff = plsrsgncv(data,lv,cvit,cvnum);
%
% Warning: If you choose to do lots of cross-validations
% this function can take a long time to execute!
% Copyright
% Barry M. Wise
% 1991
% Modified April 1994
[m,n] = size(data);
if lv >= n
error('Number of lvs must be <= number of variables - 1')
end
if cvnum >= m
error('Number of samples in test set must be < total samples')
end
coeff = -eye(n);
disp(' ')
clc
for i = 1:n
x = [data(:,1:i-1) data(:,i+1:n) data(:,i)];
press = zeros(lv,1);
for j = 1:cvit
home
s = sprintf('Working on variable number %g, iteration %g',i,j);
disp(s)
x = shuffle(x);
[P,Q,W,T,U,b,ss] = pls(x(1:m-cvnum,1:n-1),x(1:m-cvnum,n),lv);
mm = conpred(b,W,P,Q,lv);
cm = cumsum(mm);
for k = 1:lv
ypred = x(m-cvnum+1:m,1:n-1)*cm(k,:)';
press(k,1) = sum((ypred-x(m-cvnum+1:m,n)).^2) + press(k,1);
end
end
plot(press)
s = sprintf('Cumulative PRESS versus Number of Latent Variables for Variable %g',i);
title(s)
xlabel('Number of Latent Variables')
ylabel('Cumulative PRESS')
drawnow
[a,bb] = min(press);
s = sprintf('Minimum PRESS for variable %g is at %g',i,bb);
disp(s)
home
s = sprintf('Now forming final PLS model for variable %g',i);
disp(s)
[P,Q,W,T,U,b,ss] = pls(x(:,1:n-1),x(:,n),bb);
mm = conpred(b,W,P,Q,bb);
cm = (sum(mm))';
for j = 1:n-1
if i>j
coeff(j,i) = cm(j,1);
end
if i<=j
coeff(j+1,i) = cm(j,1);
end
end
end
coeff = -1*(coeff);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -