3

I am using the effects package in R to derive (and plot) the effects of a complicated linear model.

When I use allEffects(linearModel) I can see the results on screen and save it to a pdf file as usual. Since the model has many terms, the output is not useful as it stands.

Therefore I use effects(someTerm, linearModel) to focus on the terms of interest and the results on screen are what I need. However, when saving it to a pdf file, the file contains no useful output (though it does take up 3.5Kb of space). There is no error message from R at the console.

To ease understanding, I created a simple data set and a script for generating the effects plots, in the same way that I tried with the "real" model.

factorData.csv

A,B,C,y
a1,b1,c1,3
a1,b2,c1,4
a2,b1,c1,5
a2,b2,c1,6
a1,b1,c1,2
a1,b1,c2,3.5
a1,b2,c2,4
a2,b1,c2,5.1
a2,b2,c2,6.2

plotEffect.r

require(effects)

dataFile <- '/tmp/factorData.csv'
effectABfile <- '/tmp/effect_AB.pdf'
effectABCfile <- '/tmp/effect_ABC.pdf'
allEffectFile <- '/tmp/lm_eff.pdf'

df <- read.csv(file=dataFile,header=TRUE,sep=',')
linearModel <- lm(y~A*B*C,data=df)
lm_eff <- allEffects(linearModel)

pdf(file=effectABfile)
plot(effect('A:B',linearModel))
dev.off()

pdf(file=allEffectFile)
plot(lm_eff)
dev.off()

pdf(file=effectABCfile)
plot(Effect(c('A','B','C'),linearModel))
dev.off()

As you can see, I tried allEffects, effect and Effect; allEffects is the only one that works for me. Please note that the script assumes that the data is placed in /tmp/factorData.csv - you might need to change the path of course. I also randomised the order in which the plots are generated, with no effect.

I had a look elsewhere on stackoverflow and saving plots to pdfs fails was the closest but the advice there (to issue dev.off() after each plot to 'close' the pdf file) is something I do already, as seen in plotEffect.r.

I tried this script on 2 machines, each running Lubuntu 14.04.1 64-bit with R version 3.0.2 and the latest effects package installed within R using install.packages. The results were the same.

I would be very grateful for advice on how to fix the problem of saving (instances of) this plot type to a pdf.

Fix/workaround

As suggested by @Roland in the comments below, if you wish to save grid plots (such as the output from the effects plots in this instance) into pdf files, it is better/more reliable to generate the plots separately/manually (rather than in a script). It does not appear to be (as much of) an issue for base graphics or even ggplot2 graphics, where I for one have never encountered this problem/needed this workaround in the past. Thanks to @Roland for suggesting this fix!

@Roland also added that Sys.sleep() might help in scripts. Although it did not do so in my case, I discovered that it was possible to paste several such plotting commands and R would run them as a batch, saving the plots to pdf correctly. Therefore, I believe it should be possible to recover much of the benefits of running the script by taking these steps:

  1. Use R to create the text representation of the pdf(), plot() and dev.off() triad of commands
  2. The main script outputs this plotting command text (specific to each instance of a plot) to a file
  3. Open the plotting command text in a text editor
  4. Copy the entire contents of the commands file and paste it into the R console

Optionally, you may wish to use the command line in Step 3 and 4 - How can I load a file's contents into the clipboard? has useful advice.

This two stage procedure is a reasonable workaround, but arguably there should be no need for it.

Community
  • 1
  • 1
rugplots
  • 187
  • 2
  • 10
  • I cannot reproduce the problem on a win7 or ubuntu system (using RStudio/RStudio Server). – Roland Dec 17 '14 at 10:58
  • There are ways to incorporate your reproducible example in an easy to paste manner. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Dec 17 '14 at 11:29
  • @Roland - thanks for the info about your difficulty recreating the problem. I can borrow a Windows 7 laptop and will try again there. I will report on my findings once I have them. – rugplots Dec 17 '14 at 11:50
  • @RomanLuštrik - I assume your concerns are caused by the filepath strings. Since the whole point of the example is that files are generated, I don't see an easy way of fixing that but I've edited the example above so that the filepaths are stored as variables. Other than that, the dummy example is about as short as I can make it, while still causing the problem *on my machine* :-) – rugplots Dec 17 '14 at 11:57
  • @Roland: Unfortunately, when I tried running it on a Windows 7 64-bit laptop (R version 3.1.2 (not 3.0.2 as on lubuntu), with the current version of `effects` and `Rstudio`) I still get the same problem: individual effect plots look OK on screen but when saved to pdf files, the pdf is corrupted and no plots appear. I should add that other types of plots (e.g., those from the `car` package) do not suffer from this 'saving to pdf' problem on either O/S so, for me at least, the problem seems to be specific to the use of the `effects` package. – rugplots Dec 17 '14 at 12:52
  • Have you tried sourcing the code for each plot seperately? I've sometimes had problems like that when plotting many grid graphics using RStudio. However, as I wrote, your code doesn't produce corrupted files on my systems. – Roland Dec 17 '14 at 12:55
  • @Roland Thanks for your suggestion: it works now! Although I don't know why, it appears that when generating grid/lattice plots, as here, it is **safer** to generate the plots separately/manually rather than in a script if you wish to save them to pdf files. I will update the question above with this finding. Thank you! – rugplots Dec 17 '14 at 14:20
  • When I had a similar problem in one of my scripts, `Sys.sleep` helped me. – Roland Dec 17 '14 at 14:26

0 Answers0