希望好心人帮我看看程序哪错了,不甚感激!
data1=importdata('200520063.txt');
Y=data1.data(1,:)';N by P
M=length(Y);
X=data1.data(2:end,:)';%N by K
iter=2;%主成分分数
%数据标准化
for i =1:M
X_M(i,:)=(X(i,:)-mean(X))./std(X);
Y_M(i)=(Y(i,:)-mean(Y))./std(Y);
end
std_Y=std(Y);
ave_Y_new=mean(Y);
[N,K]=size(X_M);
[N,P]=size(Y_M);
if iter > min(N,K)
iter = min(N,K);
end
% start algrithm: PLS
for counter = 1:iter; % extract 3 PLS components
pls_u = Y_M(:,1); % get any column from Y
t_old_n = 1.0;% the ini of t old
t_new_n = 0.0;% the ini of t new
pls_t = 0;
while ( abs( t_old_n - t_new_n )>0.0001)
pls_t_old = pls_t;
% in the X block
pls_w = (pls_u' * X_M / ( pls_u' * pls_u ))';
pls_w = pls_w/norm(pls_w);
pls_t = X_M * pls_w;
% in the Y block
if(P>1)
pls_q = (pls_t' * Y_M /(pls_t'*pls_t))';
pls_q = pls_q/norm(pls_q);
pls_u = Y_M*pls_q;
else
pls_q=1;
end
% compare t,check for convergence
t_old_n = norm( pls_t_old );
t_new_n = norm( pls_t);
end
pls_p = (pls_t'*X_M/(pls_t'*pls_t))';
pls_p_n=norm(pls_p);
pls_p = pls_p/pls_p_n;
pls_t = pls_t*pls_p_n;
pls_w = pls_w*pls_p_n;
pls_b = pls_u'*pls_t/(pls_t'*pls_t);
X_M = X_M - pls_t*pls_p';
Y_M = Y_M - pls_b*pls_t*pls_q';
% pls_U(:,counter)=pls_u;
% pls_T(:,counter)=pls_t;
pls_P(:,counter)=pls_p;
pls_Q(:,counter)=pls_q;
pls_W(:,counter)=pls_w;
pls_B(:,counter)=pls_b;
end
% Now that correction is completed, start validation.
Y_P = 0;
for counter = 1:iter
Y_P =Y_P + pls_B(:,counter)*t_p*pls_Q(:,counter)';
end
for i = 1:N
Y_P(i)=Y_P(i)*std_Y+ave_Y_new;
end
最后的预测值有问题!