---
title: "Inferences about Means (Chapter 18)"
author: "Patrick Frenett, Vickie Ip, and Nicholas Horton (nhorton@amherst.edu)"
date: "June 16, 2016"
output: 
  pdf_document:
    fig_height: 4
    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}
# Some customization.  You can alter or delete as desired (if you know what you are doing).

# This changes the default colors in lattice plots.
trellis.par.set(theme=theme.mosaic())  

# 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 \emph{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 18: Inferences about Means

### Section 18.1: The Central Limit Theorem

Let's begin by reproducing the figure on the bottom of page 475.

```{r}
mu <- 1309
sd <- 15.7
xpnorm(c(mu-3*sd, mu-2*sd, mu-sd, mu+sd, mu+2*sd, mu+3*sd), mean=mu, sd=sd)
```

### Section 18.2: Gosset's t

Figure 18.1 (page 477) displays a normal curve (dashed green curve) and a t-model with 2 degrees of freedom (solid blue curve).

```{r fig.keep="last"}
gf_dist("norm", col="green", linetype="dashed")

gf_dist("norm", col="green", linetype="dashed", xlim=c(-4,4)) %>%
  gf_dist("t", params=2, col="blue", xlim=c(-4,4))
```

We can reproduce the calculations for the Farmed salmon example (pages 479-480) using summary statistics:

```{r}
n <- 150; ybar <- 0.0913; s = 0.0495
tstar <- qt(0.975, df=n-1); tstar
ybar + c(-tstar, tstar)*s/sqrt(n)
```

or directly:

```{r message=FALSE}
Salmon <- read.csv("https://nhorton.people.amherst.edu/sdm4/data/Farmed_Salmon.csv")
```
```{r}
df_stats(~ Mirex, data=Salmon)
gf_histogram(..density..~ Mirex, binwidth=0.01, center=0.01/2, data=Salmon,fill="royalblue2", col=TRUE) %>%
  gf_labs(y="Density")
t.test(~ Mirex, data=Salmon)
```

We note that the distribution of measurements is not particularly normal.

#### Section 18.4: A hypothesis test for the mean

We can carry out the one-sided test outlined on page 486:

```{r}
tval <- (.0913-0.08)/0.0040; tval
1-xpt(tval, df=149)
```

