---
title: "Paired Samples and Blocks (Chapter 21)"
author: "Patrick Frenett, Vickie Ip, and Nicholas Horton (nhorton@amherst.edu)"
date: "June 19, 2018"
output:
pdf_document:
fig_height: 3.7
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.
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 21: Paired samples and Blocks
### Section 21.1: Paired data
The example on page 586 compares the mileage of 11 field workers using
either a 5 day or 4 day schedule.
```{r}
require(mosaic)
fiveday <- c(2798, 7724, 7505, 838, 4592, 8107, 1228, 8718, 1097, 8089, 3807)
fourday <- c(2914, 6112, 6177, 1102, 3281, 4997, 1695, 6606, 1063, 6392, 3362)
ds <- data.frame(fiveday, fourday)
ds <- mutate(ds, diff = fiveday - fourday)
ds
```
### Section 21.2: Assumptions and conditions
```{r}
gf_histogram(..density.. ~ diff, binwidth = 1000, center = 500/2, data = ds, fill = "violet",
col = TRUE, alpha = 0.6) %>%
gf_labs(y = "Density") # page 589
t.test(~ diff, data = ds)
```
### Section 21.3: Confidence intervals for matched pairs
The same result is seen as on page 595 for the confidence interval
for the population difference in mileage using the (results not shown).
```{r eval = FALSE}
t.test(~ diff, data = ds)$conf.int
```