# [R-sig-ME] lmer predicted values with a subset of random effects

Douglas Bates bates at stat.wisc.edu
Thu Aug 28 01:06:39 CEST 2008

```On Wed, Aug 27, 2008 at 4:08 PM, Daniel Ezra Johnson
<danielezrajohnson at gmail.com> wrote:
> Is there a fairly simple way to manipulate the "mer" slots to get
> predicted/fitted values which include only a subset of the random
> effects BLUPs? For example, the predictions at one or another level if
> the effects were nested (as in nlme), or with one or the other of
> crossed random effects?

The important slots for this are

fixef - vector of fixed-effects parameter estimates
X - model matrix for the fixed-effects
ranef - vector of conditional modes of the random effects
Zt - transpose of the model matrix for the random effects
Gp - "group pointers" for the random effects (0-based) (see below).
flist - named list of grouping factors.  Has an "assign" attribute.
dims - an integer vector of dimensions and characteristics of the model.

Let nt be the number of random-effects terms in the model formula and
let nf be the number of grouping factors.  That is, nf =
length(flist).  Each term is mapped to an element for flist through
the assign attribute of flist.  This is to allow for models with
formulas like

Reaction ~ Days + (1|Subject) + (0+Days|Subject)

to associate the multiple random effects with the grouping factors.  Check

library(lme4)
example(lmer)
str(fm2 at flist)

It should look like

> str(fm2 at flist)
'data.frame':	180 obs. of  1 variable:
\$ Subject: Factor w/ 18 levels "308","309","310",..: 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "assign")= int  1 1

The dims slot contains an element called "nest" indicating if the
grouping factors are nested.  I think it is properly assigned but
since I never use it in the lmer code I can't guarantee that.  Proceed
with caution.

The Gp slot has length nt + 1.  It tells you where the random effects
for each of the random-effects terms start in the ranef slot.  The
indices are 0-based so they are off-by-1 for R indices.

> fm2 at Gp
  0 18 36

If you want to get predictions incorporating random-effects for
particular grouping factors then you should go through the assign
attribute of flist to associate grouping factors with terms.

After that it is just a matter of doing the matrix multiplications.

I just wrote some code to do that, which I attach.
```