[R-sig-ME] Extracting means and SEs from an lmer object

Douglas Bates bates at stat.wisc.edu
Tue Feb 7 00:00:22 CET 2012


On Mon, Feb 6, 2012 at 4:26 PM, Chris Eckert <chris.eckert at queensu.ca> wrote:
> Hi,
> I am trying to extract means and SEs from an lmer object.
> So, I followed the example code from http://glmm.wikidot.com/faq
> from the "Predictions and/or confidence (or prediction) intervals on predictions" section of the faq

> library(lme4)
> library(ggplot2) # Plotting
> library(MEMSS) # for Orthodont

The problem may be due to MEMSS bringing in other packages that mask
the definition of fixef in lme4.  It works for me (see enclosed) if I
use
data(Orthodont, package="MEMSS")
instead.

> fm1 = lmer(
>    formula = distance ~ age*Sex + (age|Subject)
>    , data = Orthodont
> )
> newdat <- expand.grid(
>    age=c(8,10,12,14)
>    , Sex=c("Male","Female")
>    , distance = 0
> )
> mm = model.matrix(terms(fm1),newdat)
> newdat$distance = mm %*% fixef(fm1)
> pvar1 <- diag(mm %*% tcrossprod(vcov(fm1),mm))
> tvar1 <- pvar1+VarCorr(fm1)$Subject[1]
> newdat <- data.frame(
>    newdat
>    , plo = newdat$distance-2*sqrt(pvar1)
>    , phi = newdat$distance+2*sqrt(pvar1)
>    , tlo = newdat$distance-2*sqrt(tvar1)
>    , thi = newdat$distance+2*sqrt(tvar1)
> )
> When I get to the "newdat$distance = mm %*% fixef(fm1)" I get the following error:
> "Error in UseMethod("fixef") :
>  no applicable method for 'fixef' applied to an object of class "mer""
>
> I am using lme4 version version 0.999375-42 (the most recent version on Cran) with R version 2.14.0
>
> Any explanation for this would be greatly appreciated.
>
> Chris Eckert
> Department of Biology
> Queen's University
> Kingston, Ontario, Canada

Seeing that makes me nostalgic. I spend my formative years at Queen's.
-------------- next part --------------

R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(lme4)
Loading required package: Matrix
Loading required package: lattice

Attaching package: ?Matrix?

The following object(s) are masked from ?package:base?:

    det


Attaching package: ?lme4?

The following object(s) are masked from ?package:stats?:

    AIC, BIC

> library(ggplot2) # Plotting
Loading required package: reshape
Loading required package: plyr

Attaching package: ?reshape?

The following object(s) are masked from ?package:plyr?:

    rename, round_any

The following object(s) are masked from ?package:Matrix?:

    expand

Loading required package: grid
Loading required package: proto
> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] ggplot2_0.8.9    proto_0.3-9.2    reshape_0.8.4    plyr_1.7.1      
[5] lme4_0.999375-42 Matrix_1.0-3     lattice_0.20-0  

loaded via a namespace (and not attached):
[1] nlme_3.1-103  stats4_2.14.1
> data(Orthodont, package="MEMSS")
> fm1 <- lmer(
+    formula = distance ~ age*Sex + (age|Subject)
+    , data = Orthodont
+ )
> fixef(fm1)
(Intercept)         age     SexMale age:SexMale 
 17.3727273   0.4795455  -1.0321023   0.3048295 
> newdat <- expand.grid(
+    age=c(8,10,12,14)
+    , Sex=c("Male","Female")
+    , distance = 0
+ )
> mm <- model.matrix(terms(fm1),newdat)
> newdat$distance <- mm %*% fixef(fm1)
> pvar1 <- diag(mm %*% tcrossprod(vcov(fm1),mm))
> tvar1 <- pvar1+VarCorr(fm1)$Subject[1]
> newdat <- data.frame(
+    newdat
+    , plo = newdat$distance-2*sqrt(pvar1)
+    , phi = newdat$distance+2*sqrt(pvar1)
+    , tlo = newdat$distance-2*sqrt(tvar1)
+    , thi = newdat$distance+2*sqrt(tvar1)
+ )
> 
> proc.time()
   user  system elapsed 
  4.136   0.800   4.031 


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