KKThird@Yahoo.Com
2004-Jul-26 20:52 UTC
[R] Problem with a while loop embedded in a function.
Hello all.
I have been working on a (fairly simple) function for way too long. I’ve really
hit a wall and I was hoping someone might be able to point me in the right
direction.
I have (attempted) to create a function that has an embedded while loop. The
while loop works fine by itself, however, when the while loop is embedded in the
function, the function fails. I’m not sure why this happens as the function
really doesn’t add much above and beyond the while loop. I believe it might have
something to do with the list I define in the while loop as
‘nlme.control.parms.’ Because I included print(nlme.control.parms) when the
while loop runs by itself the paramers of nlme.control.parms can be seen to be
fine. However, when I run the function print(nlme.control.parms) is full of NAs.
I’m not sure if the function fails because the list nlme.control.parms is full
of NAs or if nlme.control.parms is full of NAs because the function failed.
I should mention that the function is (supposed to be!) used with nlme so that
multiple sets of parameters can be defined (each next set being less restrictive
than the previous), and nlme runs until it outputs results given the particular
set of parameters. The values I used in the simplified example that follows
would never be used in a real situation, but for purposes of this illustrative
example, the parameters are fine. The result should be a list of the nlme object
and 'OK' (which should be 5 for the example). I'm using R version
1.9.1 on Windows XP.
Thanks for any thoughts, Ken
################
library(nlme)
data(Loblolly)
maxIter <- c(1, 1, 1, 1, 1)
returnObject <- c(FALSE, FALSE, FALSE, FALSE, TRUE)
nlsList.Object <- nlsList(SSasymp, Loblolly)
M1 <- function(maxIter, returnObject)
{
ok <- FALSE
OK <- 0
while(ok==FALSE)
{
OK <- OK + 1
nlme.control.parms <- list("maxIter"=maxIter[OK],
"returnObject"=returnObject[OK])
fm1 <- try(nlme(nlsList.Object,
fixed = Asym + R0 + lrc ~ 1,
random = Asym ~ 1,
control = nlme.control.parms), silent=TRUE)
ok <- is.numeric(fm1$numIter)
print(OK)
print(nlme.control.parms)
}
return(list(fm1, OK))
}
Test <- M1(maxIter=maxIter, returnObject=returnObject)
---------------------------------
[[alternative HTML version deleted]]
