Dear R-users,
Sorry if someone came out with a similar question but after one day of
searching I am giving up:
Does anyone know how to plot the original points used in a lm model and the
set of resulting regression lines generated by the model?
This is how I do it using the "plot" and "lines" functions but I would like
to do it using lattice and I cannot find a way to plot in the same panel
points and lines that come from different datasets.
Note: I would like to use this sort of "predict" approach rather than using
the regression coefficients of the model as in complex models I get confused
when I have to combine the coefficients to build up each regression line.
Many thanks in advance and sorry for bothering you.
Javier
fm<-lm(c4~SEX+AREA+c3+SEX:AREA+SEX:c3+AREA:c3,data=mydata)
mygrid<-expand.grid(SEX=c("male","female"),AREA=c("area1","area2","area3"),c3=seq(-2.5,2.5,length.out=20))
pred_fm<-predict(fm,newdata=mygrid)
plot(mydata$c3, mydata$c4, col = ifelse(mydata$SEX== "male", "blue",
"red"), pch = ifelse(mydata$SEX=="male",2,1),xlab="c3",ylab="c4")
lines(seq(-2.5, 2.5, length.out = 20), pred_fm[mygrid$SEX == "female" &
mygrid$AREA == "area3"] , col = "red", lwd = 2, lty = 3)
lines(seq(-2.5, 2.5, length.out = 20), pred_fm[mygrid$SEX == "female" &
mygrid$AREA == "area1"] , col = "red", lwd = 2, lty = 1)
lines(seq(-2.5, 2.5, length.out = 20), pred_fm[mygrid$SEX == "female" &
mygrid$AREA == "area2"] , col = "red", lwd = 2, lty = 2)
lines(seq(-2.5, 2.5, length.out = 20), pred_fm[mygrid$SEX == "male" &
mygrid$AREA == "area3"] , col = "blue", lwd = 2, lty = 3)
lines(seq(-2.5, 2.5, length.out = 20), pred_fm[mygrid$SEX == "male" &
mygrid$AREA == "area1"] , col = "blue", lwd = 2, lty = 1)
lines(seq(-2.5, 2.5, length.out = 20), pred_fm[mygrid$SEX == "male" &
mygrid$AREA == "area2"] , col = "blue", lwd = 2, lty = 2)
On 12/17/08, Javier PB <j.perez-barberia at macaulay.ac.uk> wrote:
Dear R-users,
Sorry if someone came out with a similar question but after one day of
searching I am giving up:
Does anyone know how to plot the original points used in a lm model and the
set of resulting regression lines generated by the model?
This is how I do it using the "plot" and "lines" functions but I would like
to do it using lattice and I cannot find a way to plot in the same panel
points and lines that come from different datasets.
Note: I would like to use this sort of "predict" approach rather than using
the regression coefficients of the model as in complex models I get confused
when I have to combine the coefficients to build up each regression line.