[R] Question about lmer
Douglas Bates
bates at stat.wisc.edu
Mon Jun 18 13:28:53 CEST 2007
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:
> 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 = 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.
More information about the R-help
mailing list