⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pls.m

📁 偏最小二乘算法在MATLAB中的实现
💻 M
字号:
function [p,q,w,t,u,b,ssqdif] = pls(x,y,lv)
%PLS Partial least squares regression
%  Inputs are the matrix of predictor variables (x) and matrix
%  of predicted variables (y) and the number of latent variables 
%  to be calculated (lv). Outputs are the x loadings (p), y
%  loadings (q), x weights (w), x scores (t), y scores (u),
%  inner relation coefficients (b) and the fraction of
%  variance used in the x and y matrices (ssqdif).
%  I/O format is: [p,q,w,t,u,b,ssqdif] = pls(x,y,lv);

%  Copyright
%  Barry M. Wise
%  1991
%  Modified by B.M. Wise, November 1993

[mx,nx] = size(x);
[my,ny] = size(y);
if nx < lv
  error('No. of LVs must be <= no. of x-block variables')
end 
p = zeros(nx,lv);
q = zeros(ny,lv);
w = zeros(nx,lv);
t = zeros(mx,lv);
u = zeros(my,lv);
b = zeros(1,lv);
ssq = zeros(lv,2);
ssqx = sum(sum(x.^2)');
ssqy = sum(sum(y.^2)');
for i = 1:lv
  [pp,qq,ww,tt,uu] = plsnipal(x,y);
  b(1,i) = uu'*tt/(tt'*tt);
  x = x - tt*pp';
  y = y - b(1,i)*tt*qq';
  ssq(i,1) = (sum(sum(x.^2)'))*100/ssqx;
  ssq(i,2) = (sum(sum(y.^2)'))*100/ssqy;
  t(:,i) = tt(:,1);
  u(:,i) = uu(:,1);
  p(:,i) = pp(:,1);
  w(:,i) = ww(:,1);
  q(:,i) = qq(:,1);
end
ssqdif = zeros(lv,2);
ssqdif(1,1) = 100 - ssq(1,1);
ssqdif(1,2) = 100 - ssq(1,2);
for i = 2:lv
  for j = 1:2
    ssqdif(i,j) = -ssq(i,j) + ssq(i-1,j);
  end
end
disp('  ')
disp('       Percent Variance Captured by PLS Model   ')
disp('  ')
disp('           -----X-Block-----    -----Y-Block-----')
disp('   LV #    This LV    Total     This LV    Total ')
disp('   ----    -------   -------    -------   -------')
ssq = [(1:lv)' ssqdif(:,1) cumsum(ssqdif(:,1)) ssqdif(:,2)...
 cumsum(ssqdif(:,2))];
format = '   %3.0f     %6.2f    %6.2f     %6.2f    %6.2f';
for i = 1:lv
  tab = sprintf(format,ssq(i,:)); disp(tab)
end
disp('  ')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -