1

G'day Everyone,

When the 'lmer' function in 'lme4' runs its produces an S4 object with a lot of slots. I am interested in one of these slots, namely model@X, and how this 'X' slot output is produced. I want to try reproduce this output for a different model function (glmmPQL) I am using which does not automatically produces this 'X' output (FYI 'lmer' produces an object of class 'mer', and slot 'X' is a model matrix for the fixed effects). Code below shows what I am talking about.

What I want to figure out is how the produced this 'X' data? I looked at the code for 'lmer' by writing it in the terminal without '()' but I couldn't find anything there. I also tried showMethod('lmer') but it says function 'lmer': .

Just wondering if there is a way to get the source code for what the 'X' slot is doing in particular (or any slot in a S4 object)? Or does anyone know how to reproduce this? Thanks lots for your help and time.

    library(lme4)
    # here is a quick example of what I am looking at using the cake dataset in the 'lme4' package
    m <- lmer(cakeglmm<- lmer(angle ~ temp + recipe + (1| replicate), family = gaussian, data = cake)

    slotNames(m)
    head(m@X)
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Adam
  • 1,147
  • 3
  • 15
  • 23

1 Answers1

1

You started off okay by printing lmer. That won't show you where m@X is set, but you can see which methods are called by lmer.

The methods within lmer can be accessed using lme4:::methodName.

If you look inside lme4:::lmer_finalize, you'll see (paraphrasing):

ans <- new(Class = "mer", ..., X = fr$X, ...)

So that's where the @X slot is being populated. Back up in lmer you'll see that fr comes from lme4:::lmerFrames, and specifically fr$X is calculated by:

X <- if (!is.empty.model(mt)) 
    model.matrix(mt, mf, contrasts)
else matrix(, NROW(Y), 0)
pete
  • 2,327
  • 2
  • 15
  • 23