---
title: "SDM4 in R: Inferences about Means (Chapter 20)"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: "June 13, 2018"
output:
pdf_document:
fig_height: 2.8
fig_width: 7
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)
options(digits = 3)
```
```{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 20: Inferences about Means
### Section 20.1: The Central Limit Theorem
Let's begin by reproducing the figure on the bottom of page 519.
```{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 20.2: Gosset's t
Figure 20.1 (page 521) 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", lty = 2, col = "green", lwd = 2, xlim = c(-8, 8)) %>%
gf_dist("t", params = 2, lty = 1, lwd = 2, col = "blue", add = TRUE,
xlim = c(-8, 8))
gf_dist("norm", lty = 2, col = "green", lwd = 2) %>%
gf_dist("t", params = 2, lty = 1, lwd = 2, col = "blue", xlim = c(-3, 3))
```
We can reproduce the calculations for the Farmed salmon example (pages 523-524) 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 warning = FALSE}
Salmon <- read.csv("http://nhorton.people.amherst.edu/sdm4/data/Farmed_Salmon.csv")
favstats(~ Mirex, data = Salmon)
gf_histogram(~ Mirex, binwidth = 0.01, center = 0.01/2, data = Salmon)
t.test(~ Mirex, data = Salmon)
```
We note that the distribution of measurements is not particularly normal.
#### Section 20.4: A hypothesis test for the mean
We can carry out the one-sided test outlined on page 530:
```{r}
tval <- (.0913 - 0.08)/0.0040
tval
1-xpt(tval, df = 149)
```