 
---
title: "Lab 10: Inference for Proportion"
author: "Insert Name"
output:
  pdf_document
 
---

## Instructions

**Due: Before lab, Thursday or Friday, April 22 or 23**

Read through the following document (it may be helpful to have both the .rmd and .pdf files open side-by-side). Interspersed throughout the text are 15 **Exercises**. Answer these questions in the .rmd file by inserting a code chunk or typing text between the stars directly below the exercise statement.


```{r, include=FALSE}
# Do not edit this code block/chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE, fig.width = 5, fig.height = 3)
```

### Load packages

For this lab we will read be using the `tidyverse` for data wrangling and visualization and `oilabs` for the data set. `infer` is included as well, in case you decide to use it.

```{r, include=FALSE, message = FALSE, warning = FALSE}
library(tidyverse)
library(knitr)
library(oilabs)
library(infer)
```

# Background 

In August of 2012, news outlets ranging from the [Washington
Post](http://www.washingtonpost.com/national/on-faith/poll-shows-atheism-on-the-rise-in-the-us/2012/08/13/90020fd6-e57d-11e1-9739-eef99c5fb285_story.html)
to the [Huffington
Post](http://www.huffingtonpost.com/2012/08/14/atheism-rise-religiosity-decline-in-america_n_1777031.html)
ran a story about the rise of atheism in America. The source for the
story was a poll that asked people, “Irrespective of whether you attend
a place of worship or not, would you say you are a religious person, not
a religious person or a convinced atheist?” This type of question, which
asks people to classify themselves in one way or another, is common in
polling and generates categorical data. In this lab we take a look at
the atheism survey and explore what’s at play when making inference
about population proportions using categorical data.

## The survey

The press release for the poll, conducted by WIN-Gallup International,
can be accessed
[here](https://sidmennt.is/wp-content/uploads/Gallup-International-um-tr%C3%BA-og-tr%C3%BAleysi-2012.pdf).

Take a moment to review the report then address the following questions.


### Exercise 1

In the first paragraph, several key findings are reported. Do these
    percentages appear to be *sample statistics* (derived from the data
    sample) or *population parameters*? Explain your reasoning.
    
**************************************************

**************************************************

### Exercise 2

The title of the report is “Global Index of Religiosity and
    Atheism”. To generalize the report’s findings to the global human
    population, what must we assume about the sampling method? Does that
    seem like a reasonable assumption?
    
**************************************************

**************************************************

## The data

Turn your attention to Table 6 (pages 14 and 15), which reports the
sample size and response percentages for all 57 countries. While this is
a useful format to summarize the data, we will base our analysis on the
original data set of individual responses to the survey. Load this data
set into R with the following command.

```{r }
data(atheism)
```


### Exercise 3

What does each row of Table 6 correspond to? What does each row of
    `atheism` correspond to?

**************************************************

**************************************************

To investigate the link between these two ways of organizing this data,
take a look at the estimated proportion of atheists in the United
States. Towards the bottom of Table 6, we see that this is 5%. We should
be able to come to the same number using the `atheism` data.

### Exercise 4

Using the code below, create a new dataframe called `us12` that
    contains only the rows in `atheism` associated with respondents to
    the 2012 survey from the United States. Next, calculate the
    proportion of atheist responses. Does it agree with the percentage
    in Table 6? If not, why?
    
```{r}
us12 <- atheism %>%
  filter(nationality == "United States", year == "2012")
```

**************************************************

**************************************************

 



## Inference on proportions

As was hinted earlier, Table 6 provides *statistics*, that is,
calculations made from the sample of 51,927 people. What we’d like,
though, is insight into the population *parameters*. You answer the
question, “What proportion of people in your sample reported being
atheists?” with a statistic; while the question “What proportion of
people on earth would report being atheists” is answered with an
estimate of the parameter.

### Exercise 5
Write out the conditions for inference using the Normal
    approximation to construct a 95% confidence interval for the
    proportion of atheists in the United States in 2012. Are you
    confident all conditions are met?
    
**************************************************

**************************************************

Although formal confidence intervals and hypothesis tests don’t show up
in the report, suggestions of inference appear at the bottom of page 6:
“In general, the error margin for surveys of this kind is \(\pm\) 3-5%
at 95% confidence”.


### Exercise 6

What is the margin of error for the estimate of the proportion of
    the proportion of atheists in US in 2012 as estimated by the theory-base
    normal model? What about as estimated using the bootstrap standard error?

**************************************************


**************************************************

### Exercise 7

Calculate confidence intervals for the proportion of atheists in
    2012 in two other countries of your choice, and report the
    associated margins of error. You are welcome to use either the
    normal approximation method or the bootstrap, but be sure to note
    whether the conditions for inference are met, and interpet the
    interval in context of the data.

**************************************************


**************************************************

## How does the proportion affect the margin of error?

Imagine you’ve set out to survey 1000 people on two questions: are you
female? and are you left-handed? Since both of these sample proportions
were calculated from the same sample size, they should have the same
margin of error, right? Nope\! While the margin of error does change
with sample size, it is also affected by the proportion.

Think back to the formula for the standard error:
$$SE = \sqrt{p(1-p)/n}$$. This is then used in the formula for the
margin of error for a 95% confidence interval:
$$ME = 1.96\times SE = 1.96\times\sqrt{p(1-p)/n}$$. Since the population
proportion $p$ is in this $ME$ formula, it should make sense that
the margin of error is in some way dependent on the population
proportion. We can visualize this relationship by creating a plot of
$ME$ vs. $p$.

Since sample size is irrelevant to this discussion, let’s just set it to
some value ($n = 1000$) and use this value in the following
calculations:

```{r}
n <- 1000
```

The first step is to make a variable `p` that is a sequence from 0 to 1
with each number incremented by 0.01. We can then create a vector of the
margin of error (`me`) associated with each of these values of `p` using
the familiar approximate formula ($ME = 2 \times SE$).

```{r}
p <- seq(0, 1, 0.01)
me <- 2 * sqrt(p * (1 - p)/n)
```

Lastly, we combine these vectors into a dataframe so that we can
visualize it either using `geom_point()` or `geom_line()` (or both\!).

```{r}
dd <- data.frame(p = p, me = me)
```

### Exercise 8
Use ggplot to plot the relationship between `p` and `me` and
    describe it in words. For a given sample size, for which value of
    `p` is margin of error maximized?

**************************************************

**************************************************

## Success-failure condition

We emphasized that you must always check conditions before making
inference. For inference on proportions, the sample proportion can be
assumed to be nearly normal if it is based upon a random sample of
independent observations and if both $np \geq 10$ and
$n(1 - p) \geq 10$. This rule of thumb is easy enough to follow, but
it makes one wonder: what’s so special about the number 10?

The short answer is: nothing. You could argue that we would be fine with
9 or that we really should be using 11. What is the “best” value for
such a rule of thumb is, at least to some degree, arbitrary. However,
when $np$ and $n(1-p)$ reaches 10 the sampling distribution is
sufficiently normal to use confidence intervals and hypothesis tests
that are based on that approximation.

We can investigate the interplay between $n$ and $p$ and the shape
of the sampling distribution by using simulations. Play around with the
following app to investigate how the shape, center, and spread of the
distribution of $\hat{p}$ changes as $n$ and $p$
changes.

Link to app:
<https://ismay.shinyapps.io/shinyDist/>

### Exercise 9

Describe the sampling distribution of sample proportions at
    $n = 300$ and $p = 0.1$. Be sure to note the center, spread, and
    shape.
    
**************************************************

**************************************************


### Exercise 10

Keep $n$ constant and change $p$. How does the shape, center,
    and spread of the sampling distribution vary as $p$ changes. You
    might want to adjust min and max for the x-axis for a better view of
    the distribution.

**************************************************

**************************************************

### Exercise 11

Now also change $n$.How does $n$ appear to affect the
    distribution of $\hat{p}$?

**************************************************

**************************************************


### Exercise 12

If you refer to Table 6, you’ll find that Australia has a sample
    proportion of 0.1 on a sample size of 1040, and that Ecuador has a
    sample proportion of 0.02 on 400 subjects. Let’s suppose for this
    exercise that these point estimates are actually the truth.
    Construct their sampling distributions by using these values as
    inputs in the app. Do you think it is sensible to proceed with
    inference and report margin of errors, as the report does?

**************************************************

**************************************************

 

## More Practice

The question of atheism was asked by WIN-Gallup International in a
similar survey that was conducted in 2005. (We assume here that sample
sizes have remained the same.) Table 4 on page 13 of the report
summarizes survey results from 2005 and 2012 for 39 countries.

### Exercise 13


Is there convincing evidence that the US has seen a change in its
    atheism index between 2005 and 2012? As always, write out the
    hypotheses for any tests you conduct and outline the status of the
    conditions for inference. If you find a significant difference, also
    quantify this difference with a confidence interval. You may use
    either a computational method (`permute` and `bootstrap`) or the
    normal approximation.

**************************************************


**************************************************

### Exercise 14
If in fact there has been no change in the atheism index in the
    countries listed in Table 4, in how many of those countries would
    you expect to detect a change (at a significance level of 0.05)
    simply by chance?  
    *Hint:* Review the definition of the Type 1 error.

**************************************************


**************************************************


### Exercise 15

Suppose you’re hired by the local government to estimate the
    proportion of residents that attend a religious service on a weekly
    basis. According to the guidelines, the estimate must have a margin
    of error no greater than 1% with 95% confidence. You have no idea
    what to expect for $p$. How many people would you have to sample
    to ensure that you are within the guidelines?  
    *Hint:* Refer to your plot of the relationship between $p$ and
    margin of error. This question does not require using the dataset.
    
**************************************************



**************************************************



## Exercise 16
Suppose A and B are events with P(A) = 0.3 and P(B) = 0.7.

A.  Is it always possible to compute P(A and B) if you only know P(A) and P(B)?

********************************************************


********************************************************

B.  Assuming that events A and B arise from independent random processes, find P(A and B) and P(A | B).

********************************************************


********************************************************
  
C.  Suppose we are given that P(A and B) = 0.1. Are events A and B independent?

********************************************************


********************************************************

D.  If we are given that P(A and B) = 0.1 , what is P(A | B)?

********************************************************


********************************************************

## Exercise 17

Lupus is a medical phenomenon where antibodies that are supposed to attack foreign cells to prevent infections instead see plasma proteins as foreign bodies, leading to a high risk of blood clotting. It is believed that 2% of the population suffer from this disease. The test is 98% accurate if a person actually has the disease. The test is 74% accurate if a person does not have the disease. There is a line from the Fox television show *House* that is often used after a patient tests positive for lupus: "It's never lupus." Do you think there is truth to this statement? Use appropriate probabilities to support your answer.


********************************************************


********************************************************

## Exercise 18

An airline charges the following baggage fees: \$25 for the first bag and \$35 for the second. Suppose 54% of passengers have no checked luggage, 34% have one piece of checked luggage and 12% have two pieces. We suppose a negligible portion of people check more than two bags.

A.  Define a random variable that describes the baggage fee revenue for a single passenger, with the possible values that it can take along with their probabilities.  The compute the average revenue per passenger, and compute the corresponding standard deviation.


********************************************************


********************************************************

B.  About how much revenue should the airline expect for a flight of 120 passengers? With what standard deviation? Note any assumptions you make and if you think they are justified.

********************************************************


********************************************************
 

### Exercise 19

Currently, President Biden's approval rating is about 53.1\%, per data aggregated by the [538 blog](https://projects.fivethirtyeight.com/biden-approval-rating/)

However, polls conducted between January 20th and March 15th by [Gallop](https://news.gallup.com/poll/339977/biden-approval-ratings-diverge-gender-education-race.aspx) of  2,937 randomly selected Americans aged 18 or over reports President Biden's Approvoal Rating at 56\%. 


A.  Let's use the results of the poll to give an interval estimate for President Biden's true approval rating. Compute the $z^*$ critical value for a $99\%$ confidence interval, and use it create a theory-based 99% confidence interval.

*********************************************************************************

*********************************************************************************


B.  Another [Gallop poll](https://news.gallup.com/poll/329945/congressional-job-approval-highest-nearly-years.aspx) asked respondents "Do you approve or disapprove of the job the U.S. Congress is doing?" The poll report includes data from February 3-13, 2021 with 357 of 1021 respondents approved of the job the U.S. Congress is doing.  In  May 1-13, 2020, 360 of 1028 respondent approved of the job Congress was doing.

Use theory-based methods to conduct a hypothesis test to determine whether the poll gives statistically significant evidence that Congressional approval ratings **increased** from May 2020 to February 2021.


*********************************************************************************

*********************************************************************************


C.  The following data frame recreates the data on biden's approval rating from the Gallop poll. Use the `infer` package and the bootstrap method to create a $99\%$ confidence interval for President Biden's approval rating. Compare your result to the one obtained in part A.


```{r}
biden<-tibble(approve = rep(c("yes", "no"), c(1645, 1292)))
```


*********************************************************************************

*********************************************************************************
 




### Exercise 20

We want to know if there is a relationship between education level and the belief that there is only one true love for each person.  In October of 2010, the Pew Foundation ran a telephone survey on a random sample of adults.  They asked the following question: "Some people say there is only one true love for each person. Do you agree or disagree?"  They also collected demographic information on the participants, such as their education level (no college, some college, or college degree).

Here are the data from their survey.


```{r echo=F}
# Load data
what_is_love <- read_csv("/home/courses/math141s20/Data/love.csv")
```

A.  Create a useful graph of the response and explanatory variables.  Make sure that the order of `Education` is: "High School", "Some College", "College" (recall taht `fct_relevel` can change the order of labels in a graph). Based on your graph, does there appear to be a relationship between education level and whether someone believes in the idea of one true love?

*********************************************************************************


*********************************************************************************

B.  State the null and alternative hypotheses.

*********************************************************************************

*********************************************************************************


C.  Run the following R code and explain the tables that are generated. Compare the results in the two tables. Where do the two tables differ significantly?


```{r}
# Table 1
table(what_is_love$Response, what_is_love$Education)

# Table 2
chisq.test(table(what_is_love$Response, what_is_love$Education))$expected
```


*********************************************************************************

*********************************************************************************

D.  Find and graph the null distribution for the test statistic using simulation-based methods. Compute the observed test statistic, plot it on the graph of the null distribution, and compute the P-value.
 
*********************************************************************************

*********************************************************************************

 

E.	The distribution of the test statistic be approximated by a known distribution.  State the distribution and use it to approximate the P-value.

*********************************************************************************

*********************************************************************************

 


F.	State your conclusions at a significance level of 0.05. 



*********************************************************************************

*********************************************************************************






G.	Based on the expected and observed tables does it appear that more education correlates with a higher belief in one true love?  Why or why not?




*********************************************************************************

*********************************************************************************




