% $Id: statweave.swv,v 1.3 2008/06/04 22:00:29 nhorton Exp $ \documentclass[a4paper,11pt]{article} \usepackage{url,fullpage} \title{Using statweave for fun and profit} \author{Nicholas J. Horton \\ Smith College and University of Auckland} \begin{document} \setkeys{Gin}{width=3in} \maketitle Copies of this file ({\tt statweave.swv}), formatted output ({\tt statweave.pdf}) and presentation ({\tt talkjun2008.pdf}) can be found in \url{http://www.math.smith.edu/~nhorton/statweave}. The Statweave distribution and documentation can be found at \begin{center} \url{http://www.stat.uiowa.edu/~rlenth/StatWeave} \end{center} We begin by reading in the dataset. \begin{Rcode} options(digits=3) options(show.signif.stars=FALSE) ds <- read.csv("http://www.math.smith.edu/~nhorton/statweave/fev.csv") attach(ds) i <- 4 ds[i,] \end{Rcode} At this point we can access particular values, for example, the age of the subject in row \Rexpr{i} is \Rexpr{age[i]}. We can fit a linear model and display the results. \begin{Rcode}{tight} lmres <- lm(logfev~age+loght+initage+loginitht, data=ds) summary(lmres) \end{Rcode} Or we can use the {\tt xtable()} function (in the {\tt xtable} package) to create a \LaTeX\ table: \begin{Rcode}{results=tex} library(xtable) lmtab <- xtable(lmres,digits=c(0,3,3,2,4),caption="Better formatted results") print(lmtab) \end{Rcode} We can and should take a look at the residuals. Figure \ref{bplot} displays the empirical density of residuals, while Figure \ref{eplot} displays the scatterplot of normalized residuals by predicted values. \begin{Rcode}{tight} resid <- residuals(lmres) pred <- predict(lmres) fivenum(resid) \end{Rcode} \begin{figure}[tbph] \caption{Empirical density of residuals} \label{bplot} \begin{center} \begin{Rcode}{tight,fig,label=bplot} plot(density(resid),main="") x <- seq(from=-3,to=3,length=100) lines(x,dnorm(x,0,sd(resid)),lty=2) \end{Rcode} \end{center} \end{figure} \begin{figure}[tbph] \caption{Scatterplot of residual by predicted value} \label{eplot} \begin{center} \begin{Rcode}{fig,label=eplot,echo=F} plot(pred,resid,pch="") points(pred,resid,cex=0.3) lines(lowess(pred,resid),lwd=2) abline(h=0) \end{Rcode} \end{center} \end{figure} \begin{Rcode}{tight} fivenum(resid) \end{Rcode} \begin{Rcode}{tight} resid[resid< -.4] pred[resid< -.4] ds[resid< -.4,] \end{Rcode} This output was created by running {\tt statweave statweave.swv}, which ran R, snarfed the output, then created the file {\tt statweave.pdf}. At the University of Auckland, StatWeave is installed on {\tt stat12.stat.auckland.ac.nz}. At Smith College, StatWeave is installed on the Scinix cluster and scheduled to be installed on Science Center classroom images for Macintosh and Windows. While this example created a \LaTeX\ file (which was automagically turned into a pdf), StatWeave also supports Open Office (.odt) files, which can then be saved as Microsoft Word (.doc) files. In the present release StatWeave supports the following languages: R, Splus, SAS, IML, Stata and Maple and runs under Windows, Linux and Mac OS X. Intrigued? I hope that you are. These static documents are a step in the right direction for reproducible research (check out Duncan Temple Lang's dynamic StatDocs projects for the holy grail). More information about StatWeave (documentation, sample files, tests, and installation instructions) can be found at \url{http://www.stat.uiowa.edu/~rlenth/StatWeave}. \end{document}