Hello R experts,

 

I am trying to fit an lme model within a function, using a formula that I
passed into the function, and then predict that model from a different
function. Could you please advise me on how to do this? The following code
illustrates the essence of what I'm trying to do. The actual scenario is
more complicated but this toy example illustrates the crux of the problem.

 

> library(nlme)

> 

> Fit <- function(f)

+ {

+     model <- lme(f, BodyWeight, random = ~ Time)   # BodyWeight is from
nlme package

+     return(model)

+ }

> m1 <- Fit(weight ~ Time * Diet)

> 

> Predict <- function(m)

+ {

+     print(predict(m, m$data))

+ }

> Predict(m1)

Error in eval(expr, envir, enclos) : object 'f' not found

> 

> traceback()

7: eval(expr, envir, enclos)

6: eval(mCall$fixed)

5: eval(eval(mCall$fixed)[-2])

4: predict.lme(m, m$data)

3: predict(m, m$data)

2: print(predict(m, m$data))

1: Predict(m1)

 

I have tried various things using parse(), eval(), force(), and so on and
could not figure it out. I suspect this all comes down to a basic ignorance
on my part regarding R's lazy evaluation mechanism, promises, and
environments. I've encountered similar problems elsewhere when passing other
things into functions. It seems like some packages, such as nlme here, have
this restriction and other packages do not. I would appreciate anything you
can do to enlighten me about how this is supposed to work.

 

Thanks very much,

 

Jason

 


	[[alternative HTML version deleted]]

