---
title: "Confidence Intervals for Population Means"
author: "Evan L. Ray"
date: "November 10, 2017"
output: ioslides_presentation
---

<script type="text/x-mathjax-config"> 
    MathJax.Hub.Config({ 
        "HTML-CSS": { scale: 90, linebreaks: { automatic: true } }, 
        SVG: { linebreaks: { automatic:true } }, 
        displayAlign: "left" });
</script>


<style>
  .col2 {
    columns: 2;         /* number of columns and width in pixels*/
    -webkit-columns: 2; /* chrome, safari */
    -moz-columns: 2;    /* firefox */
  }
  .col3 {
    columns: 3 100px;
    -webkit-columns: 3 100px;
    -moz-columns: 3 100px;
  }
</style>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, cache = FALSE)
require(ggplot2)
require(scales)
require(dplyr)
require(tidyr)
require(readr)
require(mosaic)
```

<style> 
  slides > slide > hgroup + article { 
    margin-top: 10px; 
  } 
</style>

## $t$ Distribution

* The $t$ distribution is similar to the Normal$(0, 1)$
* The $t$ has more probability in the tails
* As the degrees of freedom increases, the $t$ becomes more like a Normal$(0, 1)$

```{r, echo = FALSE, fig.height=3.5}
x_grid <- seq(from = -5, to = 5, by = 0.01)
n_grid <- length(x_grid)

mu1 <- 0
sigma1 <- 1

df <- c(2, 5, 20)

plot_df <- data.frame(
  x = rep(x_grid, 1 + length(df)),
  density = c(
    dnorm(x_grid, mean = mu1, sd = sigma1),
    dt(x_grid, df = df[1]),
    dt(x_grid, df = df[2]),
    dt(x_grid, df = df[3])
  ),
  model = c(
    rep("Normal", n_grid),
    rep(paste0("df_", df), each = n_grid)
  )
)

ggplot() +
  geom_line(aes(x = x, y = density, color = model), size = 0.8, data = plot_df) +
  scale_color_manual("Model",
    breaks = c("Normal", paste0("df_", df)),
    labels = c("Normal",
      expression(paste(t[2], "         ")),
      expression(paste(t[5], "         ")),
      expression(paste(t[20], "        "))),
    values = c("blue", "red", "orange", "black")) +
  theme_gray(base_size = 14)
```

## The Degrees of Freedom

* The degrees of freedom for the $t$ distribution is $n - 1$ because we use a denominator of $n-1$ in the sample standard deviation/variance:
$$s^2 = \frac{\sum_i (x_i - \bar{x})^2}{n - 1} \qquad \qquad s = \sqrt{\frac{\sum_i (x_i - \bar{x})^2}{n - 1}}$$
* We use $n -1$ so that the average value of $s^2$ is $\sigma^2$
* For example, suppose $\sigma^2 = 1$:

```{r, echo = FALSE, fig.height=2}
n_max <- 30
plot_df <- data.frame(
  n = rep(seq(from = 2, to = n_max), times = 2),
  mean_sigma_sq_hat = c(rep(1, n_max - 1), seq(from = 1, to = n_max - 1) / seq(from = 2, to = n_max)),
  denominator = c(rep("n - 1", n_max - 1), rep("n", n_max - 1))
)

ggplot() + 
  geom_hline(yintercept = 1) +
  geom_point(mapping = aes(x = n, y = mean_sigma_sq_hat, color = denominator), data = plot_df) +
  ylab(expression(atop(paste("Average Estimate of  ", sigma^2), "\nAcross All Samples")))
```

## Example: Flight Delays

The U.S. Bureau of Transportation Statistics reported the percentage of flights that were delayed each month from 1994 through October of 2013 (238 months in total).  Treat these as a representative sample of all months.  Here's a histogram:

```{r, echo = FALSE, message = FALSE, fig.height=1.5}
delays <- read_csv("https://mhc-stat140-2017.github.io/data/sdm4/Flight_Ontime_2013.csv")
names(delays) <- c("Year", "Month", "Month_num", "on_time_pct", "delayed_pct", "cancelled_pct")
ggplot() +
  geom_histogram(mapping = aes(x = delayed_pct), bins = 11, data = delays)
```

* Would it be appropriate to use these data to calculate a confidence interval for the mean percent of flights that are delayed per month?
    * **Symmetric?** Skewed a little to the right, but not too badly
    * **Unimodal?** Yes
    * **Sample Size?** The sample size is fairly large, so a normal model for the sample mean is appropriate


## Example: Flight Delays

* Calculate a 95% confidence interval for the mean percent of flights that are delayed per month

```{r, echo = TRUE}
t.test(delays$delayed_pct, conf.level = 0.95)
```

## Example: Flight Delays

* Calculate a 95% confidence interval for the mean percent of flights that are delayed per month

```{r, echo = TRUE}
n <- nrow(delays) # 238 observations
sample_mean <- mean(delays$delayed_pct) # sample mean = 19.75
sample_sd <- sd(delays$delayed_pct) # sample standard deviation = 4.27
mean_se <- sample_sd / sqrt(n) # standard error of sample mean = 0.28
t_critical <- qt(0.975, df = n - 1) # critical value: use .975 for a 95% CI!

sample_mean - t_critical * mean_se # lower CI bound
sample_mean + t_critical * mean_se # upper CI bound
```

## Example: Flight Delays

* Interpret the 95% confidence interval in context

We are 95% confident that the mean percent of flights that are delayed per month is between 19.2% and 20.3%.  If we took a lot of different samples of months and computed different confidence intervals from each, we would expect about 95% of the resulting intervals to contain the mean percent of flights that are delayed per month.
