[R] help:coerce lmer.coef to matrix

Douglas Bates bates at stat.wisc.edu
Fri Aug 11 16:59:18 CEST 2006


On 8/11/06, Simon Pickett <S.Pickett at exeter.ac.uk> wrote:
> Hi,
> Thanks for your response, it nearly worked! But it only wrote one coloumn
> of data and not the three columns I need.
> Using fixef(m1) doesnt give the same results as coef(m1) when you are
> using more than one random effect. I need the coefficients for each
> individual so I use coef(m1) to get this which results in an object of
> class lmer.coef, 3 columns by 700 rows.
> as.data.frame() wont work on this and I cant seem to specify that I want
> three columns when I tried <-matrix(lmer.coef,ncol=length(lmer.coef))....
> Thanks very much,

Situations like this are when Martin Maechler's str function comes in
so handy.  It displays the structure of the object from which you will
see that coef(lmerModel) returns a list of data frames, not a data
frame.  You can create a matrix from an element of thiis list easily.

> library(Matrix)
Loading required package: lattice
> data(sleepstudy)
> (fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy))
Linear mixed-effects model fit by REML
Formula: Reaction ~ Days + (Days | Subject)
   Data: sleepstudy
      AIC      BIC    logLik MLdeviance REMLdeviance
 1753.628 1769.593 -871.8141   1751.986     1743.628
Random effects:
 Groups   Name        Variance Std.Dev. Corr
 Subject  (Intercept) 612.090  24.7405
          Days         35.072   5.9221  0.066
 Residual             654.941  25.5918
number of obs: 180, groups: Subject, 18

Fixed effects:
            Estimate Std. Error t value
(Intercept) 251.4051     6.8246  36.838
Days         10.4673     1.5458   6.771

Correlation of Fixed Effects:
     (Intr)
Days -0.138
> str(coef(fm1))
Formal class 'coef.lmer' [package "Matrix"] with 1 slots
  ..@ .Data:List of 1
  .. ..$ :`data.frame':	18 obs. of  2 variables:
  .. .. ..$ (Intercept): num [1:18] 254 211 212 275 274 ...
  .. .. ..$ Days       : num [1:18] 19.67  1.85  5.02  5.65  7.40 ...
> as.matrix(coef(fm1)[[1]])
    (Intercept)       Days
308    253.6637 19.6662580
309    211.0065  1.8475846
310    212.4448  5.0184079
330    275.0956  5.6529533
331    273.6653  7.3973901
332    260.4446 10.1951148
333    268.2455 10.2436606
334    244.1725 11.5418624
335    251.0714 -0.2848734
337    286.2955 19.0955683
349    226.1950 11.6407015
350    238.3351 17.0814918
351    255.9829  7.4520285
352    272.2687 14.0032983
369    254.6806 11.3395024
370    225.7922 15.2897520
371    252.2121  9.4791308
372    263.7196 11.7513151



More information about the R-help mailing list