# Re: [R] Question about lmer

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

On 6/18/07, Julia Proudnikova <prudnikova_at_itcwin.com> 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<-as.data.frame(SA)
> 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:
>
> 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 = as.name(y_name), ff = parse(paste(ff_names, collapse = "+")), rf = as.name(rf_name))

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.

R-help_at_stat.math.ethz.ch mailing list
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. 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.