---
title: "SDM4 in R: Comparing Counts (Chapter 24)"
author: "Nicholas Horton (nhorton@amherst.edu) and Sarah McDonald"
date: "June 28, 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 = 5)
```

```{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 24: Comparing Counts

### Section 24.1: Goodness-of-fit tests


Here we verify the calculations of expected counts for
ballplayers by month (page 656).
```{r}
ballplayer <- c(137, 121, 116, 121, 126, 114, 
                102, 165, 134, 115, 105, 122)
national <- c(0.08, 0.07, 0.08, 0.08, 0.08, 0.08,
              0.09, 0.09, 0.09, 0.09, 0.08, 0.09)
n <- sum(~ ballplayer)
n
sum(~ national)
expect <- n*national
cbind(ballplayer, expect)
```

The chi-square quantile values in the table on the bottom of page 658 can be verified using the `xqt()` function.
```{r}
xqchisq(c(.90, .95, .975, .99, .995), df = 1)
```
These results match the first row: other values can be calculated by changing the
`df` argument.

The goodness of fit test on page 659 can be verified by calculating the
chi-square statistic.

```{r}
chisq <- sum((ballplayer - expect)^2/expect)
chisq
1-xpchisq(chisq, df = 11)
```

### Section 24.2: Chi-square test of homogeneity

Data from one university regarding
the association between postgraduation activity and area of study
is displayed in Table 24.1 (page 663).   The `do()` function can be used
to generate each of the rows in the table.


```{r}
schooldata <- rbind(
  do(209) * data.frame(activity = "agriculture",  area = "Employed"),
  do(198) * data.frame(activity = "arts/science", area = "Employed"),
  do(177) * data.frame(activity = "engineering",  area = "Employed"), 
  do(101) * data.frame(activity = "ILR",          area = "Employed"),
  do(104) * data.frame(activity = "agriculture",  area = "Grad school"),
  do(171) * data.frame(activity = "arts/science", area = "Grad school"),
  do(158) * data.frame(activity = "engineering",  area = "Grad school"), 
  do(33) *  data.frame(activity = "ILR",          area = "Grad school"),
  do(135) * data.frame(activity = "agriculture",  area = "Other"),
  do(115) * data.frame(activity = "arts/science", area = "Other"),
  do(39) *  data.frame(activity = "engineering",  area = "Other"), 
  do(16) *  data.frame(activity = "ILR",          area = "Other")
)
tally(~ area + activity, margins = TRUE, data = schooldata)
```


```{r fig.height = 6}
vcd::mosaic(tally(~ activity + area, data = schooldata), 
  main = "mosaicplot of activity by area",
  shade = TRUE)
```

```{r}
xchisq.test(tally(~ activity + area, data = schooldata))
```

### Section 24.3: Examining the residuals

Note that the `xchisq.test()` function displays the standardized
residuals as the last item in each cell of the table (and these
match the results in Table 24.4 on page 668).  
