# [R] NLME Fitted Values

Harold Doran hdoran at nasdc.org
Wed Jul 9 04:14:47 CEST 2003

```I would like to be able to add the random effect for the slope to the fitted value for the slope at each level (i.e, for each school and for each student).

The fitted values at the population level should be the fixed effects (B_00 and B_01). So each school should have the same fitted values, but a unique random effect. Adding the two together results in the mean adjusted growth rate for each school. But when I use fitted() and set level=0, I get results that are not the fixed effects. I am unsure what the fitted values are that are produced.

At the child level, each child has a unique random effect, but each child within the same school should share the same fitted value. But the fitted values for children at different schools should be different. Adding these should result in the mean adjusted growth rate for each child. Although I get unique random effects for each child using ranef(), I do not get the fitted values that I think I should get wth fitted().

However, when I use

level.1<-data.frame(coef(eg.model1), level=1) this gives me what HLM calls the fitted values for each child. So, I think I can add these to the random effects at the same level to get the mean growth rate for each child.

So, I am a little unclear why fitted() is not producing results that are similar to HLM fitted values, but coef() produces the values that HLM calls the level 2 fitted values.

So, the R and HLM random effects are convergent at all levels. The HLM fitted values at the population level are the fixed effects, but this is not the case when I use fitted () at level =0 in R.

However, the fitted values at the observation level in R are the same fitted values at the observation level in HLM.

In sum, HLM and R random effects are exactly the same at all levels. HLM and R fitted values are exactly the same at the level of observation. But, the fitted vaues at other levels are very different.

Am I using fitted incorrectly and should instead be using coef() to accomplish my goal?

-----Original Message-----
From: Douglas Bates [mailto:bates at stat.wisc.edu]
Sent: Tue 7/8/2003 5:52 PM
To: Harold Doran
Cc: R-help at stat.math.ethz.ch
Subject: Re: [R] NLME Fitted Values

"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

```