[R] how to use try()

Andrej Kveder andrejk at zrc-sazu.si
Mon Feb 9 23:46:52 CET 2004


Just check the class() of the lo.

You could try something like this...

for(i in 1:20){
   lo <- try(nls(y~y0+a/(1+(x/x0)^b),start=list(y0=0.1,a=a0,x0=x00,b=-8.1)))
   if (class(lo)=="try-error") beta[i]=NA else
   beta[i] <- lo$m$getPars()[4]
}

Hth

Andrej

_________
Andrej Kveder, M.A.
researcher
Institute of Medical Sciences SRS SASA; Novi trg 2, SI-1000 Ljubljana,
Slovenia
phone: +386 1 47 06 440   fax: +386 1 42 61 493

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of r.ghezzo
Sent: Monday, February 09, 2004 8:30 PM
To: r-help at stat.math.ethz.ch
Subject: [R] how to use try()


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
Thanks for any help
Heberto Ghezzo
Meakins-Christie Labs

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list