[R-sig-ME] How to re-build fitted values from lmer ?
Douglas Bates
bates at stat.wisc.edu
Thu Apr 17 20:35:44 CEST 2008
The fitted values involve both the fixed-effects parameters and the
random effects. In addition to the values that you have indicated you
need the model matrix for the random effects to form the values in
fitted(f1). The transpose of the model matrix is in the Zt slot.
Assuming that you are using the development version of lme4 from
R-forge, so that
> packageDescription("lme4")$Version
[1] "0.999375-13"
you can reproduce the fitted values as
> all.equal(fitted(f1), as.vector(f1 at X %*% fixef(f1) + t(f1 at Zt) %*% f1 at ranef))
[1] TRUE
On 4/17/08, Sandrine-et-Francois <francoisetsandrine.mercier at wanadoo.fr> wrote:
> Dear list,
> This is probably question easy to answer but I'm having some difficulties to find the way out.
> How can I re-build the fitted values from coef() and the model.matrix (obtained by fm at X) ?
>
> Here is a code to support my question:
>
> #====================================================================#
> set.seed(1520)
> ntps<-4
> nsubj<-20
> sd.gp1<-3
> sd.gp0<-2
> sd.res<-1.3
>
> group<-factor(rep(c("Group1", "Group0"), each=ntps, times=nsubj))
> subj<-factor(rep(1:(nsubj*2), each=ntps))
> time<- factor(paste("T", rep(1:ntps, nsubj*2), sep=""))
>
> over.base<-rep(c(5, 4), each=ntps, times=nsubj)
> subj.base<-rep(rnorm(nsubj*2, sd=c(sd.gp1, sd.gp0)), each=ntps)
> time.base<-rep(1:ntps, nsubj*2)*as.numeric(group=="Group1")
>
> Yn<-rnorm(nsubj*ntps*2, mean=over.base+time.base+subj.base, sd=sd.res)
> dfn<-data.frame(time, group, Yn, subj)
>
> require(lattice)
> xyplot(Yn~time|group, group=subj, type="l", data=dfn)
>
> f1<-lmer(Yn~group+time+(group|subj), data=dfn)
> summary(f1)
>
> ### Individual fitted values: 160 x 1 (40 subj x 4 times)
> fitted(f1)
>
> ### Random effect coefficients (per subj): 40 x 2 (intercept and slope)
> ranef(f1)
>
> ### Fixed effect coefficients: 5 x 1 (int, gp1, T2, T3, T4)
> fixef(f1)
>
> ### Individual coefficients: 40 x 5, ie. 40 subj x (int, gp1, T2, T3, T4)
> coef(f1)
>
> ### Model matrix: 160 x 5 (indicators)
> f1 at X
>
> #====================================================================#
>
> Best regards,
> François
>
> [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>
>
More information about the R-sig-mixed-models
mailing list