[R] how to use try()

Douglas Bates bates at stat.wisc.edu
Tue Feb 10 00:47:26 CET 2004


Check the function nlsList in package nlme.  It does something very
like what you want to do.

"r.ghezzo" <heberto.ghezzo at mcgill.ca> writes:

> Hello, I have a program with this section:
> ..
> for(i in 1:20){
>    lo <- nls(y~y0+a/(1+(x/x0)^b),start=list(y0=0.1,a=a0,x0=x00,b=-8.1))
>    beta[i] <- lo$m$getPars()[4]
> }
> ..
> If the fit works this is OK but if the fit fails, the whole program
> fails so:
> 
> ..
> for(i in 1:20){
>    try(lo <-
> 
> nls(y~y0+a/(1+(x/x0)^b),start=list(y0=0.1,a=a0,x0=x00,b=-8.1)))
>    beta[i] <- lo$m$getPars()[4]
> }

It is not a good idea to use lo$m$getPars() directly.  It is better to
use the generic function, which in this case is coef(), as in

coef(lo)[4]




More information about the R-help mailing list