---
title: "SDM4 in R: Understanding and Comparing Distributions (Chapter 4)"
author: "Nicholas Horton (nhorton@amherst.edu) and Sarah McDonald"
date: "June 13, 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
)
```

## Introduction and background 

This document is intended to help describe how to undertake analyses introduced 
as examples in the Fourth Edition of *Stats: Data and Models* (2014) 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 http://nhorton.people.amherst.edu/sdm4.

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.

## Chapter 4: Understanding and comparing distributions

### Section 4.1: Comparing groups with histograms

See Figure 4.1 on page 85

```{r message = FALSE}
library(mosaic)
library(readr)
options(digits = 3)
Hopkins <- 
read_delim("http://nhorton.people.amherst.edu/sdm4/data/Hopkins_Forest_2011.txt", delim = "\t")
names(Hopkins)   
gf_histogram(~ AvgWindSpeed, binwidth = 0.5, center = 0.24, 
          xlab = "Average wind speed (mph)", data = Hopkins)
```

Here we reproduce Figure 4.2 on page 85

```{r}
Hopkins <- mutate(Hopkins,
  Summer = Month >= 4 & Month <= 9,
  Winter = !Summer
)
gf_histogram(~ AvgWindSpeed, binwidth = 0.5, center = 0.24, 
          xlab = "Average summer wind speed (mph)", data = filter(Hopkins, Summer == TRUE))
favstats(~ AvgWindSpeed, data = filter(Hopkins, Summer == TRUE))
gf_histogram(~ AvgWindSpeed, binwidth = 0.5, center = 0.24, 
          xlab = "Average winter wind speed (mph)", data = filter(Hopkins, Winter == TRUE))
favstats(~ AvgWindSpeed, data = filter(Hopkins, Winter == TRUE))

```

### Section 4.2: Comparing groups with boxplots

Here we reproduce Figure 4.3 on page 87

```{r}
gf_boxplot(AvgWindSpeed ~ as.factor(Month), data = Hopkins)
```

### 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 92
```{r message = FALSE}
gf_point(AvgWindSpeed ~ DayofYear, data = Hopkins)
gf_point(AvgWindSpeed ~ DayofYear, data = Hopkins) %>%
  gf_lm()
gf_point(AvgWindSpeed ~ DayofYear, lwd = 3, data = Hopkins) %>%
  gf_lm() %>%
  gf_smooth(se = FALSE)
```


### Section 4.5: Re-expressing data: A first look

See Figure 4.7 on page 94
```{r message = FALSE}
CEO <- read_delim("http://nhorton.people.amherst.edu/sdm4/data/CEO_Salary_2012.txt", delim = "\t")
favstats(~ One_Year_Pay, data = CEO)
gf_histogram(~ One_Year_Pay, binwidth = 2.5, center = 1.24, data = CEO)
```

Figure 4.8 on page 95
```{r}
nrow(CEO)   # let's get rid of the CEO's with 0 salaries...
CEO <- filter(CEO, One_Year_Pay > 0)
nrow(CEO)
CEO <- mutate(CEO, log10salary = log10(One_Year_Pay * 1000000))
gf_histogram(~ log10salary, binwidth = .25, center = .124, data = CEO)
```

On the log 10 scale, we can roughly interpret the values as the number
of digits in the CEO salary.
