The full assignment for this lab can be found here
In this lab, you will assume that \(\pi=62\%\) is the very true population proportion. In reality, we cannot observe this value, but for the purpose of this lab we will create this hypothetical population. We will then sample our data from our hypothetical population, exploring how samples vary from one to another.
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).
# Load the tidyverse, mosaic, broom and infer packages
library(tidyverse)
library(mosaic)
library(broom)
library(infer)
# 2. use the read_rds file to read the dataset
us_adults <- read_rds("data/climate_believers.rds")Question: We can visualize the hypothetical
distribution of the responses in the population using a bar plot.
Recreate the plot below using the ggplot(),
geom_bar() and labs() layers. To flip the
x and y coordinates, add the
coord_flip() layer.
# Write your code to create determine the missing data
us_adults %>%
ggplot(aes(climate_believers)) +
geom_bar() +
labs(title = "Do you think climate change is affecting your local community", y = "")+
coord_flip()Question: Print the summary statistics to confirm we
constructed the data frame correctly. Use the count
function to show the numeric quantities and use
mutate(p = n /sum(n)) to calculate the proportions in the
population. What is the proportion of climate-believers in our
hypothetical population?
Answer: The proportion of believers in climate is 62%
Question: Calculate the proportions like we did in the previous question and answer the following: (1) What percent of your sample are climate-believers? (2) How does this compare to the proportion of climate-believers in the population? 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.
# Insert code for Exercise 3 here
set.seed(35797)
n <- 60
samp_1 <- us_adults %>%
sample_n(size = n)
samp_1 %>%
count(climate_believers) %>%
mutate(p = n /sum(n))Answer: From sample one, my best estimate of the real proportion of climate believers is 61.66 %, this estimation underestimates the real population proportion of 62% of climate believers.
Question: Create code to generate a second sample
(call it samp_2). Answer the same questions as before, but
this time with respect to samp_2. How do the two samples
compare? Explain any difference you found between the two samples.
# Insert code for the Exercise here
n <- 60
samp_2 <- us_adults %>%
sample_n(size = n)
samp_2 %>%
count(climate_believers) %>%
mutate(p = n /sum(n))Answer: From sample 2, my best estimate of the real proportion of climate believers is 60%, this estimation underestimates the real population proportion of 62% of climate believers. From sample one, my best estimate of the real proportion of climate believers is 61.66% but for sample 2, my best estimate of the real proportion of climate believers is 60%, both underestimate the population proportion of 62%, but the second sample underestimates it more.
Question: Run the proportion test (see code below)
on the first sample samp_1, to estimate the proportion of
climate-believers in the population. Now answer the following questions:
(1) How does the estimation compare to the real proportion of
climate-believers in the population? (2) What is the confidence interval
associated with your estimation? (3) Is the proportion of
climate-believers in the population contained within your confidence
interval?
## No `p` argument was hypothesized, so the test will assume a null hypothesis `p
## = .5`.
Answer: (1) We know that the proportion in the top population is 62%, but my sample 1 is estimating the proportion as 61.66% which is an underestimation of the real population proportion. (2)The CI [48.2 73.65] is related to my estimation 61.66% in sample 1 because this estimation is contained in the CI. (3) The population proportion of 62% in the population is contained in the CI [48.2 73.65] of sample 1.
Question: This code will create 1000 bootstrapping
samples from samp_1, and use those samples to find the 95
percent confidence interval for proportion of climate-believers. Run the
code and compare your results with the proportion test we’ve run in the
previous question.
# Insert code for the Exercise here
samp_1 %>%
specify(response = climate_believers, success = "Yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)Answer: The CI calculated in this question is almost the same as the one calculated in question 5, but the CI calculated in this question is narrower because we did 1000 resampling.
Question: Does your confidence interval capture the
true population proportion of US adults who think climate change affects
their local community? Now run the bootstrapping method on
samp_2. How do your results compare?
Each time you run a sample, you would get different intervals. What proportion of those intervals would you expect to contain the true population mean?
# Insert code for the Exercise here
samp_2 %>%
specify(response = climate_believers, success = "Yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)Answer: The CI of sample two [46.67 71.67] contains the population proportion of 62% that believe in climate change effects their local community. I expect that 95% of the calculated CI will contain the true population mean.
Question: Given a sample size of 60, 1000 bootstrap samples for each interval, and 50 confidence intervals constructed (the default values for the above app), what proportion of your confidence intervals include the true population proportion? Is this proportion exactly equal to the confidence level? If not, explain why. Include an image of your plot with your the answer (to learn how to include an image in your RMarkdown, see this).
Answer: 95% of my CI include the true population proportion, 5% of 50 is 2.5 so I got 3 CI that do not include the true population proportion so it is approximately 5% therefore ~95% of my CI include the true population proportion.
Question: Choose a different confidence level than 95%. Would you expect a confidence interval at this level to be wider or narrower than the confidence interval you calculated at the 95% confidence level? Explain your reasoning and confirm your using the app. What is the proportion of intervals that include the true population proportion? How does this percentage compare to the confidence level selected for the intervals? Include an image of your plot with your the answer.
Answer: When you decrease the confidence level the CI width decreases because you are willing to be less certain and you’re allowing for less uncertainty. The proportion of intervals that include the true population proportion is 86% but this proportion is bigger than the confidence level of 80% that I selected for the intervals.
Question: Using the app, experiment with different sample sizes and comment on how the widths of intervals change as sample size changes (increases and decreases). Include an image of your plot with your the answer.
Answer: If sample size decreases the CI get wider. The bigger the sample size the narrower the widths of the CI.
Question: Finally, given a sample size (say, 60), how does the width of the interval change as you increase the number of bootstrap samples? Include an image of your plot with your the answer.
Answer: In theory when you increase the number of bootstrap samples the CI will get narrower.