---
title: "IS4 in R: The Standard Deviation as a Ruler and the Normal Model (Chapter 5)"
author: "Patrick Frenett, Vickie Ip, and Nicholas Horton (nhorton@amherst.edu)"
date: "June 20, 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
  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.

## Chapter 5: The Standard Deviation as a Ruler and the Normal Model

### Section 5.1: Standardizing with z-scores

From page 111

```{r}
library(mosaic) 
library(readr)
library(ggformula)
options(na.rm = TRUE)
options(digits = 3)
(6.63 - 6.11)/0.238  # Dobrynska's jump was 2.18 SD's greater than the mean
```

```{r}
twohund <- as.vector(c(23.2, 23.3, 23.3, 23.6, 23.9, 23.9, 24.2, 24.2, 24.3, 
                     24.3, 24.3, 24.3, 24.3, 24.4, 24.5, 24.5, 24.6, 24.6, 
                     24.6, 24.7, 24.7, 24.9, 24.9, 24.9, 25.0, 25.0, 25.0, 
                     25.2, 25.3, 25.4, 25.4, 25.4, 25.4, 25.5, 25.9, 25.9, 26.1))
twohund <- data.frame(twohund)
df_stats(~ ., data = twohund)
```

### 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 120)

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

### Section 5.4: Finding normal percentiles

as on page 121

```{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 122

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

and page 123

```{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 Figure 5.8 on page 127

```{r message = FALSE}
Nissan <- 
read_delim("http://nhorton.people.amherst.edu/sdm4/data/Nissan.txt",
  delim = "\t")
```

```{r}
gf_histogram(..density.. ~ mpg, binwidth = 1, center = 0.5, 
             data = Nissan, fill = "royalblue2", col = TRUE)
gf_qq(~ mpg, data = Nissan) %>%
  gf_labs(x = "qnorm", y = "mpg")
```
