[R-sig-ME] lme vs. lm subset argument behaviour
Douglas Bates
bates at stat.wisc.edu
Sat Sep 12 00:03:46 CEST 2009
It's a known problem caused by the fact that we didn't know what
Thomas Lumley calls "the standard non-standard evaluation" used by the
model.frame function. The initial version of the nlme package was
written for S and the evaluation model in S and S-PLUS is different
from that in R. Most model-fitting functions in R evaluate the model
frame by re-writing the call to the function itself as a call to
model.frame and evaluating it the parent environment. That is how you
can get all the weird combinations of arguments to work. The lme and
nlme functions do not do this, in part because there can be multiple
formulas to evaluate (this was one reason that the design of lmer uses
just one formula).
I would suggest using lmer if you can, rather than lme, if you are
going to do something tricky on having a call to lme generated within
another function.
On Thu, Sep 10, 2009 at 5:26 AM, Tobias Verbeke
<tobias.verbeke at gmail.com> wrote:
> There is a small error in my code below,
> but it seems not to affect the difference
> in behaviour observed.
>
> On Thu, Sep 10, 2009 at 12:20 PM, Tobias Verbeke
> <tobias.verbeke at gmail.com> wrote:
>> Dear list,
>>
>> I came across a difference in how lme and lm respond
>> to the subset argument.
>>
>> I must be overlooking something obvious. Any idea ?
>>
>> Many thanks in advance,
>> Tobias
>>
>> ### plain statements
>> library(nlme)
>> lme(fixed = distance ~ age + Sex, data = Orthodont, random = ~1)
>> keepValues <- rep(TRUE, nrow(Orthodont))
>> keepValues[32] <- FALSE
>> lme(fixed = distance ~ age + Sex, data = Orthodont, random = ~1,
>> subset = keepValues)
>> # no Error
>>
>> ### inside function
>> lmeTest <- function(x, keepValues = NULL){
>> keepV <- if (is.null(keepValues)) rep(TRUE, nrow(x)) else keepValues
>> lme(fixed = distance ~ age + Sex, data = x, random = ~1, subset = keepV)
>> }
>>
>> lmeTest(x = Orthodont)
>> # Error in eval(expr, envir, enclos) : object 'keepV' not found
>>
>> ### same scenario using lm instead of lme
>> retainValues <- rep(TRUE, nrow(cars))
>> retainValues[5] <- FALSE
>> lm(dist ~ speed, cars, subset = retainValues)
>>
>> testLm <- function(x, retainValues = NULL){
>> retainV <- if (is.null(retainValues)) rep(TRUE, nrow(x)) else retainValues
>> lm(speed ~ dist, data = cars, subset = retainV)
>> }
>
> testLm <- function(x, retainValues = NULL){
> retainV <- if (is.null(retainValues)) rep(TRUE, nrow(x)) else retainValues
> lm(speed ~ dist, data = x, subset = retainV) # <--- x not cars
> }
>
> Best,
> Tobias
>
>> testLm(x = cars) # no Error
>> testLm(x = cars, retainValues = retainValues) # no Error
>>
>> sessionInfo()
>> # R version 2.9.2 (2009-08-24)
>> # x86_64-pc-linux-gnu
>> #
>> # locale:
>> # en_US.UTF-8
>> #
>> # attached base packages:
>> # [1] stats graphics grDevices utils datasets methods base
>> #
>> # other attached packages:
>> # [1] nlme_3.1-94
>> #
>> # loaded via a namespace (and not attached):
>> # [1] grid_2.9.2 lattice_0.17-25
>>
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>
More information about the R-sig-mixed-models
mailing list