[R] lme4 package: Fitted values and residuals
Douglas Bates
bates at stat.wisc.edu
Thu Nov 16 20:59:34 CET 2006
On 11/16/06, Frank Johannes <fjohannes at fastmail.fm> wrote:
> Dear all,
> I have three concerns:
> 1)
> I am running models with the lme4 package. I cannot find a way to pull
> out a vector of the fitted values and the residuals. Does anybody know
> how to do it?
The fitted() and resid() extractor functions are the usual way of
doing this for a fitted model in R and, astonishingly enough, they
work! Try
library(lme4)
example(lmer)
fitted(fm1)
resid(fm1)
> 2)
> How can I nest a random effect variable into a "two-level" fixed effect
> variable?
I'm not quite sure what you mean. Are you talking about a situation
like having a random effect for "Patient" and a fixed effect for
"Treatment" with Patient nested within Treatment. If the Patients are
designated in a sensible way, so that each Patient has a distinct
identifier (i.e. the number of levels of the Patient factor
corresponds to the number of Patients) then you can ignore the nesting
and specify the model as
lmer(response ~ Treatment + (1|Patient), ...)
Sometimes the Patients are designated with implicit nesting so that
the treatment group has a patient labeled "1" and the control group
has another patient also labeled as "1". In that case you need to
specify the grouping factor for the random effects as the
Treatment:Patient interaction. That is,
lmer(response ~ Treatment + (1|Treatment:Patient), ...)
The second version also works properly if the Patients had been
designated in a sensible way so, if you want to be safe, use the
second version.
> 3)
> Suppose I have the following model:
> y = a + b|c + d + error,
> where 'a' is a fixed effect, 'c' is a random effect nested with the
> random effect 'b', and 'd' is a non-nested random effect.
That's not the greatest notation to use for the R-Help list because it
doesn't correspond to the way that R formulas are parsed.
> Suppose I obtain parameter estimates for all of the predictor variables.
> Now, I would like to calculate a fitted value, 'yhat', from only the
> parameter estimates for 'a' and 'b|c'. Can this be done in lme4. That
> is; can I pull out all of the requisite elements from the output to
> calculate 'yhat'?
The answer to the question of "Can this be done in lme4?" is
install.packages("fortunes"); library(fortunes); fortune("Yoda")
I could describe how to do it if I had a better idea of what the
calculation you had in mind was. I am having difficulty grasping how
you obtain a prediction from a model involving a factor 'd' without
assigning a value to that factor. I suppose that someone who
understands the deep magic of quantities like SAS's LS-means may be
able to tell you what you want to know but I can't.
I can say that you can obtain the coefficient estimates for the fixed
effects with
fixef(fm1)
and a list of the conditional modes of the random effects (also called
the BLUPs) as
ranef(fm1)
After that you are on your own.
More information about the R-help
mailing list