[R] Environment error in the car package Anova.lme function

William Randall Henner hennerw at gmail.com
Tue Feb 24 19:24:17 CET 2015


When I call the Anova function on a lme object from inside a function
environment I get an error:

Error in eval(expr, envir, enclos) : object 'y' not found


However, if I call the exact same code in the global environment there is
no error. My theory is that for some reason the Anova.lme function always
searches the global environment for variables rather than starting in its
parent environment.

Since the function runs correctly on lmerMod objects, and I typically
prefer lme4 to nlme this is just of academic interest.

#########
# Steps to reproduce the error

# Load Packages
library(car)
library(lme4)
library(nlme)

# Create random data.
Y <- rnorm(50)
X <- rnorm(50)
Subject <- factor(rep(LETTERS[1:5],each=10))

# Define function to fit a MEM and run the ANOVA.
fun <- function(y,x,sub){
  #mod <- lmer(y ~ x +(1 | sub) )
  mod <- lme(y~x,random=~1|sub)
  print('lme model ran successfully.')
  Anova(mod)
  print('Anova function ran successfully.')
}

# Function produces an error message.
fun(Y,X,Subject)

# if run outside of a function, it runs cleanly.
mod <- lme(Y~X,random=~1|Subject)
Anova(mod)

mod1 <- lmer(Y ~ X +(1 | Subject) )
Anova(mod1)

	[[alternative HTML version deleted]]



More information about the R-help mailing list