Michael Friendly
2010-Jul-02 15:37 UTC
[R] xyplot: key inside the plot region / lme: confidence bands for predicted
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 Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA
Peter Ehlers
2010-Jul-02 17:00 UTC
[R] xyplot: key inside the plot region / lme: confidence bands for predicted
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? > > -MichaelMichael, 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