Re: [R] Question about lmer

From: Douglas Bates <>
Date: Mon, 18 Jun 2007 06:28:53 -0500

On 6/18/07, Julia Proudnikova <> wrote:
> Hello,
> We have a problem with function lmer. This is our code:
> Get_values<-function(ff_count, fixed_factors, rf_count, random_factors, y_values)
> {
> SA<-matrix(as.array(c(fixed_factors, random_factors)), ncol=3)
> data<
> y<-as.array(y_values)
> dd<-data.frame(SA)
> for(i in 1:(ff_count+rf_count)){
> dd[,i]<-as.factor(data[,i])
> }
> fit_full=lmer(y~dd[,1]+dd[,2]+(1|dd[,3]),method="ML")
> fit_full
> }
> A<-c(0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1)
> B<-c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)
> C<-c(0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1)
> Y<-c(5,3,4,1,1,2,6,1,5,3,7,1,2,3,1,1,5,3,4,1,1,2,6,1,5,3,7,1,2,3,1,1)
> r<-Get_values(2, c(A,B),1,c(C),Y)
> r
> R output:
> Error in inherits(x, "factor") : object "dd" not found
> Can this function work with random array? Because this code is
> working:

The full explanation of why lmer fails to find dd has to do with the way names are resolved in a call to model.frame. However, there may be a way to solve your problem by redesigning your function so you don't need to worry about what model.frame does.

Why not pass the data as a data frame and pass the names of the fixed factors, random factors and response variable as character strings? Your current design of creating a matrix, then converting it to a data frame then converting numeric variables back to factors is a bit convoluted.

If you knew that you were only going to have one random factor you could generate the formula as

substitute(y ~ ff + (1|rf), list(y =, ff = parse(paste(ff_names, collapse = "+")), rf =

It gets a bit trickier with multiple random factors.

Having said all this, it does appear that the call to model.frame inside lmer is getting the wrong environment from the formula and I will correct that.

If you need more detail about the redesign I am suggesting, feel free to contact me off-list. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 18 Jun 2007 - 11:44:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Jun 2007 - 12:32:03 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.