[R] Help with lattice, regressions and respective lines

Martin Henry H. Stevens HStevens at MUOhio.edu
Mon Oct 17 12:26:13 CEST 2005


Hi Jose,
I am just beginning to plumb the depths of lattice, but perhaps my  
recent experience can help.
I recently figured out (with encouragement from the list) how to plot  
predicted values from a model into the appropriate panel. I am  
certain that what I have done can be done better, but the following  
appears to work for me.

Imagine the model
mod <- lm(Y ~ A + B)
where A is continuous and B is a factor.

I created a new function to use in the panel

panel.modfinal <- function(mod, x, y, subscripts, groups)
     xfit <- seq(min(x), max(x), length=21)
     b <- factor( rep(levels(mod$data$B), rep(21,4)) )
     yfit <- predict(mod, newdata=data.frame(A=rep(xfit,4), B=b) ) 
[b==unique(groups[subscripts] ) ]
     llines(xfit,yfit,lty=1) }

I then plot the data and the fitted lines with constant slope and  
unique intercepts:

xyplot(Y ~ A | B, groups=B,
             panel=function(x,y, subscripts,groups){
             panel.xyplot(x,y)
             panel.modfinal(mod,x,y,subscripts,groups) } ).

"groups" seems to identify a variable that you want pass to a panel  
or legend/key function, and subscripts seems to identify the rows  
used in each panel.

I hope the above is correct and doesn't thereby mislead you, but it  
seems to work for me.

Hank Stevens

On Oct 14, 2005, at 7:34 PM, Jose Claudio Faria wrote:

> # Dear R list,
> #
> # I'm needing help with lattice, regression and respective lines.
> # My data is below:
>
> bra  = gl(2, 24, label = c('c', 's'))
> em   = rep(gl(3, 8,  label = c('po', 'pov', 'ce')), 2)
> tem  = rep(c(0, 0, 30, 30, 60, 60, 90, 90), 6)
> tem2 = tem^2
> r    = rep(1:2, 24)
> y    = c(40.58, 44.85, 32.55, 35.68, 64.86, 51.95, 42.52, 52.21,
>           40.58, 44.85, 33.46, 46.09, 12.75, 18.01, 16.82, 13.69,
>           40.58, 44.85, 34.45, 29.89, 34.91, 28.10, 27.52, 22.24,
>           48.68, 47.25, 45.58, 45.33, 41.03, 51.20, 45.85, 54.45,
>           48.68, 47.25, 19.88, 19.67, 16.20, 13.49, 13.75, 18.80,
>           48.68, 47.25, 42.19, 39.91, 34.69, 34.11, 32.74, 34.24)
>
> Df = data.frame(bra, em, tem, tem2, r, y)
>
> # Regressions
> attach(Df)
>    Dfs1=subset(Df, (bra=='s' & em=='pov'), select=c(bra, em, tem,  
> tem2, r, y))
>    Dfs1
>    rlin1=lm(y ~ tem + tem2, data=Dfs1)
>    summary(rlin1)
>
>    Dfs2=subset(Df, (bra=='s' & em=='po'), select=c(bra, em, tem, r,  
> y))
>    Dfs2
>    rlin2=lm(y ~ tem, data=Dfs2)
>    summary(rlin2)
>
>    Dfs3=subset(Df, (bra=='s' & em=='ce'), select=c(bra, em, tem,  
> tem2, r, y))
>    Dfs3
>    rlin3=lm(y ~ tem + tem2, data=Dfs3)
>    summary(rlin3)
> detach(Df)
>
> # I would like to plot with lattice 'y ~ tem | em',
> # with the panels ('po', 'pov' and 'ce'),
> # and the its respective regressions lines:
> # a) linear for panel 'po' or better, without line;
> # b) quadratic for 'pov' and 'ce'
>
> # Is it possible? Could somebody hel me?
>
> # I'm trying:
> library(lattice)
> attach(Df)
>    Dfs=subset(Df, bra=='s', select=c(bra, em, tem, y))
>    Dfs
>    xyplot(y ~ tem | em,
>           data = Dfs, ylim=c(10, 60), xlim=c(-10, 110),
>           ylab='y', xlab='Time, days',
>           layout = c(3,1))
> detach(Df)
>
> TIA,
> -- 
> Jose Claudio Faria
> Brasil/Bahia/UESC/DCET
> Estatistica Experimental/Prof. Adjunto
> mails:
>   joseclaudio.faria at terra.com.br
>   jc_faria at uesc.br
>   jc_faria at uol.com.br
> tel: 73-3634.2779
>
> ______________________________________________
> 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
>

Dr. Martin Henry H. Stevens, Assistant Professor
338 Pearson Hall
Botany Department
Miami University
Oxford, OH 45056

Office: (513) 529-4206
Lab: (513) 529-4262
FAX: (513) 529-4243
http://www.cas.muohio.edu/~stevenmh/
http://www.muohio.edu/ecology/
http://www.muohio.edu/botany/
"E Pluribus Unum"




More information about the R-help mailing list