偏最小二乘法 PLS 回归 NIPALS 算法的 Matlab 程序及例子②
function [T,P,W,Wstar,U,b,C,B_pls,...
Bpls_star,Xori_rec,Yori_rec,...
R2_X,R2_Y]=PLS_nipals(X,Y,nfactor)
% USAGE: [T,P,W,Wstar,U,b,C,Bpls,Bpls_star,Xhat,Yhat,R2X,R2Y]=PLS_nipals(X,Y,nfact)
% PLS regression NIPALS algorithm PLS 回归 NIPALS 算法
% Compute the PLS regression coefficients PLS 回归系数的计算
% X=T*P' Y=T*B*C'=X*Bpls X and Y being Z-scores
% B=diag(b)
% Y=X*Bpls_star with X being augmented with a col of ones
% and Y and X having their original units
% T'*T=I (NB normalization <> SAS)
% W'*W=I
%
% Test for PLS regression
% Herve Abdi November 2002/rev November 2004
%
%
% Version with T, W, and C being unit normalized
% U, P are not
% nfact=number of latent variables to keep 保持潜在变量的数量
% default = rank(X)
X_ori=X;
Y_ori=Y;
if exist('nfactor')~=1;nfactor=rank(X);end
M_X=mean(X);
M_Y=mean(Y);
S_X=std(X);
S_Y=std(Y);
X=zscore(X);
Y=zscore(Y);
[nn,np]=size(X) ;
[n,nq]=size(Y) ;
if nn~= n;
error(['Incompatible # of rows for X and Y']);
end
% Precision for convergence
epsilon=eps;
% # of components kepts
% Initialistion
% The Y set
U=zeros(n,nfactor);