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?
.'
before executing the fit. – Buck Thorn