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

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


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(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