[R] plotting multilevel / lme lines

Chuck Cleland ccleland at optonline.net
Fri Apr 6 14:24:26 CEST 2007


Rense Nieuwenhuis wrote:
> Dear expeRts,
> 
> I am trying to plot a lme-object {package nlme) in such a way, that  
> on a selected level the x-axis represents the value on a selected  
> predictor and the y-axis represents the predicted-outcome variable.  
> The graphs would than consist of several lines that each represent  
> one group. I can't find such a plotting function.
> 
> I could write such a function myself, based on ranef() and fixef(),  
> but it would be a waste of time if such a function would already exist.
> 
> Does any of you such a function?

  I don't know of a single function with an lme object as argument, but
for what I think you have in mind, here is how you might go about it:

library(nlme)

fm2 <- lme(distance ~ poly(age, 2) * Sex,
                      data = Orthodont, random = ~ 1)

newdat <- expand.grid(age = 8:14, Sex = c("Male","Female"))

newdat$PREDDIST <- predict(fm2, newdat, level = 0)

library(lattice)

xyplot(PREDDIST ~ age, groups=Sex, ylab="Model Predicted Distance",
           data = newdat, xlab="Age",
           panel = function(x, y, ...){
                   panel.grid(h=6,v=6)
                   panel.superpose(x, y, type="l", ...)},
                   main="Orthodont Growth Model",
                   key = simpleKey(levels(newdat$Sex),
                                   lines=TRUE, points=FALSE)
                   )

> Regards,
> 
> Rense Nieuwenhuis
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list