Skip to content

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

2 messages · Jeremy Koster, Ben Bolker

#
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!
#
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 05/12/2011 03:01 PM, Jeremy Koster wrote:
Use str() to see what the structure of the object actually is.  I think
it's a list of data frames.

  (For example:

library(lme4)
example(lmer)

str(ranef(fm1))
List of 1
 $ Subject:'data.frame':	18 obs. of  2 variables:
  ..$ (Intercept): num [1:18] 2.26 -40.4 -38.96 23.69 22.26 ...
  ..$ Days       : num [1:18] 9.2 -8.62 -5.45 -4.81 -3.07 ...
 - attr(*, "class")= chr "ranef.mer"

  Some options for exporting the information are: dput(), sink(); or
using write.table() to send the individual chunks.

?write.table
lapply(ranef(fm1),write.table,append=TRUE,file="out.dat")

  Might work for a two-level model (although you may end up with a file
that's hard to read back into R, since it may have unequal numbers of
columns and will have header rows in the middle of the file if you have
multiple grouping variables).

  What do you want to do with the output file?
R
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3MMIMACgkQc5UpGjwzenPF8QCcD+qSwFXsXMyTTexa6tsRAxtd
XngAn03QDJuVCPrmIabPNaOrWamF4GgD
=p+/d
-----END PGP SIGNATURE-----