Skip to content

Basic file management -- How to write.table of, ranef()

2 messages · Petar Milin, Douglas Bates

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

Finally, you can do:
write.table(rA, 'file.txt', ..., ...)

All the best,
Petar
#
On Thu, May 12, 2011 at 4:25 PM, Petar Milin <pmilin at ff.uns.ac.rs> wrote:
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.
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).