[R-sig-ME] how to extract all information from fitted object in a lme model

Douglas Bates bates at stat.wisc.edu
Wed Jun 3 17:18:10 CEST 2009


On Wed, Jun 3, 2009 at 9:48 AM, Arild Husby <arild.husby at ed.ac.uk> wrote:
> Dear list,
>
> I would like your help on how to extract the fitted values from a lme
> object.
>
> For example, the following code will return the fitted values from a lme
> object in the Orthodont dataset (n=108, one for each age/subject
> combination):
>
> library(nlme)
> fm1 <- lme(distance ~ age, data = Orthodont)
> fitted(fm1)
>
> However, I do not manage to extract each observations fitted value together
> with its subject id (I can easily extract the values themselves, using
> fittted(fm1)[[1]], but not the subject level associated with the values!).
> Is there some way to get these into, say, a dataframe with dimensions 108,2
> looking like below:
>
> Subject   fitted_values
> M01         24.81...
> M01         26.57..
> .
> .
> M02        etc.
>
> I would like to extract these values and then match them to the subject in
> the dataset using something like:

I think I would use

cbind(getData(fm1), .fitted = fitted(fm1))

to do that.  I think that will handle cases of, say, a missing value
in the original data frame correctly.

> Orthodont$fitvalues <- match(Orthodont$Subject,matrix$fitted_values)
>
> Probably need to match on both age and Subject code somehow, but age is not
> in the fitted output, but presumably in the right order as in dataset so
> might be possible to do this??
>
> If this is a trivial question, or I have misunderstood some concepts, please
> feel free to point me to relevant literature.
>
> Many thanks for your help,
> Arild
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
> _______________________________________________
> 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