I am trying to create a loop that graphs diagnostic plots of multiple mixed models so that I can easily see if the assumptions of the model are met. I am running models on 7 measurements of 5 species so creating the plots for all 35 models individually would really suck.
My code is:
sink("C:\\Users\\*My Name*\\Desktop\\Morph Data\\JAWE\\LMM2\\JAWEj LMM Output.txt")
Names<-names(int)
result.sum <- vector("list",16)
names(result.sum)<-Names
result.aov <- vector("list",16)
names(result.aov)<-Names
for(i in 9:15){
blah<-data.frame(X=int$Year,Y=int[,i],Z=int$Observer)
blah<-blah[complete.cases(blah),]
mod1<-lme(Y~X,random=~1|Z,method="ML",data=blah)
result.sum[[i]]<-summary(mod1)
result.aov[[i]]<-anova(mod1)
mypath=file.path("C:","Users","*My Name*","Desktop","Morph Data","JAWE","LMM2", paste(Names[i],"_qq",".jpg",sep=""))
jpeg(file=mypath)
p<-qqnorm(mod1,main=Names[i])
dev.off()
mypath=file.path("C:","Users","*My Name*","Desktop","Morph Data","JAWE","LMM2", paste(Names[i],"_res-hist",".jpg",sep=""))
jpeg(file=mypath)
hist(mod1$residuals[,1],freq=FALSE,main="Wing")
curve(dnorm(x,mean=mean(mod1$residuals[,1]),sd=sd(mod1$residuals[,1]),log=FALSE), col=2, lwd=4, lty=4, add=TRUE)
dev.off()
mypath=file.path("C:","Users","*My Name*","Desktop","Morph Data","JAWE","LMM2", paste(Names[i],"_res",".jpg",sep=""))
jpeg(file=mypath)
plot(blah$X,mod1$residuals[,1],main="Wing")
dev.off()
}
result.sum
result.aov
The object int is my data for this particular species, JAWE. With this code I can successfully save the output from the models and ANOVAs, and 2 of the 3 graphs per model.
The problem I am running into is that the saved Q-Q plots are blank plot space. Any clues on how to fix this would be most appreciated.
Thanks!