---
title: "IS4 in R: Understanding and Comparing Distributions (Chapter 4)"
author: "Patrick Frenett, Vickie Ip, and Nicholas Horton (nhorton@amherst.edu)"
date: "June 20, 2018"
output:
pdf_document:
fig_height: 3.3
fig_width: 6
html_document:
fig_height: 3
fig_width: 5
word_document:
fig_height: 4
fig_width: 6
---
```{r, include = FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaic)
```
```{r, include = FALSE}
# knitr settings to control how R chunks work.
require(knitr)
opts_chunk$set(
tidy = FALSE, # display code as typed
size = "small", # slightly smaller font for code
fig.align = "center"
)
```
## Introduction and Background
This document is intended to help describe how to undertake analyses introduced
as examples in the Fourth Edition of *Intro Stats}* (2013) by De Veaux, Velleman, and Bock.
More information about the book can be found at http://wps.aw.com/aw_deveaux_stats_series. This file as well as the associated R Markdown reproducible analysis source file used to create it can be found at https://nhorton.people.amherst.edu/is4.
This work leverages initiatives undertaken by Project MOSAIC (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 `mosaic` package, which was written to simplify the use of R for introductory statistics courses. A short summary of the R needed to teach introductory statistics can be found in the mosaic package vignettes (http://cran.r-project.org/web/packages/mosaic). A paper describing the mosaic approach was published in the *R Journal*: https://journal.r-project.org/archive/2017/RJ-2017-024.
Note that some of the figures in this document may differ slightly from those in the IS4 book due to small differences in datasets. However in all cases the analysis and techniques in R are accurate.
## Chapter 4: Understanding and Comparing Distributions
### Section 4.1: Comparing Groups with Histograms
See Figure 4.1 on page 83.
```{r message = FALSE}
library(mosaic)
library(readr)
library(ggformula)
options(digits = 3)
Hopkins <- read_delim("http://nhorton.people.amherst.edu/sdm4/data/Hopkins_Forest_2011.txt",
delim = "\t")
```
```{r}
names(Hopkins)
gf_histogram(..density.. ~ AvgWindSpeed, binwidth = 0.5, center = 0.24,
data = Hopkins, fill = "coral1", color = TRUE) %>%
gf_labs(x = "Average wind speed (mph)", y = "Density")
```
Here we reproduce Figure 4.2 on page 83.
XX Figures y variable does not match textbook, summary outputs differ also
```{r}
Hopkins <- mutate(Hopkins,
Summer = Month >= 4 & Month <= 9,
Winter = !Summer
)
gf_histogram(..density.. ~ AvgWindSpeed, binwidth = 0.5, center = 0.24,
data = filter(Hopkins, Summer == TRUE),
fill = "blue2", color = TRUE) %>%
gf_labs(x = "Average summer wind speed (mph)", y = "Density")
df_stats(~ AvgWindSpeed, data = filter(Hopkins, Summer == TRUE))
gf_histogram(..density.. ~ AvgWindSpeed, binwidth = 0.5, center = 0.24,
data = filter(Hopkins, Winter == TRUE),
fill = "darkolivegreen3", color = TRUE) %>%
gf_labs(x = "Average winter wind speed (mph)",y = "Density")
df_stats(~ AvgWindSpeed, data = filter(Hopkins, Winter == TRUE))
```
### Section 4.2: Comparing Groups with Boxplots
Here we reproduce Figure 4.3 on page 85.
```{r}
gf_boxplot(AvgWindSpeed ~ as.factor(Month),
data = Hopkins, col = "royalblue3") %>%
gf_labs(x = "Month")
```
### Section 4.3: Outliers
```{r}
filter(Hopkins, Month == 2, AvgWindSpeed > 6) # in February
filter(Hopkins, Month == 6, AvgWindSpeed > 3.9) # in June
```
### Section 4.4: Timeplots: Order, Please!
See Figures 4.4 through 4.6 starting on page 90.
```{r message = FALSE}
gf_point(AvgWindSpeed ~ DayofYear, data = Hopkins, color = "mediumpurple")
gf_point(AvgWindSpeed ~ DayofYear, data = Hopkins, alpha = 0) %>%
gf_line(col = "navy")
gf_point(AvgWindSpeed ~ DayofYear,data = Hopkins) %>%
gf_smooth(se = FALSE)
```