Skip to content
Prev 314866 / 398506 Next

plot residuals per factor

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<-read.csv("skin_color.csv",sep="\t") #You can replace this with your dataset
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)