Thanks to Tim Hesterberg, Nick Ellis, Luciano Molinari, Falk Huettman &
Henrik Nielsen for tips here. The first was to use the proper list
subscripting ([[]] instead of []), and several suggestions were made
suggesting use of the assign() function. In the end, I just couldn't get it
to store any values, and wound up having to change the nls() routine to not
throw an error upon non-convergence.
Would be curious to here more about how the try() function works, and if
there's a class of errors designed not to be caught?
Cheers,
Robert Field
> -----Original Message-----
> From: Field, Robert
> Sent: Monday, January 20, 2003 4:22 PM
> To: 's-news@lists.biostat.wustl.edu'
> Subject: avoiding nls() errors in a for loop
>
> Hi list,
>
> I'm running repeated nls() attempts on a series of repeated sub-samples in
> a for() loop, wanting to store the results of successful nls() estimates
> to look at afterwards. Not all nls() fits are successful; based on tips
> from the archives, I've been able to use the try() function to keep the
> loop going even when nls() throws an error.
>
> What I can't do though, is save the results of the nls() calls. I'm trying
> to store each iteration's results in a vector. I can watch the loop run,
> with some successful nls() calls, and some unsuccessful. Afterwards
> though, all entries in the vector to which I'm trying to assign the nls
> results are NULL. Code is as follows:
>
> # create vector to store nls results. Results will be either of type nls()
> or error.
> NLSResultVector <- vector( mode = "list", length = nIterations)
>
> # try nIterations worth of nls() attempts, saving results at each
> iteration
> for (currIteration in 1:nIterations)
> {
> # retrieve current random subset
> currentData <- menuRandomSample(...)
>
> # attemp fit to current model
> currentFit <- try(nls(...., data = currData))
>
> # store the results of the nls attempt, even if its an error ob.
> NLSResultVector[currIteration] <- currentFit
>
> }
>
> If I remove the try(nls(...)) line, and assign the
> NLSResultVector[currIteration] a dummy value (say currIteration), things
> work fine. It's when the try(nls(...)) is included that the
> NLSResultVector[currIteration] <- currentFit assignment fails. Also, for a
> single successful attempt, I can store nls objects to the vector of
> results.
>
> Any thoughts as to the cause? Do I have some kind of scoping problem with
> NLSResultVector, or the way that I'm using the try() function?
>
> I'm using SPLUS 6.0 on Win2K.
>
> Many thanks for any tips here....
>
> Robert Field Robert Field
> SEAFDRS SEDFASE
> Canadian Forest Service Service canadien des forêts
> Natural Resources Canada Ressources naturelles Canada
> Government of Canada Gouvernement du Canada
> 5320-122 Street 5320, 122e rue
> Edmonton, AB Edmonton (Alberta)
> T6H 3S5 T6H 3S5
> Canada Canada
>
> Tel: 780-430-3803 Tél: 780-430-3803
> Fax: 780-435-7359 Télécopieur: 780-435-7359
> Email:rfield@nrcan.gc.ca courriel:rfield@rncan.gc.ca
>
|