Skip to content

Output results to a single postscript document

8 messages · Pele, Dieter Menne, Mark Difford +1 more

#
Hello R users,

I have been trying to output all my results (text, plots, etc) into the same
postscript file as 

one document, but have been unable to...Can anyone help me improve my code
below so that I can 

accomplish this?  Currently I have to output them separately then piece them
back together into 

one document..

Thanks in Advance for any help!


options (scipen=999, digits=7)

library(foreign)
library(Hmisc)
library(prettyR)
library(DAAG)
library(lmtest)
library(car)
library(MASS)
library(nlme)
library(dyn)
library(wle)
library(lasso2)
library(zoo)

tdata <- ts(read.table("C:/tmp/data.csv" ,sep = ",",header=TRUE))
 print(tdata)

   res 	<- function(dep, indep, fn, env = parent.frame()) {
   depnm   	<- deparse(substitute(dep))
   indepnm 	<- deparse(substitute(indep))
        fo 	<- sprintf("%s ~ lag(%s, -1) + %s", depnm, depnm, indepnm)
        fo 	<- as.formula(fo, env = env)
        mod <- do.call(dyn$lm, list(fo))
        summ<- summary(mod)

	  chart <- function(mod) {
	  postscript(file="fig.ps")
   	  par(mfrow=c(2,1)) 
   	  acf(residuals(mod)) 
   	  pacf(residuals(mod)) 
   	  dev.off()                }
	  				                                  	
	  output  <- return(list(Summary=summ, Chart=chart(mod))) }
						
	  results <- res(tdata[ ,"Y"], tdata[ ,"X_VARY"]) ; results
#
Pele <drdionc <at> yahoo.com> writes:
...
... 

Your example is not reproducible because data.csv is not available. 
And better truncate the list of libraries to the absolute minimum,
which is void as I guess. Sometime, problems only turn up because of
interactions between libraries.

I do not know of a solution with ps, but there is one for pdf,
and several alternatives for HTML. See Greg Snow's summary at

http://markmail.org/message/yi4w2xmleb6hwcph

Dieter
#
Hi Pele,
Surely I missed something? Is it not simply that you are turning off the
postscript device in the wrong place? At the moment you only seem to be
using the postscript device in the plotting/chart function.

 chart <- function(mod) {
	  postscript(file="fig.ps")
   	  par(mfrow=c(2,1)) 
   	  acf(residuals(mod)) 
   	  pacf(residuals(mod)) 
   	  dev.off()                }    ## don't turn it off here

Only turn it off when you have finished printing/plotting everything that
you want in it. You really do need to be more specific about what you have
in mind.

Regards, Mark.
Pele wrote:

  
    
#
Hello R users,

Below is the code and output of what I am trying to do.  My goal is to
insert/print all items in the chart function into a pdf document. Only the
acf and pacf charts gets printed.

Again, thanks in advance for any help I can get!

####################################
options (scipen=999, digits=7)

library(lmtest)
library(dyn)

tdata <- ts(read.table("C:/test/data.csv" ,sep = ",",header=TRUE))
print(tdata)

   res 	<- function(dep, indep, fn, env = parent.frame()) {
   depnm   	<- deparse(substitute(dep))
   indepnm 	<- deparse(substitute(indep))
        fo 	<- sprintf("%s ~ lag(%s, -1) + %s", depnm, depnm, indepnm)
        fo 	<- as.formula(fo, env = env)
        mod <- do.call(dyn$lm, list(fo))
        
	  chart <- function(mod) {
	  pdf(file="results.pdf")
   	  par(mfrow=c(2,1)) 
   	  acf(residuals(mod)) 
   	  pacf(residuals(mod)) 
          summ<- summary(mod)
	  aic	<- AIC(mod)
    	  dev.off()                }
	  				                                  	
	  output  <- return(list(Charts=chart(mod))) }
	  results <- res(tdata[ ,"Y"], tdata[ ,"X_VARY"]) ; results

############output###############
Time Series:
Start = 1 
End = 20 
Frequency = 1 
   Unit  Y X_VARY  K D_WAY
 1    1  3      2  4    50
 2    2  5      3  9    50
 3    3  6     11 22    50
 4    4  8      4  7     2
 5    5  9     11 11     2
 6    6 12     13 13     2
 7    7 23     25 12     2
 8    8 22     30 31     3
 9    9 23      3  3     3
10   10 19     21 21    32
11   11  3      2  4    34
12   12  5      3  9     4
13   13  6     11 22     4
14   14  8      4  7     4
15   15  9     11 11     4
16   16 12     13 13     4
17   17 23     25 12     4
18   18 22     30 31     4
19   19 23      3  3     4
20   20 19     21 21     5
+    depnm   <- deparse(substitute(dep))
+    indepnm <- deparse(substitute(indep))
+         fo <- sprintf("%s ~ lag(%s, -1) + %s", depnm, depnm, indepnm)
+         fo <- as.formula(fo, env = env)
+         mod <- do.call(dyn$lm, list(fo))
+         
+   chart <- function(mod) {
+   pdf(file="results.pdf")
+      par(mfrow=c(2,1)) 
+      acf(residuals(mod)) 
+      pacf(residuals(mod)) 
+         summ<- summary(mod)
+   aic<- AIC(mod)
+       dev.off()                }
+                                     
+   output  <- return(list(Charts=chart(mod))) }
$Charts
null device 
http://www.nabble.com/file/p21888211/results.pdf results.pdf 


Hello R users,

I have been trying to output all my results (text, plots, etc) into the same
postscript file as 

one document, but have been unable to...Can anyone help me improve my code
below so that I can 

accomplish this?  Currently I have to output them separately then piece them
back together into 

one document..

[[elided Yahoo spam]]



http://www.nabble.com/file/p21888211/data.csv data.csv
#
I don't see anywhere that you opened a pdf device. When I try :

pdf("test.pdf") and then run your code I get what looks like the  
desired output sitting in my working directory:

test.pdf is attached below but my Mac email client sometimes does  
unexpected (to me anyway) things to graphics attachments.
#
Sorry. I now see that you did open a pdf device and attached results.  
So your request is for results of summary and AIC to appear in the pdf  
output? The plotrix library has functions capable of adding tables or  
textboxes to plots. The Sweave and odfWeave packages provide a general  
approach to creating such documents. I don't think you want the full  
contents of summary(mod) but rather what summary displays at the  
console, which is different.
#
Hi David, Pele:
David Winsemius wrote:

  
    
#
Sorry, the message seems to have got botched. Here it is again:

Pele does open a PDF device (previously it was a postscript device). It
looks like what Pele is trying to do is "plot" the printed results of the
summary of a model + the AIC, together with the acf() and pacf() plots. 

As Dieter indicated, Gavin Snow's textplot() function [in gplots package]
might help. This really is a job for Sweave, which Pele should have look at. 

Pele: you could also look at Prof. Harrell's text.anova.Design function,
which you may be able to adapt to your needs. Even "simpler" would be to
write a function that uses text() [see: ?text] to plot the parts of your
model-summary and the AIC to an empty graph. 

Regards, Mark.




Hello R users,

Below is the code and output of what I am trying to do.  My goal is to
insert/print all items in the chart function into a pdf document. Only the
acf and pacf charts gets printed.

Again, thanks in advance for any help I can get!

####################################
options (scipen=999, digits=7)

library(lmtest)
library(dyn)

tdata <- ts(read.table("C:/test/data.csv" ,sep = ",",header=TRUE))
print(tdata)

   res 	<- function(dep, indep, fn, env = parent.frame()) {
   depnm   	<- deparse(substitute(dep))
   indepnm 	<- deparse(substitute(indep))
        fo 	<- sprintf("%s ~ lag(%s, -1) + %s", depnm, depnm, indepnm)
        fo 	<- as.formula(fo, env = env)
        mod <- do.call(dyn$lm, list(fo))
        
	  chart <- function(mod) {
	  pdf(file="results.pdf")
   	  par(mfrow=c(2,1)) 
   	  acf(residuals(mod)) 
   	  pacf(residuals(mod)) 
          summ<- summary(mod)
	  aic	<- AIC(mod)
    	  dev.off()                }
	  				                                  	
	  output  <- return(list(Charts=chart(mod))) }
	  results <- res(tdata[ ,"Y"], tdata[ ,"X_VARY"]) ; results

############output###############
Time Series:
Start = 1 
End = 20 
Frequency = 1 
   Unit  Y X_VARY  K D_WAY
 1    1  3      2  4    50
 2    2  5      3  9    50
 3    3  6     11 22    50
 4    4  8      4  7     2
 5    5  9     11 11     2
 6    6 12     13 13     2
 7    7 23     25 12     2
 8    8 22     30 31     3
 9    9 23      3  3     3
10   10 19     21 21    32
11   11  3      2  4    34
12   12  5      3  9     4
13   13  6     11 22     4
14   14  8      4  7     4
15   15  9     11 11     4
16   16 12     13 13     4
17   17 23     25 12     4
18   18 22     30 31     4
19   19 23      3  3     4
20   20 19     21 21     5
+    depnm   <- deparse(substitute(dep))
+    indepnm <- deparse(substitute(indep))
+         fo <- sprintf("%s ~ lag(%s, -1) + %s", depnm, depnm, indepnm)
+         fo <- as.formula(fo, env = env)
+         mod <- do.call(dyn$lm, list(fo))
+         
+   chart <- function(mod) {
+   pdf(file="results.pdf")
+      par(mfrow=c(2,1)) 
+      acf(residuals(mod)) 
+      pacf(residuals(mod)) 
+         summ<- summary(mod)
+   aic<- AIC(mod)
+       dev.off()                }
+                                     
+   output  <- return(list(Charts=chart(mod))) }
$Charts
null device 
http://www.nabble.com/file/p21888211/results.pdf results.pdf
Pele wrote:
[[elided Yahoo spam]]