Foundations for statistical inference - Confidence intervals
```{r setup, include=FALSE} library(tidyverse) library(openintro) library(MASS) library(readr) library(learnr) library(ggplot2) library(gradethis) #remotes::install_github("rstudio/gradethis") library(learnrhash) #devtools::install_github("rundel/learnrhash") library(gapminder) library(emo) #devtools::install_github("hadley/emo") library(png) library(grid) tutorial_options(exercise.timelimit = 120, exercise.checker = gradethis::grade_learnr) gradethis_setup(exercise.reveal_solution = FALSE) ``` ### Confidence Intervals for Proportions Please type `"Your Name"` in the interactive R chunk below and run it by clicking `Run Code` or by hitting `crtl+Enter` or `cmd+Enter` for MAC users. ```{r Student-Name, exercise = TRUE} ``` ### Introduction If you have access to data on an entire population, say the opinion of every adult in the United States on whether or not they think climate change is affecting their local community, it's straightforward to answer questions like, "What percent of US adults think climate change is affecting their local community?". Similarly, if you had demographic information on the population you could examine how, if at all, this opinion varies among young and old adults and adults with different leanings. If you have access to only a sample of the population, as is often the case, the task becomes more complicated. What is your best guess for this proportion if you only have data from a small sample of adults? This type of situation requires that you use your sample to make inference on what your population looks like. ### Getting Started ### Load R Packages - In this lab, we will explore and visualize the data using the `tidyverse` suite of packages. - We will also use the **infer** package for conducting statistical inference. Let's install and load the packages. ```{r Install-packages, exercise = TRUE} library(tidyverse) library(openintro) library(infer) ``` ```{r load-packages, exercise = TRUE} library(tidyverse) library(openintro) library(infer) ``` ### The data A 2019 Pew Research report states the following: > Roughly six-in-ten U.S. adults (62%) say climate change is currently affecting their local community either a great deal or some, according to a new Pew Research Center survey. > > **Source:** [Most Americans say climate change impacts their community, but effects vary by region](https://www.pewresearch.org/fact-tank/2019/12/02/most-americans-say-climate-change-impacts-their-community-but-effects-vary-by-region/) In this lab, you will assume this 62% is the true population proportion and learn about how we can conduct statistical inference about this population prorportion using sample proportions obtained from smaller samples drawn from the population. To keep our computation simple, we will assume a total population size of 100,000 (even though that's smaller than the population size of all US adults which was estimated to be 257,279,447 according to the 2020 census). We will first create our population assuming a population size of 100,000. This means 62,000 (62%) of the adult population think climate change impacts their community, and the remaining 38,000 does not think so. ```{r generate-data, exercise = TRUE} us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) ``` The name of the data frame is `us_adults` and the name of the variable that contains responses to the question *"Do you think climate change is affecting your local community?"* is `climate_change_affects`. We can quickly visualize the distribution of these responses using a bar plot. ```{r visualize-data, exercise = TRUE} us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) ggplot(us_adults, aes(x = climate_change_affects)) + geom_bar() + labs(x = "", y = "", title = "Do you think climate change is affecting your local community?") + coord_flip() ``` We can also obtain summary statistics to confirm we constructed the data frame correctly. ```{r summary-stat, exercise = TRUE} us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) us_adults %>% count(climate_change_affects) %>% mutate(p = n /sum(n)) ``` In this lab, you'll start with a simple random sample of size 60 from the population. ```{r sampled, exercise = TRUE} us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) n <- 60 samp <- us_adults %>% sample_n(size = n) ``` 1. What percent of the adults in your sample think climate change affects their local community? **Hint:** Just like we did with the population, we can calculate the proportion of those **in this sample** who think climate change affects their local community. Make sure to call the sample proportion `phat` NOT `p` because `p` is used for the population proportion. Add the necessary code to the following code chunk to calculate and show the sample proportion (see how we calculated the population proportion above). ```{r Ques_samp1, exercise = TRUE} us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) n <- 60 samp <- us_adults %>% sample_n(size = n) ``` ```{r Ques_samp1-solution} us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) n <- 60 samp <- us_adults %>% sample_n(size = n) samp%>% count(climate_change_affects) %>% mutate(phat = n /sum(n)) ``` ```{r Ques_samp1-check} grade_code() ``` 2. Would you expect another student's sample proportion to be identical to yours? Would you expect it to be similar? Why or why not? ```{r spreadIntuitionQue2, echo = FALSE} quiz( question( "Would you expect another student's sample proportion to be identical to yours? Would you expect it to be similar? Why or why not?", answer("No, it wouldn't be identical because we will get different samples unless we use the same seed when sampling. But it would be similar because we are sampling from populations that have the same distribution (equal population proportions).", correct = TRUE), answer("Yes, it would be identical."), allow_retry = TRUE ) ) ``` ### Confidence intervals Return for a moment to the question that first motivated this lab: based on this sample, what can you infer about the population? With just one sample, the best estimate of the proportion of US adults who think climate change affects their local community would be the sample proportion, usually denoted as $\hat{p}$. That serves as a good **point estimate**, but it would be useful to also communicate how uncertain you are of that estimate. This uncertainty can be quantified using a **confidence interval**. One way of calculating a confidence interval for a population proportion is based on the Central Limit Theorem, as $\hat{p} \pm z^\star SE_{\hat{p}}$ is, or more precisely, as $$ \hat{p} \pm z^\star \sqrt{ \frac{\hat{p} (1-\hat{p})}{n} } $$ The following code will use our sample of 60 adults to compute a 95% confidence interval for the proportion of US adults who think climate change affects their local community. ```{r Conf-Int, exercise = TRUE} library(infer) us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) n <- 60 samp <- us_adults %>% sample_n(size = n) prop_test(samp, climate_change_affects ~ NULL, success = "Yes", z = TRUE, conf_int = TRUE, conf_level = 0.95, correct = FALSE) ``` Feel free to test out the rest of the arguments for these functions, since these commands will be used together to calculate confidence intervals and solve inference problems for the rest of the semester. But we will also walk you through more examples in future chapters. To recap: even though we don't know what the full population looks like, we're 95% confident that the true proportion of US adults who think climate change affects their local community is captured between the two bounds reported as result of the above code chunk. ### Confidence levels 3. In the interpretation above, we used the phrase "95% confident". What does "95% confidence" mean? In this case, you have the rare luxury of knowing the true population proportion (62%) since you have data on the entire population. ```{r spreadIntuitionQue3, echo = FALSE} quiz( question( "In the interpretation above, we used the phrase **95% confidence**. What does `95% confidence` mean? In this case, you have the rare luxury of knowing the true population proportion (62%) since you have data on the entire population?", answer("The confidence interval is equal to the true proportion of 62%"), answer("The confidence interval is 5% off the 100% than the true proportion 62%"), answer("95% confidence means that 95% of the time, the true proportion will be contained within the confidence interval for any given sample of the same size.", correct = TRUE), answer("The confidence interval measures the true proportion"), allow_retry = TRUE ) ) ``` 4. Run the code below to see the confidence interval. Notice that we set a seed before drawing the sample to ensure that we all take the same sample and get the same confidence interval. ```{r Ques_four, exercise = TRUE} library(infer) us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) set.seed(12345) n <- 60 samp <- us_adults %>% sample_n(size = n) prop_test(samp, climate_change_affects ~ NULL, success = "Yes", z = TRUE, conf_int = TRUE, conf_level = 0.95, correct = FALSE) ``` ```{r spreadIntuition4b, echo = FALSE} quiz( question( "Does the confidence interval capture the true population proportion of US adults who think climate change affects their local community?", answer("No"), answer("The true population does not exist"), answer("Not Applicable"), answer("Yes: The true proportion is 0.62. So our confidence interval captures the true proportion", correct = TRUE), allow_retry = TRUE ) ) ``` 5. If we did not set a seed in Exercise 4, each student whould have gotten a slightly different confidence interval. What proportion of those intervals would you expect to capture the true population proportion? Why? ```{r spreadIntuition5, echo = FALSE} quiz( question( "What proportion of those intervals in question 4 would you expect to capture the true population proportion? Why?", answer("We would expect about 95% of the students to have captured the true proportion in their confidence intervals. This is because every student used a confidence level of 0.95 (95%)", correct = TRUE), answer("We would expect 100% of the students to have captured the true proportion in their confidence intervals. This is because every student used a confidence level of 1.00 (100%)"), answer("We would expect about 5% of the students to have captured the true proportion in their confidence intervals. This is because every student used a confidence level of 0.05 (5%)"), allow_retry = TRUE ) ) ``` 6. Modify the code from Exercise 4 to find a **90%** confidence interval for the proportion of US Adults who think climate change is affecting their local community, and interpret it. **Keep the same seed as in Exercise 4.** ```{r Ques_samp6, exercise = TRUE} library(infer) ``` ```{r Ques_samp6-solution} library(infer) us_adults <- tibble( climate_change_affects = c(rep("Yes", 62000), rep("No", 38000))) set.seed(12345) n <- 60 samp <- us_adults %>% sample_n(size = n) prop_test(samp, climate_change_affects ~ NULL, success = "Yes", z = TRUE, conf_int = TRUE, conf_level = 0.90, correct = FALSE) ``` ```{r Ques_samp6-check} grade_code() ``` ```{r spreadIntuitionQue6, echo = FALSE} quiz( question( "Interprete the **90%** confidence interval?", answer("The confidence interval is equal to the true proportion of 62%"), answer("The confidence interval is 5% off the 100% than the true proportion 62%"), answer("We are 95% confident that the true population proportion of US Adults who think climate change is affecting their local community is contained within the confidence interval."), answer("We are 90% confident that the true population proportion of US Adults who think climate change is affecting their local community is contained within the confidence interval.", correct = TRUE), allow_retry = TRUE ) ) ``` ```{r spreadIntuition66b, echo = FALSE} quiz( question( "Is the new confidence interval wider or narrower than the interval obtained in Exercise 4?", answer("Narrower", correct = TRUE), answer("Wider"), answer("Cannot say"), allow_retry = TRUE ) ) ``` In the next part of the lab, you will collect many samples to learn more about how sample proportions and confidence intervals constructed based on those samples vary from one sample to another. - Obtain a random sample. - Calculate the sample proportion, and use these to calculate and store the lower and upper bounds of the confidence intervals. - Repeat these steps 50 times. Doing this would require learning programming concepts like iteration so that you can automate repeating running the code you've developed so far many times to obtain many (say, 100) confidence intervals. In order to keep the programming simpler, we are providing the interactive app below that basically does this for you and created a plot similar to Figure 5.6 on [OpenIntro Statistics, 4th Edition (page 182)](https://www.openintro.org/os). Use this link to answer the question below. 7. Given a sample size of 60 and the 100 confidence intervals constructed, what percentage of your confidence intervals include the true population proportion? Is this percentage exactly equal to the confidence level (95%)? If not, explain why. ```{r spreadIntuition7, echo = FALSE} quiz( question( "What percentage of your confidence intervals include the true population proportion? Is this percentage exactly equal to the confidence level (95%)? If not, explain why.", answer("No, but it is close to 95%. It is not identical to 95% because we took only 100 samples (not all possible samples).", correct = TRUE), answer("Yes, it is exactly equal to 95%", correct = TRUE), answer("Cannot say"), allow_retry = TRUE ) ) ``` ### Submit ```{r context="server"} learnrhash::encoder_logic(strip_output = TRUE) ``` ```{r encode, echo=FALSE} learnrhash::encoder_ui( ui_before = shiny::div( "If you have completed this tutorial and are happy with all of your", "solutions, please click the button below to generate your hash and", "submit it using the corresponding tutorial assignment tab on Blackboard", shiny::tags$br() ), ui_after = shiny::tags$a(href = "https://blackboard.ncat.edu/", "NCAT Blackboard") ) ``` ### Resources for learning R and working in RStudio The book [R For Data Science](https://r4ds.had.co.nz/) by Grolemund and Wickham is a great resource for data analysis in R with the tidyverse. If you are Goggling for R code, make sure to also include these package names in your search query. For example, instead of Goggling "scatterplot in R", Goggle "scatterplot in R with the tidyverse". These may come in handy throughout the semester: - [RMarkdown cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf) - [Data transformation cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/data-transformation.pdf) - [Data visualization cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/data-visualization-2.1.pdf) Note that some of the code on these cheatsheets may be too advanced for this course. However the majority of it will become useful throughout the semester. ------------------------------------------------------------------------ {style="border-width:0"}
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.