fminsearch审查了MATLAB

时间:2015-04-15 03:47:50

标签: matlab survival-analysis fminsearch

EDIT 我发现我的脚本中的y(c)给出了这个错误:

Subscript indices must either be real positive integers or logicals.

但是在示例脚本中,y(c)打印了被删除的y的值。

我正在尝试使用我找到的用于分析审查数据的脚本。脚本本身工作正常,但当我尝试将它与我自己的数据一起使用时,我收到错误。

这是示例代码:

% I have some (x,y) data
n = 100;
x = 10*rand(n,1);
y = 5 + .5*x + randn(n,1);
plot(x,y,'o','Color',[.8 .8 .8]);

% But it's censored, I can't observe values larger than 8
c = y>8
o = min(y,8);
line(x,o,'Marker','o','Color','b','LineStyle','none')

% If I fit a line to the data I observe, no good
b = polyfit(x,o,1)
s = norm(o-polyval(b,x))/sqrt(n)
xx = linspace(0,10);
line(xx,polyval(b,xx),'Color','r')

% Instead I need a likelihood function that taket it censoring into account
nloglik = @(p) - sum(log(normpdf(o(~c),p(1)*x(~c)+p(2),p(3)))) ...
               - sum(log(1-normcdf(o(c),p(1)*x(c)+p(2),p(3))));

nloglik = @(p) - sum(log(normpdf(tof(~c),p(1)*z(~c)+p(2),p(3)))) ...
           - sum(log(1-normcdf(tof(c),p(1)*z(c)+p(2),p(3))));
p = fminsearch(nloglik,[b,s])

这是我的代码:

load('UV.mat') % is an 18 column array

for i = 1 : length(UV{1,7})
    if UV{1,7}(i) ~= 0
        x(i)=log10(UV{1,4}(i));
        y(i) = UV{1,7}(i);
    end
end
c=zeros(length(y),1); % c stands for censored
for i=1:length(y)
    if UV{1,8}{i} == 'u' % u stands for upper limit
        c(i)=1;
    end
end

b = polyfit(x,y,1)
s = norm(y-polyval(b,x))/sqrt(length(x))
nloglik = @(p) - sum(log(normpdf(y(~c),p(1)*x(~c)+p(2),p(3)))) ...
               - sum(log(1-normcdf(y(c),p(1)*x(c)+p(2),p(3))));
p = fminsearch(nloglik,[b,s])

错误是:

Subscript indices must either be real positive integers or logicals.

Error in @(p)-sum(log(normpdf(y(~c),p(1)*x(~c)+p(2),p(3))))-sum(log(1-normcdf(y(c),p(1)*x(c)+p(2),p(3))))


Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});

Error in zWithCensoring (line 22)  %zWithCensoring is the name of my script
p = fminsearch(nloglik,[b,s])

我尝试调试它,但似乎在定义之前调用了p。我发现nloglik是一个输入参数为p的函数。

我的脚本如何提供此错误但不提供示例脚本?我如何通过此错误?

2 个答案:

答案 0 :(得分:1)

y(c)发出错误,不知道为什么它在示例脚本中有效。 手动将ya和yc,xa和xc分别设为y(~c),y(c),x(~c)和x(c),现在可以正常工作

编辑:

我这样做了ya,yc,xa,xc:

for i =1:length(y)
    if UV{1,8}{i} == 'u'
        yc(j)=y(i);
        xc(j)=x(i);
        j=j+1;
    else
        ya(k)=y(i);
        xa(k)=x(i);
        k=k+1;
    end
end

并在函数nloglik中为y(~c),y(c),x(~c)和x(c)加上这些

答案 1 :(得分:1)

将变量c更改为逻辑,即:

c = logical(c)
在您使用它进行索引之前

或者,创建' c'从逻辑开始:

c= false(length(y),1); % c stands for censored
for i=1:length(y)
    if UV{1,8}{i} == 'u' % u stands for upper limit
        c(i)=true;
    end
end

如果这不起作用,发布工作代码将有助于解决问题。