---
title: "IPS9 in R: Inference for Proportions (Chapter 8)"
author: "Bonnie Lin and Nicholas Horton (nhorton@amherst.edu)"
date: "January 19, 2019"
output:
pdf_document:
fig_height: 3
fig_width: 5
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.
knitr::opts_chunk$set(
tidy = FALSE, # display code as typed
size = "small" # slightly smaller font for code
)
```
## Introduction and background
These documents are intended to help describe how to undertake analyses introduced
as examples in the Ninth Edition of \emph{Introduction to the Practice of Statistics} (2017) by Moore, McCabe, and Craig.
More information about the book can be found [here](https://macmillanlearning.com/Catalog/product/introductiontothepracticeofstatistics-ninthedition-moore).
The data used in these documents can be found under Data Sets in the [Student Site](https://www.macmillanlearning.com/catalog/studentresources/ips9e?_ga=2.29224888.526668012.1531487989-1209447309.1529940008#). 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/ips9/.
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 8: Inference for Proportions
This file replicates the analyses from Chapter 8: Inference for Proportions.
First, load the packages that will be needed for this document:
```{r load-packages}
library(mosaic)
library(readr)
```
### Section 8.1: Inference for a single proportion
```{r eg8-1, message=FALSE}
ROBOT <- read_csv("https://nhorton.people.amherst.edu/ips9/data/chapter08/EG08-01ROBOT.csv")
ROBOT
### Example 8.2, page 487
prop.test(910, 910 + 986,
alternative="two.sided",
conf.level=0.95)
```
By default, the `read_csv()` function will output the types of columns, as we see above.
To improve readability for future coding, we will suppress the "Parsed with column
specification" message by adding `message = FALSE` at the top of the code chunks.
```{r eg8-5, message=FALSE}
### Example 8.5 and 8.6, page 492-493
prop.test(13, 20, correct = FALSE)
```
Here we can replicate the results in the book using the `prop.test` function with the `correct = FALSE` option to turn off continuity correction. A more appropriate approach given the small sample size would be to use exact inference and the `binom.test()` function.
```{r}
binom.test(13, 20)
```
```{r eg8-11, message=FALSE}
INSTAG <- read_csv("https://nhorton.people.amherst.edu/ips9/data/chapter08/EG08-11INSTAG.csv")
### Example 8.11, page 507
INSTAG_prop <- INSTAG %>%
group_by(Sex) %>%
mutate(Prop_by_Sex = Count/sum(Count)) %>%
ungroup() %>%
filter(User == "Yes")
INSTAG_prop
```
```{r}
### Example 8.15, page 513
prop.test(x = c(328, 234), n = c(537, 532), correct = FALSE)
```
```{r}
### Example 8.16, page 515
zstar = qnorm(.975) # for 95% confidence interval
p = 0.5 # planned proportion estimate
moe = 0.1 # margin of error
p*(zstar/moe)^2
### Example 8.17, page 516
### Power curve for two proportions
calculatePower <- function(altmean = x, prop1 = 0.4, prop2 = 0.6, n = 97, alpha = 0.05){
range <- 1-alpha/2
se <- sqrt((prop1*(1-prop2)/n) + (prop2*(1-prop1)/n))
bound1 <- qnorm(range, mean = 0.6, se) #sig lvl in null dist
bound2 <- qnorm(alpha/2, mean = 0.6, se)
1 - pnorm(bound1, altmean, se) + pnorm(bound2, altmean, se) #calc area past sig lvl
}
gf_function(fun = calculatePower, xlim = c(0.3, 0.9))
```
### Section 8.2: Comparing two proportions