24小时热门版块排行榜    

CyRhmU.jpeg
查看: 2468  |  回复: 3

李琴李恺

金虫 (初入文坛)

[求助] 在MATLAB中如何用PLS确定最优主成分数已有1人参与

在MATLAB中如何用PLS确定最优主成分数,258个样本,7个自变量,1个因变量,给出代码例子,加急,谢谢!
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Nkxz

铁杆木虫 (著名写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
感谢参与,应助指数 +1
李琴李恺: 金币+100, ★★★★★最佳答案 2014-03-18 17:22:32
dbb627: 编辑内容 2014-03-18 18:06
dbb627: 编辑内容 2014-03-18 18:08
用x代表自变量矩阵,y表示因变量向量,然后用以下几种方法来确定因子数:其中lv为最大因子数,您这个我建议用5-7试下,sel表示数据预处理方法,0为中心化,1为标准化,其它为不作处理,作完以后用rmsecv对因子数作图,找最低点或者拐点即可。(你有pls算法程序吧?)
CODE:
function [r,rmsecv,yp] = loocv2(x,y,lv,sel)
if nargin <4;sel = 1;end
T = size(x,1);
yp = zeros(size(y));
for i = 1:T
    x1 = x(i,:);
    ind = 1:T;ind(i) = [];
    x2 = x(ind,:);
    y2 = y(ind,:);
    if eq(sel,0)
        [x2,mx] = mncn(x2);
        x1 = scale(x1,mx);
        [y2,my] = mncn(y2);
    elseif eq(sel,1)
        [x2,mx,stdx] = auto(x2);
        x1 = scale(x1,mx,stdx);
        [y2,my,stdy] = auto(y2);
    end
    b = simpls(x2,y2,lv);
    yp0 = x1 * b(:,lv);
    if eq(sel,0)
        yp0 = rescale(yp0,my);
    elseif eq(sel,1)
        yp0 = rescale(yp0,my,stdy);
    end
    yp(i) = yp0;   
end
r = corr(yp,y);
rmsecv = rms(y-yp);

function [mcx,mx] = mncn(x)
%MNCN Mean center scales matrix to mean zero.
%  Mean centers matrix (x), returning a matrix with
%  mean zero columns (mcx) and the vector of means
%  (mx) used in the scaling.
%
%I/O: [mcx,mx] = mncn(x);
%
%See also: AUTO, MDAUTO, MDMNCN, MDRESCAL, MDSCALE, SCALE, RESCALE

%Copyright Eigenvector Research 1991-98
%Modified 11/93
%Checked on MATLAB 5 by BMW  1/4/97

[m,n] = size(x);
mx    = mean(x);
mcx   = (x-mx(ones(m,1),:));

function sx = scale(x,means,stds)
%SCALE Scales matrix as specified.
%  Scales a matrix (x) using means (mx) and standard
%  deviations (stds) specified.
%
%I/O:  sx = scale(x,mx,stdx);
%
%  If only two input arguments are supplied then the function
%  will not do variance scaling, but only vector subtraction.
%
%I/O:  sx = scale(x,mx);
%
%See also: AUTO, MDAUTO, MDMNCN, MDRESCAL, MDSCALE, MNCN, RESCALE

%Copyright Eigenvector Research, Inc. 1991-98
%Modified 11/93
%Checked on MATLAB 5 by BMW  1/4/97

[m,n] = size(x);
if nargin == 3
  sx = (x-means(ones(m,1),:))./stds(ones(m,1),:);
else
  sx = (x-means(ones(m,1),:));
end
function rx = rescale(x,mx,stdx)
%RESCALE Rescales matrix
%  Rescales a matrix (x) using the means (mx) and
%  standard deviation (stdx) vectors specified.
%
%I/O: rx = rescale(x,mx,stdx);
%
%  If only two input arguments are supplied the
%  function rescales the means only.
%
%I/O: rx = rescale(x,mx);
%
%See also:  AUTO, MDAUTO, MDMNCN, MDRESCAL, MDSCALE, MNCN, SCALE

%Copyright Eigenvector Research, Inc. 1991-98
%Modified 11/93
%Checked on MATLAB 5 by BMW  1/4/97

[m,n] = size(x);
if nargin == 3
  rx  = (x.*stdx(ones(m,1),:))+mx(ones(m,1),:);
else
  rx  = x+mx(ones(m,1),:);
end

function [ax,mx,stdx] = auto(x)
%AUTO Autoscales matrix to mean zero unit variance
%  Autoscales a matrix (x) and returns the resulting matrix (ax)
%  with mean-zero unit variance columns, a vector of means (mx)
%  and a vector of standard deviations (stdx) used in the scaling.
%
%I/O:  [ax,mx,stdx] = auto(x);
%
%See also: MDAUTO, MDMNCN, MDRESCAL, MDSCALE, MNCN, SCALE, RESCALE

%Copyright Eigenvector Research, Inc. 1991-98
%Modified 11/93
%Checked on MATLAB 5 by BMW  1/4/97

[m,n] = size(x);
mx    = mean(x);
stdx  = std(x);
ax    = (x-mx(ones(m,1),:))./stdx(ones(m,1),:);

[ Last edited by dbb627 on 2014-3-18 at 18:08 ]

» 本帖已获得的红花(最新10朵)

2楼2014-03-17 13:54:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Nkxz

铁杆木虫 (著名写手)

【答案】应助回帖

笑脸用小写的:)来代替,囧。
3楼2014-03-17 13:55:56
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

李琴李恺

金虫 (初入文坛)

送红花一朵
引用回帖:
2楼: Originally posted by Nkxz at 2014-03-17 13:54:55
用x代表自变量矩阵,y表示因变量向量,然后用以下几种方法来确定因子数:其中lv为最大因子数,您这个我建议用5-7试下,sel表示数据预处理方法,0为中心化,1为标准化,其它为不作处理,作完以后用rmsecv对因子数作图 ...

您好,我是MATLAB新手,我没有pls算法程序,您可以给我吗?
您建议用5-7试下,是什么意思啊?
4楼2014-03-17 16:37:45
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 李琴李恺 的主题更新
信息提示
请填处理意见