[R-sig-ME] lme vs. lm subset argument behaviour

Tobias Verbeke tobias.verbeke at gmail.com
Thu Sep 10 12:26:52 CEST 2009


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
>




More information about the R-sig-mixed-models mailing list