---
title: "IPS9 in R: Logistic Regression (Chapter 14)"
author: "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 14: Logistic Regression
This file replicates the analyses from Chapter 14: Logistic regression.

First, load the packages that will be needed for this document: 
```{r load-packages}
library(mosaic)
library(readr)
```

### Section 14.1: The Logistic Regression Model

#### Example 14.3: Comparing the proportions of female and make Instagram users

```{r}
Instagram <- read_csv("https://nhorton.people.amherst.edu/ips9/data/chapter14/EG14-03INSTAGR.csv")
Instagram

InstaMatrix <- matrix(c(Instagram$Count), nrow = 2)
rownames(InstaMatrix) <- c("Yes", "No")
colnames(InstaMatrix) <- c("Women", "Men")
InstaMatrix

oddsRatio(InstaMatrix, verbose = TRUE)
```

#### Example 14.6: Is a movie going to be profitable?

```{r}
Movies <- read_csv("https://nhorton.people.amherst.edu/ips9/data/chapter14/EG14-06MOVIES.csv")

# Log odds
moviemod <- glm(as.factor(Profit) ~ LOpening, data = Movies, family = "binomial")
moviemod

# Figure 14.3, page 8
gf_point(Profit ~ LOpening, data = Movies) %>%
  gf_smooth(span = 2) %>%
  gf_labs(x = "Log (opening)", title = "Profit vs. log (opening)") # to adjust smoothness
```

### Section 14.2: Inference for Logistic Regression

```{r}
msummary(moviemod)
```

#### Example 14.7: Software output

```{r}
Instagram <- read_csv("https://nhorton.people.amherst.edu/ips9/data/chapter14/EG14-07INSTAGR.csv")

# XX not sure how to do this
```

#### Example 14.8: An insecticide for aphids

```{r}
Insecticide <- read_csv("https://nhorton.people.amherst.edu/ips9/data/chapter14/EG14-08INSECTS.csv")

# Figure 14.8, page 12
#insectmod <- glm()

#gf_point()
```


