[R] how to use try()
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Mon Feb 9 23:11:49 CET 2004
"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]
> }
> ..
> but the try catches the error in nls and beta[i] gets assigned
> beta[i-1] from the previous loop. This is bad but no so bad as it can
> be checked,
> Now in some cases the error is in i=1 and the program stops!!
> is there a way to set lo$m$getPars() to zero before the call?
> I tried to understand the use of tryCatch() but frankly it is above
> me. Sorry
Just check the return value from try:
beta[i] <- if(inherits(try(.....),"try-error")) NA else lo$etc...
(or use sapply) and, er, shouldn't there be a dependency on i
somewhere in the model fit???
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list