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

David Winsemius dwinsemius at comcast.net
Tue Feb 24 21:47:56 CET 2015


On Feb 24, 2015, at 10:24 AM, William Randall Henner wrote:

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

Regression functions are generally set up to first look for the tokens/symbols that are in the formulae first in that data argument column-names and then in the calling environment.  It's not finding an "x" or a "y" in either of those locations (R being case sensitive). You _should_ try to pass a dataframe with column-names that match the tokens in the formula argument exactly to a `data=` parameter.


> 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]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list