[R] nlme question

Thomas Lumley tlumley at u.washington.edu
Tue Mar 23 18:49:47 CET 2004


On Tue, 23 Mar 2004 Jens_Praestgaard at hgsi.com wrote:

> I have a need to call and pass arguments to nlme()  from within another
> function. I use R version 1.8.
>  I have found an apparent way to make this work, but I would appreciate
> some comments  on whether this fix is really appropriate, or there is
> another way to do it that does not involve changing the  source code. I
> don't have enough experience to start changing the sorurce code of a
> library function.
>
> Calling nlme from within a function as done below  will give an error:
>
> testfunc<-
> function(dat=v) {
> test<-nlsList(result~a+(b-a)/(1+(conc/(c+z*cdiff))^d)
> |rep,start=dat$init,data=dat$mixeddat)
> return(nlme(test,random=b~1))
> }
>
> Here, v is an appropriate data frame in the main workspace.
>
>
> Inserting two lines in nlme.nlsList will fix it:
>
> Replacing line 21 from bottom "mData<-eval(mData) "  by " mData <-
> eval(mData,parent.frame())"
>
> Inserting     thisCall[["data"]]<-mData  on line 4 from bottom before val
> <- do.call("nlme.formula", thisCall)
>
> Doing this makes nlme.nlsList recognize the input data of the regression.
> But is there a way to do this without changing the source code?

I'm surprised this works.  I tried
data(Loblolly)
ff<-function(dat){
     fm1 <- nlsList(SSasymp, data = dat)
     fm2 <- nlme(fm1, random = Asym ~ 1)
     fm2
}
ff(Loblolly)

using the example from ?nlme.nlsList, and it failed in the nlsList()
evaluation, because nlsList was passing the name of its data argument and
losing the location.  It didn't get as far as nlme.nlsList.

In this example

ff1<-function(dat){
	fm1<-eval.parent(substitute(nlsList(SSasymp, data = dat)))
	fm2<-nlme(fm1,random=Asym~1)
	fm2
}
ff1(Loblolly)

works, but it isn't pretty (and will fail if `dat' isn't in the global
workspace).


I think it will take more surgery on nlsList and nlme to make this work
generally.


	-thomas




More information about the R-help mailing list