A simple example of a statistical analysis using Sweave
The document is essentially written in LaTeX with specially marked chunks of R code inserted along the way. The file is usually saved with an .Rnw extension.
\documentclass{amsart}
\usepackage{Sweave}
\title{Simple Sweave usage example:\\ Analysis of Fisher's iris data}
\author{BERD Reproducible Research working group}
\date{}
\SweaveOpts{echo=false, eps=false}
\begin{document}
\maketitle
<<StartUp, results=hide>>=
options(useFancyQuotes=FALSE)
data(iris)
@
The famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables
sepal length and width, and petal length and width, respectively, for \Sexpr{table(iris$Species)[1]} flowers from each of
\Sexpr{nlevels(iris$Species)} species of iris. The species are \emph{\Sexpr{paste(levels(iris$Species), collapse=", ")}}.
First, let's run a simple ANOVA comparing the sepal lengths of the three species.
<<ANOVA>>=
a1 <- aov(Sepal.Length~Species, data=iris)
summary(a1)
@
We can see that the three species have significantly different sepal lengths. Figure~\ref{F:box} shows the corresponding
boxplot.
\begin{figure}
<<SLplot,fig=true, width=4, height=4>>=
boxplot(Sepal.Length~Species, data=iris, ylab="Sepal length")
@
\caption{Boxplot of sepal lengths by species.}\label{F:box}
\end{figure}
\end{document}
This file can be compiled within an R session by calling
Sweave("iris.Rnw", stylepath=FALSE)
tools::texi2dvi("iris.tex", pdf=TRUE)
This produces the following PDF file .
-- AnikoSzabo - 17 Dec 2010