---
title: "Confidence intervals"
date: "10/16/2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

The first fundamental tool for inference is the *confidence interval*. Here is the 
basic idea.

  * We grab a random sample of $n$ individuals from a population.
  * We compute some statistic $s$ from that sample -
    * That statistic might be a proportion $\hat{p}$, a mean $\bar{x}$, or almost anything.
    * Following the book, we'll focus on proportions here.
  * We compute a *standard error* $SE$ for the statistic - more on that in a min.
  * The confidence interval is then 
     $$s \pm z^* \times SE.$$
     * The bigger $z^*$ is, the bigger the interval is and the more confident we are that
       the interval contains the actual parameter.
     * Often $z^*=2$ and we call the confidence interval a 95\% confidence interval.

## Standard error

The *standard error* is simply the standard deviation associated with a sampling distribution.
Generally, if the standard deviation associated with a population parameter is $\sigma$ and
we form a random variable $S$ by sampling from the population, the standard deviation of $S$ is
$\sigma/\sqrt{n}$. Thus, as expected, the standard error decreases as the sample size increases.

### Example

Suppose we draw a random sample of 132 people and find that 16 of them have blue eyes.
Use this data to write down a 95\% confidence interval for the proportion of people with 
blue eyes

*Solution*: We have $\hat{p}=16/132 \approx 0.1212$ and 
$$SE(\hat{p}) = \sqrt{(16/132)\times(116/132)/132} \approx 0.02840718.$$
Thus, our confidence interval is 
$$0.1212 \pm 2\times0.0284 = [0.0644, 0.178].$$

### Margin of error

If you read the details of political surveys, you're likely to come across the term 
"margin of error" at some point. [Five Thirty Eight](https://projects.fivethirtyeight.com/),
for example, maintains a running
[Trump approval rating page](https://projects.fivethirtyeight.com/trump-approval-ratings/).
The page also points to poll details for a *slew* of polls. Check out the first one, namely
the [Gallup poll](http://news.gallup.com/poll/201617/gallup-daily-trump-job-approval.aspx). 
There, we read "Daily results are based on telephone interviews with approximately 1,400
national adults; Margin of error is $\pm 3$ percentage points". What's that mean?

#### Definition

When we write a confidence interval as
$$s \pm z^* \times SE,$$
Then, $z^* \times SE$ is the *margin of error*. Geometrically, it's the distance that the
interval extends in either direction from the measured statistic $s$.

#### Computation

So, where's the $\pm 3$ come from?

Suppose we're writing down a confidence interval for a proportion. In this case, approve or 
disapprove. If the actual proportion is $p$ and our sample size is $n$, 
then the standard error is
$$\sqrt{\frac{p(1-p)}{n}}.$$
In our case, the take $n\approx 1500$. Furthermore the *biggest* that $p(1-p)$ can be is
$1/4$. You can see this by taking a look at a graph:

```{r, echo=F}
f = function(p){return(p*(1-p))}
plot(f,0,1, xlab='', ylab='', main='plot of y=x(1-x)')
```

Thus, our standard error is at *most*
$$SE \leq \sqrt{\frac{1/4}{1500}} \approx 0.01290994.$$
Now, for a $95\%$ confidence interval, we take $z^* = 2$ so that our margin of error is 
at most
$$ME \leq 2*0.01290994 \approx 0.026,$$
which is rounded up to 3 percentage points.

This is a common thing to shoot for in political polls, which is why you often see sample
sizes close to 1500.

#### A hypothetical poltical race

In a political contest, where there are two candiates and victory requires a simple majority,
a candiate likes to be more than 3 percentage points above 50\%. Can you see why?

#### An actual political race

In early November of last year, 
[FiveThirtyEight reported](https://fivethirtyeight.com/features/trump-is-just-a-normal-polling-error-behind-clinton/)
that Donald Trump was only 3.3 perecentage points behind Hilary Clinton - almost within the
margin of error. In fact, Clinton ended up winning the popular vote by 2.1\%.


#### A poll on the first admendment

A [recent poll](https://www.brookings.edu/blog/fixgov/2017/09/18/views-among-college-students-regarding-the-first-amendment-results-from-a-new-survey/)
by the [Brookings Institute](https://www.brookings.edu/) asks the following question of
1500 college students: "Is hate speech constitutionally protected?"

Here are the results:

<table style="font-family: 'franklin-gothic-urw',helvetica,sans-serif;width: 100%;max-width: 40em;border-collapse: collapse;border-spacing: 0">
<thead style="width: 100%;vertical-align: bottom;text-align: left;line-height: 1.667;font-weight: bold;background-color: #ececec">
<tr>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em"></th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em"></th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em" colspan="3">Political Affiliation</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em" colspan="2">Type of College</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em" colspan="2">Gender</th>
</tr>
<tr>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em"></th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">All</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Dem</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Rep</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Ind</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Public</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Private</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Female</th>
<th style="border: 1px solid #D8D8D8;padding: .35em .5em">Male</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">Yes</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">39</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">39</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">44</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">40</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">38</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">43</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">31</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">51</td>
</tr>
<tr>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">No</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">44</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">41</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">39</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">44</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">44</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">44</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">49</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">38</td>
</tr>
<tr>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">Don&#8217;t know</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">16</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">15</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">17</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">17</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">17</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">13</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">21</td>
<td style="border: 1px solid #D8D8D8;padding: .35em .5em">11</td>
</tr>
</tbody>
</table>


Use this to write down a confidence interval for the percentage of students who believe
that hate speech is *not* constitutionally protected.

---

---

