\documentclass[10pt]{beamer} % Document Title Declarations \title{Reproducible Research} \subtitle{knitr, LaTeX and R} \author{Statistical Consulting Group} \date{} \institute{San Diego State University} % Package Declarations \usepackage{amsmath} \usepackage{amssymb} \usepackage{graphicx} \usepackage{lmodern} \usepackage{hyperref} % Document Theme Declarations \usetheme{Berlin} \usecolortheme{beaver} % Shortcuts and New Commands: \newcommand{\xbar}{{\bar x}} \newcommand{\Xbar}{{\bar X}} \newcommand{\xhat}{{\hat x}} \newcommand{\Xhat}{{\hat X}} \newcommand{\iid}{\stackrel{iid}{\sim}} \newcommand{\yhat}{{\hat y}} \newcommand{\Yhat}{{\hat Y}} \newcommand{\ybar}{{\bar y}} \newcommand{\Ybar}{{\bar Y}} \newcommand{\bhat}{{\hat beta}} \newcommand{\sumi}{\sum_{i=1}^n} \newcommand{\that}{{\hat \theta}} \newcommand{\ho}{\text{H}_0:\hspace{.5cm}} \newcommand{\ha}{\text{H}_1:\hspace{.5cm}} % Begin the Document \begin{document} \begin{frame} \maketitle \end{frame} <>= # smaller font size for chunks opts_chunk$set(size = 'footnotesize') options(width=60) @ \section{Introduction} \subsection{What is...?} \begin{frame} \frametitle{What is R?} \begin{itemize} \item Statistical Programming Environment \item Interpreted Language \item (Comparatively) easy to work/prototype in \item (can be) slower than other programming languages \item Amazing Graphic Capabilities \item Incredible power through packages \end{itemize} \end{frame} \begin{frame} \frametitle{What is LaTeX?} \begin{itemize} \item \TeX is a typesetting environment \begin{itemize} \item Developed by Donald Knuth to typeset books he was writing \end{itemize} \item \LaTeX is an extension to that environment, to \begin{itemize} \item Automate tedious tasks of writing a paper \item Generate Professional looking output \end{itemize} \item Produces clean text with proper kerning \item Consistent and (comparatively) easy writing of mathematical formulae \end{itemize} \end{frame} \begin{frame} \frametitle{What is knitr?} \begin{itemize} \item Replacement for Sweave \item Runs \emph{R} Code, encapsulates results and prepares for typesetting in \LaTeX \item Automatically formats results to look good \end{itemize} \end{frame} \begin{frame} \frametitle{Reproducible Research} \begin{itemize} \item Reproducibility is fundamental to good science. The Scientific method hinges on the reproducibility of results. \item Use of knitr to automate final document preparation down to a single button (that will produce the same output regardless of who presses it) contributes to this idea of reproducible research \item Advantages to you: You don't have to copy and paste results from R into Word, or Powerpoint. If you find some bug or decide to do something different, a small change in the code and recompiling the document (1 button) averts the crisis. \end{itemize} \end{frame} \section{How to Use} \subsection{How to Use} \begin{frame} \frametitle{Downloads} \begin{description} \item[R] \url{http://www.r-project.org/} \item[Rstudio] \url{http://www.rstudio.com/} \item[MikTeX] \url{http://miktex.org/download} \end{description} \end{frame} \begin{frame}[fragile] \frametitle{How to Use} \begin{itemize} \item Install all three downloads \item Install knitr: install.packages('knitr') \item Set Options $>$ Sweave $>$ Weave 'Rnw' Files using knitr \item Rstudio allows the use of Projects \begin{itemize} \item Keep contained everything relating to a specific analysis \item One Project per Assignment \end{itemize} \item New $\rightarrow$ R Sweave Document \end{itemize} \end{frame} \section{LaTeX} \subsection{Introducing LaTeX} \begin{frame} \frametitle{A Basic Document} \begin{itemize} \item Preamble \begin{itemize} \item Document Class \emph{(article, beamer)} \item Package Inclusions \emph{(extend functionality)} \item Define New Commands \item Author/Title Declaration \end{itemize} \item Document \begin{itemize} \item Title? \item Abstract? \item Table of Contents? \item Sections \begin{itemize} \item Subsections... and deeper \end{itemize} \item References \end{itemize} \end{itemize} \end{frame} \begin{frame}[fragile] \small \begin{verbatim} \documentclass[10pt,letterpaper]{article} \usepackage[margin=1in]{geometry} \usepackage{amsmath} \usepackage{amssymb} \title{The New Document} \date{} \author{Peter Trubey} \newcommand{\iid}{\stackrel{iid}{\sim}} \newcommand{\eqd}{\stackrel{d}{=}} \begin{document} \maketitle \abstract{An abstract goes here} \section{In the Beginning} Some Stuff here \subsection{things happened} More stuff here \subsubsection{And more things happened} 3 levels of sections? The horrors. \end{document} \end{verbatim} \normalsize \end{frame} \begin{frame} \frametitle{Environments} \begin{itemize} \item An environment allows you to treat something differently than free text \item There are various types of environments \begin{itemize} \item \emph{Math} - For displaying single and multi-line mathematical formulae \begin{itemize} \item \emph{equation} - Single line mathematical formulae \item \emph{align} - Multi-line formulae - Use \& to align \end{itemize} \item Floats \begin{itemize} \item \emph{figure} - For displaying Images \item \emph{table} - For displaying Tables \item \emph{verbatim} - For displaying really ugly code \end{itemize} \end{itemize} \item With \emph{float} package, you can also make custon environments to suit your needs. \begin{itemize} \item E.g., psuedocode to describe an algorithm. \end{itemize} \end{itemize} \end{frame} \begin{frame} \frametitle{Math Environments} \begin{itemize} \item Equation \begin{equation*} z_i = \frac{\pi\phi_{\theta_1}(x_i)}{\pi\phi_{\theta_1}(x_i) + (1-\pi)\phi_{\theta_2}(x_i)} \end{equation*} \item Align \begin{align*} \mu_1 &= \frac{\sum_{i=1}^nz_ix_i}{\sum_{i=1}^nz_i}\\ \mu_2 &= \frac{\sum_{i=1}^n(1-z_i)x_i}{\sum_{i=1}^n(1-z_i)}\\ \sigma^2 &= \frac{\sum_{i=1}^nz_i(x_i-\mu_1)^2 + (1-z_i)(x_i-\mu_2)^2}{n}\\ \pi &= \frac{\sum_{i=1}^nz_i}{n} \end{align*} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Math Environments : equation} \begin{verbatim} \begin{equation*} z_i = \frac{\pi\phi_{\theta_1}(x_i)}{\pi\phi_{\theta_1}(x_i) + (1-\pi)\phi_{\theta_2}(x_i)} \end{equation*} \end{verbatim} \vspace{1cm} \begin{equation*} z_i = \frac{\pi\phi_{\theta_1}(x_i)}{\pi\phi_{\theta_1}(x_i) + (1-\pi)\phi_{\theta_2}(x_i)} \end{equation*} \end{frame} \small \begin{frame}[fragile] \frametitle{Math Environments : align} \begin{verbatim} \begin{align*} \mu_1 &= \frac{\sum_{i=1}^nz_ix_i}{\sum_{i=1}^nz_i}\\ \mu_2 &= \frac{\sum_{i=1}^n(1-z_i)x_i}{\sum_{i=1}^n(1-z_i)}\\ \sigma^2 &= \frac{\sum_{i=1}^nz_i(x_i-\mu_1)^2 + (1-z_i)(x_i-\mu_2)^2}{n}\\ \pi &= \frac{\sum_{i=1}^nz_i}{n} \end{align*} \end{verbatim} \begin{align*} \mu_1 &= \frac{\sum_{i=1}^nz_ix_i}{\sum_{i=1}^nz_i}\\ \mu_2 &= \frac{\sum_{i=1}^n(1-z_i)x_i}{\sum_{i=1}^n(1-z_i)}\\ \sigma^2 &= \frac{\sum_{i=1}^nz_i(x_i-\mu_1)^2 + (1-z_i)(x_i-\mu_2)^2}{n}\\ \pi &= \frac{\sum_{i=1}^nz_i}{n} \end{align*} \end{frame} \normal \begin{frame}[fragile] \frametitle{A Float Environment} A figure environment, specifically \begin{verbatim} \begin{figure}[h] \centering \caption{In this figure environment, there is a fig!} \includegraphics[width=.8\textwidth]{fig.png} \label{fig:fig} \end{figure} \end{verbatim} \begin{itemize} \item Figures automatically take number labels \item Use positioning flag to control where the figure appears \item declare reference label after caption! \item R code chunks can be declared inside figures \end{itemize} \end{frame} \begin{frame} \frametitle{Table Environments} \begin{itemize} \item table \begin{itemize} \item A wrapper for your table \item Allows you to caption, label, and float the table \end{itemize} \item tabular \begin{itemize} \item The actual table \item various types of tabular for different things \begin{itemize} \item tabular \item tabularx - evenly spaced columns \item tabulary - even whitespace around column contents \end{itemize} \end{itemize} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{A LaTeX Table} \begin{verbatim} \begin{table} \begin{tabular}{lllll} X & Y & Z & W & T \\ \hline 1 & 3 & 1.386294361 & 1.921812056 & 1.243283885 \\ 2 & 4 & 1.791759469 & 3.210401996 & 1.475207592 \\ 3 & 5 & 2.079441542 & 4.324077125 & 1.629162771 \\ 4 & 6 & 2.302585093 & 5.30189811 & 1.743721514 \\ \end{tabular} \end{table} \end{verbatim} \begin{table} \begin{tabular}{lllll} X & Y & Z & W & T \\ \hline 1 & 3 & 1.386294361 & 1.921812056 & 1.243283885 \\ 2 & 4 & 1.791759469 & 3.210401996 & 1.475207592 \\ 3 & 5 & 2.079441542 & 4.324077125 & 1.629162771 \\ 4 & 6 & 2.302585093 & 5.30189811 & 1.743721514 \\ \end{tabular} \end{table} \end{frame} \section{R in LaTeX} \subsection{knitr} \begin{frame}[fragile] \frametitle{R in LaTeX} We can dynamically run R code in LaTeX using Sweave or knitr. We package the R code in chunks. For instance, we can run analysis in LaTeX <<>>= fit1 = lm(dist ~ speed, data = cars) fit2 = lm(dist ~ speed + I(speed^2), data = cars) anova(fit1,fit2) @ We can plot these results as well. If contained within a chunk is the code to generate a plot, then knitr will generate the plot. \end{frame} \begin{frame}[fragile] <>= par(mfrow=c(2,2),mar=c(2,1,2,1)) plot(fit1) @ \end{frame} \begin{frame}[fragile] \frametitle{Tables} Certain packages make our lives easier. For instances, xtable allows high quality automatic table generation. <>= library(xtable) print(xtable(anova(fit1,fit2))) @ \end{frame} \section{Wrapping Up} \subsection{Bibliography} \begin{frame}[fragile] \frametitle{Bibliography} At the end of the document, we can place the bibliography. If you set it to, LaTeX will automatically generate a bibiliography from sources you cite. \begin{verbatim} \bibliographystyle{plain} \bibliography{refs} \end{verbatim} \begin{itemize} \item bibliographystile determines what fields from the reference file are displayed \item bibliography names the reference file (.bib file extension expected) \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{A Reference File} The Reference file allows you to define referenced works. It is filled with entries such as: \begin{verbatim} @article{blackholes, author="Rabbert Klein", title="Black Holes and Their Relation to Hiding Eggs", journal="Theoretical Easter Physics", publisher="Eggs Ltd.", year="2010", note="(to appear)" } \end{verbatim} \begin{itemize} \item The document format decides the standard fields. You won't always be able to fill every relevant field. \item There are many tools online that help to manage the bibilography. \item Of all entries in the .bib file, BibTeX will only put in the bibliography those entries who are cited in the paper. \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Some Friendly Advice...} \begin{itemize} \item Make sure all R code runs properly first, before putting it in document. \item Isolate all running code into an R script file (.R), and source it in first chunk. \begin{itemize} \item Then cache the chunk. You won't have to run the code again, making document compiling faster. \end{itemize} \item Make all subsequent chunks output only. \begin{itemize} \item store all outputs in variables, so you need only print the variable to get the output. \end{itemize} \item Don't be afraid to define new commands. That's what they're there for. \begin{itemize} \item Math equations are tedious to type. Less so with custom commands. \end{itemize} \item Asterisks change the standard behavior of many environments/items \begin{itemize} \item Math Environments don't get line numbers \item sections don't show up in TOC, don't get numbers \end{itemize} \item Easy formatting of LaTeX Tables can be accomplished with online websites: \url{http://truben.no/latex/table/} \end{itemize} \end{frame} \end{document}