Skip to content

Help with lattice, regressions and respective lines

4 messages · Jose Claudio Faria, PIKAL Petr, Martin Henry H. Stevens

#
# 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,
2 days later
#
Hi

You are looking for functions

panel.*
especially panel.lmline, but I wondered if you can use linear for one 
panel and quadratic for other panels. You could use a structure 
provided in examples in xyplot help page to try to achieve what 
you want. I do not have instant solution to your problem, maybe 
somebody does :-)

HTH
Petr
On 14 Oct 2005 at 20:34, Jose Claudio Faria wrote:
Date sent:      	Fri, 14 Oct 2005 20:34:13 -0300
From:           	Jose Claudio Faria <joseclaudio.faria at terra.com.br>
Organization:   	UESC
To:             	"r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
Subject:        	[R] Help with lattice, regressions and respective lines
Send reply to:  	joseclaudio.faria at terra.com.br
	<mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe>
	<mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>
Petr Pikal
petr.pikal at precheza.cz
#
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"
#
Hi Jose,
I need to make a small correction in my code -
mod$data$B works for glm objects, but not lm or aov objects. For  
those use, mod$model$B.
On Oct 17, 2005, at 6:26 AM, Martin Henry H. Stevens 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"