MATLAB实现偏最小,偏最小二乘法 matlab程序

函数:

function [T,P,U,Q,B,W] = pls (X,Y,tol2)

% PLS Partial Least Squares Regrassion

%

% [T,P,U,Q,B,Q] = pls(X,Y,tol) performs particial least squares

regrassion

% between the independent variables, X and dependent Y as

% X = T*P' + E;

% Y = U*Q' + F = T*B*Q' + F1;

%

% Inputs:

% X data matrix of independent variables

% Y data matrix of dependent variables

% tol the tolerant of convergence (defaut 1e-10)

%

% Outputs:

% T score matrix of X

% P loading matrix of X

% U score matrix of Y

% Q loading matrix of Y

% B matrix of regression coefficient

% W weight matrix of X

%

% Using the PLS model, for new X1, Y1 can be predicted as

% Y1 = (X1*P)*B*Q' = X1*(P*B*Q')

% or

% Y1 = X1*(W*inv(P'*W)*inv(T'*T)*T'*Y)

%

% Without Y provided, the function will return the principal

components as

% X = T*P' + E

%

% Example: taken from Geladi, P. and Kowalski, B.R., "An example of

2-block

% predictive partial least-squares regression with simulated

data",

% Analytica Chemica Acta, 185(1996) 19--32.

%{

x=[4 9 6 7 7 8 3 2;6 15 10 15 17 22 9 4;8 21 14 23 27 36 15

6;

10 21 14 13 11 10 3 4; 12 27 18 21 21 24 9 6; 14 33 22 29 31 38 15

8;

16 33 22 19 15 12 3 6; 18 39 26 27 25 26 9 8;20 45 30 35 35 40 15

10];

y=[1 1;3 1;5 1;1 3;3 3;5 3;1 5;3 5;5 5];

% leave the last sample for test

N=size(x,1);

x1=x(1:N-1,:);

y1=y(1:N-1,:);

x2=x(N,:);

y2=y(N,:);

% normalization

xmean=mean(x1);

xstd=std(x1);

ymean=mean(y1);

ystd=std(y);

X=(x1-xmean(ones(N-1,1),:))./xstd(ones(N-1,1),:);

Y=(y1-ymean(ones(N-1,1),:))./ystd(ones(N-1,1),:);

% PLS model

[T,P,U,Q,B,W]=pls(X,Y);

% Prediction and error

yp = (x2-xmean)./xstd * (P*B*Q');

fprintf('Prediction error: %g/n',norm(yp-(y2-ymean)./ystd));

%}

%

% By Yi Cao at Cranfield University on 2nd Febuary 2008

%

% Reference:

% Geladi, P and Kowalski, B.R., "Partial Least-Squares Regression:

A

% Tutorial", Analytica Chimica Acta, 185 (1986) 1--7.

%

%

Input check

error(nargchk(1,3,nargin));

error(nargoutchk(0,6,nargout));

if nargin<2

Y=X;

end

tol = 1e-10;

if nargin<3

tol2=1e-10;

end

%

Size of x and y

[rX,cX] = size(X);

[rY,cY] = size(Y);

assert(rX==rY,'Sizes of X and Y mismatch.');

%

Allocate memory to the maximum size

n=max(cX,cY);

T=zeros(rX,n);

P=zeros(cX,n);

U=zero

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值