---
title: "SDM4 in R: Testing Hypotheses about Proportions (Chapter 19)"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: "January 2, 2017"
output: 
  html_document:
    fig_height: 5
    fig_width: 7
  pdf_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}
# 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
)
```

## Introduction and background 

This document is intended to help describe how to undertake analyses introduced 
as examples in the Fourth Edition of \emph{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).

## Chapter 19: Testing hypotheses for proportions

### Section 19.1: Hypotheses

We can reproduce the calculation in Figure 19.1 (page 495).

```{r}
sdp <- sqrt(.2*.8/400); sdp
xpnorm(0.17, mean=0.20, sd=sdp)
zval <- (0.17 - 0.20)/sdp; zval
pnorm(zval, mean=0, sd=1)
```

### Section 19.3: Reasoning of hypothesis testing

The "For Example (page 499)" lays out how to find a p-value for the one proportion z-test.

```{r}
y <- 61; n <- 90; phat <- y/n; phat
nullp <- 0.8
sdp <- sqrt(nullp*(1-nullp)/n); sdp
onesidep <- xpnorm(phat, mean=nullp, sd=sdp); onesidep
twosidep <- 2*onesidep; twosidep
```

or we can carry out the exact test (not described by the book):

```{r}
binom.test(y, n, p=nullp)
```
