---
title: "IS5 in R: More About Tests and Intervals (Chapter 16)"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: "December 19, 2020"
output: 
  pdf_document:
    fig_height: 3
    fig_width: 5
  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
library(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 Fifth Edition of *Intro Stats* (2018) by De Veaux, Velleman, and Bock.
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/is5.

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 (https://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 16: More About Tests and Intervals

```{r}
library(mosaic)
```

### Section 16.1: Interpreting P-Values

#### What to Do with a Low P-Value

#### What to Do with a High P-Value

No need for tables: we can calculate everything in R!

```{r, fig.width=7}
# curve on page 511
xqnorm(p = .467, mean = 0, sd = 1, verbose = FALSE)
```

### Section 16.2: Alpha Levels and Critical Values

```{r fig.width = 7}
# Figure 16.1, page 513
xpnorm(q = c(-1.96, 1.96), mean = 0, sd = 1, verbose = FALSE)
```

### Section 16.3: Practical vs. Statistical Significance

### Section 16.4: Errors

#### Power

#### Effect Size

#### A Picture Worth $\frac{1}{P(z > 3.09)}$ Words

When in doubt, draw a picture!

```{r, fig.width=7, fig.height=4, warning = FALSE}
# Figure 16.2, page 520
gf_dist("norm",
  mean = 0, sd = 1, fill = ~ cut(x, c(-Inf, 2, 100, Inf)), geom = "area",
  alpha = .5
) %>%
  gf_dist("norm",
    mean = 4, sd = 1, fill = ~ cut(x, c(-Inf, -100, 2, Inf)), geom = "area",
    alpha = .5
  ) %>%
  gf_labs(x = "p", y = "") %>%
  gf_vline(xintercept = 2) %>%
  gf_refine(annotate(geom = "text", x = .75, y = .42, label = "Fail to Reject H0")) %>%
  gf_refine(annotate(geom = "text", x = 2.95, y = .42, label = "Reject H0")) %>%
  gf_refine(annotate(geom = "text", x = 0, y = .15, size = 3, label = "Suppose H0 is true")) %>%
  gf_refine(annotate(geom = "text", x = 1.35, y = .01, size = 2.5, label = "Type 2 Error")) %>%
  gf_refine(annotate(geom = "text", x = 2.6, y = .01, size = 2.5, label = "Type 1 Error")) %>%
  gf_refine(annotate(geom = "text", x = 4, y = .15, size = 3, label = "Suppose H0 is not true")) +
  guides(fill = FALSE) # To remove the legend
```
