Skip to content
Prev 117 / 20628 Next

augPred in lmer

Thanks for sending the example, Kyle.
On 3/19/07, Roberts, J. Kyle <jkrobert at bcm.tmc.edu> wrote:
One possible way to get a similar plot is to use the with()
construction on the fitted model.  I enclose some code to plot the
per-subject fits along with the mixed-model fits for the sleepstudy
data.

The technique being used is to extract the per-group coefficients and
within the panel function determine which group is being plotted and
hence what coefficients should be used in the plot.

This could definitely be done more cleanly.  The problem I have when
trying to design functions like this is to be able to define the
method for cases with non-nested random effects.
-------------- next part --------------
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
with(fm1, {
    cc <- coef(.)$Subject
    xyplot(Reaction ~ Days | Subject, 
                 index.cond = function(x, y) coef(lm(y ~ x))[1],
                 panel = function(x, y, groups, subscripts, ...) {
                     panel.grid(h = -1, v = -1)
                     panel.points(x, y, ...)
                     subj <- as.character(Subject[subscripts][1])
                     panel.abline(cc[subj,1], cc[subj, 2])
                     panel.lmline(x, y, ...)
                 })
})