├── .gitignore ├── CommonMaterial └── FrontMatter.Rnw ├── CoverImages ├── A-flipped.jpg ├── A.jpg ├── CompendiumSpine.png ├── Daniel.png ├── ISBN-8-6.png ├── ISBN-9780983965831-StudentGuide.pdf ├── ISBN.pdf ├── InCalculus.png ├── RStudio.png ├── Slide1.png ├── StartR.png ├── StartRCalculusSpine.png ├── Title-author.pptx ├── Title1.png ├── Title2.png ├── author-bios.tex ├── author-bios.tex.aux ├── horton.jpg ├── kaplan.png ├── mosaic-logo-small.png ├── mosaic-square.png └── pruim.jpg ├── Modeling ├── .gitignore ├── MOSAIC-Modeling.Rnw ├── Master │ └── Master-Functions.Rnw ├── Multivariable.Rnw ├── USCOTS-cover.docx ├── USCOTS-cover1.docx ├── USCOTS-cover1.pdf ├── images │ └── Trebuchet2.png ├── makefile ├── modeling-cover.pptx └── taken-out.Rmd ├── ModelingV2 ├── .gitignore ├── Anova.Rnw ├── Anova.tex ├── Connections.Rnw ├── Covariates.Rnw ├── Foundations.Rnw ├── FunctionsData.Rnw ├── MOSAIC-Modeling.Rnw ├── MultipleVariables.Rnw ├── Outline-Modeling.Rmd ├── Outline-Modeling.html ├── Preface.Rnw ├── Proportions.Rnw ├── SamplingVariation.Rnw ├── Trebuchet2.png ├── discreteFun.R ├── guitar-amp.jpg ├── prelim-cover.docx └── prelim-cover.pdf ├── PrintedFormat.tex ├── README.Rmd ├── README.html ├── README.md ├── Simulation ├── .gitignore ├── MOSAIC-SimulationBased.Rnw └── Outline-Simulation.Rmd ├── Starting ├── .gitignore ├── 220px-John_Tukey.jpg ├── Cover │ ├── .gitignore │ ├── BackMain.jpg │ ├── Flaps │ │ ├── RMarkdown-example-source.pdf │ │ ├── RMarkdown-example.Rmd │ │ ├── RMarkdown-example.pdf │ │ ├── RMarkdown-example.png │ │ ├── backflap.pdf │ │ ├── flaps.Rnw │ │ ├── flaps.pdf │ │ └── front-flap.pdf │ ├── FrontMain.jpg │ ├── ISBN-6-2.pdf │ ├── Images │ │ ├── A-flipped.jpg │ │ ├── A.jpg │ │ ├── Daniel.png │ │ ├── ISBN.pdf │ │ ├── InCalculus.png │ │ ├── RStudio.png │ │ ├── StartR.png │ │ ├── Title-author.pptx │ │ ├── mosaic-logo-small.png │ │ ├── mosaic-square.png │ │ └── spine.png │ ├── Insert.jpg │ ├── StartR-bookmobile-2012-12-21.pdf │ ├── StartR-thumbnail.png │ ├── StartTeachingCover.pdf │ ├── Title1.png │ ├── Title2.png │ ├── Title3.png │ ├── back-flap.png │ ├── front-flap.png │ ├── horton.jpg │ ├── kaplan.png │ ├── pruim.jpg │ └── testing.pdf ├── EarlyRExamples.Rnw ├── FrontMatter.Rnw ├── MOSAIC-StartTeaching.Rnw ├── MOSAIC-StartTeaching.pdf ├── Manipulate.Rnw ├── Outline-Starting.Rmd ├── RForInstructors.Rnw ├── RForStudents.Rnw ├── RStudio.Rnw ├── SBI.Rnw ├── Starting-Book-Form.pdf ├── Starting-Printed-Form.pdf ├── StartingAdvice.Rnw ├── TheTemplate.Rnw ├── USCOTS-cover.docx ├── USCOTS-cover.pdf ├── WORKFLOW ├── file.R ├── file.csv ├── file.txt ├── file.xlsx ├── frontice.docx ├── frontice.pdf ├── images │ ├── Callaway2.jpg │ ├── GoogleSpreadsheet.png │ ├── Nike3.jpg │ ├── Noodle0.jpg │ ├── Olsen2006-abstract.png │ ├── Olsen2006-results.png │ ├── Olsen2006-table.png │ ├── RStudio-bigview.png │ ├── RStudio-packages.png │ ├── Titleist4.jpg │ ├── Wilson1.jpg │ ├── dropbox1.png │ ├── dropbox2.png │ ├── fig-mosaic1.pdf │ ├── google-roadless.png │ ├── google-spreadsheet1.png │ ├── google-water1.png │ ├── google-water2.png │ ├── googleForm-water.png │ ├── mPlot.png │ ├── manip-hist1.png │ ├── manip-hist2.png │ ├── manipulate-checkbox.png │ ├── manipulate-mCIprop.pdf │ ├── manipulate-mHypTest.pdf │ ├── manipulate-mLineFit.pdf │ ├── manipulate-multihist.png │ ├── manipulate-picker.png │ ├── manipulate-xpnorm.png │ ├── mplot-HELPrct.png │ ├── planet-alt.png │ ├── planet-sample.png │ ├── planet_earth.png │ ├── publishing-google1.png │ ├── venus.png │ ├── zillow1.png │ ├── zillow_api.pdf │ └── zillow_profile.pdf ├── prelim-cover.docx └── prelim-cover.pdf ├── StudentGuide ├── CategoricalResponse.Rnw ├── Cover │ ├── .gitignore │ ├── CoverTemplate.tex │ ├── RMarkdown-example-source.pdf │ ├── RMarkdown-example.Rmd │ ├── RMarkdown-example.log │ ├── RMarkdown-example.pdf │ ├── RMarkdown-example.synctex.gz │ ├── RMarkdown-example.tex │ ├── StudentGuideCover.pdf │ ├── StudentGuideCover.tex │ ├── backflap.pdf │ ├── flaps.Rnw │ ├── flaps.tex │ ├── frontflap.pdf │ ├── frontice.docx │ └── frontice.pdf ├── DataManagement.Rnw ├── GettingStarted.Rnw ├── HELP-Study.Rnw ├── Introduction.Rnw ├── MOSAIC-StudentGuide.Rnw ├── MOSAIC-StudentGuide.pdf ├── MoreThanTwoVars.Rnw ├── OneCategorical.Rnw ├── OneQuantitative.Rnw ├── Power.Rnw ├── ProbabilityDistributions.Rnw ├── QuantitativeResponse.Rnw ├── RBook.sty ├── RBooks.sty ├── StudentGuide-Printed-Form.pdf ├── StudentGuide-Printed-Form.tex ├── Studentguide2015-10-25.pdf ├── Studentguide2015-11-09.pdf ├── Studentguide2015-11-15.pdf ├── SurvivalTime.Rnw ├── TwoCategorical.Rnw ├── TwoQuantitative.Rnw ├── authNote.sty ├── language.sty ├── markdown1.png ├── markdown2.png ├── markdown3.png ├── markdown4.png ├── problems.sty ├── probstat.sty ├── r-interface.jpg ├── r-markdown.jpg ├── rstudio-init.png └── rstudio-login.png ├── _output.yaml ├── include ├── .gitignore ├── MainDocument.Rnw ├── RBook.sty ├── RBooks.sty ├── TufteDocument.Rnw ├── USCOTS.bib ├── authNote.sty ├── language.sty ├── problems.sty ├── probstat.sty └── setup.R └── sandbox ├── Gapminder ├── falls.csv ├── merge.Rmd └── traffic.csv ├── NearRoad.pdf ├── Rmodeling.Rnw ├── fixedCI2.pdf ├── fixedMap2.pdf ├── regression.Rmd ├── roadless.R ├── roadless.Rmd ├── roadless.Rnw └── transferring.Rmd /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | TeachStatsWithR.Rproj 5 | *~ 6 | *.bk 7 | *.notes 8 | *.idx 9 | *.toc 10 | *.ilg 11 | *.ind 12 | framed.sty 13 | *.out 14 | *.fls 15 | *_latexmk 16 | -------------------------------------------------------------------------------- /CoverImages/A-flipped.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/A-flipped.jpg -------------------------------------------------------------------------------- /CoverImages/A.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/A.jpg -------------------------------------------------------------------------------- /CoverImages/CompendiumSpine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/CompendiumSpine.png -------------------------------------------------------------------------------- /CoverImages/Daniel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/Daniel.png -------------------------------------------------------------------------------- /CoverImages/ISBN-8-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/ISBN-8-6.png -------------------------------------------------------------------------------- /CoverImages/ISBN-9780983965831-StudentGuide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/ISBN-9780983965831-StudentGuide.pdf -------------------------------------------------------------------------------- /CoverImages/ISBN.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/ISBN.pdf -------------------------------------------------------------------------------- /CoverImages/InCalculus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/InCalculus.png -------------------------------------------------------------------------------- /CoverImages/RStudio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/RStudio.png -------------------------------------------------------------------------------- /CoverImages/Slide1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/Slide1.png -------------------------------------------------------------------------------- /CoverImages/StartR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/StartR.png -------------------------------------------------------------------------------- /CoverImages/StartRCalculusSpine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/StartRCalculusSpine.png -------------------------------------------------------------------------------- /CoverImages/Title-author.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/Title-author.pptx -------------------------------------------------------------------------------- /CoverImages/Title1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/Title1.png -------------------------------------------------------------------------------- /CoverImages/Title2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/Title2.png -------------------------------------------------------------------------------- /CoverImages/author-bios.tex: -------------------------------------------------------------------------------- 1 | \noindent {\bf Nicholas Horton}, {\bf Randall Pruim}, and {\bf Daniel Kaplan} teach statistics, mathematics, and computation at Amherst College, Calvin College, and Macalester College respectively. They are co-PIs on the National Science Foundation support Project MOSAIC (NSF DUE-0920350). 2 | 3 | %\centerline{\begin{tabular}{ccc} 4 | %\includegraphics[height=1in]{CompendiumTitle/horton.jpg} & \includegraphics[height=1in]{CompendiumTitle/pruim.jpg} & %\includegraphics[height=1in]{CompendiumTitle/kaplan.png}\\ 5 | %Horton & Pruim & Kaplan\\ 6 | %\end{tabular}} 7 | 8 | 9 | \noindent {\bf Other books by the authors:}\\ 10 | {\em Using R for Data Management, Statistical Analysis and Graphics} (NJH \& KK)\\ 11 | {\em Foundations and Applications of Statistics: An Introduction Using R } (RJP), {\em Gems of Theoretical Computer Science} (US, RJP \& RP)\\ 12 | {\em Understanding Nonlinear Dynamics} (DTK), {\em Statistical Modeling: A Fresh Approach} (DTK)}, {\em Start R in Calculus} (DTK) 13 | -------------------------------------------------------------------------------- /CoverImages/author-bios.tex.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \@setckpt{CompendiumTitle/author-bios.tex}{ 3 | \setcounter{page}{1} 4 | \setcounter{equation}{0} 5 | \setcounter{enumi}{0} 6 | \setcounter{enumii}{0} 7 | \setcounter{enumiii}{0} 8 | \setcounter{enumiv}{0} 9 | \setcounter{footnote}{0} 10 | \setcounter{mpfootnote}{0} 11 | \setcounter{part}{0} 12 | \setcounter{section}{0} 13 | \setcounter{subsection}{0} 14 | \setcounter{subsubsection}{0} 15 | \setcounter{paragraph}{0} 16 | \setcounter{subparagraph}{0} 17 | \setcounter{figure}{0} 18 | \setcounter{table}{0} 19 | } 20 | -------------------------------------------------------------------------------- /CoverImages/horton.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/horton.jpg -------------------------------------------------------------------------------- /CoverImages/kaplan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/kaplan.png -------------------------------------------------------------------------------- /CoverImages/mosaic-logo-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/mosaic-logo-small.png -------------------------------------------------------------------------------- /CoverImages/mosaic-square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/mosaic-square.png -------------------------------------------------------------------------------- /CoverImages/pruim.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/CoverImages/pruim.jpg -------------------------------------------------------------------------------- /Modeling/.gitignore: -------------------------------------------------------------------------------- 1 | *.aux 2 | *.bbl 3 | *.blg 4 | *.log 5 | *.notes 6 | *.synctex.gz 7 | *-concordance.tex 8 | *.pdf 9 | *.tex 10 | *.idx 11 | frame.sty 12 | figure 13 | Rindex.idx 14 | framed.sty 15 | -------------------------------------------------------------------------------- /Modeling/MOSAIC-Modeling.Rnw: -------------------------------------------------------------------------------- 1 | 2 | 3 | \documentclass[open-any,12pt]{tufte-book} 4 | 5 | \usepackage{../include/RBook} 6 | \usepackage{pdfpages} 7 | %\usepackage[shownotes]{authNote} 8 | \usepackage[hidenotes]{authNote} 9 | 10 | \def\tilde{\texttt{\~}} 11 | 12 | \title[Start Modeling in R]{Start Modeling\\$\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ $with R} 13 | \author[Kaplan, Pruim, Horton]{Daniel T. Kaplan\\Randall J. Pruim\\Nicholas J. Horton} 14 | \date{\today} 15 | 16 | \renewenvironment{knitrout}{\relax}{\noindent} 17 | \begin{document} 18 | 19 | <>= 20 | #setCacheDir("cache") 21 | require(MASS) 22 | require(grDevices) 23 | require(datasets) 24 | require(stats) 25 | require(lattice) 26 | require(grid) 27 | # require(fastR) # commented out by NH on 7/12/2012 28 | require(mosaic) 29 | require(mosaicData) 30 | trellis.par.set(theme=col.mosaic(bw=FALSE)) 31 | trellis.par.set(fontsize=list(text=9)) 32 | options(keep.blank.line=FALSE) 33 | options(width=60) 34 | require(vcd) 35 | require(knitr) 36 | opts_chunk$set( tidy=TRUE, 37 | size='small', 38 | dev="pdf", 39 | fig.path="figures/fig-", 40 | fig.width=3, fig.height=2, 41 | fig.align="center", 42 | fig.show="hold", 43 | comment=NA) 44 | @ 45 | 46 | <>= 47 | knit_hooks$set(document = function(x) { 48 | sub('\\usepackage[]{color}', '\\usepackage[]{xcolor}', 49 | x, fixed = TRUE) 50 | }) 51 | @ 52 | 53 | 54 | 55 | %\maketitle 56 | 57 | \includepdf{USCOTS-cover1} 58 | 59 | \newpage 60 | 61 | \tableofcontents 62 | 63 | \newpage 64 | 65 | 66 | <>= 67 | @ 68 | 69 | <>= 70 | @ 71 | 72 | 73 | \end{document} 74 | -------------------------------------------------------------------------------- /Modeling/Master/Master-Functions.Rnw: -------------------------------------------------------------------------------- 1 | % All pre-amble stuff should go into ../include/MainDocument.Rnw 2 | \title{Modeling Statistics} 3 | \author{Randall Pruim and Nicholas Horton and Daniel Kaplan} 4 | \date{DRAFT: \today} 5 | \Sexpr{set_parent('../../include/MainDocument.Rnw')} % All the latex pre-amble for the book 6 | \maketitle 7 | 8 | \tableofcontents 9 | 10 | \newpage 11 | 12 | \import{../}{Multivariable} 13 | 14 | -------------------------------------------------------------------------------- /Modeling/USCOTS-cover.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Modeling/USCOTS-cover.docx -------------------------------------------------------------------------------- /Modeling/USCOTS-cover1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Modeling/USCOTS-cover1.docx -------------------------------------------------------------------------------- /Modeling/USCOTS-cover1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Modeling/USCOTS-cover1.pdf -------------------------------------------------------------------------------- /Modeling/images/Trebuchet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Modeling/images/Trebuchet2.png -------------------------------------------------------------------------------- /Modeling/makefile: -------------------------------------------------------------------------------- 1 | all: Master-Modeling.pdf 2 | 3 | Master-Modeling.pdf: Master-Modeling.tex 4 | pdflatex Master-Modeling 5 | bibtex Master-Modeling 6 | pdflatex Master-Modeling 7 | 8 | Master-Modeling.tex: Master-Modeling.Rnw Multivariable.Rnw 9 | knitr Master-Modeling.Rnw 10 | 11 | 12 | -------------------------------------------------------------------------------- /Modeling/modeling-cover.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Modeling/modeling-cover.pptx -------------------------------------------------------------------------------- /ModelingV2/.gitignore: -------------------------------------------------------------------------------- 1 | Foundations.html 2 | Foundations.pdf 3 | FunctionsData-concordance.tex 4 | FunctionsData.log 5 | FunctionsData.tex 6 | Master-Modeling.log 7 | Master-Modeling.pdf 8 | Master-Modeling.synctex.gz 9 | Master-Modeling.tex 10 | MultipleVariables-concordance.tex 11 | MultipleVariables.log 12 | MultipleVariables.tex 13 | -------------------------------------------------------------------------------- /ModelingV2/Anova.tex: -------------------------------------------------------------------------------- 1 | HELLO 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ModelingV2/FunctionsData.Rnw: -------------------------------------------------------------------------------- 1 | 2 | 3 | \marginnote{Remember to load the \pkg{mosaic} and \pkg{mosaicData} packages: 4 | <<>>= 5 | require(mosaic) 6 | require(mosaicData) 7 | @ 8 | Data for the trebuchet example come from the \pkg{fastR} package. 9 | <<>>= 10 | require(fastR) 11 | @ 12 | } 13 | 14 | Let's start by building a statistical model of a trebuchet. 15 | A trebuchet is a device used for throwing projectiles. A heavy counter-weight pulls down the short end of the arm, rapidly accelerating a projectile hanging in a sack at the long end of the arm. Introduced in medieval times, trebuchets were used as a siege weapons to destroy fortifications. 16 | 17 | \marginnote{\includegraphics[width=2in]{Trebuchet2.png}\\A trebuchet.} 18 | 19 | As always, it's important to know the purpose for which the model is intended. Imagining ourselves to have been commissioned by a trebuchet operator to construct a model that predicts how far a projectile will be thrown so that the operator can set up the trebuchet to hit a target at a specified distance. In reality, the trebuchet operator is Andrew Pruim in a Science Olympiad competition when he was a high-school student. 20 | 21 | To support the predictions, Andrew made many trial throws with his trebuchet. He tried several different types of projectiles (e.g. a golf ball, a tennis ball), varied the counter-weight and the configuration of the trebuchet's sling, and measured the distance flown by the projectile for each trial. The weight of the projectile likely plays a role, so Andrew weighed each projectile. 22 | 23 | \marginpar{Note in DRAFT: I'm planning revert back to using \code{fastR::trebuchet}. I introduced \code{TrebTrials} to shorten the variable names. But with the book revision, it's not clear that this is needed. If it is still needed, I'll do it with \function{dplyr::rename}.} 24 | 25 | The data Andrew collected are in a spreadsheet named \code{TrebTrials.csv}. You can read that table in to R with this command: 26 | <<>>= 27 | TrebTrials <- read.csv( "http://www.mosaic-web.org/go/datasets/TrebTrials.csv" ) 28 | @ 29 | 30 | To show the layout, here are the first few cases: 31 | <<>>= 32 | head(TrebTrials) 33 | @ 34 | 35 | \code{proj} is the kind of object used as the projectile, \code{pwt} is the object's mass in grams, \code{cwt} is the counter-weight mass in kg, \code{form} describes the configuration of the trebuchet itself, \code{dist} records how far (in cm) the projectile was thrown. 36 | 37 | There are many different possible relationships among these variables. Consider this one: 38 | 39 | \centerline{\code{dist} $\sim$ \code{proj}} 40 | 41 | The formula specifies that \code{dist} is the output of the model and \code{proj} is the input. Or, in plainer English: account for \code{dist} using \code{proj}. 42 | 43 | The variable on the left-hand side of $\sim$ is called the ``response variable." In these notes, each model will have only one response variable. On the right-hand side of $\sim$ are one or more explanatory variables.\marginnote{Another terminology has the ``independent variable" on the right-hand side and the ``dependent variable" on the left-hand side.} 44 | 45 | Deciding what form of relationship to use is one of the primary choices that a modeler makes. But there are others, such as the mathematical form (specified by the ``goal" in the template). As indicated in Section \ref{sec:frameworks}, there is one mathematical form that is particularly widely used in modeling: a linear combination of the inputs. Fitting a linear combination model is done with the \function{lm} function. The name stands for "linear model." 46 | 47 | The command for fitting is 48 | <<>>= 49 | mod1 <- lm(dist ~ proj, data=TrebTrials) 50 | @ 51 | Notice that the syntax is that of the data/formula template. Here, the fitted model has been stored as \code{mod1} so that we can refer to it later. 52 | 53 | As you'll see, there are several different ways of looking at a model. For the purpose stated by the trebuchet engineer, the relevant use of the model is as a function: to say what the distance output will be for a given input. Use \function{makeFun} to set up the model in the form of a function. 54 | <<>>= 55 | fun1 <- makeFun(mod1) 56 | @ 57 | 58 | Once the function is constructed, the output of the function for any valid input can be calculated. 59 | <<>>= 60 | fun1("golf") 61 | fun1("tennis ball") 62 | @ 63 | 64 | \TeachingTip{Notice that confidence intervals are being introduced here without anything like a formal definition. That's intentional. Let students work with a concept before refining their idea of the concept. Once students are able to use the concept, they can explore its meaning. For instance, what affect the level has on the interval. How are prediction intervals different from confidence intervals? } 65 | Statisticians and modelers have other ways of looking at a model that can help in evaluating, refining, and interpreting the model. One of these that the trebuchet engineer might find helpful is the uncertainty in the prediction. For models constructed with \code{lm}, this uncertainty can be computed along with the prediction itself. Statisticians often represent prediction uncertainty by a confidence interval: 66 | <<>>= 67 | fun1("golf", interval="prediction", level=0.5) 68 | @ 69 | 70 | \DiggingDeeper{You can, of course, use whatever confidence level you want. In many fields, 95\% is typical. As it happens, in ballistics a different level is used: 50\%. This corresponds to the ballistics term ``circular error probable," the median distance from the target that projectiles will land.} 71 | 72 | Is this a good model? That depends on what it is to be used for. Suppose the ballistics engineer tells you, ``Tennis and golf balls, etc. were just for the trial runs. In actual use, we would use whatever projectile came to hand, such as a rock. The only measurement we can make in the field is the projectile's weight. 73 | 74 | With this additional information, another form of model would serve better.\Pointer{Note that \code{mod1} uses the projectile type, \code{prod}, as the explanatory variable while \code{mod2} uses the projectile weight, \code{pw}.} 75 | <<>>= 76 | mod2 <- lm(dist ~ pwt, data=TrebTrials) 77 | @ 78 | 79 | This model can also be presented as a function which can be used to make predictions about distance knowing only the projectile weight. 80 | <<>>= 81 | fun2 <- makeFun(mod2) 82 | @ 83 | 84 | For instance, for a projectile weighing 50 gms, the prediction is 85 | <<>>= 86 | fun2(pwt=50, interval="prediction", level=0.5) 87 | @ 88 | 89 | An important way to display such functions is as a graph: 90 | <<>>= 91 | plotFun( fun2(pwt) ~ pwt, pwt.lim=c(20,60)) 92 | @ 93 | This graph indicates that lighter object fly further. The size of the effect can be presented as a rate: the distance loss produced by adding a unit of weight to the projectile. 94 | <<>>= 95 | fun2(pwt=51) - fun2(pwt=50) 96 | @ 97 | Adding a gram of mass to the projectile will result in a loss of range of about 10 cm. 98 | 99 | <>= 100 | # BACKGROUND 101 | # Make the TrebTrials data set from trebuchet in fastR 102 | require(fastR) 103 | data( trebuchet ) 104 | TrebTrials <- trebuchet 105 | names(TrebTrials) <- c("proj", 'pwt', 'cwt', 'dist', 'config') 106 | write.csv( TrebTrials, file="~/Downloads/TrebTrials.csv", row.names=FALSE) 107 | # Move to mosaic-web. 108 | @ 109 | 110 | -------------------------------------------------------------------------------- /ModelingV2/MOSAIC-Modeling.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass[openany]{tufte-book} 2 | 3 | 4 | \usepackage{RBook} 5 | \usepackage{pdfpages} 6 | %\usepackage[shownotes]{authNote} 7 | \usepackage[hidenotes]{authNote} 8 | \usepackage{language} 9 | \usepackage{hyperref} 10 | 11 | 12 | \title{Start Modeling with R} 13 | \author{Daniel Kaplan, Randall Pruim, and Nicholas J. Horton} 14 | \date{DRAFT: \today} 15 | 16 | <>= 17 | .include. <- TRUE 18 | .testing. <- TRUE 19 | @ 20 | 21 | \begin{document} 22 | \def\cplabel{^X} 23 | 24 | \def\tilde{\texttt{\~}} 25 | % stupid hack to get rid of unwanted space before next line when there is not 26 | % a blank line following an R chunk. 27 | \renewenvironment{knitrout}{}{\noindent\ignorespaces\!\!} 28 | 29 | <>= 30 | #setCacheDir("cache") 31 | require(MASS) 32 | require(grDevices) 33 | require(datasets) 34 | require(stats) 35 | require(lattice) 36 | require(grid) 37 | # require(fastR) # commented out by NH on 7/12/2012 38 | require(mosaic) 39 | require(mosaicData) 40 | require(fastR) 41 | trellis.par.set(theme=col.mosaic(bw=FALSE)) 42 | trellis.par.set(fontsize=list(text=9)) 43 | options(keep.blank.line=FALSE) 44 | options(width=60) 45 | require(vcd) 46 | require(knitr) 47 | opts_chunk$set( tidy=TRUE, 48 | size='small', 49 | dev="pdf", 50 | fig.path="figures/fig-", 51 | fig.width=3, fig.height=2, 52 | fig.align="center", 53 | fig.show="hold", 54 | comment=NA) 55 | @ 56 | 57 | <>= 58 | knit_hooks$set(document = function(x) { 59 | sub('\\usepackage[]{color}', '\\usepackage[]{xcolor}', 60 | x, fixed = TRUE) 61 | }) 62 | 63 | #knit_hooks$set(document = function(x) { 64 | # gsub('(\\\\end\\{knitrout\\})\n', '\\1', x) 65 | #}) 66 | @ 67 | 68 | %\maketitle 69 | \includepdf{prelim-cover} 70 | \newpage 71 | 72 | \chapter*{Preface} 73 | <>= 74 | @ 75 | 76 | \tableofcontents 77 | 78 | \newpage 79 | 80 | 81 | 82 | \chapter{Foundations} 83 | <>= 84 | @ 85 | 86 | \chapter{Functions and data} 87 | <>= 88 | @ 89 | 90 | \chapter{Multiple explanatory variables} 91 | <>= 92 | @ 93 | 94 | \chapter{{\sc Anova}: a tool for guiding model construction} 95 | \label{chap:anova} 96 | <>= 97 | @ 98 | 99 | 100 | \chapter{Connections to textbook statistics} 101 | <>= 102 | @ 103 | 104 | \chapter{Covariates} 105 | <>= 106 | @ 107 | 108 | \chapter{Keeping models in proportion} 109 | \label{chap:logistic} 110 | <>= 111 | @ 112 | 113 | 114 | %\backmatter 115 | 116 | \bibliographystyle{alpha} 117 | \bibliography{../include/USCOTS} 118 | 119 | \printindex 120 | 121 | \end{document} 122 | -------------------------------------------------------------------------------- /ModelingV2/Outline-Modeling.Rmd: -------------------------------------------------------------------------------- 1 | # Start Modeling Early: Outline of Version 2 2 | 3 | 4 | 5 | #. Foundations: Computing 6 | 1. Why? 7 | a. Why is modeling important? 8 | b. Why are you building a model? The purpose drives the model. There's no important metric for the quality of a model other than whether it serves the purpose. 9 | 1. Saratoga Houses? 10 | 1. Data 11 | #. Computing template 12 | #. Foundations: Functions and relationships 13 | 1. Informal from graphics 14 | #. Formal from fits 15 | #. What do they mean 16 | a. Graphs 17 | #. Slopes and effect size 18 | #. R^2 19 | #. Complex models 20 | a. You won't need them much, but if you must, try `poly()` 21 | #. Multiple variables is often more important than complex forms. 22 | #. Multiple Variables 23 | 1. Main effects 24 | #. Interaction 25 | #. Partial derivatives 26 | #. Adjustment -- holding covariates constant 27 | #. R^2 goes up and the problems of inference 28 | #. Inference 29 | 1. Equivalence to the t-test and the p-test. 30 | #. Proportions 31 | 32 | -------------------------------------------------------------------------------- /ModelingV2/Preface.Rnw: -------------------------------------------------------------------------------- 1 | These notes present a strategy for teaching statistical modeling. Modeling is a way of making sense of the world by building a representation that is easy to explore and manipulate. Modeling is a key capacity for dealing with complexity that's used in many ways. Mathematical modeling refers to representations built out of mathematical objects, particularly functions. Statistical modeling is an adaption of mathematical modeling to the extraction of information from data. 2 | 3 | The strategy for introducing modeling is based around notation: a way of writing down forms of relationships among variables. Coupled with modern computing, the notation comes to life. The form of a relationship is translated by the computer into a model that describes the data at hand. 4 | 5 | One reason for introducing students to modeling is to give them a way to think about ``confounding" or "lurking" variables. These pejorative names reflect the conventional attitude in introductory statistics, that is, warning students away from situations where covariates play an important role. For instance, it's often stated in statistics textbooks that one shouldn't draw inferences about causation from observational studies. This ignores the practical need to do so. There are many situations where an experiment can't be done but where it's important to gain insight based on causal connections. For instance, you can hardly read a newspaper article about a health-related finding without seeing the words ``after adjusting for," or ``holding constant." 6 | 7 | We've assumed in these notes that the students you might be teaching about modeling have already learned a few basic statistical topics, that they will know at least something about confidence intervals and hypothesis testing. As such, an instructor might adopt the strategy here for adding modeling in to a conventional statistics course or as a meaningful follow-up course to an AP-statistics type course. 8 | 9 | In our own teaching, we find that modeling is an excellent way to {\em introduce} students to statistics. Our objective here, however, is to provide a way for students who have already seen a conventional introduction to learn about modeling. If you are interested in how modeling can {\em replace} the conventional introduction, materials for students, including a textbook \marginnote{DT Kaplan (2011) ``Statistical Modeling: A Fresh Approach'' 2nd ed., {\em Project MOSAIC}} are available at \url{www.mosaic-web.org/StatisticalModeling}. 10 | 11 | 12 | The notes are part of a series on teaching with R, but they are not primarily about R. Other notes in the series introduce R, discuss how to teach with R, and show how to carry out basic processes of statistical inference using conceptually simple operations implemented transparently in R. 13 | 14 | If you don't already know R, we hope that you will think the commands are simple enough that you can use them yourself; that you can learn R by observation. Our students appear to be able to do that. 15 | 16 | \bigskip 17 | 18 | We wish to acknowledge the support of the Howard Hughes Medical Institute, the W.M. Keck Foundation, and the US National Science Foundation (DUE-0920350). 19 | -------------------------------------------------------------------------------- /ModelingV2/Proportions.Rnw: -------------------------------------------------------------------------------- 1 | 2 | \marginnote{Remember to load the \pkg{mosaic} and \pkg{mosaicData} packages: 3 | <<>>= 4 | require(mosaic) 5 | require(mosaicData) 6 | @ 7 | } 8 | 9 | A conventional introduction to statistics has methods relating to means (t-tests, one-way and two-way ANOVA) and to proportions (p-tests) and to counts ($\chi^2$ tests). Quantities such as $\sqrt{p(1-p)/N}$ and $\sum \frac{(obs-exp)^2}{exp}$ are almost iconic. The modeling approach might at first seem to have nothing to do with proportions and counts, but that's not true. Indeed, proportions and counts fit in well with the modeling framework. Modeling unifies seemingly disparate methods under one roof. 10 | 11 | To illustrate, consider a simple set of data about the health effects of smoking: \dataframe{Whickham}. The data are from a survey of women in Whickhamshire, UK. The women were surveyed to find (among other things) their ages and whether they smoke. A follow-up was conducted 20 years later, at which time it was recorded whether the woman was still alive. 12 | 13 | Here's the count of women, broken down by the alive/dead outcome: 14 | <<>>= 15 | tally(~outcome, data=Whickham) 16 | @ 17 | And the proportion in each group: 18 | <<>>= 19 | tally(~outcome, data=Whickham, format="proportion") 20 | @ 21 | It's a conventional problem in intro stats to give a standard error on the proportion, typically using the Wald formula. For the fraction who have died, the standard error is: $\sqrt{p(1-p)/N} = \sqrt{0.2808 ( 1 - 0.2808)/1314} = 0.012$. This gives a 95\% margin of error of 0.024 and therefore a Wald confidence interval of $0.2808 \pm 0.0235 = [0.257,0.305]$. 22 | 23 | Another way to get the same proportions is to take the mean of the Yes/No variable that indicates whether the person has died: 24 | <<>>= 25 | mean(~outcome=="Dead", data=Whickham) 26 | @ 27 | 28 | Now remember that the model with just an intercept produces model values that are the same as means, so the proportion can also be calculated as: 29 | <<>>= 30 | mod0 = lm(outcome=="Dead" ~ 1, data=Whickham) 31 | coef(mod0) 32 | @ 33 | Now the apparatus of modeling can be applied, for instance to generate the 95\% confidence interval: 34 | <<>>= 35 | confint(mod0) 36 | @ 37 | 38 | \newpage 39 | 40 | A more typical application involves the difference between two proportions, say the fraction of the smokers who died versus the fraction of non-smokers. 41 | <<>>= 42 | tally(~outcome|smoker, data=Whickham, format="proportion") 43 | @ 44 | It looks like the smokers are a little {\em less} likely to have died. 45 | 46 | In modeling language, we're interested in whether the outcome depends on the explanatory variable \variable{smoker}: 47 | <<>>= 48 | mod1 = lm(outcome=="Dead" ~ smoker, data=Whickham) 49 | summary(mod1) 50 | @ 51 | The smokers have a lower death rate. The difference is significant. 52 | 53 | In a non-modeling course, this question might be addressed by a p-test, or by a $\chi^2$ test. For example: 54 | <<>>= 55 | counts = tally(~outcome & smoker, data=Whickham, margins=FALSE) 56 | counts 57 | chisq.test(counts) 58 | @ 59 | 60 | The p-value is similar to that from regression. This is not to say that the $\chi^2$ p-value is right, even though the counts are large. The Fisher exact test gives yet another p-value: 61 | <<>>= 62 | fisher.test(counts) 63 | @ 64 | 65 | Should you be concerned about the differences between 0.0025 and 0.0030? There's no practical meaning to the difference. However, there is a very large practical importance to the covariate \variable{age}, which has not been included in the analysis to this point. 66 | 67 | It's easy enough to build a model that incorporates \variable{age}: 68 | <<>>= 69 | mod2 = lm(outcome=="Dead" ~ smoker+ age, data=Whickham) 70 | summary(mod2) 71 | @ 72 | This model suggests that the null hypothesis cannot be rejected. The p-value on \variable{smoker} is about 0.6. There is little point in worrying about the second significant digit of a p-value on the difference of counts or proportions when the first digit of the p-value depends on a covariate. 73 | 74 | It's worth mentioning that a linear model, while reasonable, is not the right sort of model to build to model proportions. Better to use a logistic regression model. This involves a few new concepts (odds, log-odds) and a new R command, but the similarities of logistic regression to linear modeling are larger than the differences. For reference, here's the logistic regression version of just-smoker model and the age-adjusted model: 75 | <>= 76 | mod3 = glm(outcome=="Dead"~smoker, 77 | data=Whickham, family="binomial") 78 | summary(mod3) 79 | mod4 = glm(outcome=="Dead"~smoker+age, 80 | data=Whickham,family="binomial") 81 | summary(mod4) 82 | @ 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /ModelingV2/Trebuchet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/ModelingV2/Trebuchet2.png -------------------------------------------------------------------------------- /ModelingV2/discreteFun.R: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | # makeDiscreteFun( "golf"=7, "fus"=8) 4 | # Let any expression be on the RHS 5 | # pull out all variable names and make the resulting function 6 | # take those as arguments 7 | 8 | eval(parse(text=as.character(substitute(b))[3]),list(x=2,y=10)) 9 | [1] TRUE 10 | Browse[1]> class(a) 11 | [1] "numeric" 12 | Browse[1]> a 13 | [1] 7 14 | Browse[1]> class(b) 15 | Error: object 'x' not found 16 | Browse[1]> class(substitute(b)) 17 | [1] "<-" 18 | Browse[1]> class(substitute(a ~ 3)) 19 | [1] "call" 20 | Browse[1]> class(substitute(a)) 21 | [1] "<-" 22 | 23 | 24 | # Any of these will work 25 | # 26 | # fish := 7 + 3*x 27 | # fish == 7 + 3*x 28 | # fish <- 7 + 3*x 29 | # return a function that's already vectorized. 30 | makeDiscreteFun <- function(a,b,c,d){ 31 | browser() 32 | Expressions <- list(...) 33 | 34 | browser() 35 | 3+4 36 | } -------------------------------------------------------------------------------- /ModelingV2/guitar-amp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/ModelingV2/guitar-amp.jpg -------------------------------------------------------------------------------- /ModelingV2/prelim-cover.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/ModelingV2/prelim-cover.docx -------------------------------------------------------------------------------- /ModelingV2/prelim-cover.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/ModelingV2/prelim-cover.pdf -------------------------------------------------------------------------------- /PrintedFormat.tex: -------------------------------------------------------------------------------- 1 | \documentclass{book} 2 | \usepackage{pdfpages} 3 | \usepackage[paperheight=9.5in,paperwidth=7in,twoside,inner=.75in,top=.005in,bottom=.005in]{geometry} 4 | 5 | \begin{document} 6 | \includepdf[nup=1x1,pages=-,trim=35 10 35 10,,offset=.2in 0in]{Starting/Master-Starting.pdf} 7 | \end{document} 8 | 9 | 10 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | Project MOSAIC Little Books 4 | =============== 5 | 6 | ```{r include=FALSE} 7 | require(mosaic) 8 | ``` 9 | 10 | You can grab PDFs of the Little Books here: 11 | 12 | * *Start Teaching Statistics Using R* 13 | [[view]](Starting/MOSAIC-StartTeaching.pdf) 14 | [[download]](../../raw/master/Starting/MOSAIC-StartTeaching.pdf) 15 | 16 | This book presents instructors with an overview of our approach to 17 | teaching statistics with R and an introduction to our primary R toolkit. 18 | 19 | * *A Student's Guide to R* 20 | [[view]](StudentGuide/MOSAIC-StudentGuide.pdf) 21 | [[download]](../../raw/master/StudentGuide/MOSAIC-StudentGuide.pdf) 22 | 23 | This book is organized by analysis method and demonstrates how to perform 24 | all of the statistical analyses typically covered in an Intro Stats course. 25 | It can serve a good reference for both students and faculty. 26 | 27 | This was formerly known as *A Compendium of Commands to Teach Statistics With R* but has been reworked to make it more student friendly. 28 | 29 | * *Start R in Calculus* [[Amazon]](http://www.amazon.com/Start-Calculus-Daniel-T-Kaplan/dp/0983965897) 30 | 31 | This book describes the use of R in calculus based on the successful 32 | redesign of the first semester calculus course at Macalester College. 33 | 34 | Others that are in progress include 35 | 36 | * Start Modeling with R 37 | 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | Project MOSAIC Little Books 4 | =============== 5 | 6 | 7 | 8 | You can find PDFs of the Little Books here: 9 | 10 | * *Start Teaching Statistics Using R* 11 | [[view]](Starting/MOSAIC-StartTeaching.pdf) 12 | [[download]](../../raw/master/Starting/MOSAIC-StartTeaching.pdf) 13 | 14 | This book presents instructors with an overview of our approach to 15 | teaching statistics with R and an introduction to our primary R toolkit. 16 | 17 | * *A Student's Guide to R* 18 | [[view]](StudentGuide/MOSAIC-StudentGuide.pdf) 19 | [[download]](../../raw/master/StudentGuide/MOSAIC-StudentGuide.pdf) 20 | 21 | This book is organized by analysis method and demonstrates how to perform 22 | all of the statistical analyses typically covered in an Intro Stats course. 23 | It can serve a good reference for both students and faculty. 24 | 25 | This was formerly known as *A Compendium of Commands to Teach Statistics With R* but has been reworked to make it more student friendly. 26 | 27 | * *Start R in Calculus* [[Amazon]](http://www.amazon.com/Start-Calculus-Daniel-T-Kaplan/dp/0983965897) 28 | 29 | This book describes the use of R in calculus based on the successful 30 | redesign of the first semester calculus course at Macalester College. 31 | 32 | 33 | There is a spanish language translation of the *Student's Guide to R* available at https://github.com/jarochoeltrocho/MOSAIC-LittleBooks-Spanish (kudos to Francisco Javier Jara Ávila, https://github.com/jarochoeltrocho) 34 | -------------------------------------------------------------------------------- /Simulation/.gitignore: -------------------------------------------------------------------------------- 1 | *.aux 2 | *.bbl 3 | *.blg 4 | *.log 5 | *.notes 6 | *.synctex.gz 7 | *-concordance.tex 8 | *.pdf 9 | *.tex 10 | figure 11 | -------------------------------------------------------------------------------- /Simulation/Outline-Simulation.Rmd: -------------------------------------------------------------------------------- 1 | ## Simulation-Based Inference: Outline 2 | -------------------------------------------------------------------------------- /Starting/.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.aux 3 | *.blg 4 | *.tex 5 | *.bk 6 | *~ 7 | *swp 8 | *-concordance.tex 9 | cache 10 | *.notes 11 | ddd.* 12 | *.tex 13 | *.bbl 14 | figure 15 | figures 16 | Births.rda 17 | GettingStarted.bbl 18 | example-file.pdf 19 | *.synctex.gz 20 | ByHand.pdf 21 | MoreExamples.aux 22 | .DS_Store 23 | myHELP.Rda 24 | myHELP.csv 25 | myHELP.txt 26 | debugging.Rnw 27 | -------------------------------------------------------------------------------- /Starting/220px-John_Tukey.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/220px-John_Tukey.jpg -------------------------------------------------------------------------------- /Starting/Cover/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /Starting/Cover/BackMain.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/BackMain.jpg -------------------------------------------------------------------------------- /Starting/Cover/Flaps/RMarkdown-example-source.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Flaps/RMarkdown-example-source.pdf -------------------------------------------------------------------------------- /Starting/Cover/Flaps/RMarkdown-example.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Homework #3" 3 | author: "Abby Seedief" 4 | date: "January 7, 2015" 5 | output: pdf_document 6 | --- 7 | 8 | ```{r include=FALSE} 9 | require(mosaic) 10 | require(mosaicData) 11 | ``` 12 | 13 | ## Problem 1 14 | 15 | Build a model of wage as a function of sex, 16 | adjusting for relevant covariates. 17 | ```{r} 18 | lm(wage ~ sex + exper, data=CPS85) 19 | ``` 20 | 21 | ## Problem 2 22 | 23 | Show whether the covariate is related to sex. 24 | ```{r} 25 | bwplot(exper ~ sex, data=CPS85) 26 | ``` -------------------------------------------------------------------------------- /Starting/Cover/Flaps/RMarkdown-example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Flaps/RMarkdown-example.pdf -------------------------------------------------------------------------------- /Starting/Cover/Flaps/RMarkdown-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Flaps/RMarkdown-example.png -------------------------------------------------------------------------------- /Starting/Cover/Flaps/backflap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Flaps/backflap.pdf -------------------------------------------------------------------------------- /Starting/Cover/Flaps/flaps.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Flaps/flaps.pdf -------------------------------------------------------------------------------- /Starting/Cover/Flaps/front-flap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Flaps/front-flap.pdf -------------------------------------------------------------------------------- /Starting/Cover/FrontMain.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/FrontMain.jpg -------------------------------------------------------------------------------- /Starting/Cover/ISBN-6-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/ISBN-6-2.pdf -------------------------------------------------------------------------------- /Starting/Cover/Images/A-flipped.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/A-flipped.jpg -------------------------------------------------------------------------------- /Starting/Cover/Images/A.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/A.jpg -------------------------------------------------------------------------------- /Starting/Cover/Images/Daniel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/Daniel.png -------------------------------------------------------------------------------- /Starting/Cover/Images/ISBN.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/ISBN.pdf -------------------------------------------------------------------------------- /Starting/Cover/Images/InCalculus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/InCalculus.png -------------------------------------------------------------------------------- /Starting/Cover/Images/RStudio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/RStudio.png -------------------------------------------------------------------------------- /Starting/Cover/Images/StartR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/StartR.png -------------------------------------------------------------------------------- /Starting/Cover/Images/Title-author.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/Title-author.pptx -------------------------------------------------------------------------------- /Starting/Cover/Images/mosaic-logo-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/mosaic-logo-small.png -------------------------------------------------------------------------------- /Starting/Cover/Images/mosaic-square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/mosaic-square.png -------------------------------------------------------------------------------- /Starting/Cover/Images/spine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Images/spine.png -------------------------------------------------------------------------------- /Starting/Cover/Insert.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Insert.jpg -------------------------------------------------------------------------------- /Starting/Cover/StartR-bookmobile-2012-12-21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/StartR-bookmobile-2012-12-21.pdf -------------------------------------------------------------------------------- /Starting/Cover/StartR-thumbnail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/StartR-thumbnail.png -------------------------------------------------------------------------------- /Starting/Cover/StartTeachingCover.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/StartTeachingCover.pdf -------------------------------------------------------------------------------- /Starting/Cover/Title1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Title1.png -------------------------------------------------------------------------------- /Starting/Cover/Title2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Title2.png -------------------------------------------------------------------------------- /Starting/Cover/Title3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/Title3.png -------------------------------------------------------------------------------- /Starting/Cover/back-flap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/back-flap.png -------------------------------------------------------------------------------- /Starting/Cover/front-flap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/front-flap.png -------------------------------------------------------------------------------- /Starting/Cover/horton.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/horton.jpg -------------------------------------------------------------------------------- /Starting/Cover/kaplan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/kaplan.png -------------------------------------------------------------------------------- /Starting/Cover/pruim.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/pruim.jpg -------------------------------------------------------------------------------- /Starting/Cover/testing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Cover/testing.pdf -------------------------------------------------------------------------------- /Starting/MOSAIC-StartTeaching.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass[openany]{tufte-book} 2 | 3 | <>= 4 | #setCacheDir("cache") 5 | require(MASS) 6 | require(grDevices) 7 | require(datasets) 8 | require(stats) 9 | require(lattice) 10 | require(grid) 11 | # require(fastR) # commented out by NH on 7/12/2012 12 | require(mosaic) 13 | require(mosaicData) 14 | trellis.par.set(theme=col.mosaic(bw=FALSE)) 15 | trellis.par.set(fontsize=list(text=9)) 16 | options(format.R.blank=FALSE) 17 | options(width=60) 18 | options(digits=3) 19 | require(vcd) 20 | require(knitr) 21 | opts_chunk$set( tidy=FALSE, 22 | size='small', 23 | dev="pdf", 24 | fig.path="figures/fig-", 25 | fig.width=3, fig.height=2, 26 | fig.align="center", 27 | fig.show="hold", 28 | comment=NA) 29 | knit_theme$set("greyscale0") 30 | @ 31 | 32 | <>= 33 | includeChapter <- FALSE # don't show chapters not yet being paginated. 34 | showEdited <- FALSE # displaying chapters already paginated for printing 35 | knit_hooks$set(document = function(x) { 36 | sub('\\usepackage[]{color}', '\\usepackage[]{xcolor}', 37 | x, fixed = TRUE) 38 | }) 39 | 40 | #knit_hooks$set(document = function(x) { 41 | # gsub('(\\\\end\\{knitrout\\})\n', '\\1', x) 42 | #}) 43 | @ 44 | 45 | 46 | \usepackage{../include/RBook} 47 | \usepackage{pdfpages} 48 | %\usepackage[shownotes]{authNote} 49 | \usepackage[hidenotes]{authNote} 50 | \usepackage{language} % available at https://github.com/rpruim/latex 51 | \usepackage{hyperref} 52 | \usepackage{fancyhdr} % DTK added for header. 53 | 54 | % Larger font sizes (from size12.clo) 55 | \makeatletter% allows us to use macros with @ in their names 56 | \renewcommand\normalsize{% 57 | \@setfontsize\normalsize\@xiipt{14.5}% 58 | \abovedisplayskip 12\p@ \@plus3\p@ \@minus7\p@ 59 | \abovedisplayshortskip \z@ \@plus3\p@ 60 | \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@ 61 | \belowdisplayskip \abovedisplayskip 62 | \let\@listi\@listI} 63 | \renewcommand\small{% 64 | \@setfontsize\small\@xipt{13.6}% 65 | \abovedisplayskip 11\p@ \@plus3\p@ \@minus6\p@ 66 | \abovedisplayshortskip \z@ \@plus3\p@ 67 | \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@ 68 | \def\@listi{\leftmargin\leftmargini 69 | \topsep 9\p@ \@plus3\p@ \@minus5\p@ 70 | \parsep 4.5\p@ \@plus2\p@ \@minus\p@ 71 | \itemsep \parsep}% 72 | \belowdisplayskip \abovedisplayskip 73 | } 74 | \renewcommand\footnotesize{% 75 | \@setfontsize\footnotesize\@xpt\@xiipt 76 | \abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@ 77 | \abovedisplayshortskip \z@ \@plus3\p@ 78 | \belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@ 79 | \def\@listi{\leftmargin\leftmargini 80 | \topsep 6\p@ \@plus2\p@ \@minus2\p@ 81 | \parsep 3\p@ \@plus2\p@ \@minus\p@ 82 | \itemsep \parsep}% 83 | \belowdisplayskip \abovedisplayskip 84 | } 85 | \renewcommand\scriptsize{\@setfontsize\scriptsize\@viiipt{9.5}} 86 | \renewcommand\tiny{\@setfontsize\tiny\@vipt\@viipt} 87 | \renewcommand\large{\@setfontsize\large\@xivpt{18}} 88 | \renewcommand\Large{\@setfontsize\Large\@xviipt{22}} 89 | \renewcommand\LARGE{\@setfontsize\LARGE\@xxpt{25}} 90 | \renewcommand\huge{\@setfontsize\huge\@xxvpt{30}} 91 | \let\Huge=\huge 92 | \setlength\leftmargini {1.5em} 93 | \setlength\leftmarginii {1.5em} 94 | \setlength\leftmarginiii {1.5em} 95 | \setlength\leftmarginiv {1.5em} 96 | \setlength\leftmarginv {1.5em} 97 | \setlength\leftmarginvi {1.5em} 98 | \setlength\labelsep {.5pc} 99 | \setlength\labelwidth {\leftmargini} 100 | \addtolength\labelwidth{-\labelsep} 101 | \setlength{\parindent}{1.5em}% 102 | \renewcommand{\@tufte@reset@par}{% 103 | \setlength{\RaggedRightParindent}{1.5em}% 104 | \setlength{\JustifyingParindent}{1.5em}% 105 | \setlength{\parindent}{1.5em}% 106 | \setlength{\parskip}{0pt}% 107 | } 108 | \@tufte@reset@par 109 | \renewcommand{\@tufte@margin@par}{% 110 | \setlength{\RaggedRightParindent}{1.0em}% 111 | \setlength{\JustifyingParindent}{1.0em}% 112 | \setlength{\parindent}{1.0em}% 113 | \setlength{\parskip}{0pt}% 114 | } 115 | \makeatother% restores meaning of @ 116 | 117 | % For the printed version. Commented out for Web version 118 | \renewcommand{\variable}[1]{{\color{black}\texttt{#1}}} 119 | \renewcommand{\dataframe}[1]{{\color{black}\texttt{#1}}} 120 | \renewcommand{\function}[1]{{\color{blue}\texttt{\StrSubstitute{#1}{()}{}()}}} 121 | \renewcommand{\option}[1]{{\color{black}\texttt{#1}}} 122 | \renewcommand{\pkg}[1]{{\color{black}\texttt{#1}}} 123 | \renewcommand{\code}[1]{{\color{black}\texttt{#1}}} 124 | 125 | 126 | 127 | \title{Start Teaching with R} 128 | \author[Pruim, Horton \& Kaplan]{Randall Pruim, Nicholas J. Horton, and Daniel T. Kaplan} 129 | \date{January 2015} 130 | 131 | \begin{document} 132 | \def\cplabel{^X} 133 | 134 | \def\tilde{\texttt{\~}} 135 | % stupid hack to get rid of unwanted space before next line when there is not 136 | % a blank line following an R chunk. 137 | \renewenvironment{knitrout}{}{\noindent\ignorespaces\!\!} 138 | 139 | %\maketitle 140 | 141 | \includepdf{frontice} 142 | 143 | \newpage 144 | \vspace*{2in} 145 | 146 | \parbox{4in}{\noindent Copyright (c) 2015 by Randall Pruim, Nicholas J. Horton, \& Daniel T. Kaplan.} 147 | \medskip 148 | 149 | \parbox{4in}{\noindent Edition 1.1, November 2015} 150 | 151 | \bigskip 152 | 153 | \parbox{4in}{\noindent This material is copyrighted by the authors under a Creative Commons Attribution 3.0 Unported License. You are free to \emph{Share} (to copy, distribute and transmit the work) and to \emph{Remix} (to adapt the work) if you attribute our work. More detailed information about the licensing is available at this web page: 154 | \url{http://www.mosaic-web.org/go/teachingRlicense.html}. 155 | } 156 | 157 | 158 | \vspace*{2.in} 159 | 160 | \parbox{4in}{\noindent {\bf Cover Photo}: Maya Hanna} 161 | 162 | \newpage 163 | 164 | \hspace{1.3cm}\parbox{8in}{\tableofcontents} 165 | 166 | \newpage 167 | 168 | <>= 169 | @ 170 | 171 | <>= 172 | @ 173 | 174 | <>= 175 | @ 176 | 177 | <>= 178 | @ 179 | 180 | <>= 181 | @ 182 | 183 | <>= 184 | @ 185 | 186 | <>= 187 | @ 188 | 189 | <>= 190 | @ 191 | 192 | <>= 193 | @ 194 | 195 | \backmatter 196 | 197 | \bibliographystyle{alpha} 198 | \bibliography{../include/USCOTS} 199 | 200 | \printindex 201 | 202 | \end{document} 203 | 204 | -------------------------------------------------------------------------------- /Starting/MOSAIC-StartTeaching.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/MOSAIC-StartTeaching.pdf -------------------------------------------------------------------------------- /Starting/Outline-Starting.Rmd: -------------------------------------------------------------------------------- 1 | ## Start Teaching with R: Outline 2 | 3 | 4 | Random list of things to include: 5 | * RStudio introduction 6 | * Packages and why to use them 7 | * The mosaic package and others that we recommend 8 | * Minimal R reference 9 | * Rmd 10 | * Umbrella for other books 11 | * Error messages. Things to look for in error messages. 12 | * overwriting names 13 | * recycling vectors 14 | * factors and characters 15 | * Data 16 | * `data()` 17 | * Group data with Google forms 18 | * `fetchData()` and requesting a repository name 19 | * distributing other kinds of files, e.g. templates for .Rmd, scripts, ... 20 | * Style 21 | * Don't use one-letter names in your examples. Your students will pick this up and end up saying things like `c = makeFun(x~x)`, which will mask `base::c()` and mess everything up until you `remove(c)` -------------------------------------------------------------------------------- /Starting/Starting-Book-Form.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Starting-Book-Form.pdf -------------------------------------------------------------------------------- /Starting/Starting-Printed-Form.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/Starting-Printed-Form.pdf -------------------------------------------------------------------------------- /Starting/USCOTS-cover.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/USCOTS-cover.docx -------------------------------------------------------------------------------- /Starting/USCOTS-cover.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/USCOTS-cover.pdf -------------------------------------------------------------------------------- /Starting/WORKFLOW: -------------------------------------------------------------------------------- 1 | WORKFLOW for generating the book. 2 | 3 | 1. Compile "Master-Starting.Rnw" 4 | 5 | When you're ready to get the final version 6 | 7 | 2. Run 8 | makeindex Master-Starting.idx 9 | at the shell prompt 10 | 11 | 3. Compile "Master-Book-Form.Rnw" which will trim the pages and thereby make the font look bigger. -------------------------------------------------------------------------------- /Starting/file.R: -------------------------------------------------------------------------------- 1 | cat("Congratulations, you just sourced a file.") 2 | -------------------------------------------------------------------------------- /Starting/file.csv: -------------------------------------------------------------------------------- 1 | A,B,C,D 2 | 1,2,3,a 3 | .,-,, 4 | 3,4,5,c 5 | -------------------------------------------------------------------------------- /Starting/file.txt: -------------------------------------------------------------------------------- 1 | "number" "letter" 2 | "1" 1 "a" 3 | "2" 2 "b" 4 | "3" 3 "c" 5 | "4" 4 "d" 6 | "5" 5 "e" 7 | -------------------------------------------------------------------------------- /Starting/file.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/file.xlsx -------------------------------------------------------------------------------- /Starting/frontice.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/frontice.docx -------------------------------------------------------------------------------- /Starting/frontice.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/frontice.pdf -------------------------------------------------------------------------------- /Starting/images/Callaway2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Callaway2.jpg -------------------------------------------------------------------------------- /Starting/images/GoogleSpreadsheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/GoogleSpreadsheet.png -------------------------------------------------------------------------------- /Starting/images/Nike3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Nike3.jpg -------------------------------------------------------------------------------- /Starting/images/Noodle0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Noodle0.jpg -------------------------------------------------------------------------------- /Starting/images/Olsen2006-abstract.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Olsen2006-abstract.png -------------------------------------------------------------------------------- /Starting/images/Olsen2006-results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Olsen2006-results.png -------------------------------------------------------------------------------- /Starting/images/Olsen2006-table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Olsen2006-table.png -------------------------------------------------------------------------------- /Starting/images/RStudio-bigview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/RStudio-bigview.png -------------------------------------------------------------------------------- /Starting/images/RStudio-packages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/RStudio-packages.png -------------------------------------------------------------------------------- /Starting/images/Titleist4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Titleist4.jpg -------------------------------------------------------------------------------- /Starting/images/Wilson1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/Wilson1.jpg -------------------------------------------------------------------------------- /Starting/images/dropbox1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/dropbox1.png -------------------------------------------------------------------------------- /Starting/images/dropbox2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/dropbox2.png -------------------------------------------------------------------------------- /Starting/images/fig-mosaic1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/fig-mosaic1.pdf -------------------------------------------------------------------------------- /Starting/images/google-roadless.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/google-roadless.png -------------------------------------------------------------------------------- /Starting/images/google-spreadsheet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/google-spreadsheet1.png -------------------------------------------------------------------------------- /Starting/images/google-water1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/google-water1.png -------------------------------------------------------------------------------- /Starting/images/google-water2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/google-water2.png -------------------------------------------------------------------------------- /Starting/images/googleForm-water.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/googleForm-water.png -------------------------------------------------------------------------------- /Starting/images/mPlot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/mPlot.png -------------------------------------------------------------------------------- /Starting/images/manip-hist1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manip-hist1.png -------------------------------------------------------------------------------- /Starting/images/manip-hist2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manip-hist2.png -------------------------------------------------------------------------------- /Starting/images/manipulate-checkbox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-checkbox.png -------------------------------------------------------------------------------- /Starting/images/manipulate-mCIprop.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-mCIprop.pdf -------------------------------------------------------------------------------- /Starting/images/manipulate-mHypTest.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-mHypTest.pdf -------------------------------------------------------------------------------- /Starting/images/manipulate-mLineFit.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-mLineFit.pdf -------------------------------------------------------------------------------- /Starting/images/manipulate-multihist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-multihist.png -------------------------------------------------------------------------------- /Starting/images/manipulate-picker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-picker.png -------------------------------------------------------------------------------- /Starting/images/manipulate-xpnorm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/manipulate-xpnorm.png -------------------------------------------------------------------------------- /Starting/images/mplot-HELPrct.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/mplot-HELPrct.png -------------------------------------------------------------------------------- /Starting/images/planet-alt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/planet-alt.png -------------------------------------------------------------------------------- /Starting/images/planet-sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/planet-sample.png -------------------------------------------------------------------------------- /Starting/images/planet_earth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/planet_earth.png -------------------------------------------------------------------------------- /Starting/images/publishing-google1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/publishing-google1.png -------------------------------------------------------------------------------- /Starting/images/venus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/venus.png -------------------------------------------------------------------------------- /Starting/images/zillow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/zillow1.png -------------------------------------------------------------------------------- /Starting/images/zillow_api.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/zillow_api.pdf -------------------------------------------------------------------------------- /Starting/images/zillow_profile.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/images/zillow_profile.pdf -------------------------------------------------------------------------------- /Starting/prelim-cover.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/prelim-cover.docx -------------------------------------------------------------------------------- /Starting/prelim-cover.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/Starting/prelim-cover.pdf -------------------------------------------------------------------------------- /StudentGuide/CategoricalResponse.Rnw: -------------------------------------------------------------------------------- 1 | 2 | \section{Logistic regression} 3 | \myindex{logistic regression}% 4 | 5 | Logistic regression is available using the \function{glm()} function, 6 | which supports 7 | a variety of 8 | link functions and distributional forms for generalized linear models, including logistic regression. 9 | \FoodForThought[-1cm]{The \function{glm()} function has argument \option{family}, which can take an option 10 | \option{link}. The \code{logit} link is the default link for the binomial family, 11 | so we don't need to specify it here. The more verbose usage would be \code{family=binomial(link=logit)}.}% 12 | \Rindex{glm()}% 13 | \Rindex{family option}% 14 | \Rindex{exp()}% 15 | \Rindex{msummary()}% 16 | <>= 17 | logitmod <- glm(homeless ~ age + female, 18 | family = binomial, data = HELPrct) 19 | msummary(logitmod) 20 | exp(coef(logitmod)) 21 | exp(confint(logitmod)) 22 | @ 23 | 24 | We can compare two models (for multiple degree of freedom tests). For example, we 25 | might be interested in the association of homeless status and age for each of the three substance groups. 26 | \Rindex{anova()}% 27 | \Rindex{test option}% 28 | <>= 29 | mymodsubage <- glm((homeless == "homeless") ~ age + substance, 30 | family = binomial, data = HELPrct) 31 | mymodage <- glm((homeless == "homeless") ~ age, family = binomial, 32 | data = HELPrct) 33 | msummary(mymodsubage) 34 | exp(coef(mymodsubage)) 35 | anova(mymodage, mymodsubage, test = "Chisq") 36 | @ 37 | We observe that the cocaine and heroin groups are significantly less likely to be homeless than alcohol involved subjects, after controlling for age. (A similar result is seen when considering just homeless status and substance.) 38 | 39 | <<>>= 40 | tally(~ homeless | substance, format = "percent", 41 | margins = TRUE, data = HELPrct) 42 | @ 43 | 44 | 45 | -------------------------------------------------------------------------------- /StudentGuide/Cover/.gitignore: -------------------------------------------------------------------------------- 1 | StudentGuideCover.aux 2 | StudentGuideCover.log 3 | -------------------------------------------------------------------------------- /StudentGuide/Cover/CoverTemplate.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \newcommand{\fullwidth}{18.875in} % including edge bleed on both sides 3 | \newcommand{\fullheight}{8.25in} % including edge bleed on top and bottom 4 | % Bookmobile Template widths. Set these according to the template 5 | \newcommand{\trim}{0.125in} 6 | \newcommand{\flap}{3in} 7 | \newcommand{\wrap}{0.188in} 8 | \newcommand{\cover}{6in} 9 | \newcommand{\spine}{0.25in} 10 | % Order: trim flap wrap cover spine cover wrap flap trim 11 | 12 | % These are calculated from the above 13 | \newlength{\Xlogo} 14 | % Change the Xmm to move the item relative to it's default position 15 | 16 | \usepackage[paperwidth=\fullwidth,paperheight=\fullheight,margin=0in]{geometry} 17 | \usepackage{graphicx} 18 | \usepackage[colorgrid,texcoord]{eso-pic}[2002/11/16] 19 | \usepackage{calc} 20 | %\usepackage[showboxes,absolute]{textpos} 21 | \usepackage[showboxes,absolute]{textpos} 22 | \usepackage{bold-extra} 23 | \usepackage{tikz} 24 | \usepackage{color} 25 | \usepackage{tgbonum} 26 | \usepackage{pdfpages} 27 | \setlength{\TPHorizModule}{1in} 28 | \setlength{\TPVertModule}{1in} 29 | \definecolor{AuthorColor}{rgb}{1 1 1} 30 | \definecolor{TitleColor}{rgb}{1 1 1 } 31 | \definecolor{RColor}{rgb}{.8 .8 .8} 32 | 33 | 34 | % These are calculated from the above. The non-symbolic quantity can be used 35 | % for relative positioning of the item 36 | 37 | 38 | % ISBN 978-0-9839658-9-3 39 | \begin{document} 40 | \setlength{\Xlogo}{\trim+\flap+\wrap+7mm} 41 | %%%% Cover Main Part 42 | \begin{textblock}{6.188}(3.125,0) 43 | \noindent\begin{tikzpicture} 44 | %\fill[white,opacity=.2] (0in,-8.25in) rectangle (12.81in,0in); 45 | \end{tikzpicture} 46 | \end{textblock} 47 | 48 | %%%% Back Cover Contents 49 | \begin{textblock*}{5}(3.5in,.6in) 50 | \parbox{130mm}{ 51 | 52 | \textsc{\bfseries{A Student Reference for Statistics Using R}} is one 53 | of a series of books designed to help statistics educators master 54 | integrating modern computation in their courses. We refer to our 55 | approach as {\bf computational statistics} because 56 | the availability of computation is shaping how statistics is done, 57 | taught, and understood. Computational statistics is a key component of 58 | {\bf data science}, using data to answer questions and communicate 59 | results. 60 | 61 | \noindent Other books in the series include: 62 | \begin{itemize} 63 | \setlength\itemsep{0mm} 64 | \item \textsc{Start Modeling with R} 65 | \item \textsc{Start Teaching with R} 66 | \item \textsc{Start R in Calculus} 67 | \end{itemize} 68 | 69 | 70 | These materials have been shared with hundreds of statistics educators 71 | through workshops run under the auspices of Project Mosaic, CAUSE, the 72 | Mathematical Association of America, the American Statistical 73 | Association, the W.M. Keck Foundation, the Howard Hughes Medical 74 | Institute, and the US National 75 | Science Foundation. 76 | 77 | 78 | \raggedright 79 | \noindent \noindent {\bf Nicholas Horton}, {\bf Randall Pruim}, and {\bf 80 | Daniel Kaplan} teach statistics, mathematics, and computation at 81 | Amherst College, Calvin College, and Macalester College 82 | respectively. They are co-PIs on the National Science Foundation 83 | support Project MOSAIC (NSF DUE-0920350). 84 | 85 | \medskip 86 | 87 | \centerline{\begin{tabular}{ccc} 88 | \includegraphics[height=1in]{../../CoverImages/horton.jpg} & \includegraphics[height=1in]{../../CoverImages/pruim.jpg} & \includegraphics[height=1in]{../../CoverImages/kaplan.png}\\ 89 | {\sc Horton} & {\sc Pruim} & {\sc Kaplan}\\ 90 | \end{tabular}} 91 | 92 | \medskip 93 | 94 | \noindent {\bf Other books by the authors:}\\ 95 | {\em Using R for Data Management, Statistical Analysis and Graphics} (NJH \& KK)\\ 96 | {\em Foundations and Applications of Statistics: An Introduction Using 97 | R } (RJP), {\em Gems of Theoretical Computer Science} (US \& RJP), 98 | {\em Understanding Nonlinear Dynamics} (DTK), {\em Statistical Modeling: A Fresh Approach} (DTK), {\em Start R in Calculus} (DTK) 99 | } 100 | \end{textblock*} 101 | 102 | \begin{textblock}{4}(3.5,.5) 103 | \noindent\begin{tikzpicture} 104 | \fill [white,opacity=.75] (0in,-6.1in) rectangle (5.4in,0in); 105 | \end{tikzpicture} 106 | \end{textblock} 107 | 108 | %%%% Front Cover Lightener 109 | \begin{textblock}{6.188}(9.55,0) % moved 0.25 from 9.3 110 | \noindent\begin{tikzpicture} 111 | % \fill [white,opacity=.3] (0in,-8.25in) rectangle (6.881in,0in); 112 | \end{tikzpicture} 113 | \end{textblock} 114 | 115 | 116 | %%%% Title 117 | 118 | %%%% Title 119 | 120 | \begin{textblock*}{5.2}(237mm,1.8in) %8.6 121 | \noindent{% 122 | \hbox{\parbox{4.5in}{\noindent\raggedleft{\textsc{\bfseries{% 123 | \fontsize{28pt}{60pt}\selectfont\textcolor{TitleColor}{% 124 | \hfill A Compendium of\newline \hspace{0pt} \newline\hspace{0pt}{\tiny .}\hfill 125 | Commands to Teach% 126 | \newline \hspace{0pt} \newline\hfill Statistics with }}}}}\bfseries{{\fontsize{150pt}{60pt}\selectfont\textcolor{RColor}{\raisebox{-.75in}{R}}}}}} 127 | \end{textblock*} 128 | 129 | \begin{textblock*}{4.5in}(285mm,5in) 130 | 131 | \noindent{\textsc{\bfseries{\fontsize{22pt}{60pt}\selectfont 132 | \textcolor{AuthorColor}{Randall Pruim\\\\Nicholas J. Horton\\\\Daniel T. Kaplan}}}} 133 | \end{textblock*} 134 | 135 | % Spine New 136 | 137 | \begin{textblock*}{\spine}(\trim+\flap+\wrap+\cover+\spine/2 + 7pt,0) % was 9.44 138 | 139 | \vspace*{.4in} 140 | 141 | \noindent\hspace{-.2in}\rotatebox{270}{\large\bf 142 | \textcolor{yellow}{Start Teaching with 143 | R}\hspace{.3in}Pruim, Horton, Kaplan 144 | \hspace{.3in}{\textcolor{yellow}{\sf Project MOSAIC}}\hspace*{.25in}\raisebox{-1.3mm}{\includegraphics[width=0.21in]{../../CoverImages/mosaic-square.png}}} 145 | \end{textblock*} 146 | 147 | %%% Logos 148 | \begin{textblock*}{1.5}(\Xlogo,6.65in) 149 | \noindent\includegraphics[width=1.5in]{../../CoverImages/mosaic-logo-small.png} 150 | \medskip 151 | \noindent \rule{2pt}{0pt}\includegraphics[width=1.425in]{../../CoverImages/RStudio.png} 152 | \end{textblock*} 153 | 154 | 155 | %%% ISBN 978-0-9839658-6-2 156 | \begin{textblock}{1.65}(6.6,6.85) % Same as above 157 | \noindent\includegraphics[width=1.75in]{../../CoverImages/ISBN-8-6.png} 158 | \end{textblock} 159 | \begin{textblock}{1.65}(6.6,6.85) 160 | \noindent\begin{tikzpicture} 161 | \fill [white,opacity=.75] (-1.75in,-1in) rectangle (0in,0in); 162 | \end{tikzpicture} 163 | \end{textblock} 164 | 165 | 166 | %% Back Flap 167 | 168 | \begin{textblock*}{3}(\trim,.25in) 169 | \noindent\includegraphics[width=2.9in]{backflap.pdf} 170 | \end{textblock*} 171 | 172 | \begin{textblock}{3}(0,0) 173 | \noindent\begin{tikzpicture} 174 | \fill [white,opacity=.75] (0in,0in) rectangle (3.1in,8.4in); 175 | \end{tikzpicture} 176 | \end{textblock} 177 | 178 | % Front Flap 179 | 180 | \begin{textblock*}{3}(\trim+\flap+\wrap+\cover+\spine+\cover+\wrap-2mm,.25in) % 15.73 181 | \includegraphics[width=2.9in]{frontflap.pdf} 182 | \end{textblock*} 183 | 184 | \begin{textblock*}{3}(\trim+\flap+\wrap+\cover+\spine+\cover+\wrap,0) % was 15.9 185 | \noindent\begin{tikzpicture} 186 | \fill [white,opacity=.75] (0in,0in) rectangle (3.125in,8.25in); 187 | \end{tikzpicture} 188 | \end{textblock*} 189 | 190 | % Front Photo 191 | \begin{textblock}{6.125}(9.3,0) 192 | \noindent\includegraphics[angle=90,height=8.25in,width=9.881in]{../../CoverImages/FrontMain.jpg} 193 | \end{textblock} 194 | 195 | % Back Photo 196 | \begin{textblock}{6.125}(-0.2,0) 197 | \noindent\includegraphics[angle=90,height=8.25in,width=9.881in]{../../CoverImages/BackMain.jpg} 198 | \end{textblock} 199 | 200 | 201 | \end{document} 202 | 203 | -------------------------------------------------------------------------------- /StudentGuide/Cover/RMarkdown-example-source.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/RMarkdown-example-source.pdf -------------------------------------------------------------------------------- /StudentGuide/Cover/RMarkdown-example.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Homework #3" 3 | author: "Abby Seedief" 4 | date: "January 7, 2015" 5 | output: pdf_document 6 | --- 7 | 8 | ```{r include=FALSE} 9 | require(mosaic) 10 | require(mosaicData) 11 | ``` 12 | 13 | ## Problem 1 14 | 15 | Build a model of wage as a function of sex, 16 | adjusting for relevant covariates. 17 | ```{r} 18 | lm(wage ~ sex + exper, data=CPS85) 19 | ``` 20 | 21 | ## Problem 2 22 | 23 | Show whether the covariate is related to sex. 24 | ```{r} 25 | bwplot(exper ~ sex, data=CPS85) 26 | ``` -------------------------------------------------------------------------------- /StudentGuide/Cover/RMarkdown-example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/RMarkdown-example.pdf -------------------------------------------------------------------------------- /StudentGuide/Cover/RMarkdown-example.synctex.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/RMarkdown-example.synctex.gz -------------------------------------------------------------------------------- /StudentGuide/Cover/RMarkdown-example.tex: -------------------------------------------------------------------------------- 1 | \documentclass[]{article} 2 | \usepackage[T1]{fontenc} 3 | \usepackage{lmodern} 4 | \usepackage{amssymb,amsmath} 5 | \usepackage{ifxetex,ifluatex} 6 | \usepackage{fixltx2e} % provides \textsubscript 7 | % Set line spacing 8 | % use upquote if available, for straight quotes in verbatim environments 9 | \IfFileExists{upquote.sty}{\usepackage{upquote}}{} 10 | \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex 11 | \usepackage[utf8]{inputenc} 12 | \else % if luatex or xelatex 13 | \ifxetex 14 | \usepackage{mathspec} 15 | \usepackage{xltxtra,xunicode} 16 | \else 17 | \usepackage{fontspec} 18 | \fi 19 | \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase} 20 | \newcommand{\euro}{€} 21 | \fi 22 | % use microtype if available 23 | \IfFileExists{microtype.sty}{\usepackage{microtype}}{} 24 | \usepackage[paperwidth=5.5in, paperheight=8.5in,margin=.2in]{geometry} 25 | \usepackage{color} 26 | \usepackage{fancyvrb} 27 | \newcommand{\VerbBar}{|} 28 | \newcommand{\VERB}{\Verb[commandchars=\\\{\}]} 29 | \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} 30 | % Add ',fontsize=\small' for more characters per line 31 | \usepackage{framed} 32 | \definecolor{shadecolor}{RGB}{248,248,248} 33 | \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}} 34 | \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}} 35 | \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{{#1}}} 36 | \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}} 37 | \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}} 38 | \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}} 39 | \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}} 40 | \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}} 41 | \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}} 42 | \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{{#1}}} 43 | \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{{#1}}} 44 | \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}} 45 | \newcommand{\RegionMarkerTok}[1]{{#1}} 46 | \newcommand{\ErrorTok}[1]{\textbf{{#1}}} 47 | \newcommand{\NormalTok}[1]{{#1}} 48 | \usepackage{graphicx} 49 | % Redefine \includegraphics so that, unless explicit options are 50 | % given, the image width will not exceed the width of the page. 51 | % Images get their normal width if they fit onto the page, but 52 | % are scaled down if they would overflow the margins. 53 | \makeatletter 54 | \def\ScaleIfNeeded{% 55 | \ifdim\Gin@nat@width>\linewidth 56 | \linewidth 57 | \else 58 | \Gin@nat@width 59 | \fi 60 | } 61 | \makeatother 62 | \let\Oldincludegraphics\includegraphics 63 | {% 64 | \catcode`\@=11\relax% 65 | \gdef\includegraphics{\@ifnextchar[{\Oldincludegraphics}{\Oldincludegraphics[width=\ScaleIfNeeded]}}% 66 | }% 67 | \ifxetex 68 | \usepackage[setpagesize=false, % page size defined by xetex 69 | unicode=false, % unicode breaks when used with xetex 70 | xetex]{hyperref} 71 | \else 72 | \usepackage[unicode=true]{hyperref} 73 | \fi 74 | \hypersetup{breaklinks=true, 75 | bookmarks=true, 76 | pdfauthor={Abby Seedief}, 77 | pdftitle={Homework \#3}, 78 | colorlinks=true, 79 | citecolor=blue, 80 | urlcolor=blue, 81 | linkcolor=magenta, 82 | pdfborder={0 0 0}} 83 | \urlstyle{same} % don't use monospace font for urls 84 | \setlength{\parindent}{0pt} 85 | \setlength{\parskip}{6pt plus 2pt minus 1pt} 86 | \setlength{\emergencystretch}{3em} % prevent overfull lines 87 | \setcounter{secnumdepth}{0} 88 | 89 | %%% Change title format to be more compact 90 | \usepackage{titling} 91 | \setlength{\droptitle}{-2em} 92 | \title{Homework \#3} 93 | \pretitle{\vspace{\droptitle}\centering\huge} 94 | \posttitle{\par} 95 | \author{Abby Seedief} 96 | \preauthor{\centering\large\emph} 97 | \postauthor{\par} 98 | \predate{\centering\large\emph} 99 | \postdate{\par} 100 | \date{January 7, 2015} 101 | 102 | 103 | 104 | 105 | \begin{document} 106 | 107 | \maketitle 108 | 109 | 110 | \subsection{Problem 1}\label{problem-1} 111 | 112 | Build a model of wage as a function of sex, adjusting for relevant 113 | covariates. 114 | 115 | \begin{Shaded} 116 | \begin{Highlighting}[] 117 | \KeywordTok{lm}\NormalTok{(wage ~}\StringTok{ }\NormalTok{sex +}\StringTok{ }\NormalTok{exper, }\DataTypeTok{data=}\NormalTok{CPS85)} 118 | \end{Highlighting} 119 | \end{Shaded} 120 | 121 | \begin{verbatim} 122 | ## 123 | ## Call: 124 | ## lm(formula = wage ~ sex + exper, data = CPS85) 125 | ## 126 | ## Coefficients: 127 | ## (Intercept) sexM exper 128 | ## 7.0729 2.1960 0.0428 129 | \end{verbatim} 130 | 131 | \subsection{Problem 2}\label{problem-2} 132 | 133 | Show whether the covariate is related to sex. 134 | 135 | \begin{Shaded} 136 | \begin{Highlighting}[] 137 | \KeywordTok{bwplot}\NormalTok{(exper ~}\StringTok{ }\NormalTok{sex, }\DataTypeTok{data=}\NormalTok{CPS85)} 138 | \end{Highlighting} 139 | \end{Shaded} 140 | 141 | \includegraphics{RMarkdown-example_files/figure-latex/unnamed-chunk-3-1.pdf} 142 | 143 | \end{document} 144 | -------------------------------------------------------------------------------- /StudentGuide/Cover/StudentGuideCover.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/StudentGuideCover.pdf -------------------------------------------------------------------------------- /StudentGuide/Cover/StudentGuideCover.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | % All measurements in mm 3 | \newcommand{\fullwidth}{479.4mm} % including edge bleed on both sides 4 | \newcommand{\fullheight}{209.55mm} % including edge bleed on top and bottom 5 | % Bookmobile Template widths. Set these according to the template 6 | \newcommand{\trim}{3.175} % but don't put the mm for these lengths 7 | \newcommand{\flap}{76.2} 8 | \newcommand{\wrap}{4.78} 9 | \newcommand{\cover}{152.4} 10 | \newcommand{\spine}{6.35} 11 | % Order: trim flap wrap cover spine cover wrap flap trim 12 | 13 | % These are calculated from the above 14 | %\newlength{\Xlogo} 15 | % Change the Xmm to move the item relative to it's default position 16 | 17 | \usepackage[paperwidth=\fullwidth,paperheight=\fullheight,margin=0in]{geometry} 18 | \usepackage{graphicx} 19 | %\usepackage[colorgrid,texcoord]{eso-pic}[2002/11/16] % FOR THE GRID 20 | \usepackage{calc} 21 | %\usepackage[showboxes,absolute]{textpos} 22 | \usepackage[absolute]{textpos} 23 | \usepackage{bold-extra} 24 | \usepackage{tikz} 25 | \usepackage{color} 26 | \usepackage{tgbonum} 27 | \usepackage{pdfpages} 28 | \setlength{\TPHorizModule}{1mm} 29 | \setlength{\TPVertModule}{1mm} 30 | \definecolor{AuthorColor}{rgb}{1 1 1} 31 | \definecolor{TitleColor}{rgb}{1 1 0 } % should be 1 1 1 32 | \definecolor{RColor}{rgb}{.8 .8 .8} 33 | 34 | 35 | % These are calculated from the above. The non-symbolic quantity can be used 36 | % for relative positioning of the item 37 | 38 | 39 | % ISBN 978-0-9839658-31 40 | \begin{document} 41 | %%%% Cover Main Part 42 | \begin{textblock}{6.188}(100,0) 43 | \noindent\begin{tikzpicture} 44 | \fill[white,opacity=.2] (0in,-8.25in) rectangle (12.81in,0in); 45 | \end{tikzpicture} 46 | \end{textblock} 47 | 48 | %%%% Back Cover Contents 49 | \begin{textblock}{5}(95,10) 50 | \noindent\parbox{130mm}{ 51 | 52 | \textsc{\bfseries{A Student's Guide to R}} is one 53 | of a series of books designed to help integrate 54 | modern computation into statistics courses. We refer to our 55 | approach as {\bf computational statistics} because 56 | the availability of computation is shaping how statistics is done, 57 | taught, and understood. Computational statistics is a key component of 58 | {\bf data science}, using data to answer questions and communicate 59 | results. 60 | 61 | \noindent Other books in the series include: 62 | \begin{itemize} 63 | \setlength\itemsep{0mm} 64 | \item \textsc{Start Modeling with R} 65 | \item \textsc{Start Teaching with R} 66 | \item \textsc{Start R in Calculus} 67 | \end{itemize} 68 | 69 | 70 | These materials have been shared with hundreds of statistics educators 71 | through workshops run under the auspices of Project Mosaic, CAUSE, the 72 | Mathematical Association of America, the American Statistical 73 | Association, the W.M. Keck Foundation, the Howard Hughes Medical 74 | Institute, and the US National 75 | Science Foundation. 76 | 77 | 78 | \raggedright 79 | \noindent \noindent {\bf Nicholas Horton}, {\bf Randall Pruim}, and {\bf 80 | Daniel Kaplan} teach statistics, mathematics, and computation at 81 | Amherst College, Calvin College, and Macalester College 82 | respectively. They are co-PIs on the National Science Foundation 83 | support Project MOSAIC (NSF DUE-0920350). 84 | 85 | \medskip 86 | 87 | \centerline{\begin{tabular}{ccc} 88 | \includegraphics[height=1in]{../../CoverImages/horton.jpg} & \includegraphics[height=1in]{../../CoverImages/pruim.jpg} & \includegraphics[height=1in]{../../CoverImages/kaplan.png}\\ 89 | {\sc Horton} & {\sc Pruim} & {\sc Kaplan}\\ 90 | \end{tabular}} 91 | 92 | \medskip 93 | 94 | \noindent {\bf Other books by the authors:}\\ 95 | {\em Using R for Data Management, Statistical Analysis and Graphics (2nd edition)} (NJH \& KK)\\ 96 | {\em Foundations and Applications of Statistics: An Introduction Using 97 | R } (RJP), {\em Gems of Theoretical Computer Science} (US \& RJP), 98 | {\em Understanding Nonlinear Dynamics} (DTK), {\em Statistical Modeling: A Fresh Approach} (DTK), {\em Start R in Calculus} (DTK), {\em Data Computing} (DTK) 99 | } 100 | \end{textblock} 101 | 102 | \begin{textblock}{4}(91.5,5) 103 | \noindent\begin{tikzpicture} 104 | \fill [white,opacity=.75] (0in,-6.3in) rectangle (5.4in,0in); 105 | \end{tikzpicture} 106 | \end{textblock} 107 | 108 | %%%% Front Cover Lightener 109 | \begin{textblock}{6.188}(9.55,0) % moved 0.25 from 9.3 110 | \noindent\begin{tikzpicture} 111 | % \fill [white,opacity=.3] (0in,-8.25in) rectangle (6.881in,0in); 112 | \end{tikzpicture} 113 | \end{textblock} 114 | 115 | 116 | %%%% Title 117 | 118 | %%%% Title 119 | 120 | \begin{textblock}{52}(232,45.7) %8.6 121 | \noindent{% 122 | \hbox{\parbox{4.5in}{\noindent\raggedleft{\textsc{\bfseries{% 123 | \fontsize{42pt}{68pt}\selectfont\textcolor{TitleColor}{% 124 | \hfill A Student's \newline \hspace{0pt} \newline\hspace{0pt}{\tiny .}\hfill 125 | Guide% 126 | \newline \hspace{0pt} \newline\hfill to}}}}}\hspace{5mm}\bfseries{{\fontsize{150pt}{60pt}\selectfont\textcolor{RColor}{\raisebox{-17.0mm}{R}}}}}} 127 | \end{textblock} 128 | 129 | \begin{textblock}{114}(285,127) 130 | 131 | \noindent{\textsc{\bfseries{\fontsize{22pt}{60pt}\selectfont 132 | \textcolor{AuthorColor}{Nicholas J. Horton\\\\Randall Pruim\\\\Daniel T. Kaplan}}}} 133 | \end{textblock} 134 | 135 | % Spine New 136 | 137 | \begin{textblock}{\spine}(242,12) 138 | 139 | \noindent\hspace{-.2in}\rotatebox{270}{\Large\bf 140 | \textcolor{yellow}{A Student's Guide to R} 141 | \hspace{.3in}Horton, Pruim, Kaplan 142 | \hspace{.3in}{\textcolor{yellow}{\sf Project MOSAIC}}\hspace*{.25in}\raisebox{-1.3mm}{\includegraphics[width=0.23in]{../../CoverImages/mosaic-square.png}}} 143 | \end{textblock} 144 | 145 | %%% Logos 146 | \begin{textblock}{1.5}(95,168.9) 147 | \noindent\includegraphics[width=1.5in]{../../CoverImages/mosaic-logo-small.png} 148 | \medskip 149 | \noindent\rule{2pt}{0pt}\includegraphics[width=1.425in]{../../CoverImages/RStudio.png} 150 | \end{textblock} 151 | 152 | 153 | %%% ISBN 978-0-9839658-6-2 154 | \begin{textblock}{1.65}(180,172) % Same as above 155 | \noindent\includegraphics[width=1.75in]{../../CoverImages/ISBN-9780983965831-StudentGuide.pdf} 156 | \end{textblock} 157 | \begin{textblock}{1.65}(180,173) 158 | \noindent\begin{tikzpicture} 159 | \fill [white,opacity=.75] (-1.75in,-1in) rectangle (0in,0in); 160 | \end{tikzpicture} 161 | \end{textblock} 162 | 163 | 164 | 165 | %% Back Flap 166 | 167 | \begin{textblock}{3}(3.3,10) 168 | \noindent\includegraphics[width=2.9in]{backflap.pdf} 169 | \end{textblock} 170 | 171 | \begin{textblock}{3}(0,0) 172 | \noindent\begin{tikzpicture} 173 | \fill [white,opacity=.75] (0in,0in) rectangle (3.1in,8.4in); 174 | \end{tikzpicture} 175 | \end{textblock} 176 | 177 | % Front Flap 178 | 179 | \begin{textblock}{3}(401,10) % 15.73 180 | \noindent\includegraphics[width=2.9in]{frontflap.pdf} 181 | \end{textblock} 182 | 183 | 184 | \begin{textblock}{3}(401,0) % was 15.9 185 | \noindent\begin{tikzpicture} 186 | \fill [white,opacity=.75] (0in,0in) rectangle (3.125in,8.25in); 187 | \end{tikzpicture} 188 | \end{textblock} 189 | 190 | 191 | % Front Photo 192 | \begin{textblock}{6.125}(236,0) 193 | \noindent\includegraphics[angle=90,height=8.25in,width=9.881in]{../../Starting/Cover/FrontMain.jpg} 194 | \end{textblock} 195 | 196 | % Back Photo 197 | \begin{textblock}{6.125}(-0.2,0) 198 | \noindent\includegraphics[angle=90,height=8.25in,width=9.881in]{../../Starting/Cover/BackMain.jpg} 199 | \end{textblock} 200 | 201 | 202 | \end{document} 203 | 204 | -------------------------------------------------------------------------------- /StudentGuide/Cover/backflap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/backflap.pdf -------------------------------------------------------------------------------- /StudentGuide/Cover/flaps.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage[total={3in, 8in}]{geometry} 3 | \usepackage{graphicx} 4 | \usepackage{color} 5 | \usepackage{bold-extra} 6 | \pagestyle{empty} 7 | \def\tilde{\texttt{\~}} 8 | \newcommand{\Wspace}{{\tiny\color{white}.}\hfill} 9 | \newcommand{\NewSection}[1]{{\noindent\color{blue}\bfseries\scshape#1}} 10 | 11 | <>= 12 | require(mosaic) 13 | require(mosaicData) 14 | require(knitr) 15 | options(digits=3) 16 | opts_chunk$set( out.width="1in", echo=FALSE, 17 | comment="| ",fig.show='hold', 18 | warning=FALSE,message=FALSE, 19 | out.height="2cm", 20 | background='#D0D0F0') 21 | thickSettings <- list(box.umbrella=list(lwd=6), 22 | box.rectangle=list(lwd=6), 23 | xlab=list(cex=2), ylab=list(cex=2), 24 | main=list(cex=2), cex=2, pch=19) 25 | trellis.par.set(theme.mosaic()) 26 | @ 27 | 28 | %\AtBeginDocument{\raggedright} 29 | 30 | \begin{document} 31 | \NewSection{Load Packages}\hfill\verb+require(mosaic)+\\ 32 | \hspace*{1cm}\hfill\verb+require(mosaicData)+\\ 33 | \\ 34 | \NewSection{Essential R Syntax}\\ 35 | Function \& arguments:\hfill \verb+rflip(10)+\\ 36 | Optional arguments:\hfill \verb+rflip(10, prob=0.3)+\\ 37 | Assignment:\hfill \verb+x <- rflip(10, prob=0.3)+\\ 38 | \\ 39 | \NewSection{Formula Interface}\\ 40 | Used for graphics, statistics, inference, and modeling operations.\\ 41 | \def\phbox#1{\fbox{\phantom{g#1l}}} % 42 | \def\ffbox#1{\fbox{\phantom{g}#1\phantom{b}}} % 43 | \vspace*{-.5cm}\begin{center} 44 | \sf 45 | \ffbox{goal} $\Big($ 46 | \ffbox{y} $\sim$ \ffbox{x} {\LARGE,} data = \ffbox{mydata} $\Big)$ 47 | \end{center} 48 | \noindent {\small Read as:} {Calculate {\sf goal} using {\sf mydata} for {\sf y} ``broken down" by {\sf x}, or ``modeled by" {\sf x}.} Examples:\\ 49 | \Wspace\verb+mean(age~homeless, data=HELPrct)+% 50 | \vspace*{-2mm} 51 | <>= 52 | mean(age~homeless,data=HELPrct) 53 | @ 54 | \noindent\Wspace\verb+quantile(age~sex,data=HELPrct,p=c(.2,.8))+% 55 | \vspace*{-6mm} 56 | <>= 57 | quantile(age~sex, p=c(.2,.8), data=HELPrct) 58 | @ 59 | \Wspace\verb+tally(homeless~sex, data=HELPrct)+% 60 | \vspace*{-2mm} 61 | <>= 62 | tally(homeless~sex, data=HELPrct) 63 | @ 64 | \NewSection{RMarkdown documents}\\ 65 | \\ 66 | \hbox{\fbox{\includegraphics[width=1.6in]{RMarkdown-example-source.pdf}} \hfill \fbox{\includegraphics[width=1.2in]{RMarkdown-example.pdf}}}\\ 67 | \begin{small}% 68 | \noindent Compile to any of HTML, PDF, or Word.\\ 69 | See \texttt{mosaic plain} template through RStudio menu:\\ 70 | {\sc File/New File/RMarkdown/From Template} 71 | \end{small}% 72 | 73 | \newpage 74 | \hrule\vspace*{1mm}% 75 | \NewSection{Graphics Formula Syntax}\\ 76 | \noindent{\sf 77 | \ffbox{goal} $\Big($ 78 | \ffbox{y} $\sim$ \ffbox{x} {$\ \Big\boldmath|\ $} \ffbox{z} {\LARGE,}}\\\Wspace 79 | {\sf groups=\ffbox{w} , data = \ffbox{mydata} $\Big)$} 80 | 81 | 82 | \noindent$\bullet$ {\sf y} --- y-axis variable ({\sc optional})\\ 83 | $\bullet$ {\sf x} --- x-axis variable ({\sc\color{blue}Required})\\ 84 | $\bullet$ {\sf z} --- facet-by variable ({\sc optional})\\ 85 | $\bullet$ {\sf w} --- color-by variable ({\sc optional})\\ 86 | \vspace*{-2mm} 87 | \hrule 88 | \noindent% 89 | \hspace*{-2mm}% 90 | <<>>= 91 | bwplot( wage ~ sex, data=CPS85, 92 | par.settings = thickSettings, 93 | xlab=list(cex=2), ylab=list(cex=2), main=list(cex=2), 94 | lwd=3, cex=3, fill="grey") 95 | xyplot( wage ~ educ | sex, data=CPS85, 96 | scales=list(cex=2), 97 | xlab=list(cex=2), ylab=list(cex=2), main=list(cex=2), 98 | cex=2, pch=19, alpha=0.4) 99 | xyplot( wage ~ educ, groups=sex, data=CPS85, 100 | scales=list(cex=2), 101 | xlab=list(cex=2), ylab=list(cex=2), main=list(cex=2), 102 | cex=2, pch=19, alpha=0.4) 103 | @ 104 | \begin{small} 105 | \noindent {\tiny \sc Left:}\hfill\verb+bwplot(wage~sex, data= CPS85)+\\ 106 | {\tiny \sc Middle:}\hfill\verb+xyplot(wage~educ | sex, data= CPS85)+\\ 107 | {\tiny \sc Right:}\hfill\verb+xyplot(wage~educ, groups=sex, data=CPS85)+\\ 108 | \end{small} 109 | \hrule% 110 | \noindent\hspace*{-2mm}% 111 | <<>>= 112 | densityplot( ~ wage, data=CPS85, 113 | par.settings = thickSettings, 114 | xlab=list(cex=2), ylab=list(cex=2), main=list(cex=2), 115 | lwd=3, cex=3, fill="grey") 116 | densityplot( ~ wage | sex, data=CPS85, 117 | scales=list(cex=2), 118 | xlab=list(cex=2), ylab=list(cex=2), main=list(cex=2), 119 | cex=2, pch=19, alpha=0.4) 120 | densityplot( ~ wage, groups=sex, data=CPS85, 121 | scales=list(cex=2), 122 | xlab=list(cex=2), ylab=list(cex=2), main=list(cex=2), 123 | cex=2, pch=19, alpha=0.4) 124 | @ 125 | \begin{small} 126 | \noindent {\tiny \sc Left:}\hfill\verb+densityplot(~wage, data= CPS85)+\\ 127 | {\tiny \sc Middle:}\hfill\verb+densityplot(~wage | sex, data= CPS85)+\\ 128 | {\tiny \sc Right:}\hfill\verb+densityplot(~wage,groups=sex, data=CPS85)+\\ 129 | \end{small} 130 | \hrule 131 | \hrule 132 | \vspace*{2mm} 133 | \NewSection{Randomization and Iteration}\\ 134 | \begin{small} 135 | \noindent{\sc Resample/Bootstrap:}\\ 136 | \verb+do(100)*mean(wage ~ sex, data=resample(CPS85))+\\ 137 | {\sc Random Permutations:}\\ 138 | \verb+do(100)*mean(wage ~ shuffle(sex), data=CPS85)+\\ 139 | \end{small} 140 | \hrule 141 | \hrule 142 | \vspace*{2mm} 143 | \NewSection{Confidence Intervals \& Statistical Tests}\\ 144 | \begin{small}\Wspace\verb+t.test(wage ~ sex, data=CPS85)+\\ 145 | \Wspace\verb+prop.test(43, 100)+\\ 146 | \Wspace\verb#crosstab <- tally(~union+sex, data=CPS85)#\\ 147 | \Wspace\verb#chisq.test( crosstab ) fisher.test(crosstab)#\\ 148 | \Wspace\verb+mod <- lm(wage ~ sector, data=CPS85)+\\ 149 | \Wspace{Then ... \verb+anova(mod) TukeyHSD(mod)+ etc.}\\ 150 | \noindent{\bf\sc\small Modeling \& Covariates}\\ 151 | \Wspace\verb#mod <- lm(wage ~ sex + educ, data=CPS85)#\\ 152 | \Wspace\verb#summary(mod)# or \verb#anova(mod)# or \verb#confint(mod)#\\ 153 | \noindent {\sc\small Extract model function:}\\ 154 | \Wspace\verb+fun <- makeFun(mod)+\\ 155 | \Wspace \verb+fun(sex="F",educ=10)+}\\ 156 | \Wspace{\footnotesize{\small\verb+plotFun(+}\verb+fun(sex="F",educ=x)} ~ x,x.lim=range(0,8))+}\\\hrule 157 | \end{small} 158 | \vfill 159 | 160 | \end{document} -------------------------------------------------------------------------------- /StudentGuide/Cover/frontflap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/frontflap.pdf -------------------------------------------------------------------------------- /StudentGuide/Cover/frontice.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/frontice.docx -------------------------------------------------------------------------------- /StudentGuide/Cover/frontice.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Cover/frontice.pdf -------------------------------------------------------------------------------- /StudentGuide/HELP-Study.Rnw: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | \label{sec:help} 5 | 6 | \myindex{HELP study}% 7 | \myindex{Health Evaluation and Linkage to Primary Care study}% 8 | Many of the examples in this guide utilize data from the HELP study, 9 | a randomized clinical trial for adult inpatients recruited from a detoxification unit. 10 | Patients with no primary care physician were randomized to receive a multidisciplinary assessment and a brief motivational intervention or usual care, 11 | with the goal of linking them to primary medical care. 12 | Funding for the HELP study was provided by the National Institute 13 | on Alcohol Abuse and Alcoholism (R01-AA10870, Samet PI) and 14 | National Institute on Drug Abuse (R01-DA10019, Samet PI). 15 | The details of the 16 | randomized trial along with the results from a series of additional analyses have been published\cite{same:lars:hort:2003,lieb:save:2002,kert:hort:frie:2003}. 17 | 18 | Eligible subjects were 19 | adults, who spoke Spanish or English, reported alcohol, heroin or 20 | cocaine as their first or second drug of choice, resided in proximity 21 | to the primary care clinic to which they would be referred or were 22 | homeless. Patients with established primary care relationships 23 | they planned to continue, significant dementia, specific plans to 24 | leave the Boston area that would prevent research participation, 25 | failure to provide contact information for tracking purposes, or 26 | pregnancy were excluded. 27 | 28 | Subjects were interviewed at baseline during 29 | their detoxification stay and follow-up interviews were undertaken 30 | every 6 months for 2 years. A variety of continuous, count, discrete, and survival time predictors and outcomes were collected at each of these five occasions. 31 | The Institutional Review Board of 32 | Boston University Medical Center approved all aspects of the study, including the creation of the de-identified dataset. Additional 33 | privacy protection was secured by the issuance of a Certificate of 34 | Confidentiality by the Department of Health and Human Services. 35 | 36 | The \pkg{mosaicData} package contains several forms of the de-identified HELP dataset. 37 | We will focus on \pkg{HELPrct}, which contains 38 | 27 variables for the 453 subjects 39 | with minimal missing data, primarily at baseline. 40 | Variables included in the HELP dataset are described in Table \ref{tab:helpvars}. More information can be found at: \url{https://nhorton.people.amherst.edu/r2}. 41 | A copy of the study instruments can be found at: \url{https://nhorton.people.amherst.edu/help}. 42 | \begin{longtable}{|p{2.1cm}|p{6.8cm}|p{3.5cm}|} 43 | \caption{Annotated description of variables in the \dataframe{HELPrct} dataset} 44 | \label{tab:helpvars} \\ 45 | \hline 46 | VARIABLE & DESCRIPTION (VALUES) & NOTE \\ \hline 47 | \variable{age} & age at baseline (in years) (range 19--60) & \\ \hline 48 | \variable{anysub} & use of any substance post-detox & see also \variable{daysanysub} 49 | \\ \hline 50 | \variable{cesd} & Center for Epidemiologic Studies Depression scale (range 0--60, higher scores indicate more depressive symptoms) & \\ \hline 51 | \variable{d1} & how many times hospitalized for medical problems (lifetime) (range 0--100) & \\ \hline 52 | \variable{daysanysub} & time (in days) to first use of any substance post-detox (range 0--268) & see also \variable{anysubstatus} \\ \hline 53 | \variable{dayslink} & time (in days) to linkage to primary care (range 0--456) & see also \variable{linkstatus} 54 | \\ \hline 55 | \variable{drugrisk} & Risk-Assessment Battery (RAB) drug risk score (range 0--21) & see also \variable{sexrisk} 56 | \\ \hline 57 | \variable{e2b} & number of times in past 6 months entered a detox program (range 1--21) & \\ \hline 58 | \variable{female} & gender of respondent (0=male, 1=female) & 59 | \\ \hline 60 | \variable{g1b} & experienced serious thoughts of suicide (last 30 days, values 0=no, 1=yes) & 61 | \\ \hline 62 | \variable{homeless} & 1 or more nights on the street or shelter in past 6 months (0=no, 1=yes) & 63 | \\ \hline 64 | \variable{i1} & average number of drinks (standard units) consumed per day (in the past 30 days, range 0--142) & see also \variable{i2} 65 | \\ \hline 66 | \variable{i2} & maximum number of drinks (standard units) consumed per day (in the past 30 days range 0--184) & see also \variable{i1} 67 | \\ \hline 68 | \variable{id} & random subject identifier (range 1--470) & 69 | \\ \hline 70 | \variable{indtot} & Inventory of Drug Use Consequences (InDUC) total score (range 4--45) & 71 | \\ \hline 72 | \variable{linkstatus} & post-detox linkage to primary care (0=no, 1=yes) & see also \variable{dayslink} 73 | \\ \hline 74 | \variable{mcs} & SF-36 Mental Component Score (range 7-62, higher scores are better) & see also \variable{pcs} 75 | \\ \hline 76 | \variable{pcs} & SF-36 Physical Component Score (range 14-75, higher scores are better) & see also \variable{mcs} 77 | \\ \hline 78 | \variable{pss\_fr} & perceived social supports (friends, range 0--14) & 79 | \\ \hline 80 | \variable{racegrp} & race/ethnicity (black, white, hispanic or other) & \\ \hline 81 | \variable{satreat} & any BSAS substance abuse treatment at baseline (0=no, 1=yes) & \\ \hline 82 | \variable{sex} & sex of respondent (male or female) & \\ \hline 83 | \variable{sexrisk} & Risk-Assessment Battery (RAB) sex risk score (range 0--21) & see also \variable{drugrisk} 84 | \\ \hline 85 | \variable{substance} & primary substance of abuse (alcohol, cocaine or heroin) & 86 | \\ \hline 87 | \variable{treat} & randomization group (randomize to HELP clinic, no or yes) & 88 | \\ \hline 89 | \end{longtable} 90 | \noindent 91 | Notes: Observed range is provided (at baseline) for continuous variables. 92 | 93 | -------------------------------------------------------------------------------- /StudentGuide/Introduction.Rnw: -------------------------------------------------------------------------------- 1 | 2 | \vspace*{-.5cm} 3 | 4 | In this reference book, we briefly review the commands and functions needed to analyze data from introductory and second courses in statistics. This is intended to complement the \emph{Start Teaching with R} and \emph{Start Modeling with R} books. 5 | 6 | Most of our examples will use data from the HELP (Health Evaluation and Linkage to Primary Care) study: a randomized clinical trial of a novel way to link at-risk subjects with primary care. More information on the dataset can be found in chapter \ref{sec:help}. 7 | 8 | 9 | Since the selection and order of topics can vary greatly from 10 | textbook to textbook and instructor to instructor, we have chosen to 11 | organize this material by the kind of data being analyzed. This should make it straightforward to find what you are looking for. 12 | Some data management skills are needed by students\cite{hort:2015}. A basic introduction to key idioms 13 | is provided in Chapter \ref{sec:manipulatingData}. 14 | 15 | \myindex{vignettes}% 16 | This work leverages initiatives undertaken by Project MOSAIC (\url{http://www.mosaic-web.org}), an NSF-funded effort to improve the teaching of statistics, calculus, science and computing in the undergraduate curriculum. In particular, we utilize the \pkg{mosaic} package, which was written to simplify the use of \R\ for introductory statistics courses, and the \pkg{mosaicData} package which includes a number of data sets. The \pkg{ggformula} package provides support for high quality graphics using the mosaic modeling language. A paper describing the mosaic approach to teaching statistics and data science can be found at \url{https://journal.r-project.org/archive/2017/RJ-2017-024}. A short summary of the \R\ commands needed to teach introductory statistics can be found in the mosaic package vignette: 17 | \url{https://cran.r-project.org/web/packages/mosaic}. 18 | 19 | Other related resources from Project MOSAIC may be helpful, including an annotated set of examples from a number of textbooks (see \url{https://cran.r-project.org/web/packages/mosaic/vignettes/mosaic-resources.html}). 20 | 21 | \myindex{installing packages}% 22 | \Rindex{install.packages()}% 23 | To use a package within R, it must be installed (one time), and loaded (each session). The \pkg{mosaic} package can be installed using the following commands: 24 | <>= 25 | install.packages("mosaic") # note the quotation marks 26 | @ 27 | \FoodForThought[-1.5cm]{\Rstudio\ features a simplified package installation tab (in the bottom right panel).} The {\tt \#} character is a comment in R, and all text after that on the current line is ignored. 28 | 29 | \myindex{loading packages}% 30 | \Rindex{library()}% 31 | \Rindex{require()}% 32 | Once the package is installed (one time only), it can be loaded by running the command: 33 | <>= 34 | library(mosaic) 35 | # require(mosaic) can also be used to load packages 36 | @ 37 | 38 | \FoodForThought[-3cm]{The \pkg{knitr}/\LaTeX\ system allows experienced users to combine \R\ and \LaTeX\ in the same document. The reward for learning this more complicated system is much finer control over the output format. But RMarkdown is much easier to learn and is adequate even for professional-level work.}% 39 | 40 | 41 | \myindex{reproducible analysis}% 42 | \myindex{markdown}% 43 | \myindex{knitr}% 44 | \marginnote[.4cm]{Using Markdown or \pkg{knitr}/\LaTeX\ requires that 45 | the \pkg{markdown} package be installed.}% 46 | The RMarkdown system provides a simple markup language and renders the results in PDF, Word, or HTML. This allows students to undertake their analyses using a workflow that facilitates ``reproducibility'' and avoids cut and paste errors. 47 | 48 | We typically introduce students to RMarkdown very early, requiring students to use it for assignments and reports\cite{baum:2014}. 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /StudentGuide/MOSAIC-StudentGuide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/MOSAIC-StudentGuide.pdf -------------------------------------------------------------------------------- /StudentGuide/OneCategorical.Rnw: -------------------------------------------------------------------------------- 1 | \section{Numerical summaries} 2 | 3 | \myindex{categorical variables} 4 | \myindex{contingency tables} 5 | \myindex{tables} 6 | 7 | \DiggingDeeper{The \emph{Start Teaching with R} companion book introduces the formula notation used throughout this book. See also \emph{Start Teaching with R} for the connections to statistical modeling.} 8 | 9 | The \function{tally()} function can be used to calculate 10 | counts, percentages and proportions for a categorical variable. 11 | 12 | \Rindex{tally()}% 13 | \Rindex{margins option}% 14 | <>= 15 | tally(~ homeless, data = HELPrct) 16 | tally(~ homeless, margins = TRUE, data = HELPrct) 17 | tally(~ homeless, format = "percent", data = HELPrct) 18 | tally(~ homeless, format = "proportion", data = HELPrct) 19 | @ 20 | 21 | \section{The binomial test} 22 | 23 | \myindex{binomial test}% 24 | \Rindex{binom.test()}% 25 | An exact confidence interval for a proportion (as well as a test of the null 26 | hypothesis that the population proportion is equal to a particular value [by default 0.5]) can be calculated 27 | using the \function{binom.test()} function. 28 | The standard \function{binom.test()} requires us to tabulate. 29 | <<>>= 30 | binom.test(209, 209 + 244) 31 | @ 32 | The \pkg{mosaic} package provides a formula interface that avoids the need to pre-tally 33 | the data. 34 | <>= 35 | result <- binom.test(~ (homeless == "homeless"), data = HELPrct) 36 | result 37 | @ 38 | 39 | As is generally the case with commands of this sort, 40 | there are a number of useful quantities available from 41 | the object returned by the function. 42 | <<>>= 43 | names(result) 44 | @ 45 | These can be extracted using the {\tt \$} operator or an extractor function. 46 | For example, the user can extract the confidence interval or p-value. 47 | \Rindex{confint()}% 48 | \Rindex{pval()}% 49 | \Rindex{print()}% 50 | <>= 51 | result$statistic 52 | confint(result) 53 | pval(result) 54 | @ 55 | \DiggingDeeper{Most of the objects in \R\ have a \function{print()} 56 | method. So when we get \code{result}, what we are seeing displayed in the console is 57 | \code{print(result)}. There may be a good deal of additional information 58 | lurking inside the object itself. 59 | 60 | In some situations, such as graphics, the object is returned \emph{invisibly}, so nothing prints. That avoids your having to look at a long printout not intended for human consumption. You can still assign the returned object to a variable and process it later, even if nothing shows up on the screen. This is sometimes helpful for \pkg{lattice} graphics functions.}% 61 | 62 | 63 | \section{The proportion test} 64 | 65 | A similar interval and test can be calculated using the function \function{prop.test()}. 66 | \Rindex{prop.test()}% 67 | \Rindex{correct option}% 68 | Here is a count of the number of people at each of the two levels of \variable{homeless} 69 | 70 | <>= 71 | tally(~ homeless, data = HELPrct) 72 | @ 73 | 74 | The \function{prop.test} function will carry out the calculations of the proportion test and report the result. 75 | 76 | \hfill 77 | 78 | <>= 79 | prop.test(~ (homeless == "homeless"), correct = FALSE, 80 | data = HELPrct) 81 | @ 82 | In this statement, prop.test is examining the \variable{homeless} variable in the same way that \function{tally} would. \Pointer{We write \code{homeless == "homeless"} to define unambiguously which proportion we are considering. We could also have written \code{homeless == "housed"}. } 83 | The \function{prop.test} function can also work directly with numerical counts, the way \function{binom.test()} does. 84 | \InstructorNote{\function{prop.test()} calculates a Chi-squared statistic. 85 | Most introductory texts use a $z$-statistic. They are mathematically equivalent in terms of inferential statements, but you may need to address the discrepancy with your students.}% 86 | <<>>= 87 | prop.test(209, 209 + 244, correct = FALSE) 88 | @ 89 | 90 | \section{Goodness of fit tests} 91 | 92 | A variety of goodness of fit tests can be calculated against a reference distribution. For the HELP data, we could test the null hypothesis that there is an equal proportion of subjects in each substance abuse group back in the original populations. 93 | 94 | 95 | <>= 96 | tally(~ substance, format = "percent", data = HELPrct) 97 | observed <- tally(~ substance, data = HELPrct) 98 | observed 99 | @ 100 | 101 | \Caution[-1cm]{In addition to the \option{format} option, there is an option \option{margins} to include marginal totals in the table. The default in \function{tally} is \option{margins = FALSE}. Try it out!} 102 | \Rindex{chisq.test()}% 103 | <>= 104 | p <- c(1/3, 1/3, 1/3) # equivalent to rep(1/3, 3) 105 | chisq.test(observed, p = p) 106 | total <- sum(observed) 107 | total 108 | expected <- total*p 109 | expected 110 | @ 111 | 112 | We can also calculate the $\chi^2$ statistic manually, as a function of observed and expected values. 113 | 114 | \Rindex{sum()}% 115 | \Rindex{pchisq()}% 116 | <>= 117 | chisq <- sum((observed - expected)^2/(expected)) 118 | chisq 119 | 1 - pchisq(chisq, df = 2) 120 | @ 121 | \FoodForThought[-2cm]{The \function{pchisq} function calculates the probability that a $\chi^2$ random variable with \function{df} degrees is freedom is less than or equal to a given value. Here we calculate the complement to find the area to the right of the observed Chi-square statistic.}% 122 | 123 | It may be helpful to consult a graph of the statistic, where the shaded area represents the value to the right of the observed value. 124 | 125 | \Rindex{gf\_dist()}% 126 | <>= 127 | gf_dist("chisq", df = 2, fill = ~ (x > 9.31), geom = "area") 128 | @ 129 | 130 | 131 | Alternatively, the \pkg{mosaic} package provides a version of \function{chisq.test()} with more verbose output. 132 | \Rindex{xchisq.test()}% 133 | <<>>= 134 | xchisq.test(observed, p = p) 135 | @ 136 | \FoodForThought[-1.5cm]{\code{x} in \function{xchisq.test} stands for eXtra.} 137 | 138 | \FoodForThought{Objects in the workspace are listed in the {\sc Environment} tab in \RStudio. If you want to clean up that listing, remove objects that are no longer needed with \function{rm}.} 139 | <<>>= 140 | # clean up variables no longer needed 141 | rm(observed, p, total, chisq) 142 | @ 143 | -------------------------------------------------------------------------------- /StudentGuide/Power.Rnw: -------------------------------------------------------------------------------- 1 | \vspace*{-.7cm} 2 | While not generally a major topic in introductory courses, power and sample size calculations 3 | help to reinforce key ideas in statistics. In this section, we will explore how \R\ can 4 | be used to undertake power calculations using analytic approaches. 5 | We consider a simple problem with two tests (t-test and 6 | sign test) of 7 | a one-sided comparison. 8 | 9 | We will compare the power of the sign test and the power of the test based on normal theory (one sample one sided t-test) assuming that $\sigma$ 10 | is known. 11 | Let $X_1, ..., X_{25}$ be i.i.d. $N(0.3, 1)$ (this is the alternate that we wish to calculate power for). Consider testing the null hypothesis $H_0: \mu=0$ versus $H_A: \mu>0$ at significance level $\alpha=.05$. 12 | 13 | \section{Sign test} 14 | 15 | We start by calculating the Type I error rate for the sign test. Here we want to 16 | reject when the number of positive values is large. Under the null hypothesis, this is 17 | distributed as a Binomial random variable with n=25 trials and p=0.5 probability of being 18 | a positive value. Let's consider values between 15 and 19. 19 | 20 | <>= 21 | xvals <- 15:19 22 | probs <- 1 - pbinom(xvals, size = 25, prob = 0.5) 23 | cbind(xvals, probs) 24 | qbinom(.95, size = 25, prob = 0.5) 25 | @ 26 | So we see that if we decide to reject when the number of positive values is 27 | 17 or larger, we will have an $\alpha$ level of \Sexpr{round(1-pbinom(16, 25, 0.5), 3)}, 28 | which is near the nominal value in the problem. 29 | 30 | We calculate the power of the sign test as follows. The probability that $X_i > 0$, given that $H_A$ is true is given by: 31 | <>= 32 | 1 - pnorm(0, mean = 0.3, sd = 1) 33 | @ 34 | We can view this graphically using the command: 35 | \begin{center} 36 | <>= 37 | xpnorm(0, mean = 0.3, sd = 1, lower.tail = FALSE) 38 | @ 39 | \end{center} 40 | The power under the alternative is equal to the probability of getting 17 or more positive values, 41 | given that $p=0.6179$: 42 | 43 | \Rindex{pbinom()}% 44 | <>= 45 | 1 - pbinom(16, size = 25, prob = 0.6179) 46 | @ 47 | The power is modest at best. 48 | 49 | \section{T-test} 50 | 51 | We next calculate the power of the test based on normal theory. To keep the comparison 52 | fair, we will set our $\alpha$ level equal to 0.05388. 53 | 54 | <>= 55 | alpha <- 1 - pbinom(16, size = 25, prob = 0.5) 56 | alpha 57 | @ 58 | 59 | First we find the rejection region. 60 | \Rindex{qnorm()}% 61 | \Rindex{xqnorm()}% 62 | <>= 63 | n <- 25 64 | sigma <- 1 # given 65 | stderr <- sigma/sqrt(n) 66 | zstar <- xqnorm(1 - alpha, mean = 0, sd = 1) 67 | zstar 68 | crit <- zstar * stderr 69 | crit 70 | @ 71 | 72 | 73 | \noindent 74 | Therefore, we reject for observed means greater than \Sexpr{round(crit,3)}. 75 | 76 | To calculate the power of this one-sided test we find the probability 77 | under the alternative hypothesis 78 | to the right of this cutoff. 79 | 80 | <<>>= 81 | power <- 1 - pnorm(crit, mean = 0.3, sd = stderr) 82 | power 83 | @ 84 | 85 | The power of the test based on normal theory is \Sexpr{round(power,3)}. 86 | To provide a check (or for future calculations of this sort) we can use the 87 | \function{power.t.test()} function. 88 | \Rindex{power.t.test()}% 89 | <<>>= 90 | power.t.test(n = 25, delta = .3, sd = 1, sig.level = alpha, 91 | alternative = "one.sided", 92 | type = "one.sample")$power 93 | @ 94 | 95 | This analytic (formula-based approach) yields a similar estimate to the value that we calculated directly. 96 | 97 | Overall, we see that the t-test has higher power than the sign test, if the underlying 98 | data are truly normal. \FoodForThought{Calculating power empirically 99 | demonstrates the power of simulations.} 100 | \begin{problem} 101 | \label{prob:power1}% 102 | Find the power of a two-sided two-sample t-test where both distributions 103 | are approximately normally distributed with the same standard deviation, but the group differ by 50\% of the standard deviation. Assume that there are 104 | \Sexpr{n} 105 | observations per group and an alpha level of \Sexpr{alpha}. 106 | \end{problem} 107 | \begin{solution} 108 | <>= 109 | n <- 100 110 | alpha <- 0.01 111 | @ 112 | <>= 113 | n 114 | alpha 115 | power.t.test(n = n, delta = .5, sd = 1, sig.level = alpha) 116 | @ 117 | \end{solution} 118 | \begin{problem} 119 | Find the sample size needed to have 90\% power for a two group t-test 120 | where the true 121 | difference between means is 25\% of the standard deviation in the groups 122 | (with $\alpha=0.05$). 123 | \end{problem} 124 | \begin{solution} 125 | <>= 126 | power.t.test(delta = .25, sd = 1, sig.level = alpha, power = 0.90) 127 | @ 128 | \end{solution} 129 | 130 | -------------------------------------------------------------------------------- /StudentGuide/ProbabilityDistributions.Rnw: -------------------------------------------------------------------------------- 1 | 2 | 3 | \label{sec:DiscreteDistributions} 4 | \label{sec:probability} 5 | \myindex{random variables}% 6 | 7 | \R\ can calculate quantities related to probability distributions of all types. 8 | It is straightforward to generate 9 | random samples from these distributions, which can be used 10 | for simulation and exploration. 11 | <>= 12 | xpnorm(1.96, mean = 0, sd = 1) # P(Z < 1.96) 13 | @ 14 | \Rindex{qnorm()}% 15 | \Rindex{dnorm()}% 16 | \Rindex{pnorm()}% 17 | \Rindex{xpnorm()}% 18 | \Rindex{rnorm()}% 19 | \Rindex{integrate()}% 20 | <<>>= 21 | # value which satisfies P(Z < z) = 0.975 22 | qnorm(.975, mean = 0, sd = 1) 23 | integrate(dnorm, -Inf, 0) # P(Z < 0) 24 | @ 25 | 26 | A similar display is available for the F distribution. 27 | 28 | <<>>= 29 | xpf(3, df1 = 4, df2 = 20) 30 | @ 31 | 32 | The following table displays the basenames for probability distributions 33 | available within base \R. These functions can be prefixed by {\tt d} to 34 | find the density function for the distribution, {\tt p} to find the 35 | cumulative distribution function, {\tt q} to find quantiles, and {\tt r} to 36 | generate random draws. For example, to find the density function of an exponential 37 | random variable, use the command \function{dexp()}. 38 | The \function{qDIST()} function is the inverse of the 39 | \function{pDIST()} function, for a given basename {\tt DIST}. 40 | \begin{center} 41 | \begin{tabular}{|c|c|c|} \hline 42 | Distribution & Basename \\ \hline 43 | Beta & {\tt beta} \\ 44 | binomial & {\tt binom} \\ 45 | Cauchy & {\tt cauchy} \\ 46 | chi-square & {\tt chisq} \\ 47 | exponential & {\tt exp} \\ 48 | F & {\tt f} \\ 49 | gamma & {\tt gamma} \\ 50 | geometric & {\tt geom} \\ 51 | hypergeometric & {\tt hyper} \\ 52 | logistic & {\tt logis} \\ 53 | lognormal & {\tt lnorm} \\ 54 | negative binomial & {\tt nbinom} \\ 55 | normal & {\tt norm} \\ 56 | Poisson & {\tt pois} \\ 57 | Student's t & {\tt t} \\ 58 | Uniform & {\tt unif} \\ 59 | Weibull & {\tt weibull} \\ \hline 60 | \end{tabular} 61 | \end{center} 62 | \DiggingDeeper{The \function{gf\_fitdistr()} facilitates estimation 63 | of parameters for many distributions.} 64 | The \function{gf\_dist()} can be used to display distributions in a variety of ways. 65 | \Rindex{gf\_dist()}% 66 | <>= 67 | gf_dist('norm', mean = 100, sd = 10, kind = 'cdf') 68 | @ 69 | <>= 70 | gf_dist('exp', kind = 'histogram', xlab = "x") 71 | @ 72 | Note that this sets the rate parameter to 1 by default and is equivalent to the following command. 73 | <>= 74 | gf_dist('exp', rate = 1, kind = 'histogram', xlab = "x") 75 | @ 76 | 77 | <>= 78 | gf_dist('binom', size = 25, prob = 0.25, xlim = c(-1, 26)) 79 | @ 80 | 81 | Multiple distributions can be plotted on the same plot. 82 | \Rindex{fill option}% 83 | \Rindex{cut()}% 84 | \Rindex{gf\_labs()}% 85 | \Rindex{geom option}% 86 | \Rindex{pch option}% 87 | <>= 88 | gf_dist("norm", mean = 50 * .3, sd = sqrt(50 * .3 * .7), 89 | fill = ~ cut(x, c(-Inf, 15 - 3, 15 + 3, Inf)), geom = "area") %>% 90 | gf_dist("binom", size = 50, prob = .3, col = "black", 91 | pch = 16) %>% 92 | gf_labs(fill = "Intervals") 93 | @ 94 | 95 | The \function{gf\_fun()} function can be used to plot an arbitrary 96 | function (in this case an exponential random variable). 97 | 98 | \Rindex{makeFun()}% 99 | \Rindex{rexp()}% 100 | \Rindex{gf\_histogram()}% 101 | \Rindex{binwidth option}% 102 | \Rindex{center option}% 103 | \Rindex{gf\_fun()}% 104 | \Rindex{color option}% 105 | \Rindex{size option}% 106 | \Rindex{xlim option}% 107 | <>= 108 | f <- makeFun(2 * exp(-2 * x) ~ x) # exponential with rate parameter 2 109 | x <- rexp(1000, rate = 2) 110 | gf_dhistogram(~ x, binwidth = 0.2, center = 0.1) %>% 111 | gf_fun(f(x) ~ x, color = "red", size = 2, xlim = c(0, 3)) 112 | @ 113 | \begin{problem} 114 | Generate a sample of 1000 exponential random variables with rate parameter 115 | equal to 2, and calculate the mean of those variables. 116 | \end{problem} 117 | \begin{solution} 118 | <>= 119 | x <- rexp(1000, rate = 2) 120 | mean(x) 121 | @ 122 | \end{solution} 123 | 124 | \begin{problem} 125 | Find the median of the random variable X, if it is exponentially distributed 126 | with rate parameter 10. 127 | \end{problem} 128 | \begin{solution} 129 | <>= 130 | qexp(.5, rate = 10) 131 | @ 132 | \end{solution} 133 | -------------------------------------------------------------------------------- /StudentGuide/RBook.sty: -------------------------------------------------------------------------------- 1 | 2 | \ProvidesPackage{RBook}[2013/04/04 Mosaic R Books style] 3 | 4 | \makeatletter 5 | \def\IfClass#1#2#3{\@ifundefined{opt@#1.cls}{#3}{#2}} 6 | \makeatother 7 | \IfClass{tufte-book}{\setcounter{secnumdepth}{3}}{\relax} 8 | \IfClass{tufte-book}{\def\subsubsection#1{\newthought{#1}}}{} 9 | 10 | \RequirePackage{import} 11 | \RequirePackage{graphicx} 12 | \RequirePackage{alltt} 13 | \RequirePackage{mparhack} 14 | \RequirePackage{xstring} 15 | 16 | \RequirePackage{etoolbox} 17 | \RequirePackage{multicol} 18 | \RequirePackage{xcolor} 19 | \RequirePackage{framed} 20 | \RequirePackage{hyperref} 21 | \RequirePackage{fancyhdr} 22 | 23 | \RequirePackage{probstat} 24 | \RequirePackage[answerdelayed,exercisedelayed,lastexercise,chapter]{problems} 25 | \RequirePackage{longtable} 26 | \RequirePackage{language} 27 | 28 | \RequirePackage{tikz} 29 | \usetikzlibrary{shadows} 30 | \usetikzlibrary{decorations} 31 | \usetikzlibrary{shapes.multipart} 32 | \usetikzlibrary{shapes.symbols} 33 | \usetikzlibrary{shapes.misc} 34 | \usetikzlibrary{shapes.geometric} 35 | 36 | \RequirePackage[utf8]{inputenc} 37 | \RequirePackage{underscore} 38 | 39 | \newcommand{\mymarginpar}[1]{% 40 | \vadjust{\smash{\llap{\parbox[t]{\marginparwidth}{#1}\kern\marginparsep}}}} 41 | 42 | \newcommand{\tikzNote}[3]{% 43 | \marginpar[% 44 | \hspace*{0.5in} 45 | \parbox{1.2in}{\begin{tikzpicture} 46 | \node at (0,0) [#3] 47 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 48 | \end{tikzpicture}} 49 | ]{% 50 | \parbox{1.2in}{\begin{tikzpicture} 51 | \node at (0,0) [#3] 52 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 53 | \end{tikzpicture}}}\ignorespaces 54 | }% 55 | 56 | \renewcommand{\tikzNote}[4][0pt]{ 57 | \marginnote[#1]{ 58 | \textsc{#2} 59 | 60 | \noindent #3 61 | }% 62 | } 63 | 64 | %\newcommand{\InstructorNote}[3][]{% 65 | %\tikzNote[#1]{#2}{#3}{double copy shadow={opacity=.5},tape,fill=blue!10,draw=blue,thick} 66 | %} 67 | 68 | \newcommand{\InstructorNote}[2][0pt]{% 69 | \tikzNote[#1]{}{#2}{tape,fill=blue!10,draw=blue,thick}% 70 | } 71 | 72 | \newcommand{\DiggingDeeper}[2][0pt]{% 73 | \tikzNote[#1]{\centerline{Digging Deeper}}{#2}{tape,fill=blue!10,draw=blue,thick}% 74 | } 75 | 76 | 77 | \newcommand{\TeachingTip}[2][0pt]{% 78 | \tikzNote[#1]{\centerline{Teaching Tip}}{#2}{tape,fill=blue!10,draw=blue,thick}% 79 | } 80 | 81 | 82 | \newcommand{\FoodForThought}[2][0pt]{% 83 | \tikzNote[#1]{}{#2}{rectangle,fill=green!10,draw=green,thick}% 84 | } 85 | 86 | \newcommand{\SuggestionBox}[2][0pt]{% 87 | \tikzNote[#1]{\centerline{Suggestion Box}}{#2}{rectangle,fill=green!10,draw=green,thick}% 88 | } 89 | 90 | \newcommand{\Caution}[2][0pt]{% 91 | \tikzNote[#1]{\centerline{Caution!}}{#2}{chamfered rectangle,fill=red!10,draw=red,thick}% 92 | } 93 | 94 | \newcommand{\Pointer}[2][0pt]{% 95 | \tikzNote[#1]{\centerline{More Info}}{#2}{}% 96 | } 97 | 98 | \newcommand{\Note}[2][0pt]{% 99 | \tikzNote[#1]{\centerline{Note}}{#2}{}% 100 | } 101 | 102 | \newcommand{\BlankNote}[1][0pt]{% 103 | \tikzNote[#1]{}{}{}% 104 | } 105 | 106 | 107 | 108 | \newcounter{examplenum}[chapter] 109 | \newenvironment{example}[1][\relax]{ 110 | \refstepcounter{examplenum} 111 | \textbf{Example \thechapter.\arabic{examplenum}.{#1}} 112 | }{% 113 | \hfill {\Large $\diamond$} 114 | %\centerline{\rule{5in}{.5pt}} 115 | } 116 | 117 | 118 | \newcounter{myenumi} 119 | \newcommand{\saveenumi}{\setcounter{myenumi}{\value{enumi}}} 120 | \newcommand{\reuseenumi}{\setcounter{enumi}{\value{myenumi}}} 121 | 122 | \newcommand{\cran}{\href{http://www.R-project.org/}{CRAN}} 123 | 124 | %%%%%%%% tufte-book work-arounds %%%%%%%%% 125 | \makeatletter 126 | \newenvironment{widestuff}% 127 | { 128 | \hspace*{-2ex}\begin{minipage}{\@tufte@fullwidth}}% 129 | {\end{minipage}} 130 | \makeatother 131 | 132 | %%%%%%%% Some R Stuff %%%%%%%%%%%%%%% 133 | \newcommand{\rterm}[1]{\textbf{#1}} 134 | \def\R{{\sf R}} 135 | \def\Rstudio{{\sf RStudio}} 136 | \def\RStudio{{\sf RStudio}} 137 | \def\term#1{\textbf{#1}} 138 | \def\tab#1{{\sf #1}} 139 | 140 | %%%%%%%%%%%%% some boxed elements %%%%%%%%%%%%%%%% 141 | 142 | \newlength{\tempfmlength} 143 | \newsavebox{\fmbox} 144 | \newenvironment{fmpage}[1] 145 | { 146 | \medskip 147 | \setlength{\tempfmlength}{#1} 148 | \begin{lrbox}{\fmbox} 149 | \begin{minipage}{#1} 150 | \vspace*{.02\tempfmlength} 151 | \hfill 152 | \begin{minipage}{.95 \tempfmlength}} 153 | {\end{minipage}\hfill 154 | \vspace*{.015\tempfmlength} 155 | \end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}} 156 | \medskip 157 | } 158 | 159 | \newenvironment{boxedText}[1][.98\textwidth]% 160 | {% 161 | \begin{center} 162 | \begin{fmpage}{#1} 163 | }% 164 | {% 165 | \end{fmpage} 166 | \end{center} 167 | } 168 | 169 | \newenvironment{boxedTable}[2][tbp]% 170 | {% 171 | \begin{table}[#1] 172 | \refstepcounter{table} 173 | \begin{center} 174 | \begin{fmpage}{.98\textwidth} 175 | \begin{center} 176 | \sf \large Box~\expandafter\thetable. #2 177 | \end{center} 178 | \medskip 179 | }% 180 | {% 181 | \end{fmpage} 182 | \end{center} 183 | \end{table} % need to do something about exercises that follow boxedTable 184 | } 185 | 186 | %%% indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 187 | %\newcommand{\printindex}[1]{\relax} 188 | %\newcommand{\indexchap}[1]{\relax} 189 | %\RequirePackage{amsmidx} 190 | \RequirePackage{makeidx} 191 | 192 | \newcommand{\exampleidx}[1]{{\it #1}} 193 | \newcommand{\defidx}[1]{{\bf #1}} 194 | \newcommand{\mainidx}[1]{{\bf #1}} 195 | \newcommand{\probidx}[1]{{{\underline{#1}}}} 196 | \makeindex 197 | %\makeindex{Rindex} 198 | %\makeindex{mainIndex} 199 | %\newcommand{\Rindex}[1]{\index{Rindex}{#1@\texttt{#1}}} 200 | %\newcommand{\myindex}[1]{\index{mainIndex}{#1}} 201 | %\newcommand{\mathindex}[1]{\index{mainIndex}{$#1$}} 202 | \newcommand{\Rindex}[1]{\index{#1@\texttt{#1}}} 203 | \newcommand{\myindex}[1]{\index{#1}} 204 | \newcommand{\mathindex}[1]{\index{$#1$}} 205 | 206 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 207 | %%%% probably don't need this --rjp 208 | \makeatletter 209 | \newcommand\gobblepar{% 210 | \@ifnextchar\par% 211 | {\expandafter\gobblepar\@gobble}% 212 | {}} 213 | \makeatother 214 | 215 | \pagestyle{fancy} 216 | 217 | %% allow more of page to be used for a figure or table 218 | \renewcommand{\textfraction}{0.05} 219 | \renewcommand{\topfraction}{0.8} 220 | \renewcommand{\bottomfraction}{0.8} 221 | \renewcommand{\floatpagefraction}{0.75} 222 | \setcounter{topnumber}{4} 223 | \setcounter{bottomnumber}{4} 224 | \setcounter{totalnumber}{8} 225 | 226 | -------------------------------------------------------------------------------- /StudentGuide/RBooks.sty: -------------------------------------------------------------------------------- 1 | \documentclass[open-any,12pt]{tufte-book} 2 | \setcounter{secnumdepth}{3} 3 | \usepackage{import} 4 | \usepackage{graphicx} 5 | 6 | \usepackage{alltt} 7 | \usepackage{mparhack} 8 | \usepackage{xstring} 9 | 10 | \usepackage{etoolbox} 11 | \usepackage{multicol} 12 | \usepackage{xcolor} 13 | \usepackage{framed} 14 | \usepackage{hyperref} 15 | %%%%%%%%%%%% Things Danny omitted %%%%%%%%%%%%%%%%%%%%% 16 | \usepackage{fancyhdr} 17 | 18 | %\newdimen\Rwidth 19 | %\Rwidth=\textwidth 20 | 21 | %\usepackage[margin=.5in,outer=1.5in,inner=.9in,includehead,includefoot,paperwidth=7.25in,paperheight=9.5in]{geometry} 22 | \usepackage{probstat} 23 | \usepackage[shownotes]{authNote} 24 | % \usepackage[hidenotes]{authNote} 25 | \usepackage[answerdelayed,exercisedelayed,lastexercise,chapter]{problems} 26 | \usepackage{longtable} 27 | \usepackage{language} 28 | 29 | \usepackage{tikz} 30 | \usetikzlibrary{shadows} 31 | \usetikzlibrary{decorations} 32 | \usetikzlibrary{shapes.multipart} 33 | \usetikzlibrary{shapes.symbols} 34 | \usetikzlibrary{shapes.misc} 35 | \usetikzlibrary{shapes.geometric} 36 | 37 | \newcommand{\mymarginpar}[1]{% 38 | \vadjust{\smash{\llap{\parbox[t]{\marginparwidth}{#1}\kern\marginparsep}}}} 39 | 40 | \newcommand{\tikzNote}[3]{% 41 | \marginpar[% 42 | \hspace*{0.5in} 43 | \parbox{1.2in}{\begin{tikzpicture} 44 | \node at (0,0) [#3] 45 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 46 | \end{tikzpicture}} 47 | ]{% 48 | \parbox{1.2in}{\begin{tikzpicture} 49 | \node at (0,0) [#3] 50 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 51 | \end{tikzpicture}}} 52 | } 53 | 54 | \renewcommand{\tikzNote}[3]{ 55 | \marginnote{ 56 | \textsc{#1} 57 | 58 | #2 59 | } 60 | } 61 | 62 | \newcommand{\InstructorNote}[2][\relax]{% 63 | \tikzNote{#1}{#2}{double copy shadow={opacity=.5},tape,fill=blue!10,draw=blue,thick} 64 | } 65 | 66 | \renewcommand{\InstructorNote}[2][\relax]{% 67 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 68 | } 69 | 70 | \newcommand{\DiggingDeeper}[2][\centerline{Digging Deeper}]{% 71 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 72 | } 73 | 74 | 75 | \newcommand{\TeachingTip}[2][\centerline{Teaching Tip}]{% 76 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 77 | } 78 | 79 | 80 | \newcommand{\FoodForThought}[2][\relax]{% 81 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 82 | } 83 | 84 | \newcommand{\SuggestionBox}[2][\centerline{Suggestion Box}]{% 85 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 86 | } 87 | 88 | \newcommand{\Caution}[2][\centerline{Caution!}]{% 89 | \tikzNote{#1}{#2}{chamfered rectangle,fill=red!10,draw=red,thick} 90 | } 91 | 92 | \newcounter{examplenum}[chapter] 93 | \newenvironment{example}[1][\relax]{ 94 | \refstepcounter{examplenum} 95 | \textbf{Example \thechapter.\arabic{examplenum}.{#1}} 96 | }{% 97 | \hfill {\Large $\diamond$} 98 | %\centerline{\rule{5in}{.5pt}} 99 | } 100 | 101 | \usepackage[utf8]{inputenc} 102 | 103 | \newcounter{myenumi} 104 | \newcommand{\saveenumi}{\setcounter{myenumi}{\value{enumi}}} 105 | \newcommand{\reuseenumi}{\setcounter{enumi}{\value{myenumi}}} 106 | 107 | \newcommand{\cran}{\href{http://www.R-project.org/}{CRAN}} 108 | %%%%%%%% Some R Stuff %%%%%%%%%%%%%%% 109 | \newcommand{\rterm}[1]{\textbf{#1}} 110 | \def\R{{\sf R}} 111 | \def\Rstudio{{\sf RStudio}} 112 | \def\RStudio{{\sf RStudio}} 113 | \def\term#1{\textbf{#1}} 114 | \def\tab#1{{\sf #1}} 115 | 116 | %%%%%%%%%%%%% some boxed elements %%%%%%%%%%%%%%%% 117 | 118 | \newlength{\tempfmlength} 119 | \newsavebox{\fmbox} 120 | \newenvironment{fmpage}[1] 121 | { 122 | \medskip 123 | \setlength{\tempfmlength}{#1} 124 | \begin{lrbox}{\fmbox} 125 | \begin{minipage}{#1} 126 | \vspace*{.02\tempfmlength} 127 | \hfill 128 | \begin{minipage}{.95 \tempfmlength}} 129 | {\end{minipage}\hfill 130 | \vspace*{.015\tempfmlength} 131 | \end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}} 132 | \medskip 133 | } 134 | 135 | \newenvironment{boxedText}[1][.98\textwidth]% 136 | {% 137 | \begin{center} 138 | \begin{fmpage}{#1} 139 | }% 140 | {% 141 | \end{fmpage} 142 | \end{center} 143 | } 144 | 145 | \newenvironment{boxedTable}[2][tbp]% 146 | {% 147 | \begin{table}[#1] 148 | \refstepcounter{table} 149 | \begin{center} 150 | \begin{fmpage}{.98\textwidth} 151 | \begin{center} 152 | \sf \large Box~\expandafter\thetable. #2 153 | \end{center} 154 | \medskip 155 | }% 156 | {% 157 | \end{fmpage} 158 | \end{center} 159 | \end{table} % need to do something about exercises that follow boxedTable 160 | } 161 | 162 | %%% indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 163 | %%% 164 | \newcommand{\printindex}[1]{\relax} 165 | \newcommand{\indexchap}[1]{\relax} 166 | \usepackage{amsmidx} 167 | \newcommand{\exampleidx}[1]{{\it #1}} 168 | \newcommand{\defidx}[1]{{\bf #1}} 169 | \newcommand{\mainidx}[1]{{\bf #1}} 170 | \newcommand{\probidx}[1]{{{\underline{#1}}}} 171 | \makeindex{Rindex} 172 | \makeindex{mainIndex} 173 | \newcommand{\Rindex}[1]{\index{Rindex}{#1@\texttt{#1}}} 174 | \newcommand{\myindex}[1]{\index{mainIndex}{#1}} 175 | \newcommand{\mathindex}[1]{\index{mainIndex}{$#1$}} 176 | 177 | 178 | \pagestyle{fancy} 179 | 180 | 181 | \begin{document} 182 | 183 | \end{document} 184 | -------------------------------------------------------------------------------- /StudentGuide/StudentGuide-Printed-Form.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/StudentGuide-Printed-Form.pdf -------------------------------------------------------------------------------- /StudentGuide/StudentGuide-Printed-Form.tex: -------------------------------------------------------------------------------- 1 | \documentclass{book} 2 | \usepackage{pdfpages} 3 | \usepackage[paperheight=8in,paperwidth=6in,twoside,inner=.75in,top=.005in,bottom=.005in]{geometry} 4 | 5 | \begin{document} 6 | \includepdf[nup=1x1,pages=1-3,trim=35 10 35 10,,offset=.2in 0in]{Compendium.pdf} 7 | \includepdf[nup=1x1,pages=4,trim=35 10 35 10,,offset=-1cm 0in]{Compendium.pdf} 8 | \includepdf[nup=1x1,pages=5-,trim=35 10 35 10,,offset=.2in 0in]{Compendium.pdf} 9 | \end{document} 10 | 11 | 12 | -------------------------------------------------------------------------------- /StudentGuide/Studentguide2015-10-25.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Studentguide2015-10-25.pdf -------------------------------------------------------------------------------- /StudentGuide/Studentguide2015-11-09.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Studentguide2015-11-09.pdf -------------------------------------------------------------------------------- /StudentGuide/Studentguide2015-11-15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/Studentguide2015-11-15.pdf -------------------------------------------------------------------------------- /StudentGuide/SurvivalTime.Rnw: -------------------------------------------------------------------------------- 1 | \myindex{survival analysis}% 2 | \myindex{failure time analysis}% 3 | \myindex{time to event analysis}% 4 | Extensive support for survival (time to event) analysis is available within the 5 | \pkg{survival} package. 6 | 7 | \section{Kaplan-Meier plot} 8 | 9 | \myindex{Kaplan-Meier plot}% 10 | \Rindex{survfit()}% 11 | \Rindex{Surv()}% 12 | \Rindex{gf\_step()}% 13 | \Rindex{xlab option}% 14 | \Rindex{title option}% 15 | \Rindex{ylab option}% 16 | \Rindex{linetype option}% 17 | \begin{center} 18 | <>= 19 | library(survival) 20 | library(broom) 21 | fit <- survfit(Surv(dayslink, linkstatus) ~ treat, 22 | data = HELPrct) 23 | fit <- broom::tidy(fit) 24 | gf_step(fit, estimate ~ time, linetype = ~ strata, 25 | title = "Product-Limit Survival Estimates (time to linkage)", 26 | xlab = "time (in days)", ylab = "P(not linked)") 27 | @ 28 | \end{center} 29 | 30 | We see that the subjects in the treatment (Health Evaluation and Linkage to Primary Care clinic) were significantly more likely to 31 | link to primary care (less likely to ``survive'') than the control (usual care) group. 32 | 33 | \section{Cox proportional hazards model} 34 | \myindex{Cox proportional hazards model}% 35 | \myindex{proportional hazards model}% 36 | \Rindex{coxph()}% 37 | 38 | <>= 39 | library(survival) 40 | summary(coxph(Surv(dayslink, linkstatus) ~ age + substance, 41 | data = HELPrct)) 42 | @ 43 | 44 | Neither age nor substance group was significantly associated with linkage to primary care. 45 | 46 | -------------------------------------------------------------------------------- /StudentGuide/TwoCategorical.Rnw: -------------------------------------------------------------------------------- 1 | 2 | \section{Cross classification tables} 3 | \label{sec:cross} 4 | 5 | \myindex{cross classification tables}% 6 | \myindex{contingency tables}% 7 | \myindex{tables}% 8 | 9 | Cross classification (two-way or $R$ by $C$) tables can be constructed for 10 | two (or more) categorical variables. Here we consider the contingency table 11 | for homeless status (homeless one or more nights in the past 6 months or housed) 12 | and sex. 13 | 14 | <>= 15 | tally(~ homeless + sex, margins = FALSE, data = HELPrct) 16 | @ 17 | 18 | We can also calculate column percentages: 19 | \Rindex{tally()}% 20 | <>= 21 | tally(~ sex | homeless, margins = TRUE, format = "percent", 22 | data = HELPrct) 23 | @ 24 | 25 | We can calculate the odds ratio directly from the table: 26 | <>= 27 | OR <- (40/169)/(67/177) 28 | OR 29 | @ 30 | 31 | The 32 | \pkg{mosaic} package has a function which will calculate odds ratios: 33 | \Rindex{oddsRatio()}% 34 | <>= 35 | oddsRatio(tally(~ (homeless == "housed") + sex, margins = FALSE, 36 | data = HELPrct)) 37 | @ 38 | 39 | The \function{CrossTable()} function in the \pkg{gmodels} package also displays 40 | a cross classification table. 41 | 42 | \Rindex{CrossTable()}% 43 | <>= 44 | library(gmodels) 45 | with(HELPrct, CrossTable(homeless, sex, 46 | prop.r = FALSE, prop.chisq = FALSE, prop.t = FALSE)) 47 | @ 48 | 49 | Graphical summaries of cross classification tables may be helpful in visualizing 50 | associations. Mosaic plots are one example, where the total area (all observations) is proportional to one. 51 | \Caution{The jury is still out 52 | regarding the utility of mosaic plots (also known as eikosograms), 53 | due to their low data to ink ratio.\cite{Tufte:2001:Visual}% 54 | We have found them to be helpful to reinforce understanding of a two way contingency table.}% 55 | Here we see that males tend to be over-represented 56 | amongst the homeless subjects (as represented by the horizontal line which is higher for 57 | the homeless rather than the housed). 58 | \FoodForThought{The \function{mosaic()} function 59 | in the \pkg{vcd} package makes mosaic plots.} 60 | \Rindex{mosaic()}% 61 | \Rindex{vcd package}% 62 | \begin{center} 63 | <>= 64 | mytab <- tally(~ homeless + sex, margins = FALSE, 65 | data = HELPrct) 66 | vcd::mosaic(mytab) 67 | @ 68 | <>= 69 | vcd::mosaic(~ homeless + substance, data = HELPrct, 70 | shade = TRUE) #example with color 71 | @ 72 | \end{center} 73 | 74 | \section{Creating tables from summary statistics} 75 | 76 | Tables can be created from summary statistics using the \function{do} function. 77 | 78 | \Rindex{do()}% 79 | \Rindex{rbind()}% 80 | <<>>= 81 | HELPtable <- rbind( 82 | do(40) * data.frame(sex = "female", homeless = "homeless"), 83 | do(169) * data.frame(sex = "male", homeless = "homeless"), 84 | do(67) * data.frame(sex = "female", homeless = "housed"), 85 | do(177) * data.frame(sex = "male", homeless = "housed") 86 | ) 87 | tally(~ homeless + sex, data = HELPtable) 88 | @ 89 | 90 | \section{Chi-squared tests} 91 | 92 | \Rindex{chisq.test()}% 93 | <>= 94 | chisq.test(tally(~ homeless + sex, margins = FALSE, 95 | data = HELPrct), correct = FALSE) 96 | @ 97 | 98 | There is a statistically significant association found: it is unlikely that we would observe 99 | an association this strong if homeless status and sex were independent in the 100 | population. 101 | 102 | When a student finds a significant association, 103 | it's important for them to be able to interpret this in the context of the problem. 104 | The \function{xchisq.test()} function provides additional details (observed, expected, contribution to statistic, and residual) to help with this process. 105 | \FoodForThought{\code{x} is for eXtra.} 106 | 107 | \Rindex{xchisq.test()}% 108 | <>= 109 | xchisq.test(tally(~ homeless + sex, margins = FALSE, 110 | data = HELPrct), correct = FALSE) 111 | @ 112 | 113 | We observe that there are fewer homeless women, and more homeless men that would be expected. 114 | 115 | \section{Fisher's exact test} 116 | \myindex{Fisher's exact test}% 117 | 118 | An exact test can also be calculated. This is computationally straightforward for 2 by 2 119 | tables. Options to help constrain the size of the problem for larger tables exist 120 | (see \verb!?fisher.test()!). 121 | 122 | \DiggingDeeper{Note the different estimate of the odds ratio from that seen in section \ref{sec:cross}. 123 | The \function{fisher.test()} function uses a different estimator (and different interval based 124 | on the profile likelihood).} 125 | \Rindex{fisher.test()}% 126 | <>= 127 | fisher.test(tally(~ homeless + sex, margins = FALSE, 128 | data = HELPrct)) 129 | @ 130 | -------------------------------------------------------------------------------- /StudentGuide/TwoQuantitative.Rnw: -------------------------------------------------------------------------------- 1 | 2 | \section{Scatterplots} 3 | \myindex{scatterplots}% 4 | \myindex{lowess}% 5 | \myindex{smoothers}% 6 | \myindex{linearity}% 7 | 8 | We always encourage students to start any analysis by graphing their data. 9 | Here we augment a scatterplot 10 | of the CESD (a measure of depressive symptoms, higher scores indicate more symptoms) and the MCS (mental component score from the SF-36, where higher scores indicate better functioning) for female subjects 11 | with a lowess (locally weighted scatterplot smoother) line, using a circle 12 | as the plotting character and slightly thicker line. 13 | 14 | \InstructorNote{The lowess line can help to assess linearity of a relationship. This is added by specifying both points (using `p') and a lowess smoother.} 15 | \Rindex{gf\_point()}% 16 | \Rindex{shape option}% 17 | \Rindex{size option}% 18 | \Rindex{gf\_smooth()}% 19 | \begin{center} 20 | <>= 21 | Female <- filter(HELPrct, female == 1) 22 | gf_point(cesd ~ mcs, data = Female, shape = 1) %>% 23 | gf_smooth(se = FALSE, size = 2) 24 | @ 25 | \end{center} 26 | \DiggingDeeper{The \emph{Start Modeling with R} companion book will be helpful if you are unfamiliar with the 27 | modeling language. The \emph{Start Teaching with R} also provides useful guidance in getting started.} 28 | 29 | It's straightforward to plot something besides a character in a scatterplot. 30 | In this example, the \dataframe{USArrests} can be used to plot the association 31 | between murder and assault rates, with the state name displayed. 32 | \Rindex{rownames()}% 33 | \Rindex{gf\_text()}% 34 | \Rindex{label option}% 35 | \Rindex{size option}% 36 | <>= 37 | gf_text(Murder ~ Assault, 38 | label = ~ rownames(USArrests), 39 | data = USArrests) 40 | @ 41 | 42 | 43 | \vspace*{-1cm} 44 | 45 | \section{Correlation} 46 | 47 | 48 | Correlations can be calculated for a pair of variables, or for a matrix of variables. 49 | \myindex{correlation}% 50 | \Rindex{cor()}% 51 | <<>>= 52 | cor(cesd ~ mcs, data = Female) 53 | smallHELP <- select(Female, cesd, mcs, pcs) 54 | cor(smallHELP) 55 | @ 56 | \myindex{Pearson correlation}% 57 | \myindex{Spearman correlation}% 58 | 59 | By default, Pearson correlations are provided. Other variants (e.g., Spearman) can be specified using the 60 | \option{method} option. 61 | <<>>= 62 | cor(cesd ~ mcs, method = "spearman", data = Female) 63 | @ 64 | 65 | \section{Pairs plots} 66 | \myindex{pairs plot}% 67 | \myindex{scatterplot matrix}% 68 | 69 | A pairs plot (scatterplot matrix) can be calculated for each pair of a set of variables. 70 | \FoodForThought{The \pkg{GGally} package has support for more elaborate pairs plots.} 71 | \Rindex{ggpairs()}% 72 | <>= 73 | library(GGally) 74 | ggpairs(smallHELP) 75 | @ 76 | 77 | \section{Simple linear regression} 78 | 79 | \InstructorNote{We tend to introduce linear regression 80 | early in our courses, as a purely descriptive technique.} 81 | \myindex{linear regression}% 82 | \myindex{regression}% 83 | 84 | Linear regression models are described in detail in \emph{Start Modeling with R}. 85 | These use the same formula interface introduced previously for numerical and graphical 86 | summaries 87 | to specify the outcome 88 | and predictors. Here we consider fitting the model \model{\variable{cesd}}{\variable{mcs}}. 89 | 90 | 91 | \Rindex{lm()}% 92 | \Rindex{coef()}% 93 | <<>>= 94 | cesdmodel <- lm(cesd ~ mcs, data = Female) 95 | coef(cesdmodel) 96 | @ 97 | \InstructorNote{It's important to pick good names for modeling 98 | objects. Here the output of \function{lm} is saved as \code{cesdmodel}, 99 | which denotes that it is a regression model of depressive symptom 100 | scores.} 101 | 102 | To simplify the output, we turn off the option to display significance stars. 103 | \myindex{significance stars}% 104 | \Rindex{msummary()}% 105 | \Rindex{confint()}% 106 | \Rindex{rsquared()}% 107 | \Rindex{coef()}% 108 | <<>>= 109 | options(show.signif.stars = FALSE) 110 | coef(cesdmodel) 111 | msummary(cesdmodel) 112 | coef(summary(cesdmodel)) 113 | confint(cesdmodel) 114 | rsquared(cesdmodel) 115 | @ 116 | 117 | 118 | \Rindex{class()}% 119 | <>= 120 | class(cesdmodel) 121 | @ 122 | The return value from \function{lm()} is a linear model object. 123 | A number of functions can operate on these objects, as 124 | seen previously with \function{coef()}. 125 | The function \function{residuals()} returns a 126 | vector of the residuals. 127 | \Rindex{residuals()}% 128 | \FoodForThought{The function \function{residuals()} can be abbreviated 129 | \function{resid()}. Another useful function is \function{fitted()}, which 130 | returns a vector of predicted values.} 131 | 132 | 133 | \Rindex{density option}% 134 | \begin{center} 135 | <>= 136 | gf_histogram(~ residuals(cesdmodel), density = TRUE) 137 | @ 138 | \end{center} 139 | \Rindex{gf\_qq()}% 140 | \begin{center} 141 | <>= 142 | gf_qq(~ resid(cesdmodel)) 143 | @ 144 | \end{center} 145 | \Rindex{alpha option}% 146 | \Rindex{gf\_hline()}% 147 | \begin{center} 148 | <>= 149 | gf_point(resid(cesdmodel) ~ fitted(cesdmodel), 150 | alpha = 0.5, cex = 0.3, pch = 20) %>% 151 | gf_smooth(se = FALSE) %>% 152 | gf_hline(yintercept = 0) 153 | @ 154 | \end{center} 155 | 156 | The \function{mplot()} function can facilitate creating a variety of useful plots, including the same residuals vs. fitted scatterplots, by specifying the \option{which = 1} option. 157 | \Rindex{mplot()}% 158 | \Rindex{which option}% 159 | <>= 160 | mplot(cesdmodel, which = 1) 161 | @ 162 | 163 | It can also generate a normal quantile-quantile plot (\option{which = 2}), 164 | <>= 165 | mplot(cesdmodel, which = 2) 166 | @ 167 | 168 | \myindex{scale versus location}% 169 | scale vs.\,location, 170 | <>= 171 | mplot(cesdmodel, which = 3) 172 | @ 173 | 174 | \myindex{Cook's distance}% 175 | Cook's distance by observation number, 176 | <>= 177 | mplot(cesdmodel, which = 4) 178 | @ 179 | 180 | \myindex{leverage}% 181 | residuals vs.\,leverage, 182 | <>= 183 | mplot(cesdmodel, which = 5) 184 | @ 185 | 186 | and Cook's distance vs. leverage. 187 | <>= 188 | mplot(cesdmodel, which = 6) 189 | @ 190 | 191 | \myindex{prediction intervals}% 192 | \Rindex{gf\_lm()}% 193 | \Rindex{interval option}% 194 | Prediction intervals can be added to a plot using the \option{interval} option in \function{gf\_lm()}. 195 | \begin{center} 196 | <>= 197 | gf_point(cesd ~ mcs, data = HELPrct) %>% 198 | gf_lm(interval = "confidence", fill = "red") %>% 199 | gf_lm(interval = "prediction", fill = "navy") 200 | @ 201 | \end{center} 202 | 203 | \begin{problem} 204 | Using the \dataframe{HELPrct} dataset, fit a simple linear regression model 205 | predicting the number of drinks per day as a function of the mental 206 | component score. 207 | This model can be specified using the formula: 208 | \model{\variable{i1}}{\variable{mcs}}. 209 | Assess the distribution of the residuals for this model. 210 | \end{problem} 211 | 212 | -------------------------------------------------------------------------------- /StudentGuide/authNote.sty: -------------------------------------------------------------------------------- 1 | 2 | \NeedsTeXFormat{LaTeX2e}[1995/12/01] 3 | \ProvidesPackage{authNote}[2005/06/14 1.0 (RJP)] 4 | 5 | 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | % my package requirements 8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9 | 10 | \RequirePackage{ifthen} 11 | 12 | 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | % options and booleans for them 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | 17 | %\reversemarginpar % ?? 18 | 19 | %\smartqed % makes \qed print at the rightmargin 20 | 21 | \newboolean{shownotes} 22 | \setboolean{shownotes}{true} 23 | \DeclareOption{hidenotes}{\setboolean{shownotes}{false}} 24 | \DeclareOption{shownotes}{\setboolean{shownotes}{true}} 25 | \DeclareOption{hide}{\setboolean{shownotes}{false}} 26 | \DeclareOption{show}{\setboolean{shownotes}{true}} 27 | 28 | \newboolean{showhmm} 29 | \setboolean{showhmm}{true} 30 | \DeclareOption{hidehmm}{\setboolean{showhmm}{false}} 31 | \DeclareOption{showhmm}{\setboolean{showhmm}{true}} 32 | 33 | \newboolean{showopt} 34 | \setboolean{showopt}{true} 35 | \DeclareOption{hideopt}{\setboolean{showopt}{false}} 36 | \DeclareOption{showopt}{\setboolean{showopt}{true}} 37 | 38 | \newboolean{showold} 39 | \setboolean{showold}{false} 40 | \DeclareOption{showold}{\setboolean{showold}{true}} 41 | \DeclareOption{hideold}{\setboolean{showold}{false}} 42 | 43 | \DeclareOption{primary}{% 44 | \setboolean{showhmm}{true} 45 | \setboolean{showopt}{true} 46 | \setboolean{shownotes}{true} 47 | \setboolean{showold}{false} 48 | } 49 | 50 | \DeclareOption{secondary}{% 51 | \setboolean{showhmm}{false} 52 | \setboolean{showopt}{false} 53 | \setboolean{shownotes}{true} 54 | \setboolean{showold}{false} 55 | } 56 | 57 | \DeclareOption{clean}{% 58 | \setboolean{showhmm}{false} 59 | \setboolean{showopt}{false} 60 | \setboolean{shownotes}{false} 61 | \setboolean{showold}{false} 62 | } 63 | 64 | 65 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 66 | 67 | 68 | \ProcessOptions* 69 | 70 | 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 72 | %Translation Helps 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | 75 | %\def\transbox#1{{\textbf{#1}}} 76 | \def\transbox#1{{\textbf{#1}}} 77 | 78 | \def\hmm[#1]#2{\ifthenelse% 79 | {\boolean{showhmm}}% 80 | {\transbox{#2}\smallmarginpar{#1}{}}% 81 | {#2}% 82 | } 83 | 84 | \def\hmmok[#1]#2{% 85 | \ifthenelse{\boolean{showold}}% 86 | {\transbox{#2}\smallmarginpar{#1}}% 87 | {#2}% 88 | } 89 | 90 | \def\hmmOK[#1]#2{% 91 | \ifthenelse{\boolean{showold}}% 92 | {\transbox{#2}\smallmarginpar{#1}}% 93 | {#2}% 94 | } 95 | 96 | \newcommand{\options}[2]{% 97 | \ifthenelse{\boolean{showopt}}% 98 | %{\textbf{$\mathbf<$#1 $\mathbf\mid$ #2$\mathbf >$}\smallmarginpar{$<\mid>$}}% 99 | {% 100 | \smallmarginpar{$<$#1$\mid$#2$>$}% 101 | #1% 102 | }% 103 | {#1}% 104 | } 105 | 106 | 107 | \newcommand{\optionsok}[2]{% 108 | \ifthenelse{\boolean{showold}}% 109 | % {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 110 | {% 111 | \smallmarginpar{$<$#1$\mid$#2$>$}% 112 | #1% 113 | }% 114 | {#1}% 115 | } 116 | 117 | \newcommand{\optionsOk}[2]{% 118 | \ifthenelse{\boolean{showold}}% 119 | % {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 120 | {% 121 | \smallmarginpar{$<$#1$\mid$#2$>$}% 122 | #1% 123 | }% 124 | {#1}% 125 | } 126 | 127 | \newcommand{\optionsOne}[2]{% 128 | \ifthenelse{\boolean{showold}}% 129 | {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 130 | {#1}% 131 | } 132 | 133 | \newcommand{\optionsone}[2]{% 134 | \ifthenelse{\boolean{showold}}% 135 | {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 136 | {#1}% 137 | } 138 | 139 | \newcommand{\optionsTwo}[2]{% 140 | \ifthenelse{\boolean{showold}}% 141 | {\textbf{$\mathbf<$#2$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 142 | {#2}% 143 | } 144 | 145 | \newcommand{\optionstwo}[2]{% 146 | \ifthenelse{\boolean{showold}}% 147 | {\textbf{$\mathbf<$#2$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 148 | {#2}% 149 | } 150 | 151 | 152 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 153 | % authNote stuff 154 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 155 | \newtoks\tempTok 156 | \newcounter{noteNum}[section] 157 | \newwrite\noteFile% 158 | \immediate\openout\noteFile=\jobname.notes 159 | 160 | \long\def\saveNote#1{% 161 | \refstepcounter{noteNum}% 162 | \immediate\write\noteFile{\string\begingroup\string\bf }% 163 | \immediate\write\noteFile{\thesection .% 164 | \expandafter\arabic{noteNum}}% 165 | \immediate\write\noteFile{(p. \expandafter\thepage): }% 166 | \immediate\write\noteFile{\string\endgroup}% 167 | \tempTok={#1} 168 | \immediate\write\noteFile{\the\tempTok}% 169 | \immediate\write\noteFile{}% 170 | } 171 | 172 | 173 | \def\smallmarginpar#1{\marginpar[\hfill \tiny #1]{\raggedright \tiny #1 \hfill}} 174 | 175 | \long\def\saveNshowNote#1#2{% 176 | \saveNote{#2}% 177 | \ifthenelse{\boolean{shownotes}}{% 178 | \marginpar[\hfill {\tiny #1 179 | \thesection.\arabic{noteNum} $\rightarrow$}]% 180 | {{\tiny $\leftarrow$ \thesection.\arabic{noteNum} #1 \hfill}}% 181 | }{\relax}% 182 | } 183 | 184 | % to remove marginal notes (for submissions, etc) use below instead: 185 | 186 | \long\def\authNote#1{\saveNshowNote{}{#1}} 187 | \long\def\oldNote#1{\saveNshowNote{old}{old: #1}} 188 | \long\def\authNoted#1{% 189 | \ifthenelse{\boolean{showold}}% 190 | {\saveNshowNote{$\surd$}{(Done) #1}}% 191 | {\relax}% 192 | } 193 | 194 | \long\def\authNotedOld#1{\relax} 195 | 196 | 197 | \def\authNotes{% 198 | \ifthenelse{\boolean{shownotes}}{% 199 | %\section*{Author Notes} 200 | \begingroup 201 | \immediate\closeout\noteFile 202 | \parindent=0pt 203 | \input \jobname.notes 204 | \endgroup 205 | } 206 | {\relax} 207 | } 208 | 209 | 210 | -------------------------------------------------------------------------------- /StudentGuide/language.sty: -------------------------------------------------------------------------------- 1 | \ProvidesPackage{language} 2 | 3 | \RequirePackage{xstring} 4 | \RequirePackage{xcolor} 5 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 6 | % Looking for a consistent typography for language elements. 7 | 8 | \providecommand{\R}{} 9 | \renewcommand{\R}{\mbox{\sf{R}}} 10 | \providecommand{\RStudio}{} 11 | \renewcommand{\RStudio}{\mbox{\sf{R}Studio}} 12 | \providecommand{\Sage}{} 13 | \renewcommand{\Sage}{\mbox{\sf{Sage}}} 14 | 15 | \providecommand{\variable}[1]{} 16 | \renewcommand{\variable}[1]{{\color{green!50!black}\texttt{#1}}} 17 | \providecommand{\dataframe}[1]{} 18 | \renewcommand{\dataframe}[1]{{\color{blue!80!black}\texttt{#1}}} 19 | \providecommand{\function}[1]{} 20 | \renewcommand{\function}[1]{{\color{purple!75!blue}\texttt{\StrSubstitute{#1}{()}{}()}}} 21 | \providecommand{\option}[1]{} 22 | \renewcommand{\option}[1]{{\color{brown!80!black}\texttt{#1}}} 23 | \providecommand{\pkg}[1]{} 24 | \renewcommand{\pkg}[1]{{\color{red!80!black}\texttt{#1}}} 25 | \providecommand{\code}[1]{} 26 | \renewcommand{\code}[1]{{\color{blue!80!black}\texttt{#1}}} 27 | 28 | \providecommand{\file}[1]{} 29 | \renewcommand{\file}[1]{{\tt #1}} 30 | 31 | % This looks really hokey. Probably need to redefine this. 32 | \providecommand{\model}[2]{} 33 | \renewcommand{\model}[2]{{$\,$\hbox{#1}\ \ensuremath{\sim}\ \hbox{#2}}} 34 | 35 | % These should be considered deprecated -- cease and disist 36 | \providecommand{\VN}[1]{} 37 | \renewcommand{\VN}[1]{{\color{green!50!black}\texttt{#1}}} 38 | \providecommand{\vn}[1]{} 39 | \renewcommand{\vn}[1]{{\color{green!50!black}\texttt{#1}}} 40 | \providecommand{\DFN}[1]{} 41 | \renewcommand{\DFN}[1]{{\color{blue!80!black}\texttt{#1}}} 42 | \providecommand{\dfn}[1]{} 43 | \renewcommand{\dfn}[1]{{\color{blue!80!black}\texttt{#1}}} 44 | 45 | -------------------------------------------------------------------------------- /StudentGuide/markdown1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/markdown1.png -------------------------------------------------------------------------------- /StudentGuide/markdown2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/markdown2.png -------------------------------------------------------------------------------- /StudentGuide/markdown3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/markdown3.png -------------------------------------------------------------------------------- /StudentGuide/markdown4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/markdown4.png -------------------------------------------------------------------------------- /StudentGuide/r-interface.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/r-interface.jpg -------------------------------------------------------------------------------- /StudentGuide/r-markdown.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/r-markdown.jpg -------------------------------------------------------------------------------- /StudentGuide/rstudio-init.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/rstudio-init.png -------------------------------------------------------------------------------- /StudentGuide/rstudio-login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/StudentGuide/rstudio-login.png -------------------------------------------------------------------------------- /_output.yaml: -------------------------------------------------------------------------------- 1 | html_document: 2 | keep_md: yes 3 | -------------------------------------------------------------------------------- /include/.gitignore: -------------------------------------------------------------------------------- 1 | MainDocument.log 2 | MainDocument.synctex.gz 3 | MainDocument.tex 4 | MainDocument.pdf 5 | MainDocument-concordance.tex 6 | MainDocument.notes 7 | -------------------------------------------------------------------------------- /include/MainDocument.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass[open-any,12pt]{tufte-book} 2 | \setcounter{secnumdepth}{3} 3 | \usepackage{import} 4 | \usepackage{graphicx} 5 | 6 | \usepackage{alltt} 7 | \usepackage{mparhack} 8 | \usepackage{xstring} 9 | 10 | \usepackage{etoolbox} 11 | \usepackage{multicol} 12 | \usepackage{xcolor} 13 | \usepackage{framed} 14 | \usepackage{hyperref} 15 | %%%%%%%%%%%% Things Danny omitted %%%%%%%%%%%%%%%%%%%%% 16 | \usepackage{fancyhdr} 17 | 18 | %\newdimen\Rwidth 19 | %\Rwidth=\textwidth 20 | 21 | %\usepackage[margin=.5in,outer=1.5in,inner=.9in,includehead,includefoot,paperwidth=7.25in,paperheight=9.5in]{geometry} 22 | \usepackage{probstat} 23 | \usepackage[shownotes]{authNote} 24 | % \usepackage[hidenotes]{authNote} 25 | \usepackage[answerdelayed,exercisedelayed,lastexercise,chapter]{problems} 26 | \usepackage{longtable} 27 | \usepackage{language} 28 | 29 | \usepackage{tikz} 30 | \usetikzlibrary{shadows} 31 | \usetikzlibrary{decorations} 32 | \usetikzlibrary{shapes.multipart} 33 | \usetikzlibrary{shapes.symbols} 34 | \usetikzlibrary{shapes.misc} 35 | \usetikzlibrary{shapes.geometric} 36 | 37 | \newcommand{\mymarginpar}[1]{% 38 | \vadjust{\smash{\llap{\parbox[t]{\marginparwidth}{#1}\kern\marginparsep}}}} 39 | 40 | \newcommand{\tikzNote}[3]{% 41 | \marginpar[% 42 | \hspace*{0.5in} 43 | \parbox{1.2in}{\begin{tikzpicture} 44 | \node at (0,0) [#3] 45 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 46 | \end{tikzpicture}} 47 | ]{% 48 | \parbox{1.2in}{\begin{tikzpicture} 49 | \node at (0,0) [#3] 50 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 51 | \end{tikzpicture}}} 52 | } 53 | 54 | \renewcommand{\tikzNote}[3]{ 55 | \marginnote{ 56 | \textsc{#1} 57 | 58 | #2 59 | } 60 | } 61 | 62 | \newcommand{\InstructorNote}[2][\relax]{% 63 | \tikzNote{#1}{#2}{double copy shadow={opacity=.5},tape,fill=blue!10,draw=blue,thick} 64 | } 65 | 66 | \renewcommand{\InstructorNote}[2][\relax]{% 67 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 68 | } 69 | 70 | \newcommand{\DiggingDeeper}[2][\centerline{Digging Deeper}]{% 71 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 72 | } 73 | 74 | 75 | \newcommand{\TeachingTip}[2][\centerline{Teaching Tip}]{% 76 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 77 | } 78 | 79 | 80 | \newcommand{\FoodForThought}[2][\relax]{% 81 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 82 | } 83 | 84 | \newcommand{\SuggestionBox}[2][\centerline{Suggestion Box}]{% 85 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 86 | } 87 | 88 | \newcommand{\Caution}[2][\centerline{Caution!}]{% 89 | \tikzNote{#1}{#2}{chamfered rectangle,fill=red!10,draw=red,thick} 90 | } 91 | 92 | \newcounter{examplenum}[chapter] 93 | \newenvironment{example}[1][\relax]{ 94 | \refstepcounter{examplenum} 95 | \textbf{Example \thechapter.\arabic{examplenum}.{#1}} 96 | }{% 97 | \hfill {\Large $\diamond$} 98 | %\centerline{\rule{5in}{.5pt}} 99 | } 100 | 101 | \usepackage[utf8]{inputenc} 102 | 103 | \newcounter{myenumi} 104 | \newcommand{\saveenumi}{\setcounter{myenumi}{\value{enumi}}} 105 | \newcommand{\reuseenumi}{\setcounter{enumi}{\value{myenumi}}} 106 | 107 | \newcommand{\cran}{\href{http://www.R-project.org/}{CRAN}} 108 | %%%%%%%% Some R Stuff %%%%%%%%%%%%%%% 109 | \newcommand{\rterm}[1]{\textbf{#1}} 110 | \def\R{{\sf R}} 111 | \def\Rstudio{{\sf RStudio}} 112 | \def\RStudio{{\sf RStudio}} 113 | \def\term#1{\textbf{#1}} 114 | \def\tab#1{{\sf #1}} 115 | 116 | %%%%%%%%%%%%% some boxed elements %%%%%%%%%%%%%%%% 117 | 118 | \newlength{\tempfmlength} 119 | \newsavebox{\fmbox} 120 | \newenvironment{fmpage}[1] 121 | { 122 | \medskip 123 | \setlength{\tempfmlength}{#1} 124 | \begin{lrbox}{\fmbox} 125 | \begin{minipage}{#1} 126 | \vspace*{.02\tempfmlength} 127 | \hfill 128 | \begin{minipage}{.95 \tempfmlength}} 129 | {\end{minipage}\hfill 130 | \vspace*{.015\tempfmlength} 131 | \end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}} 132 | \medskip 133 | } 134 | 135 | \newenvironment{boxedText}[1][.98\textwidth]% 136 | {% 137 | \begin{center} 138 | \begin{fmpage}{#1} 139 | }% 140 | {% 141 | \end{fmpage} 142 | \end{center} 143 | } 144 | 145 | \newenvironment{boxedTable}[2][tbp]% 146 | {% 147 | \begin{table}[#1] 148 | \refstepcounter{table} 149 | \begin{center} 150 | \begin{fmpage}{.98\textwidth} 151 | \begin{center} 152 | \sf \large Box~\expandafter\thetable. #2 153 | \end{center} 154 | \medskip 155 | }% 156 | {% 157 | \end{fmpage} 158 | \end{center} 159 | \end{table} % need to do something about exercises that follow boxedTable 160 | } 161 | 162 | %%% indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 163 | %%% 164 | \newcommand{\printindex}[1]{\relax} 165 | \newcommand{\indexchap}[1]{\relax} 166 | \usepackage{amsmidx} 167 | \newcommand{\exampleidx}[1]{{\it #1}} 168 | \newcommand{\defidx}[1]{{\bf #1}} 169 | \newcommand{\mainidx}[1]{{\bf #1}} 170 | \newcommand{\probidx}[1]{{{\underline{#1}}}} 171 | \makeindex{Rindex} 172 | \makeindex{mainIndex} 173 | \newcommand{\Rindex}[1]{\index{Rindex}{#1@\texttt{#1}}} 174 | \newcommand{\myindex}[1]{\index{mainIndex}{#1}} 175 | \newcommand{\mathindex}[1]{\index{mainIndex}{$#1$}} 176 | 177 | 178 | \pagestyle{fancy} 179 | 180 | 181 | \begin{document} 182 | 183 | \end{document} 184 | -------------------------------------------------------------------------------- /include/RBook.sty: -------------------------------------------------------------------------------- 1 | 2 | \ProvidesPackage{RBook}[2013/04/04 Mosaic R Books style] 3 | 4 | \makeatletter 5 | \def\IfClass#1#2#3{\@ifundefined{opt@#1.cls}{#3}{#2}} 6 | \makeatother 7 | \IfClass{tufte-book}{\setcounter{secnumdepth}{3}}{\relax} 8 | \IfClass{tufte-book}{\def\subsubsection#1{\newthought{#1}}}{} 9 | 10 | \RequirePackage{import} 11 | \RequirePackage{graphicx} 12 | \RequirePackage{alltt} 13 | \RequirePackage{mparhack} 14 | \RequirePackage{xstring} 15 | 16 | \RequirePackage{etoolbox} 17 | \RequirePackage{multicol} 18 | \RequirePackage{xcolor} 19 | \RequirePackage{framed} 20 | \RequirePackage{hyperref} 21 | \RequirePackage{fancyhdr} 22 | 23 | \RequirePackage{probstat} 24 | \RequirePackage[answerdelayed,exercisedelayed,lastexercise,chapter]{problems} 25 | \RequirePackage{longtable} 26 | \RequirePackage{language} 27 | 28 | \RequirePackage{tikz} 29 | \usetikzlibrary{shadows} 30 | \usetikzlibrary{decorations} 31 | \usetikzlibrary{shapes.multipart} 32 | \usetikzlibrary{shapes.symbols} 33 | \usetikzlibrary{shapes.misc} 34 | \usetikzlibrary{shapes.geometric} 35 | 36 | \RequirePackage[utf8]{inputenc} 37 | \RequirePackage{underscore} 38 | 39 | \newcommand{\mymarginpar}[1]{% 40 | \vadjust{\smash{\llap{\parbox[t]{\marginparwidth}{#1}\kern\marginparsep}}}} 41 | 42 | \newcommand{\tikzNote}[3]{% 43 | \marginpar[% 44 | \hspace*{0.5in} 45 | \parbox{1.2in}{\begin{tikzpicture} 46 | \node at (0,0) [#3] 47 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 48 | \end{tikzpicture}} 49 | ]{% 50 | \parbox{1.2in}{\begin{tikzpicture} 51 | \node at (0,0) [#3] 52 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 53 | \end{tikzpicture}}}\ignorespaces 54 | }% 55 | 56 | \renewcommand{\tikzNote}[4][0pt]{ 57 | \marginnote[#1]{ 58 | \textsc{#2} 59 | 60 | \noindent #3 61 | }% 62 | } 63 | 64 | %\newcommand{\InstructorNote}[3][]{% 65 | %\tikzNote[#1]{#2}{#3}{double copy shadow={opacity=.5},tape,fill=blue!10,draw=blue,thick} 66 | %} 67 | 68 | \newcommand{\InstructorNote}[2][0pt]{% 69 | \tikzNote[#1]{}{#2}{tape,fill=blue!10,draw=blue,thick}% 70 | } 71 | 72 | \newcommand{\DiggingDeeper}[2][0pt]{% 73 | \tikzNote[#1]{\centerline{Digging Deeper}}{#2}{tape,fill=blue!10,draw=blue,thick}% 74 | } 75 | 76 | 77 | \newcommand{\TeachingTip}[2][0pt]{% 78 | \tikzNote[#1]{\centerline{Teaching Tip}}{#2}{tape,fill=blue!10,draw=blue,thick}% 79 | } 80 | 81 | 82 | \newcommand{\FoodForThought}[2][0pt]{% 83 | \tikzNote[#1]{}{#2}{rectangle,fill=green!10,draw=green,thick}% 84 | } 85 | 86 | \newcommand{\SuggestionBox}[2][0pt]{% 87 | \tikzNote[#1]{\centerline{Suggestion Box}}{#2}{rectangle,fill=green!10,draw=green,thick}% 88 | } 89 | 90 | \newcommand{\Caution}[2][0pt]{% 91 | \tikzNote[#1]{\centerline{Caution!}}{#2}{chamfered rectangle,fill=red!10,draw=red,thick}% 92 | } 93 | 94 | \newcommand{\Pointer}[2][0pt]{% 95 | \tikzNote[#1]{\centerline{More Info}}{#2}{}% 96 | } 97 | 98 | \newcommand{\Note}[2][0pt]{% 99 | \tikzNote[#1]{\centerline{Note}}{#2}{}% 100 | } 101 | 102 | \newcommand{\BlankNote}[2][0pt]{% 103 | \tikzNote[#1]{\relax}{#2}{}% 104 | } 105 | 106 | 107 | 108 | \newcounter{examplenum}[chapter] 109 | \newenvironment{example}[1][\relax]{ 110 | \refstepcounter{examplenum} 111 | \textbf{Example \thechapter.\arabic{examplenum}.{#1}} 112 | }{% 113 | \hfill {\Large $\diamond$} 114 | %\centerline{\rule{5in}{.5pt}} 115 | } 116 | 117 | 118 | \newcounter{myenumi} 119 | \newcommand{\saveenumi}{\setcounter{myenumi}{\value{enumi}}} 120 | \newcommand{\reuseenumi}{\setcounter{enumi}{\value{myenumi}}} 121 | 122 | \newcommand{\cran}{\href{http://www.R-project.org/}{CRAN}} 123 | 124 | %%%%%%%% tufte-book work-arounds %%%%%%%%% 125 | \makeatletter 126 | \newenvironment{widestuff}% 127 | { 128 | \hspace*{-2ex}\begin{minipage}{\@tufte@fullwidth}}% 129 | {\end{minipage}} 130 | \makeatother 131 | 132 | %%%%%%%% Some R Stuff %%%%%%%%%%%%%%% 133 | \newcommand{\rterm}[1]{\textbf{#1}} 134 | \def\R{{\sf R}} 135 | \def\Rstudio{{\sf RStudio}} 136 | \def\RStudio{{\sf RStudio}} 137 | \def\term#1{\textbf{#1}} 138 | \def\tab#1{{\sf #1}} 139 | 140 | %%%%%%%%%%%%% some boxed elements %%%%%%%%%%%%%%%% 141 | 142 | \newlength{\tempfmlength} 143 | \newsavebox{\fmbox} 144 | \newenvironment{fmpage}[1] 145 | { 146 | \medskip 147 | \setlength{\tempfmlength}{#1} 148 | \begin{lrbox}{\fmbox} 149 | \begin{minipage}{#1} 150 | \vspace*{.02\tempfmlength} 151 | \hfill 152 | \begin{minipage}{.95 \tempfmlength}} 153 | {\end{minipage}\hfill 154 | \vspace*{.015\tempfmlength} 155 | \end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}} 156 | \medskip 157 | } 158 | 159 | \newenvironment{boxedText}[1][.98\textwidth]% 160 | {% 161 | \begin{center} 162 | \begin{fmpage}{#1} 163 | }% 164 | {% 165 | \end{fmpage} 166 | \end{center} 167 | } 168 | 169 | \newenvironment{boxedTable}[2][tbp]% 170 | {% 171 | \begin{table}[#1] 172 | \refstepcounter{table} 173 | \begin{center} 174 | \begin{fmpage}{.98\textwidth} 175 | \begin{center} 176 | \sf \large Box~\expandafter\thetable. #2 177 | \end{center} 178 | \medskip 179 | }% 180 | {% 181 | \end{fmpage} 182 | \end{center} 183 | \end{table} % need to do something about exercises that follow boxedTable 184 | } 185 | 186 | %%% indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 187 | %\newcommand{\printindex}[1]{\relax} 188 | %\newcommand{\indexchap}[1]{\relax} 189 | %\RequirePackage{amsmidx} 190 | \RequirePackage{makeidx} 191 | 192 | \newcommand{\exampleidx}[1]{{\it #1}} 193 | \newcommand{\defidx}[1]{{\bf #1}} 194 | \newcommand{\mainidx}[1]{{\bf #1}} 195 | \newcommand{\probidx}[1]{{{\underline{#1}}}} 196 | \makeindex 197 | %\makeindex{Rindex} 198 | %\makeindex{mainIndex} 199 | %\newcommand{\Rindex}[1]{\index{Rindex}{#1@\texttt{#1}}} 200 | %\newcommand{\myindex}[1]{\index{mainIndex}{#1}} 201 | %\newcommand{\mathindex}[1]{\index{mainIndex}{$#1$}} 202 | \newcommand{\Rindex}[1]{\index{#1@\texttt{#1}}} 203 | \newcommand{\myindex}[1]{\index{#1}} 204 | \newcommand{\mathindex}[1]{\index{$#1$}} 205 | 206 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 207 | %%%% probably don't need this --rjp 208 | \makeatletter 209 | \newcommand\gobblepar{% 210 | \@ifnextchar\par% 211 | {\expandafter\gobblepar\@gobble}% 212 | {}} 213 | \makeatother 214 | 215 | \pagestyle{fancy} 216 | 217 | %% allow more of page to be used for a figure or table 218 | \renewcommand{\textfraction}{0.05} 219 | \renewcommand{\topfraction}{0.8} 220 | \renewcommand{\bottomfraction}{0.8} 221 | \renewcommand{\floatpagefraction}{0.75} 222 | \setcounter{topnumber}{4} 223 | \setcounter{bottomnumber}{4} 224 | \setcounter{totalnumber}{8} 225 | 226 | -------------------------------------------------------------------------------- /include/RBooks.sty: -------------------------------------------------------------------------------- 1 | \documentclass[open-any,12pt]{tufte-book} 2 | \setcounter{secnumdepth}{3} 3 | \usepackage{import} 4 | \usepackage{graphicx} 5 | 6 | \usepackage{alltt} 7 | \usepackage{mparhack} 8 | \usepackage{xstring} 9 | 10 | \usepackage{etoolbox} 11 | \usepackage{multicol} 12 | \usepackage{xcolor} 13 | \usepackage{framed} 14 | \usepackage{hyperref} 15 | %%%%%%%%%%%% Things Danny omitted %%%%%%%%%%%%%%%%%%%%% 16 | \usepackage{fancyhdr} 17 | 18 | %\newdimen\Rwidth 19 | %\Rwidth=\textwidth 20 | 21 | %\usepackage[margin=.5in,outer=1.5in,inner=.9in,includehead,includefoot,paperwidth=7.25in,paperheight=9.5in]{geometry} 22 | \usepackage{probstat} 23 | \usepackage[shownotes]{authNote} 24 | % \usepackage[hidenotes]{authNote} 25 | \usepackage[answerdelayed,exercisedelayed,lastexercise,chapter]{problems} 26 | \usepackage{longtable} 27 | \usepackage{language} 28 | 29 | \usepackage{tikz} 30 | \usetikzlibrary{shadows} 31 | \usetikzlibrary{decorations} 32 | \usetikzlibrary{shapes.multipart} 33 | \usetikzlibrary{shapes.symbols} 34 | \usetikzlibrary{shapes.misc} 35 | \usetikzlibrary{shapes.geometric} 36 | 37 | \newcommand{\mymarginpar}[1]{% 38 | \vadjust{\smash{\llap{\parbox[t]{\marginparwidth}{#1}\kern\marginparsep}}}} 39 | 40 | \newcommand{\tikzNote}[3]{% 41 | \marginpar[% 42 | \hspace*{0.5in} 43 | \parbox{1.2in}{\begin{tikzpicture} 44 | \node at (0,0) [#3] 45 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 46 | \end{tikzpicture}} 47 | ]{% 48 | \parbox{1.2in}{\begin{tikzpicture} 49 | \node at (0,0) [#3] 50 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 51 | \end{tikzpicture}}} 52 | } 53 | 54 | \renewcommand{\tikzNote}[3]{ 55 | \marginnote{ 56 | \textsc{#1} 57 | 58 | #2 59 | } 60 | } 61 | 62 | \newcommand{\InstructorNote}[2][\relax]{% 63 | \tikzNote{#1}{#2}{double copy shadow={opacity=.5},tape,fill=blue!10,draw=blue,thick} 64 | } 65 | 66 | \renewcommand{\InstructorNote}[2][\relax]{% 67 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 68 | } 69 | 70 | \newcommand{\DiggingDeeper}[2][\centerline{Digging Deeper}]{% 71 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 72 | } 73 | 74 | 75 | \newcommand{\TeachingTip}[2][\centerline{Teaching Tip}]{% 76 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 77 | } 78 | 79 | 80 | \newcommand{\FoodForThought}[2][\relax]{% 81 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 82 | } 83 | 84 | \newcommand{\SuggestionBox}[2][\centerline{Suggestion Box}]{% 85 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 86 | } 87 | 88 | \newcommand{\Caution}[2][\centerline{Caution!}]{% 89 | \tikzNote{#1}{#2}{chamfered rectangle,fill=red!10,draw=red,thick} 90 | } 91 | 92 | \newcounter{examplenum}[chapter] 93 | \newenvironment{example}[1][\relax]{ 94 | \refstepcounter{examplenum} 95 | \textbf{Example \thechapter.\arabic{examplenum}.{#1}} 96 | }{% 97 | \hfill {\Large $\diamond$} 98 | %\centerline{\rule{5in}{.5pt}} 99 | } 100 | 101 | \usepackage[utf8]{inputenc} 102 | 103 | \newcounter{myenumi} 104 | \newcommand{\saveenumi}{\setcounter{myenumi}{\value{enumi}}} 105 | \newcommand{\reuseenumi}{\setcounter{enumi}{\value{myenumi}}} 106 | 107 | \newcommand{\cran}{\href{http://www.R-project.org/}{CRAN}} 108 | %%%%%%%% Some R Stuff %%%%%%%%%%%%%%% 109 | \newcommand{\rterm}[1]{\textbf{#1}} 110 | \def\R{{\sf R}} 111 | \def\Rstudio{{\sf RStudio}} 112 | \def\RStudio{{\sf RStudio}} 113 | \def\term#1{\textbf{#1}} 114 | \def\tab#1{{\sf #1}} 115 | 116 | %%%%%%%%%%%%% some boxed elements %%%%%%%%%%%%%%%% 117 | 118 | \newlength{\tempfmlength} 119 | \newsavebox{\fmbox} 120 | \newenvironment{fmpage}[1] 121 | { 122 | \medskip 123 | \setlength{\tempfmlength}{#1} 124 | \begin{lrbox}{\fmbox} 125 | \begin{minipage}{#1} 126 | \vspace*{.02\tempfmlength} 127 | \hfill 128 | \begin{minipage}{.95 \tempfmlength}} 129 | {\end{minipage}\hfill 130 | \vspace*{.015\tempfmlength} 131 | \end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}} 132 | \medskip 133 | } 134 | 135 | \newenvironment{boxedText}[1][.98\textwidth]% 136 | {% 137 | \begin{center} 138 | \begin{fmpage}{#1} 139 | }% 140 | {% 141 | \end{fmpage} 142 | \end{center} 143 | } 144 | 145 | \newenvironment{boxedTable}[2][tbp]% 146 | {% 147 | \begin{table}[#1] 148 | \refstepcounter{table} 149 | \begin{center} 150 | \begin{fmpage}{.98\textwidth} 151 | \begin{center} 152 | \sf \large Box~\expandafter\thetable. #2 153 | \end{center} 154 | \medskip 155 | }% 156 | {% 157 | \end{fmpage} 158 | \end{center} 159 | \end{table} % need to do something about exercises that follow boxedTable 160 | } 161 | 162 | %%% indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 163 | %%% 164 | \newcommand{\printindex}[1]{\relax} 165 | \newcommand{\indexchap}[1]{\relax} 166 | \usepackage{amsmidx} 167 | \newcommand{\exampleidx}[1]{{\it #1}} 168 | \newcommand{\defidx}[1]{{\bf #1}} 169 | \newcommand{\mainidx}[1]{{\bf #1}} 170 | \newcommand{\probidx}[1]{{{\underline{#1}}}} 171 | \makeindex{Rindex} 172 | \makeindex{mainIndex} 173 | \newcommand{\Rindex}[1]{\index{Rindex}{#1@\texttt{#1}}} 174 | \newcommand{\myindex}[1]{\index{mainIndex}{#1}} 175 | \newcommand{\mathindex}[1]{\index{mainIndex}{$#1$}} 176 | 177 | 178 | \pagestyle{fancy} 179 | 180 | 181 | \begin{document} 182 | 183 | \end{document} 184 | -------------------------------------------------------------------------------- /include/TufteDocument.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass[openany,12pt]{tufte-book} 2 | \usepackage{import} 3 | \usepackage{graphicx} 4 | 5 | \usepackage{alltt} 6 | \usepackage{mparhack} 7 | \usepackage{xstring} 8 | 9 | \usepackage{etoolbox} 10 | \usepackage{multicol} 11 | \usepackage{xcolor} 12 | \usepackage{framed} 13 | \usepackage{hyperref} 14 | 15 | \newdimen\Rwidth 16 | \Rwidth=\textwidth 17 | 18 | 19 | \usepackage[margin=.5in,outer=1.5in,inner=.9in,includehead,includefoot,paperwidth=7.25in,paperheight=9.5in]{geometry} 20 | \usepackage{/Users/kaplan/texmf/tex/latex/probstat} 21 | \usepackage[shownotes]{/Users/kaplan/texmf/tex/latex/authNote} 22 | % \usepackage[hidenotes]{authNote} 23 | \usepackage[answerdelayed,exercisedelayed,lastexercise,chapter]{/Users/kaplan/texmf/tex/latex/problems} 24 | \usepackage{longtable} 25 | 26 | \def\R{{\sf R}} 27 | \def\Rstudio{{\sf RStudio}} 28 | 29 | \usepackage{tikz} 30 | \usetikzlibrary{shadows} 31 | \usetikzlibrary{decorations} 32 | \usetikzlibrary{shapes.multipart} 33 | \usetikzlibrary{shapes.symbols} 34 | \usetikzlibrary{shapes.misc} 35 | \usetikzlibrary{shapes.geometric} 36 | 37 | \newcommand{\mymarginpar}[1]{% 38 | \vadjust{\smash{\llap{\parbox[t]{\marginparwidth}{#1}\kern\marginparsep}}}} 39 | 40 | \newcommand{\tikzNote}[3]{% 41 | \marginpar[% 42 | \hspace*{0.5in} 43 | \parbox{1.2in}{\begin{tikzpicture} 44 | \node at (0,0) [#3] 45 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 46 | \end{tikzpicture}} 47 | ]{% 48 | \parbox{1.2in}{\begin{tikzpicture} 49 | \node at (0,0) [#3] 50 | {\parbox{1.05in}{\footnotesize {\sc #1 }{\raggedright #2}}}; 51 | \end{tikzpicture}}} 52 | } 53 | 54 | \newcommand{\InstructorNote}[2][\relax]{% 55 | \tikzNote{#1}{#2}{double copy shadow={opacity=.5},tape,fill=blue!10,draw=blue,thick} 56 | } 57 | 58 | \renewcommand{\InstructorNote}[2][\relax]{% 59 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 60 | } 61 | 62 | \newcommand{\DiggingDeeper}[2][\centerline{Digging Deeper}]{% 63 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 64 | } 65 | 66 | 67 | \newcommand{\TeachingTip}[2][\centerline{Teaching Tip}]{% 68 | \tikzNote{#1}{#2}{tape,fill=blue!10,draw=blue,thick} 69 | } 70 | 71 | 72 | \newcommand{\FoodForThought}[2][\relax]{% 73 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 74 | } 75 | 76 | \newcommand{\SuggestionBox}[2][\centerline{Suggestion Box}]{% 77 | \tikzNote{#1}{#2}{rectangle,fill=green!10,draw=green,thick} 78 | } 79 | 80 | \newcommand{\Caution}[2][\centerline{Caution!}]{% 81 | \tikzNote{#1}{#2}{chamfered rectangle,fill=red!10,draw=red,thick} 82 | } 83 | 84 | \newcounter{examplenum}[chapter] 85 | \newenvironment{example}[1][\relax]{ 86 | \refstepcounter{examplenum} 87 | \textbf{Example \thechapter.\arabic{examplenum}.{#1}} 88 | }{% 89 | \hfill {\Large $\diamond$} 90 | %\centerline{\rule{5in}{.5pt}} 91 | } 92 | 93 | \usepackage[utf8]{inputenc} 94 | 95 | \newcounter{myenumi} 96 | \newcommand{\saveenumi}{\setcounter{myenumi}{\value{enumi}}} 97 | \newcommand{\reuseenumi}{\setcounter{enumi}{\value{myenumi}}} 98 | 99 | \pagestyle{fancy} 100 | 101 | 102 | \begin{document} 103 | 104 | \end{document} -------------------------------------------------------------------------------- /include/authNote.sty: -------------------------------------------------------------------------------- 1 | 2 | \NeedsTeXFormat{LaTeX2e}[1995/12/01] 3 | \ProvidesPackage{authNote}[2005/06/14 1.0 (RJP)] 4 | 5 | 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | % my package requirements 8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9 | 10 | \RequirePackage{ifthen} 11 | 12 | 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | % options and booleans for them 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | 17 | %\reversemarginpar % ?? 18 | 19 | %\smartqed % makes \qed print at the rightmargin 20 | 21 | \newboolean{shownotes} 22 | \setboolean{shownotes}{true} 23 | \DeclareOption{hidenotes}{\setboolean{shownotes}{false}} 24 | \DeclareOption{shownotes}{\setboolean{shownotes}{true}} 25 | \DeclareOption{hide}{\setboolean{shownotes}{false}} 26 | \DeclareOption{show}{\setboolean{shownotes}{true}} 27 | 28 | \newboolean{showhmm} 29 | \setboolean{showhmm}{true} 30 | \DeclareOption{hidehmm}{\setboolean{showhmm}{false}} 31 | \DeclareOption{showhmm}{\setboolean{showhmm}{true}} 32 | 33 | \newboolean{showopt} 34 | \setboolean{showopt}{true} 35 | \DeclareOption{hideopt}{\setboolean{showopt}{false}} 36 | \DeclareOption{showopt}{\setboolean{showopt}{true}} 37 | 38 | \newboolean{showold} 39 | \setboolean{showold}{false} 40 | \DeclareOption{showold}{\setboolean{showold}{true}} 41 | \DeclareOption{hideold}{\setboolean{showold}{false}} 42 | 43 | \DeclareOption{primary}{% 44 | \setboolean{showhmm}{true} 45 | \setboolean{showopt}{true} 46 | \setboolean{shownotes}{true} 47 | \setboolean{showold}{false} 48 | } 49 | 50 | \DeclareOption{secondary}{% 51 | \setboolean{showhmm}{false} 52 | \setboolean{showopt}{false} 53 | \setboolean{shownotes}{true} 54 | \setboolean{showold}{false} 55 | } 56 | 57 | \DeclareOption{clean}{% 58 | \setboolean{showhmm}{false} 59 | \setboolean{showopt}{false} 60 | \setboolean{shownotes}{false} 61 | \setboolean{showold}{false} 62 | } 63 | 64 | 65 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 66 | 67 | 68 | \ProcessOptions* 69 | 70 | 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 72 | %Translation Helps 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | 75 | %\def\transbox#1{{\textbf{#1}}} 76 | \def\transbox#1{{\textbf{#1}}} 77 | 78 | \def\hmm[#1]#2{\ifthenelse% 79 | {\boolean{showhmm}}% 80 | {\transbox{#2}\smallmarginpar{#1}{}}% 81 | {#2}% 82 | } 83 | 84 | \def\hmmok[#1]#2{% 85 | \ifthenelse{\boolean{showold}}% 86 | {\transbox{#2}\smallmarginpar{#1}}% 87 | {#2}% 88 | } 89 | 90 | \def\hmmOK[#1]#2{% 91 | \ifthenelse{\boolean{showold}}% 92 | {\transbox{#2}\smallmarginpar{#1}}% 93 | {#2}% 94 | } 95 | 96 | \newcommand{\options}[2]{% 97 | \ifthenelse{\boolean{showopt}}% 98 | %{\textbf{$\mathbf<$#1 $\mathbf\mid$ #2$\mathbf >$}\smallmarginpar{$<\mid>$}}% 99 | {% 100 | \smallmarginpar{$<$#1$\mid$#2$>$}% 101 | #1% 102 | }% 103 | {#1}% 104 | } 105 | 106 | 107 | \newcommand{\optionsok}[2]{% 108 | \ifthenelse{\boolean{showold}}% 109 | % {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 110 | {% 111 | \smallmarginpar{$<$#1$\mid$#2$>$}% 112 | #1% 113 | }% 114 | {#1}% 115 | } 116 | 117 | \newcommand{\optionsOk}[2]{% 118 | \ifthenelse{\boolean{showold}}% 119 | % {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 120 | {% 121 | \smallmarginpar{$<$#1$\mid$#2$>$}% 122 | #1% 123 | }% 124 | {#1}% 125 | } 126 | 127 | \newcommand{\optionsOne}[2]{% 128 | \ifthenelse{\boolean{showold}}% 129 | {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 130 | {#1}% 131 | } 132 | 133 | \newcommand{\optionsone}[2]{% 134 | \ifthenelse{\boolean{showold}}% 135 | {\textbf{$\mathbf<$#1$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 136 | {#1}% 137 | } 138 | 139 | \newcommand{\optionsTwo}[2]{% 140 | \ifthenelse{\boolean{showold}}% 141 | {\textbf{$\mathbf<$#2$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 142 | {#2}% 143 | } 144 | 145 | \newcommand{\optionstwo}[2]{% 146 | \ifthenelse{\boolean{showold}}% 147 | {\textbf{$\mathbf<$#2$\mathbf >$}\smallmarginpar{$(<\mid>)$}}% 148 | {#2}% 149 | } 150 | 151 | 152 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 153 | % authNote stuff 154 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 155 | \newtoks\tempTok 156 | \newcounter{noteNum}[section] 157 | \newwrite\noteFile% 158 | \immediate\openout\noteFile=\jobname.notes 159 | 160 | \long\def\saveNote#1{% 161 | \refstepcounter{noteNum}% 162 | \immediate\write\noteFile{\string\begingroup\string\bf }% 163 | \immediate\write\noteFile{\thesection .% 164 | \expandafter\arabic{noteNum}}% 165 | \immediate\write\noteFile{(p. \expandafter\thepage): }% 166 | \immediate\write\noteFile{\string\endgroup}% 167 | \tempTok={#1} 168 | \immediate\write\noteFile{\the\tempTok}% 169 | \immediate\write\noteFile{}% 170 | } 171 | 172 | 173 | \def\smallmarginpar#1{\marginpar[\hfill \tiny #1]{\raggedright \tiny #1 \hfill}} 174 | 175 | \long\def\saveNshowNote#1#2{% 176 | \saveNote{#2}% 177 | \ifthenelse{\boolean{shownotes}}{% 178 | \marginpar[\hfill {\tiny #1 179 | \thesection.\arabic{noteNum} $\rightarrow$}]% 180 | {{\tiny $\leftarrow$ \thesection.\arabic{noteNum} #1 \hfill}}% 181 | }{\relax}% 182 | } 183 | 184 | % to remove marginal notes (for submissions, etc) use below instead: 185 | 186 | \long\def\authNote#1{\saveNshowNote{}{#1}} 187 | \long\def\oldNote#1{\saveNshowNote{old}{old: #1}} 188 | \long\def\authNoted#1{% 189 | \ifthenelse{\boolean{showold}}% 190 | {\saveNshowNote{$\surd$}{(Done) #1}}% 191 | {\relax}% 192 | } 193 | 194 | \long\def\authNotedOld#1{\relax} 195 | 196 | 197 | \def\authNotes{% 198 | \ifthenelse{\boolean{shownotes}}{% 199 | %\section*{Author Notes} 200 | \begingroup 201 | \immediate\closeout\noteFile 202 | \parindent=0pt 203 | \input \jobname.notes 204 | \endgroup 205 | } 206 | {\relax} 207 | } 208 | 209 | 210 | -------------------------------------------------------------------------------- /include/language.sty: -------------------------------------------------------------------------------- 1 | \ProvidesPackage{language} 2 | 3 | \RequirePackage{xstring} 4 | \RequirePackage{xcolor} 5 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 6 | % Looking for a consistent typography for language elements. 7 | 8 | \providecommand{\R}{} 9 | \renewcommand{\R}{\mbox{\sf{R}}} 10 | \providecommand{\RStudio}{} 11 | \renewcommand{\RStudio}{\mbox{\sf{R}Studio}} 12 | \providecommand{\Sage}{} 13 | \renewcommand{\Sage}{\mbox{\sf{Sage}}} 14 | 15 | \providecommand{\variable}[1]{} 16 | \renewcommand{\variable}[1]{{\color{green!50!black}\texttt{#1}}} 17 | \providecommand{\dataframe}[1]{} 18 | \renewcommand{\dataframe}[1]{{\color{blue!80!black}\texttt{#1}}} 19 | \providecommand{\function}[1]{} 20 | \renewcommand{\function}[1]{{\color{purple!75!blue}\texttt{\StrSubstitute{#1}{()}{}()}}} 21 | \providecommand{\option}[1]{} 22 | \renewcommand{\option}[1]{{\color{brown!80!black}\texttt{#1}}} 23 | \providecommand{\pkg}[1]{} 24 | \renewcommand{\pkg}[1]{{\color{red!80!black}\texttt{#1}}} 25 | \providecommand{\code}[1]{} 26 | \renewcommand{\code}[1]{{\color{blue!80!black}\texttt{#1}}} 27 | 28 | \providecommand{\file}[1]{} 29 | \renewcommand{\file}[1]{{\tt #1}} 30 | 31 | % This looks really hokey. Probably need to redefine this. 32 | \providecommand{\model}[2]{} 33 | \renewcommand{\model}[2]{{$\,$\hbox{#1}\ \ensuremath{\sim}\ \hbox{#2}}} 34 | 35 | % These should be considered deprecated -- cease and disist 36 | \providecommand{\VN}[1]{} 37 | \renewcommand{\VN}[1]{{\color{green!50!black}\texttt{#1}}} 38 | \providecommand{\vn}[1]{} 39 | \renewcommand{\vn}[1]{{\color{green!50!black}\texttt{#1}}} 40 | \providecommand{\DFN}[1]{} 41 | \renewcommand{\DFN}[1]{{\color{blue!80!black}\texttt{#1}}} 42 | \providecommand{\dfn}[1]{} 43 | \renewcommand{\dfn}[1]{{\color{blue!80!black}\texttt{#1}}} 44 | 45 | -------------------------------------------------------------------------------- /include/setup.R: -------------------------------------------------------------------------------- 1 | 2 | ## @knitr setup 3 | #setCacheDir("cache") 4 | require(grDevices) 5 | require(datasets) 6 | require(stats) 7 | require(lattice) 8 | require(grid) 9 | require(mosaic) 10 | require(mosaicData) 11 | trellis.par.set(theme=col.mosaic(bw=FALSE)) 12 | trellis.par.set(fontsize=list(text=9)) 13 | options(keep.blank.line=FALSE) 14 | options(width=60) 15 | # require(vcd) # went away 11/5/2015 by njh 16 | require(knitr) 17 | opts_chunk$set( tidy=TRUE, 18 | size='small', 19 | dev="pdf", 20 | fig.path="figures/fig-", 21 | fig.width=3, fig.height=2, 22 | fig.align="center", 23 | fig.show="hold", 24 | comment=NA) 25 | knit_hooks$set(chunk=function(x,options){ 26 | vshift <- options$vshift 27 | if (is.null(vshift)) vshift <- "0ex" 28 | return(paste("%% Using custom chunk hook function", 29 | paste("\\vspace*{",vshift,"}%", sep=""), 30 | "\\begin{knitrout}%", 31 | "\\definecolor{shadecolor}{rgb}{0.9, 0.9, 0.9}\\color{fgcolor}%", 32 | "\\begin{kframe}%", 33 | sub("\\n$","",x), 34 | "\\end{kframe}%", 35 | paste("\\vspace*{",vshift,"}%", sep=""), 36 | "\\end{knitrout}", 37 | sep="\n" 38 | )) 39 | }) 40 | 41 | knit_hooks$set(document = function(x) { 42 | gsub('\\\\end\\{knitrout\\}%\n', '\\\\end\\{knitrout\\}%', paste(x, collapse = '\n')) 43 | gsub('\\\\end\\{knitrout\\}\n', '\\\\end\\{knitrout\\}%', paste(x, collapse = '\n')) 44 | }) 45 | 46 | if(FALSE){ 47 | knit_hooks$set(output=function(x,options) { 48 | vshift <- options$vshift 49 | if (is.null(vshift)) vshift <- "0ex" 50 | return(paste("%% Using custom output hook function", 51 | paste("\\vspace*{",vshift,"}", sep=""), 52 | "\\begin{verbatim}", 53 | sub("\\n$","",x), 54 | "\\end{verbatim}%", 55 | paste("\\vspace*{",vshift,"}", sep=""), 56 | sep="\n" )) 57 | }) 58 | } 59 | -------------------------------------------------------------------------------- /sandbox/Gapminder/falls.csv: -------------------------------------------------------------------------------- 1 | "Falls, age adjusted mortality per 100 000",2002,2004 Afghanistan,6.331071,5.331773758 Albania,0.788038,2.135769606 Algeria,4.422987,3.915556908 Andorra,2.160558,2.126070976 Angola,7.728636,7.208484173 Antigua and Barbuda,0,0.980080128 Argentina,0.9156841,0.764998674 Armenia,1.389538,1.696600676 Australia,2.145666,2.194619179 Austria,6.149815,6.50606823 Azerbaijan,0.3272918,0.534083724 Bahamas,2.636469,2.663670063 Bahrain,0.922175,1.048099875 Bangladesh,11.54749,11.09069443 Barbados,1.952743,0.829251885 Belarus,6.217752,10.31347561 Belgium,5.611277,6.299408436 Belize,2.722101,6.651389599 Benin,5.192746,3.581347227 Bhutan,13.41168,12.23476124 Bolivia,0.6592804,0.606545389 Bosnia and Herzegovina,0.3968507,0.371162683 Botswana,3.313356,5.090471268 Brazil,4.782853,6.068707943 Brunei,2.760267,2.422203779 Bulgaria,2.833619,3.143793344 Burkina Faso,6.027211,4.525622368 Burundi,3.886196,5.079249382 Cambodia,5.588905,5.423884869 Cameroon,5.41176,4.248663425 Canada,3.087231,3.585384369 Cape Verde,1.47547,3.313320637 Central African Rep.,4.726402,5.358716011 Chad,5.42399,4.525423527 Chile,2.16193,4.501862049 China,8.447947,8.98939991 Colombia,4.180001,4.472536564 Comoros,3.404397,2.520788908 "Congo, Rep.",3.709612,4.312780857 Cook Is,0.0302162,0.01940852 Costa Rica,2.92673,2.844651461 Croatia,7.306144,9.772564888 Cuba,10.75212,10.38177299 Cyprus,0.4540549,0.361344337 Czech Rep.,10.83183,12.07424545 Cote d'Ivoire,5.341683,6.89838028 "Korea, Dem. Rep.",5.932664,5.490858555 "Congo, Dem. Rep.",4.815111,4.561295033 Denmark,12.70644,3.102794886 Djibouti,6.021299,5.331958294 Dominica,3.212226,1.222314596 Dominican Rep.,0.3562116,2.118693352 Ecuador,4.751126,4.376473904 Egypt,2.60788,2.637702227 El Salvador,4.371328,5.26265192 Equatorial Guinea,5.58201,4.629140854 Eritrea,3.47787,3.602208853 Estonia,8.242321,6.871423721 Ethiopia,3.748736,3.840878725 Fiji,0.0762395,0.043011017 Finland,10.74791,11.48399925 France,7.538716,4.636332035 Gabon,5.149407,5.091026783 Gambia,5.190156,3.930907726 Georgia,0.2372586,0.614108324 Germany,4.398823,4.830601215 Ghana,4.761881,4.059474468 Greece,3.39626,3.034126043 Grenada,5.388952,4.464422703 Guatemala,7.328953,4.509347916 Guinea,5.983692,4.693752289 Guinea-Bissau,4.744575,3.472041845 Guyana,16.48476,3.785176277 Haiti,3.559812,1.421693921 Honduras,1.290347,1.276504993 Hungary,17.53149,16.64228058 Iceland,3.384483,3.506905794 India,12.60013,12.1744833 Indonesia,8.662262,8.024801254 Iran,8.719461,4.438941479 Iraq,8.205505,11.85843277 Ireland,6.802936,5.428429604 Israel,1.379587,1.180207968 Italy,6.86275,6.824293613 Jamaica,1.715075,4.079553127 Japan,2.618268,2.524842262 Jordan,4.722551,2.22212863 Kazakhstan,3.243732,3.475875616 Kenya,3.963776,4.758968353 Kiribati,0,0 Kuwait,2.133363,2.048094511 Kyrgyzstan,4.44678,4.223422527 Laos,15.18016,13.66785049 Latvia,13.21245,9.458460808 Lebanon,5.83998,5.451313019 Lesotho,4.016922,3.385127544 Liberia,6.895104,4.254863739 Libya,3.422505,3.551558971 Lithuania,11.09042,10.39620018 Luxembourg,5.850252,6.195565701 Madagascar,5.096874,3.516938925 Malawi,3.980167,3.731557369 Malaysia,4.748957,4.846709728 Maldives,9.592298,10.73034477 Mali,5.500837,4.410247326 Malta,6.727343,9.175183296 Marshall Islands,0.0893605,0.048309103 Mauritania,5.956443,4.140105247 Mauritius,2.654331,2.173601389 Mexico,3.090917,3.788119555 "Micronesia, Fed. Sts.",0.093911,0.026307229 Monaco,6.378746,6.339615345 Mongolia,7.567759,6.890179157 Morocco,3.297315,3.129680634 Mozambique,3.546353,4.619746208 Myanmar,9.722318,8.890291214 Namibia,3.710479,4.850430489 Nauru,23.38896,17.84573746 Nepal,9.775215,8.931825638 Netherlands,2.641691,3.757113695 New Zealand,4.205973,4.886121273 Nicaragua,0.3473513,3.21727252 Niger,6.301385,4.842306137 Nigeria,5.606611,4.748979092 Niue,0.0472521,0.045809146 Norway,8.234253,7.188120842 Oman,3.362102,3.251220465 Pakistan,12.92274,12.02375126 Palau,0.0326177,0.039848767 Panama,4.00434,3.003424168 Papua New Guinea,10.1018,9.515307426 Paraguay,1.011129,0.822467208 Peru,0.6533876,0.55972147 Philippines,3.922125,3.564413071 Poland,7.350277,7.336278915 Portugal,4.268473,4.641287804 Qatar,1.150084,1.24138701 "Korea, Rep.",7.420903,8.278238297 Moldova,4.481233,5.939527988 Romania,5.553517,5.614512444 Russia,9.340448,8.196986198 Rwanda,4.290508,5.047314167 Saint Kitts and Nevis,5.071509,4.892878532 Saint Lucia,3.243204,0.30613175 Saint Vincent and the Grenadines,7.557895,6.104589939 Samoa,0.0459309,0.022459587 San Marino,0,0 Sao Tome and Principe,3.593869,3.875590801 Saudi Arabia,6.548616,7.029717445 Senegal,5.054834,4.103675365 Serbia and Montenegro,4.826154,1.852395296 Seychelles,1.094875,0.925927639 Sierra Leone,8.029929,6.217244625 Singapore,3.900983,4.118116379 Slovak Republic,7.18152,6.906487942 Slovenia,11.50085,10.57460308 Solomon Islands,0.1221166,0.160131052 Somalia,4.389216,9.542284966 South Africa,4.493975,3.47616291 Spain,2.119295,2.295529366 Sri Lanka,3.83734,3.320240021 Sudan,3.940754,5.087699413 Suriname,9.670567,4.316963196 Swaziland,2.761676,5.824388027 Sweden,2.481098,2.894412994 Switzerland,5.883059,7.417273521 Syria,1.273022,1.18450439 Tajikistan,1.154919,1.235026121 Thailand,5.23597,4.485964298 "Macedonia, FYR",3.306002,1.972636819 Timor-Leste,10.55452,8.450176239 Togo,5.389999,3.774657011 Tonga,0.0309489,0.022620838 Trinidad and Tobago,3.835178,2.598762274 Tunisia,4.751326,3.453145027 Turkey,1.675977,8.000944138 Turkmenistan,2.024848,1.935352325 Tuvalu,0.1500323,0.116566814 Uganda,4.178698,4.589588642 Ukraine,5.370097,6.721670151 United Arab Emirates,3.707171,1.791452646 United Kingdom,4.474754,3.453276873 Tanzania,4.008013,4.368362904 United States,3.064667,3.96007967 Uruguay,0.657182,0.945542574 Uzbekistan,1.284248,2.608140707 Vanuatu,0.0733076,0.058709171 Venezuela,5.330313,4.966413975 Vietnam,8.69049,7.921552181 "Yemen, Rep.",6.485294,6.745578289 Zambia,3.54413,4.415143013 Zimbabwe,4.323385,5.279467106 -------------------------------------------------------------------------------- /sandbox/Gapminder/merge.Rmd: -------------------------------------------------------------------------------- 1 | Merging data 2 | ======================================================== 3 | 4 | It's often useful to be able to merge multiple datasets that share a common identifier. We've used this to ask questions which span multiple topics in the GapMinder.org collection of datasets. 5 | 6 | In this example, we pull together a dataset with traffic mortality rate in 2004 as well as deaths from falls in 2004. This utilizes the material described in chapter 12 of the "Compendium" (Data Management). 7 | 8 | ```{r message=FALSE} 9 | require(mosaic) 10 | trellis.par.set(theme=col.mosaic()) 11 | options(digits=3) 12 | ``` 13 | 14 | Read the traffic dataset (and clean it up) 15 | 16 | > traffic deaths per 100,000 17 | 18 | ```{r} 19 | ds1 = read.csv("http://www.math.smith.edu/~nhorton/traffic.csv") 20 | names(ds1) 21 | names(ds1)[names(ds1)=="Traffic.mortality.per.100.000..age.adjusted"] = "country" 22 | names(ds1)[names(ds1)=="X2004"] = "traffic2004" 23 | ds1 = subset(ds1, select=c("country", "traffic2004")) 24 | head(ds1) 25 | ``` 26 | 27 | Read the falls dataset (and clean it up) 28 | 29 | > mortality from falling per 100,000 30 | ```{r} 31 | ds2 = read.csv("http://www.math.smith.edu/~nhorton/falls.csv") 32 | names(ds2) 33 | names(ds2)[names(ds2)=="Falls..age.adjusted.mortality.per.100.000"] = "country" 34 | names(ds2)[names(ds2)=="X2004"] = "falls2004" 35 | ds2 = subset(ds2, select=c("country", "falls2004")) 36 | head(ds2) 37 | ``` 38 | 39 | Merge datasets and take a look at the results. 40 | ```{r} 41 | ds = merge(ds1, ds2, by="country") 42 | head(ds) 43 | ``` 44 | 45 | Let's peruse the datasets and look for interesting observations. 46 | 47 | ```{r} 48 | favstats(~ falls2004, data=ds) 49 | favstats(~ traffic2004, data=ds) 50 | subset(ds, traffic2004 > 115) 51 | subset(ds, falls2004 > 17) 52 | subset(ds, is.na(traffic2004)) 53 | noiraq = subset(ds, country != "Iraq") 54 | ``` 55 | 56 | Unadorned scatterplot 57 | ```{r fig.width=7, fig.height=6} 58 | xyplot(traffic2004 ~ falls2004, data=ds) 59 | ``` 60 | 61 | Scatterplot displaying the country names. (Note use of "with()" to allow the "panel.labels()" function to access the country variable). 62 | 63 | ```{r fig.width=7, fig.height=6} 64 | panel.labels = function(x, y, col='black', labels='x', ...) 65 | { panel.text(x, y, labels, col=col, ...)} 66 | with(noiraq, xyplot(traffic2004 ~ falls2004, cex=0.5, 67 | panel=panel.labels, 68 | labels=country)) 69 | ``` 70 | 71 | 72 | -------------------------------------------------------------------------------- /sandbox/NearRoad.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/sandbox/NearRoad.pdf -------------------------------------------------------------------------------- /sandbox/Rmodeling.Rnw: -------------------------------------------------------------------------------- 1 | 2 | 3 | % Try the class options [notes], [notes=only], [trans], [handout], 4 | % [red], [compress], [draft], [class=article] and see what happens! 5 | 6 | \documentclass[slidestop,11pt]{beamer} 7 | 8 | %\usepackage{beamerbasearticle} 9 | %\usepackage{beamerthemeshadow} 10 | %\usepackage{mybeamer} 11 | %\usepackage{probstat} 12 | \usepackage{language} 13 | %\input{/Users/rpruim/public_html/fast/AMSedition1sol/tex/fastMacros} 14 | %\usepackage{fancyvrb} 15 | \usepackage{graphicx} 16 | \usepackage{multicol} 17 | %\usepackage{xcolor} 18 | \usepackage{alltt} 19 | %\usepackage{ifthen} 20 | %\usepackage{amsmath} 21 | %\usepackage{ulem} 22 | %\usepackage{helvet} 23 | %\usepackage{tikz} 24 | \usetheme{Singapore} 25 | \usecolortheme{seahorse} 26 | \usecolortheme{lily} 27 | 28 | 29 | \def\myemph#1{{\emph{#1}}} 30 | 31 | \def\cbox#1{\fbox{\phantom{$\mid$}{\ #1\ }}} 32 | \def\quickcode#1#2{\fbox{\phantom{$\mid$} \ \ #1 \ \ }% 33 | \fbox{\phantom{$\mid$} \ \ #2 \ \ } 34 | } 35 | 36 | 37 | \def\pkg#1{\texttt{#1}} 38 | \usepackage[nogin]{Sweave} 39 | \usepackage[utf8]{inputenc} 40 | 41 | \newlength{\tempfmlength} 42 | \newsavebox{\fmbox} 43 | \newenvironment{fmpage}[1] 44 | { 45 | \medskip 46 | \setlength{\tempfmlength}{#1} 47 | \begin{lrbox}{\fmbox} 48 | \begin{minipage}{#1} 49 | \vspace*{.02\tempfmlength} 50 | \hfill 51 | \begin{minipage}{.95 \tempfmlength}} 52 | {\end{minipage}\hfill 53 | \vspace*{.015\tempfmlength} 54 | \end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}} 55 | \medskip 56 | } 57 | 58 | 59 | \newenvironment{boxedText}[1][.98\textwidth]% 60 | {% 61 | \begin{center} 62 | \begin{fmpage}{#1} 63 | }% 64 | {% 65 | \end{fmpage} 66 | \end{center} 67 | } 68 | 69 | \newenvironment{boxedTable}[2][tbp]% 70 | {% 71 | \begin{table}[#1] 72 | \refstepcounter{table} 73 | \begin{center} 74 | \begin{fmpage}{.98\textwidth} 75 | \begin{center} 76 | \sf \large Box~\expandafter\thetable. #2 77 | \end{center} 78 | \medskip 79 | }% 80 | {% 81 | \end{fmpage} 82 | \end{center} 83 | \end{table} % need to do something about exercises that follow boxedTable 84 | } 85 | 86 | 87 | 88 | \begin{document} 89 | 90 | \newcommand\smallfonts{\fontsize{8}{9.2}\selectfont} 91 | \newcommand\regularfonts{\fontsize{10}{11.4}\selectfont} 92 | \regularfonts 93 | 94 | <>= 95 | require(mosaic) 96 | trellis.par.set(theme=col.mosaic()) 97 | trellis.par.set(fontsize=list(text=5)) 98 | options(keep.blank.line=FALSE) 99 | options(show.signif.stars=FALSE) 100 | options(width=70) 101 | @ 102 | 103 | <>= 104 | opts_chunk$set(fig.path='figures/fig-',fig.ext='pdf',fig.width=2.0, 105 | fig.height=1.5, 106 | fig.show='hold', 107 | fig.align='center', 108 | comment=NA,prompt=TRUE) 109 | @ 110 | 111 | 112 | \setlength{\parskip}{1.0ex plus0.1ex minus0.1ex} 113 | \bibliographystyle{apalike} 114 | 115 | 116 | \title{Modeling in R} 117 | 118 | \hypersetup{% 119 | pdftitle={},% 120 | pdfauthor={Randall Pruim}, 121 | pdfsubject={teaching,computation,software,R,RStudio}, 122 | pdfkeywords={}% 123 | } 124 | 125 | \author{Randall Pruim, Danny Kaplan and Nicholas Horton} 126 | 127 | \date{\copyright 2013} 128 | 129 | \institute{Project MOSAIC} 130 | 131 | %\date{slides available at \url{http://www.calvin.edu/~rpruim/talks/}} 132 | 133 | 134 | \frame{\titlepage} 135 | 136 | %\frame{\frametitle{Outline}\tableofcontents} 137 | 138 | %------------------------------------------------------------------- 139 | 140 | \begin{frame}[fragile] 141 | \frametitle{Kids Feet} 142 | 143 | <<>>= 144 | require(mosaic) 145 | densityplot(~ length, data=KidsFeet) 146 | @ 147 | \end{frame} 148 | 149 | \begin{frame}[fragile] 150 | \frametitle{Kids Feet} 151 | 152 | <<>>= 153 | require(mosaic) 154 | favstats(~ length, data=KidsFeet) 155 | mean(~ length, data=KidsFeet) 156 | sd(~ length, data=KidsFeet) 157 | @ 158 | \end{frame} 159 | 160 | 161 | \begin{frame}[fragile] 162 | \frametitle{Are Boys' Feet Bigger Than Girls'?} 163 | 164 | <<>>= 165 | favstats(length ~ sex, data=KidsFeet) 166 | mean(length ~ sex, data=KidsFeet) 167 | @ 168 | \end{frame} 169 | 170 | %------------------------------------------------------------------- 171 | \begin{frame}[fragile] 172 | \frametitle{Are Boys' Feet Bigger Than Girls'?} 173 | 174 | \smallfonts 175 | <>= 176 | bwplot(length ~ sex, data=KidsFeet) 177 | @ 178 | \end{frame} 179 | 180 | %------------------------------------------------------------------- 181 | \begin{frame}[fragile] 182 | \frametitle{Are Boys' Feet Bigger Than Girls'?} 183 | 184 | \smallfonts 185 | <>= 186 | densityplot(~ length, groups=sex, auto.key=TRUE, data=KidsFeet) 187 | @ 188 | \end{frame} 189 | 190 | \begin{frame}[fragile] 191 | \frametitle{Are Boys' Feet Bigger Than Girls'?} 192 | 193 | <<>>= 194 | t.test(length ~ sex, data=KidsFeet) 195 | @ 196 | \end{frame} 197 | 198 | \begin{frame}[fragile] 199 | \frametitle{Are Boys' Feet Bigger Than Girls'?} 200 | 201 | <<>>= 202 | fm = lm(length ~ sex, data=KidsFeet) 203 | summary(fm) 204 | @ 205 | \end{frame} 206 | 207 | %------------------------------------------------------------------- 208 | 209 | \begin{frame}[fragile] 210 | \frametitle{Are Boys' Feet Bigger Than Girls'?} 211 | 212 | \smallfonts 213 | <>= 214 | xyplot(length ~ width, groups=sex, auto.key=TRUE, data=KidsFeet) 215 | @ 216 | \end{frame} 217 | 218 | 219 | \end{document} 220 | 221 | -------------------------------------------------------------------------------- /sandbox/fixedCI2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/sandbox/fixedCI2.pdf -------------------------------------------------------------------------------- /sandbox/fixedMap2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProjectMOSAIC/LittleBooks/f80a28c6345b8db79c7ba362111cf59b30a4cbad/sandbox/fixedMap2.pdf -------------------------------------------------------------------------------- /sandbox/regression.Rmd: -------------------------------------------------------------------------------- 1 | USCOTS: Exploring data using the modeling language 2 | =========== 3 | ### Horton, Kaplan and Pruim 4 | 5 | #### Introduction and setup 6 | In this activity you will explore the EPA dataset available from within the mosaic package. 7 | Every year, the Environmental Protection Agency (EPA) collects data on fuel economy. With rising gasoline prices, consumers are using these figures as they decide which automobile to purchase. We will look at two-seater automobiles, many of which are sporty vehicles, using data from 2007. 8 | 9 | We begin by loading the mosaic package and setting a graphics them. (Note that for this block we include the *message=FALSE* option to suppress output when we load the package). 10 | ```{r, message=FALSE} 11 | require(mosaic) 12 | trellis.par.set(theme=col.mosaic()) 13 | ``` 14 | 15 | #### Exploratory analyses 16 | Since this data set comes from the mosaic package, we can load it with **data** command (this ensures that we have a fresh copy). We can also see what variables are contained within it. 17 | ```{r} 18 | data(EPA) 19 | names(EPA) 20 | ``` 21 | 22 | The summary() and str() commands give us more information about the variables in the dataframe. 23 | ```{r} 24 | str(EPA) 25 | summary(EPA) 26 | ``` 27 | 28 | It's always a good idea to read the documentation for any data set before working with it, so that you know what the variables are actually measuring. In particular, this *should* tell you important things like: "in what units are the variables measured?" and "how are the categorical variables coded?" 29 | 30 | In this block we've used the *eval=FALSE* option to not run the following command, but just display it. You can also access the *Help* tab on the bottom right pane in RStudio. 31 | ```{r, eval=FALSE} 32 | help(EPA) 33 | ``` 34 | 35 | Let's begin by taking a look at the cars manufactured by Porsche. 36 | ```{r} 37 | subset(EPA, mfr=="PORSCHE") 38 | ``` 39 | 40 | We want to get a sense of the fuel economy (measured in miles per gallon) as a function of the size of the engine (measured in cubic centimeters), and how this might relate to the transmission (Auto vs. Manual). We begin by calculating some summary statistics and graphical displays for miles per gallon. 41 | 42 | ```{r} 43 | favstats(~ mpg, data=EPA) 44 | xhistogram(~ mpg, data=EPA) 45 | ``` 46 | 47 | Next we can do the same for the categorical transmission variable. 48 | ```{r} 49 | tally(~ trans, data=EPA) 50 | ``` 51 | 52 | #### Data derivation 53 | 54 | Let's turn this into a variable with two levels: either Auto or Manual, using the transform() function. 55 | ```{r} 56 | EPA = transform(EPA, transmission = ifelse(grepl("Auto", trans), "Auto", "Manual")) 57 | ``` 58 | 59 | We need to check that this has been appropriately recoded. 60 | ```{r} 61 | tally(~ trans + transmission, data=EPA) 62 | ``` 63 | 64 | #### Analysis: mpg, displacement and transmission type. 65 | Success! Now we can start to undertake an analysis to help answer the question of understanding the relationships between miles per gallon, displacement and transmission type. 66 | 67 | ```{r} 68 | bwplot(mpg~ transmission, data=EPA) 69 | densityplot(~ mpg, groups=transmission, auto.key=TRUE, data=EPA) 70 | ``` 71 | 72 | We can also assess the association between mpg and displacement. 73 | 74 | ```{r} 75 | # Note the use of transparency (the "alpha" option) in order to show multiple points that are right on top of one another. 76 | xyplot(mpg ~ displace, alpha=0.3, data=EPA) 77 | ``` 78 | The pattern seems roughly linear, with a couple of exceptions, so a linear model seems like a reasonable approach. 79 | 80 | Next we can generate a scatterplot of mpg and displacement, separately for each of the transmission types. 81 | ```{r} 82 | # Note the use of transparency (the "alpha" option) in order to show multiple points that are right on top of one another. 83 | xyplot(mpg ~ displace, groups=transmission, auto.key=TRUE, type=c("p", "r"), alpha=0.3, data=EPA) 84 | ``` 85 | 86 | There's not much difference to be seen. Both groups have a moderately strong negative linear relationships, with similar slope and intercept. 87 | 88 | ASSIGNMENT: Is there a difference in terms of drive (rear wheel vs. 4 wheel drive?). Explore the relationship between displacement, MPG and drive. Can you generate a figure or two which characterize the relationship? 89 | 90 | -------------------------------------------------------------------------------- /sandbox/roadless.R: -------------------------------------------------------------------------------- 1 | require(mosaic) 2 | getLocation = function(counter) { 3 | googleMap(myroadless[counter,"latitude"], myroadless[counter,"longitude"], 4 | mark=TRUE, maptype="terrain", radius=1, browse=TRUE) 5 | } 6 | nsamp = 20 7 | long = -runif(nsamp, min=65, max=125) # approximate location of continental US 8 | lat = runif(nsamp,min=25, max=50) # approximate location of continental US 9 | myroadless = data.frame(sample=1:nsamp, latitude=round(lat, 4),longitude=round(long, 4), 10 | withincontinent=c(rep(NA, nsamp)),within1mile=c(rep(NA, nsamp)), 11 | location=character(nsamp),notes=character(nsamp)) 12 | myroadless 13 | -------------------------------------------------------------------------------- /sandbox/roadless.Rmd: -------------------------------------------------------------------------------- 1 | Roadless USA 2 | ======================================================== 3 | 4 | This Markdown file accompanies the handout for the USCOTS 2013 workshop on Teaching Statistics Using R (Horton, Kaplan and Pruim). 5 | 6 | ```{r} 7 | source("http://www.math.smith.edu/~nhorton/roadless.R") 8 | ``` 9 | 10 | Let's display our 20 locations that were generated 11 | 12 | ```{r fig.width=7, fig.height=6} 13 | myroadless 14 | ``` 15 | 16 | Now run each of the following blocks, keeping track of your results as you go. 17 | ```{r} 18 | getLocation(1) 19 | ``` 20 | ```{r} 21 | getLocation(2) 22 | ``` 23 | ```{r} 24 | getLocation(3) 25 | ``` 26 | ```{r} 27 | getLocation(4) 28 | ``` 29 | ```{r} 30 | getLocation(5) 31 | ``` 32 | ```{r} 33 | getLocation(6) 34 | ``` 35 | ```{r} 36 | getLocation(7) 37 | ``` 38 | ```{r} 39 | getLocation(8) 40 | ``` 41 | ```{r} 42 | getLocation(9) 43 | ``` 44 | ```{r} 45 | getLocation(10) 46 | ``` 47 | ```{r} 48 | getLocation(11) 49 | ``` 50 | ```{r} 51 | getLocation(12) 52 | ``` 53 | ```{r} 54 | getLocation(13) 55 | ``` 56 | ```{r} 57 | getLocation(14) 58 | ``` 59 | ```{r} 60 | getLocation(15) 61 | ``` 62 | ```{r} 63 | getLocation(16) 64 | ``` 65 | ```{r} 66 | getLocation(17) 67 | ``` 68 | ```{r} 69 | getLocation(18) 70 | ``` 71 | ```{r} 72 | getLocation(19) 73 | ``` 74 | ```{r} 75 | getLocation(20) 76 | ``` 77 | -------------------------------------------------------------------------------- /sandbox/roadless.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass[10pt]{article} 2 | 3 | \usepackage{amsmath,amssymb,amsthm} 4 | \usepackage{fancyhdr,url} 5 | \usepackage{graphicx} 6 | 7 | \oddsidemargin 0in %0.5in 8 | \topmargin 0in 9 | \leftmargin 0in 10 | \rightmargin 0in 11 | \textheight 9in 12 | \textwidth 6in %6in 13 | %\headheight 0in 14 | %\headsep 0in 15 | %\footskip 0.5in 16 | 17 | 18 | \pagestyle{fancy} 19 | 20 | \lhead{\textsc{USCOTS 2013 Workshop}} 21 | \chead{\textsc{Teaching Stats with R}} 22 | \rhead{\textsc{May, 2013}} 23 | \lfoot{} 24 | \cfoot{} 25 | %\cfoot{\thepage} 26 | \rfoot{} 27 | \renewcommand{\headrulewidth}{0.2pt} 28 | \renewcommand{\footrulewidth}{0.0pt} 29 | 30 | \newcommand{\ans}{\vspace{1in}} 31 | 32 | \begin{document} 33 | %\maketitle 34 | 35 | \paragraph{Roadless America} 36 | 37 | Wilderness is, by definition, roadless. Untouched areas provide important habitat and play a key role in ensuring ecological balance. According to the U.S. Geological Survey, roads affect the ecology of at least 22\% of the land area of the continental United States. Knowing what portion of the US is within 1 mile of a road can help determine wilderness areas since there is a link between access by road and human presence. 38 | 39 | A conservation policy called \emph{roadless area conservation} aims to limit road construction in order to halt negative environmental impact on designated public lands. The US Forest Service has formalized the concept of \emph{Inventoried Roadless Areas} as lands identified by governmental agencies without roads that could be candidates for roadless area conservation. According to the US Forest Service, inventoried roadless areas comprise only 2\% of the land in the continental United States. 40 | 41 | This activity is intended to help reinforce concepts of sampling and confidence intervals while simultaneously understanding how to estimate the proportion of the United States that is within 1 mile of a road. 42 | 43 | We've included this in the workshop to demonstrate how to integrate computing to bring data into the classroom (using samples of locations on the earth). We will utilize the {\tt googleMap()} function within the {\tt mosaic} package using a helper routine which we've defined (called {\tt getLocation()}). 44 | 45 | You will work in pairs, and will need one computer per group. Please run the following command to load the necessary commands: 46 | <>= 47 | source("http://www.math.smith.edu/~nhorton/roadless.R") 48 | @ 49 | This will create a data frame in your workspace called {\tt myroadless}. 50 | 51 | Now, one at a time, run the {\tt getLocation()} function. This takes a single argument, which indicates which of the twenty samples to plot. For example, the first command will be: 52 | <>= 53 | getLocation(1) 54 | @ 55 | This will pop up a map of your location, with a circle of radius one mile. Here's an example of a location that is near a road. 56 | 57 | \noindent 58 | \includegraphics[width=0.95\textwidth]{NearRoad.pdf} 59 | 60 | \newpage 61 | For each of your 20 samples, you should takes notes of whether: 62 | \begin{itemize} 63 | \item it was within the continental US 64 | \item if so, whether it was within a mile of a road, 65 | \item notes about the location (e.g. is it in Canada, Mexico, Atlantic Ocean, Pacific, Gulf or in the US which state) 66 | \end{itemize} 67 | 68 | \begin{tabular}{|l|c|c|c|} 69 | \hline 70 | \# & w/in cont. & w/in 1 mile & notes/descript \\ \hline \hline 71 | 1 & & & \\ \hline 72 | 2 & & & \\ \hline 73 | 3 & & & \\ \hline 74 | 4 & & & \\ \hline 75 | 5 & & & \\ \hline 76 | 6 & & & \\ \hline 77 | 7 & & & \\ \hline 78 | 8 & & & \\ \hline 79 | 9 & & & \\ \hline 80 | 10 & & & \\ \hline 81 | \end{tabular} 82 | \begin{tabular}{|l|c|c|c|} 83 | \hline 84 | \# & w/in cont. & w/in 1 mile & notes/descript \\ \hline \hline 85 | 11 & & & \\ \hline 86 | 12 & & & \\ \hline 87 | 13 & & & \\ \hline 88 | 14 & & & \\ \hline 89 | 15 & & & \\ \hline 90 | 16 & & & \\ \hline 91 | 17 & & & \\ \hline 92 | 18 & & & \\ \hline 93 | 19 & & & \\ \hline 94 | 20 & & & \\ \hline 95 | \end{tabular} 96 | 97 | \noindent 98 | \begin{enumerate} 99 | \item Calculate the number within both the continental US and 1 mile of a road ($X$) 100 | \item Calculate the total number within the continent 101 | ($n$). 102 | \item Calculate their ratio. This is your observed proportion. 103 | \item Calculate a 95\% confidence interval for the population proportion. The command {\tt require(mosaic); binom.test(x, n)} will calculate this interval for you 104 | \item Add this interval on the board (along with your values of $X$ and $n$). 105 | \end{enumerate} 106 | 107 | \vspace{.2in} 108 | \noindent 109 | $X = $ 110 | 111 | \vspace{.2in} 112 | \noindent 113 | $n = $ 114 | 115 | \vspace{.2in} 116 | \noindent 117 | $\hat{p}=$ 118 | 119 | \vspace{.2in} 120 | \noindent 121 | Your 95\% confidence interval for the proportion of the US that is within 1 mile of a road: 122 | 123 | \vspace{0.4in} 124 | \noindent 125 | CLASS RESULTS: 126 | 127 | 128 | \vspace{1.4in} 129 | 130 | As an extension, you can collect the annotated spreadsheets and create a map of all of the locations (combining the groups). 131 | \newpage 132 | \noindent 133 | \includegraphics[width=1\textwidth]{fixedCI2.pdf} 134 | 135 | \noindent 136 | \includegraphics[width=1\textwidth]{fixedMap2.pdf} 137 | 138 | 139 | 140 | 141 | \end{document} 142 | -------------------------------------------------------------------------------- /sandbox/transferring.Rmd: -------------------------------------------------------------------------------- 1 | Transferring Files from the `mosaic-web.org` repository 2 | ======================================================== 3 | 4 | ### March 19, 2013 5 | 6 | * ./Chap-ByHand.Rnw --- moved 7 | * ./Chap-CalculusForStats.Rnw --- moved 8 | * ./Chap-Core.Rnw --- moved 9 | * ./Chap-ForAuthors.Rnw --- included in outline 10 | * ./Chap-FrontMatter.Rnw --- moved 11 | * ./Chap-GettingStarted.Rnw --- moved. 12 | * ./Chap-GettingStartedshort.Rnw --- seems to be the same thing as Chap-GettingStarted 13 | * ./Chap-Handouts.Rnw --- moved 14 | * ./Chap-Internet.Rnw --- moved 15 | * ./Chap-Manipulate.Rnw --- moved 16 | * ./Chap-Misc.Rnw --- included in outline 17 | * ./Chap-MoreExamples.Rnw --- moved 18 | * ./Chap-MoreR.Rnw --- moved 19 | * ./Chap-Multivariable.Rnw --- moved 20 | * ./Chap-RForInstructors.Rnw --- moved 21 | * ./Chap-RForStudents.Rnw --- moved 22 | * ./Chap-RIntro.Rnw --- moved 23 | * ./Chap-SimulationBased.Rnw --- moved 24 | * ./Dierker.Rnw 25 | * ./Master-TeachingStatsWithR.Rnw --- not needed 26 | * ./Scraps.Rnw 27 | * ./Testing.Rnw 28 | 29 | 30 | --------------------------------------------------------------------------------