0
votes

EDIT I found that y(c) in my script gives this error:

Subscript indices must either be real positive integers or logicals.

but in the example script y(c) prints the values of y that are censored.

I am trying to use a script that I found for analysis of censored data. The script itself works fine but when I try to use it with my own data I get an error.

This is the example code:

% 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])

Here is my code:

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])

The error is:

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])

I tried to debug it but p seems to be called before it is defined. I found that nloglik is a function with input parameter p.

How does my script give this error but not the example script? And how do I get past this error?

2
I can't run matlab right now but I think was is happening is x and y are row arrays but should be column arrays. Perform a transpose on these with .' before executing the fit.Buck Thorn
Thanks, I tried transposing them but it still gives the same errorMatt Majic

2 Answers

1
votes

Change the variable c to logical, i.e.:

c = logical(c)

before you use it for indexing.

Alternatively, create 'c' as logical to begin with:

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

If that does not work, posting a working code will help fix the problem.

1
votes

y(c) was giving the error, not sure why it worked in the example script. Made ya and yc, xa and xc manually to be y(~c), y(c), x(~c) and x(c) respectively and it now works

EDIT:

I made ya, yc, xa, xc like this:

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

and subbed these in for y(~c), y(c), x(~c) and x(c) in the function nloglik