# [R] NLME Fitted Values

Douglas Bates bates at stat.wisc.edu
Tue Jul 8 23:52:52 CEST 2003

```"Harold Doran" <hdoran at nasdc.org> writes:

> Dear List:
>
> I am having difficulties with the fitted values at different levels
> of a multilevel model. My data set is a series of student test
> scores over time with a total of 7,280 observations, 1,720 students
> nested witin 60 schools. The data set is not balanced.
>
> The model was fit using
>
> eg.model.1<-lme(math~year, random=~year|schoolid/childid, data=single).
>
> When I call the random effects at all levels using
>
> EB.1<-data.frame(ranef(eg.model1, level=1)) and
> EB.2<-data.frame(ranef(eg.model1, level=2)), I get the shrinkage
> estimators that I expect. That is, I get 2 random effects for each
> child (1 intercept and 1 slope) and 2 for each school (1 intercept
> and 1 slope).
>
> However, when I call the fitted values using:
>
> fitted<-data.frame(fitted(eg.model1, level=0:2)), I get 7,280 fitted
> values at the level of observation. This makes sense (one for each
> observed score). However, I also get 7,280 fitted values at the
> child and at the school level. This does not seem correct to me.
>
> I should only have, I think, 60 fitted values at the school level
> (actually, 1 intercept and 1 slope for each of 60 schools) and 1,720
> fitted values at the child level (again, 1 intercept and one for the
> slope for each child).

I think you are confusing the random effects with the fitted values.
The fitted values will depend on the fixed-effects and, when level >
0, on the random effects.  Because the year term, which is associated
with the fixed effects, can change within school and within child we
always return one fitted value for each observation.

> Why am I always getting 7,280 fitted values?

There is some redundancy but we cannot determine the redundancy
without knowing the exact form of both the fixed effects and the
random effects.  In your case where year is the only term in the fixed
effects when level = 0 the fitted values for the same year should be
the same.  When level = 1 the fitted values for the same year and same
school but different children should be the same.  When level = 2
potentially all the fitted values will be different.

However, it could be that a fixed effects term would have a unique
value at each row and then even the level = 0 fitted values could all
be distinct.

> I have tried
> fitted.1<-data.frame(fitted(eg.model1, level=1)) and fitted.2<-data.frame(fitted(eg.model1, level=2)), but this does not appear to be working either.

But it appears that what you want is what you already got from the
ranef extractor.  Is there some reason that you don't want to use
those values?

Regards,
Doug Bates

```