[R] xyplot: key inside the plot region / lme: confidence bands for predicted

Peter Ehlers ehlers at ucalgary.ca
Fri Jul 2 19:00:54 CEST 2010


On 2010-07-02 9:37, Michael Friendly wrote:
> I have two questions related to plotting predicted values for a linear
> mixed model using xyplot:
>
> 1: With a groups= argument, I can't seem to get the key to appear inside
> the xyplot. (I have the Lattice book,
> but don't find an example that actually does this.)
> 2: With lme(), how can I generate confidence bands or prediction
> intervals around the fitted values? Once
> I get them, I'd like to add them to the plot.
> Example:
>
> library(nlme)
> library(lattice)
> Ortho <- Orthodont
> Ortho$year <- Ortho$year - 8 # make intercept = initial status
>
> Ortho.mix1 <- lme(distance ~ year * Sex, data=Ortho,
> random = ~ 1 + year | Subject, method="ML",
> # correlation = corAR1 (form = ~ 1 | Subject)
> )
> Ortho.mix1
>
> # predicted values
> grid <- expand.grid(year=0:6, Sex=c("Male", "Female"))
> grid$age <- grid$year+8 # plot vs. age
> fm.mix1 <-cbind(grid, distance = predict(Ortho.mix1, newdata = grid,
> level=0))
>
> xyplot(distance ~ age, data=fm.mix1, groups=Sex, type="b", pch=c(15,16),
> cex=1.2,
> auto.key=list(text=c("Male", "Female"), points = TRUE, x=8, y=26),
> main="Linear mixed model: predicted growth")
>
> Can someone help?
>
> -Michael


Michael,

the x,y location should be specified in the unit square
(and you might want to set the 'corner' component).

  Ortho$year <- Ortho$age - 8 ##fix typo
  xyplot(distance ~ age, data=fm.mix1, groups=Sex,
         type="b", pch=c(15,16), cex=1.2,
         auto.key=list(text=c("Male", "Female"),
                       points = TRUE,
                       x=0.1, y=0.8, corner=c(0,1)))

See description of 'key' in ?xyplot.

   -Peter Ehlers



More information about the R-help mailing list