s-news
[Top] [All Lists]

avoiding nls() errors in a for loop

To: "'s-news@lists.biostat.wustl.edu'" <s-news@lists.biostat.wustl.edu>
Subject: avoiding nls() errors in a for loop
From: "Field, Robert" <RField@NRCan.gc.ca>
Date: Mon, 20 Jan 2003 18:22:22 -0500
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


<Prev in Thread] Current Thread [Next in Thread>