An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130108/3998ee54/attachment-0001.pl>
plot residuals per factor
3 messages · catalin roibu, arun
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)
--- 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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi,
I forgot to mention:
levels(dat1$d)
#[1] "1" "2" "3" "4" "5"
Suppose, if I use different levels
library(car)
dat1$d1<-recode(dat1$d,"1='A';2='B';3='C';4='D';5='E'")
?levels(dat1$d1) # check the order of the levels
#[1] "A" "B" "C" "D" "E"
mypath<-file.path("/home/arun/Trial1",paste("catalin_",LETTERS[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[as.numeric(dat1$d1)==i,])
? plot(line,which=1:4)# if you want only residual vs. fitted, change which=1
? #abline(0,0)
? dev.off()
? }
In case you need to change the order of levels
?dat1$d1<-factor(dat1$d1,levels=c("C","D","E","A","B"))
?levels(dat1$d1)
#[1] "C" "D" "E" "A" "B"
mypath<-file.path("/home/arun/Trial1",paste("catalin_",LETTERS[c(3,4,5,1,2)],".jpg",sep=""))
?for(i in seq_along(mypath)){
?jpeg(file=mypath[i])
? par(mfrow=c(2,2))
? line<-lm(y~x,data=dat1[as.numeric(dat1$d1)==i,])
?? plot(line,which=1:4)
?? #abline(0,0)
?? dev.off()
?? }
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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.