Skip to content
Prev 79146 / 398502 Next

Help with lattice, regressions and respective lines

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:

            
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"