If you change your model fitting command to the following, your matching
approach works:
my.lmer <- glmer(y ~ x1 + (1 | subject), data = df, family = binomial, nAGQ = 0)
The key change is the nAGQ = 0
, which matches your approach, whereas the
default (nAGQ = 1
) does not. nAGQ
means 'number of adaptive
Gauss-Hermite quadrature points', and sets how glmer
will integrate out
the random effects when fitting the mixed model. When nAGQ
is greater
than 1, then adaptive quadrature is used with nAGQ
points. When nAGQ =
1
, the Laplace approximation is used, and when nAGQ = 0
, the integral is
'ignored'. Without being too specific (and therefore perhaps too
technical), nAGQ = 0
means that the random effects only influence the
estimates of the fixed effects through their estimated conditional modes
-- therefore, nAGQ = 0
does not completely account for the randomness of
the random effects. To fully account for the random effects, they need
to be integrated out. However, as you discovered this difference
between nAGQ = 0
and nAGQ = 1
can often be fairly small.
Your matching approach will not work with nAGQ > 0
. This is because in
these cases there are three steps to the optimization: (1) penalized
iteratively reweighted least squares (PIRLS) to estimate the conditional
modes of the random effects, (2) (approximately) integrate out the
random effects about their conditional modes, and (3) nonlinear
optimization of the objective function (i.e. the result of the
integration). These steps are themselves iterated until convergence.
You are simply doing an iteratively reweighted least squares (IRLS) run,
which assumes b
is known and putting Z%*%b
in an offset term. Your
approach turns out to be equivalent to PIRLS, but this equivalence only
holds because you use glmer
to get estimated conditional modes (which
you wouldn't otherwise know).
Apologies if this isn't well explained, but it isn't a topic that lends
itself well to a quick description. You might find
https://github.com/lme4/lme4pureR useful, which is an (incomplete)
implementation of the lme4
approach in pure R code. lme4pureR
is
designed to be more readable than lme4
itself (although much slower).
As I said in my comment, I expect the problem to be one of additional random effects parameters in model
compared to in modelT0
+ modelT1
(specifically corraletions).
Hence I would first check for the number of random effects parameters. Is attr(logLik(model), "df") - length(fixef(model))
equal to 2 * attr(logLik(modelT0), "df") - length(fixef(modelT0))
?
If not, the additional random effects parameters explain error variance which helps to more precisely estimate the fixed effects.
Best Answer
From lme4 documentation you can learn that
and
ranef
is