How can I plot multiple residuals plots in a loop?

自古美人都是妖i 提交于 2019-12-18 09:28:42

问题


In the following example, I want to write the residuals plot of each model in a file. I do not need to see them in my display.

for (i in 1:500){
     temp.model<-lme(as.formula(paste("Var",i) ~ X1*X2, sep=""), data = example, random=~1| Exp/Person)
     jpeg(paste("C:/Myfolder", i, ".jpg", sep = ""), quality=50, bg="white")
     plot(temp.model)
     dev.off ()
     graphics.off()
}

When I run this code without loop, I obtain what I want. However, it creates blank files within the loop.

Any ideas?

Thank you.


回答1:


The answer is in the FAQ, FAQ 7.22 in fact. However this is not obvious until you realize that the plot.lme function from the nlme package uses lattice/trellis graphics to do the actual plotting (there are references on the help page for plot.lme, but not obvious).

The short form of the solution (but I still recommend reading the FAQ and the other documentation to fully understand the issue) is to wrap the plot in a print command.



来源:https://stackoverflow.com/questions/24314858/how-can-i-plot-multiple-residuals-plots-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!