1

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.

Dan
  • 9,391
  • 5
  • 41
  • 73
user3757738
  • 13
  • 1
  • 3
  • 1
    Try doing `print(plot(temp.model))` inside the loop. Not sure how that plot function works, but if it uses grid graphics, that should help. – MrFlick Jun 19 '14 at 19:53
  • it does use lattice (grid) functions. This is [R FAQ 7.22](http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f) – Ben Bolker Jun 19 '14 at 19:56

1 Answers1

3

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.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110