[R] SSlogis problem with min(y)==0
Chris Knight
chris.knight at manchester.ac.uk
Mon Jun 27 18:33:44 CEST 2005
Hi, I think this is a problem solved but I would be interested to know
if there is some good reason why SSlogis() behaves like this (apologies
if this has been noticed before- I'm not confident my archive searches
were effective):
I have been fitting large numbers of regressions using nls with a
self-starting 3 parameter logistic model (SSlogis()). I got a series of
unexpected errors of the sort:
model<-nls(y~SSlogis(x, Asym, xmid, scal))
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in foreign function call (arg 1)
Investigation seemed to suggest that this occurs for versions of y where
the minimum value of y was precisely zero. and sure enough, the
following produces that error:
x<-c(1,2,3,4,5,6,7)
y<-c(0,0,1,2,3,4,4)
model<-nls(y~SSlogis(x, Asym, xmid, scal))
whereas the following succeed:
model<-nls(y-0.01~SSlogis(x, Asym, xmid, scal))
model<-nls(y+0.01~SSlogis(x, Asym, xmid, scal))
I don't claim to understand the source code, but it does contain the
lines
if (min(z) <= 0) {
z <- z - 1.05 * min(z)
}
z <- z/(1.05 * max(z))
xy[["z"]] <- log(z/(1 - z))
aux <- coef(lm(x ~ z, xy))
Which would seem to explain it given that if the minimum of z is
precisely zero it will remain the same after z <- z - 1.05 * min(z) so
produce a -Inf for log(z/(1 - z)) going into the lm call and producing
the error. Changing the beginning of the above to:
if (min(z) < 0) {
z <- z - 1.05 * min(z)
}
if (min(z) == 0) {
z <- z + 0.01 * (max(z)-min(z))
}
Seems to produce a function that does what I'm after, so I'm now happy,
though I haven't gone through to check other self-starting functions.
Chris
More information about the R-help
mailing list