Skip to content
Prev 13143 / 20628 Next

nlme prediction with predictor function

Dear Mario,

You'll remember I was struggling to find a way of passing a function to nlme within a package. After about 5 years I have finally cracked it!

This is the structure I want, but unless both instructions are in globalenv nlme does not see fitnlme.
fitnlme <- function(...) {...}
nlme(y ~ fitnlme(...), fixed = ..., random = ..., ...)

The solution is as follows: create a new environment, write fitnlme to it and then attach it:
fitenv <- new.env()
fitenv$fitnlme <- function(...) {...}
attach(fitenv)
on.exit(detach(fitenv))
nlmecode <- "text containing nlme command code"
nlme.out <- eval(parse(text=nlmecode))

It works!

Best wishes,
Tim
---
Tim.Cole at ucl.ac.uk<mailto:Tim.Cole at ich.ucl.ac.uk> Phone +44(0)20 7905 2666 Fax +44(0)20 7905 2381
Population, Policy and Practice Programme
UCL Institute of Child Health, London WC1N 1EH, UK