Hello, I am working with the nls() function and inserting a formula into it that uses the pweibull function. However the pweibull function is annoyingly producing NaNs, which nls() refuses to handle. I have put a sample of the code below. Is there a way to prevent these NaNs from interfering, for example a method to catch them? I get the following error when I try to run the code: res.nls <- nls(cumsumcaught ~ Pcatch*Prepens.released*(1-pweibull(distances,k,l)), + start = list(k=1,l=12), trace=T, control = my.control) Error in numericDeriv(form[[3]], names(ind), env) : Missing value or an infinity produced when evaluating the model In addition: Warning messages: 1: NaNs produced in: pweibull(q, shape, scale, lower.tail, log.p) 2: NaNs produced in: pweibull(q, shape, scale, lower.tail, log.p) HERE IS THE CODE: # Observed data Prepens.caughtintraps = c(94,45,71,64,71,31,16,19,27,16,113) # Numbers of seeds released Prepens.released = 1250 distances = c(2,3,4,5,7.5,10,12.5,15,17.5,20,25) totalseedscaught = sum(Prepens.caughtintraps) Pcatch = totalseedscaught/Prepens.released i=11 cumsumcaught = numeric(11) while (i>0){ cumsumcaught[i] <- sum(Prepens.caughtintraps[i:11]) i=i-1 } res.nls <- nls(cumsumcaught ~ Pcatch*Prepens.released*(1-pweibull(distances,k,l)), start = list(k=1,l=12), trace=T) Regards, Rob -- View this message in context: http://www.nabble.com/Catching-NaNs-from-pweibull%28%29-tp15356718p15356718.html Sent from the R help mailing list archive at Nabble.com.
That's odd, it works just fine for me (R 2.6.1, i486-pc-linux-gnu) Can you send the results of sessionInfo() ? # Observed data Prepens.caughtintraps = c(94,45,71,64,71,31,16,19,27,16,113) # Numbers of seeds released Prepens.released = 1250 distances = c(2,3,4,5,7.5,10,12.5,15,17.5,20,25) totalseedscaught = sum(Prepens.caughtintraps) Pcatch = totalseedscaught/Prepens.released ## minor tweak: this is more efficient, gives same answer cumsumcaught <- rev(cumsum(rev(Prepens.caughtintraps))) res.nls <- nls(cumsumcaught ~ Pcatch*Prepens.released*(1-pweibull(distances,k,l)), start = list(k=1,l=12), trace=TRUE) ## slightly more accurate -- use lower.tail instead of 1-pweibull() ## but makes no difference in this case res.nls <- nls(cumsumcaught ~ Pcatch*Prepens.released* pweibull(distances,k,l,lower.tail=FALSE), start = list(k=1,l=12), trace=TRUE) 13122.82 : 1 12 11783.76 : 1.045207 12.668148 11774.70 : 1.035480 12.676250 11774.54 : 1.036564 12.679938 11774.54 : 1.036398 12.679737 11774.54 : 1.036420 12.679783 11774.54 : 1.036417 12.679778 Nonlinear regression model model: cumsumcaught ~ Pcatch * Prepens.released * pweibull(distances, k, l, lower.tail = FALSE) data: parent.frame() k l 1.036 12.680 residual sum-of-squares: 11775 Number of iterations to convergence: 6 Achieved convergence tolerance: 1.388e-06
Hello Ben, Ok this is going to sound very bizare, but the code now works. I struggled for a long time to see why it was returning NaN values. I'm sure the code is exactly the same. But anyway it now seems to be working (for the time being). Thanks for the input though. Regards, Rob -- View this message in context: http://www.nabble.com/Catching-NaNs-from-pweibull%28%29-tp15356718p15359644.html Sent from the R help mailing list archive at Nabble.com.