[R] How to create grouping in the residual plot

Dieter Menne dieter.menne at menne-biomed.de
Mon Feb 9 20:30:01 CET 2009


Ram Pandit <pandit.ram <at> gmail.com> writes:

> I am working in a country level data. After running the regression, I would
> like to plot the residuals of each observation based on the group created
> for a particular variable.
> 
> For example, one of my independent variable is "Income", I would like to
> plot the residual based on income categories (<5000, 5001-10,000,
> 10001-15,000 etc) with "different color" for each income group.
> 
> Any hints or pointers will be highly appreciated.

(Any sample data set would also be appreciated)


library(nlme) # only required for data set
library(lattice)
data(Orthodont)
lm.or = lm(distance~age+Sex,data=Orthodont)
Orthodont$res = residuals(lm.or)
# in one plot
xyplot(res~age,groups=Sex,data=Orthodont)
# in two panels, with a bit of additional luxury
xyplot(res~age|Sex,data=Orthodont,aspect=1,
      panel = function(x, y) {
           panel.grid(h=-1, v= -2)
           panel.xyplot(x, y,pch=16)
           panel.loess(x,y, span=1)
       })




More information about the R-help mailing list