[R] plot residuals per factor

arun smartpink111 at yahoo.com
Tue Jan 8 20:22:17 CET 2013

HI,

Not sure whether ggplot() works with lists.
If you want to plot residuals.vs.fitted for multiple groups, this could help you.  Assuming that you want separate plots for each group:
#You didn't provide any example.

dat1\$d<-factor(dat1\$skin_color)
colnames(dat1)[2:3]<-c("y","x")
models<-dlply(dat1,"d",function(df) mod <- lm(y~x,data=df))
models[[1]]

#Call:
#lm(formula = y ~ x, data = df)

#Coefficients:
#(Intercept)            x
#    51.8357       0.1407

mypath<-file.path("/home/arun/Trial1",paste("catalin_",1:5,".jpg",sep=""))  #change the file.path according to your system
for(i in seq_along(mypath)){
jpeg(file=mypath[i])
par(mfrow=c(2,2))
line<-lm(y~x,data=dat1[dat1\$d==i,])
plot(line,which=1:4)# if you want only residual vs. fitted, change which=1
#abline(0,0)
dev.off()
}

line1<-lm(y~x,data=dat1[dat1\$d==1,])
line1
#
#Call:
#lm(formula = y ~ x, data = dat1[dat1\$d == 1, ])
#
#Coefficients:
#(Intercept)            x
#   51.8357       0.1407

A.K.

----- Original Message -----
From: catalin roibu <catalinroibu at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Tuesday, January 8, 2013 4:22 AM
Subject: [R] plot residuals per factor

Dear R-users,
I want to plot residuals vs fitted for multiple groups with ggplot2.
I try this code, but unsuccessful.
library("plyr")
models<-dlply(dat1,"d",function(df)
mod<-lm(y~x,data=df)

ggplot(models,aes(.fitted,.resid), color=factor(d))+
geom_hline(yintercept=0,col="white",size=2)+
geom_point()+
geom_smooth(se=F)

--
---
Catalin-Constantin ROIBU
Forestry engineer, PhD
Forestry Faculty of Suceava
Str. Universitatii no. 13, Suceava, 720229, Romania
office phone     +4 0230 52 29 78, ext. 531
mobile phone   +4 0745 53 18 01
+4 0766 71 76 58
FAX:                +4 0230 52 16 64
silvic.usv.ro

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help