---
title: "SDM4 in R: The Standard Deviation as a Ruler and the Normal Model (Chapter 5)"
author: "Nicholas Horton (nhorton@amherst.edu) and Sarah McDonald"
date: "June 13, 2018"
output: 
  pdf_document:
    fig_height: 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 5: The standard deviation as a ruler and the normal model

### Section 5.1: Standardizing with z-scores


```{r}
library(mosaic)
library(readr)
options(na.rm = TRUE)
options(digits = 3)
(6.54 - 5.91)/0.56  # should be 1.1 sd better, see page 112
```

```{r message = FALSE}
Heptathlon <- 
read_delim("http://nhorton.people.amherst.edu/sdm4/data/Womens_Heptathlon_2012.txt",
  delim = "\t")
nrow(Heptathlon)  
filter(Heptathlon, LJ >= max(LJ, na.rm = TRUE)) %>% 
  data.frame()
favstats(~ LJ, data = Heptathlon)
(6.54 - mean(~ LJ, data = Heptathlon))/sd(~ LJ, data = Heptathlon)
```

### Section 5.2: Shifting and scaling

### Section 5.3: Normal models

The 68-95-99.7 rule

```{r, message = FALSE, warning = FALSE}
xpnorm(c(-3, -1.96, -1, 1, 1.96, 3), mean = 0, sd = 1, verbose = FALSE)
xpnorm(c(-3, -1.96, 1.96, 3), mean = 0, sd = 1, verbose = FALSE)
xpnorm(c(-3, 3), mean = 0, sd = 1, verbose = FALSE)
```

Step-by-step (page 122)

```{r}
xpnorm(600, mean = 500, sd = 100)
```

### Section 5.4: Finding normal percentiles

as on page 123

```{r}
xpnorm(680, mean = 500, sd = 100)
qnorm(0.964, mean = 500, sd = 100)   # inverse of pnorm()
qnorm(0.964, mean = 0, sd = 1)   # what is the z-score?
```

or on page 124

```{r}
xpnorm(450, mean = 500, sd = 100)
```

and page 125

```{r}
qnorm(.9, mean = 500, sd = 100)
qnorm(.9, mean = 0, sd = 1)   # or as a Z-score
```

### Section 5.5: Normal probability plots

See (sideways) Figure 5.8 on page 129

```{r message = FALSE}
Nissan <- 
read_delim("http://nhorton.people.amherst.edu/sdm4/data/Nissan.txt",
  delim = "\t")
gf_histogram(~ mpg, binwidth = 1, center = 0.5, data = Nissan)
```
```{r}
gf_qq(~ mpg, data = Nissan)
```
