[R-sig-ME] Basic file management -- How to write.table of, ranef()

Douglas Bates bates at stat.wisc.edu
Thu May 12 23:44:42 CEST 2011


On Thu, May 12, 2011 at 4:25 PM, Petar Milin <pmilin at ff.uns.ac.rs> wrote:
> Hello Jeremy,
> Probably this could be done in more elegant manner, but here are my first
> thoughts:
> First of all, ranef.mer is not a proper table. That is, it consists of
> several "tables" -- one per random effect.

In the sense of one for each distinct grouping factor. There can be
more than one random-effects term associated with the same grouping
factor and, in that case, the conditional means of the random effects
are combined into one data frame.

> Thus, you could chap off those
> into separate tables and, voila, you can have proper data frame.
>
> For example, consider a model with three random effects:
> lmer1 <- lmer(y ~ x1 + x2 * x3 + (1|A) + (1|B) + (1|C), data=dat)
>
> then, you can do following:
> rA = as.data.frame(ranef(lmer1)$A)
> rB = as.data.frame(ranef(lmer1)$B)
> rC = as.data.frame(ranef(lmer1)$C)
> (even 'as.data.frame(ranef(lmer1)[1])' should work)

Those will work but are somewhat redundant.  As Ben pointed out, if
you use str() on the result of ranef() you will find that it is a list
of data frames - one per grouping factor.  Thus a simple

rr <- ranef(lmer1)
write.table(rr$A)  # rr$A is already a data frame

will work.

Your last suggestion doesn't quite work because ranef(lmer1)[1] is
itself a list.  You need two brackets, as in ranef(lmer1)[[1]] to get
the data frame.  It is like that distinction between a subset of size
1 (the single bracket form) and that particular element of the set
(double brackets).


> Finally, you can do:
> write.table(rA, 'file.txt', ..., ...)
>
> All the best,
> Petar
>
>> Hi all,
>>
>> Apologies for asking such a simple question, but I have been trying to use
>> write.table to export the random-effect intercept estimates for a
>> straightforward two-level varying-intercept model using lme4.
>>
>> For instance, after estimating the model, this code produces a list of the
>> estimated intercepts:
>>
>> ranef(full.model)
>>
>> Then I assign those estimates to an object:
>>
>> random.intercepts <- ranef(full.model)
>>
>> Whenever I try to use write.table to export those estimates, however, I
>> get an error message that the object cannot be coerced into a data frame.
>>
>> File management is clearly not my strong suit, but am I assuming correctly
>> that there should be a relatively painless way of accomplishing this?  Any
>> suggestions?
>>
>> Thanks!
>>
>>
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>




More information about the R-sig-mixed-models mailing list