# Re: [R] Using lme() inside a function (should be: stepAIC from lme inside function)

From: Dieter Menne <dieter.menne_at_menne-biomed.de>
Date: Fri, 09 May 2008 13:55:34 +0000 (UTC)

ONKELINX, Thierry <Thierry.ONKELINX <at> inbo.be> writes:

> This solutions works with R 2.7.0 under windows
>
> library(MASS)
> library(nlme)
> PredRes <- function(cal, val){
> cal <<- cal
> lmemod <- lme(distance ~ age * Sex, random = ~1|Subject, data = cal,
> method="ML")
> themod <- stepAIC(lmemod, dir="both", trace = FALSE)
> prs <- predict(themod, newdata = val)
> obs <- val\$distance
> print(mean(obs - prs))
> }
>
> PredRes(cal = subset(Orthodont, age!=14), val = subset(Orthodont,
> age==14))
> obs<-subset(Orthodont,age==14)\$distance
> > print(mean(obs-prs))

That's a workaround, but it effectively makes cal global, and so should be considered not so nice programming practice. The problem is an environment problem in stepAIC, and I am sure Brian Ripley will tell us what's wrong...

1. inherits(x, "data.frame")
2. is.data.frame(data)
3. lme.formula(fixed = distance ~ age + Sex, data = D1, random = ~1 | Subject, subset = age != 14, method = "ML")
4. eval(expr, envir, enclos)
5. eval(expr, p)
6. eval.parent(nfit)
7. dropterm.default(fit, scope\$drop, scale = scale, trace = max(0, trace - 1), k = k, ...)
8. dropterm(fit, scope\$drop, scale = scale, trace = max(0, trace - 1), k = k, ...)
9. stepAIC(lmemod, dir = "both")
10. PredRes(Orthodont)

R version 2.8.0 Under development (unstable) (2008-05-03 r45596) i386-pc-mingw32

locale:
LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252; LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] nlme_3.1-88 MASS_7.2-42

loaded via a namespace (and not attached): [1] grid_2.8.0 lattice_0.17-7 tools_2.8.0
>

Dieter

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 09 May 2008 - 14:21:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 09 May 2008 - 15:00:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.