Foundations for statistical inference - Sampling distributions
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 2.0.1 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(infer)
library(ggplot2)
library(readr)
library(tibble)
library(dplyr)
global_monitor <- tibble(
scientist_work = c(rep("Benefits", 80000), rep("Doesn't benefit", 20000))
)
ggplot(global_monitor, aes(x = scientist_work)) +
geom_bar() +
labs(
x = "", y = "",
title = "Do you believe that the work scientist do benefit people like you?"
) +
coord_flip()
global_monitor %>%
count(scientist_work) %>%
mutate(p = n / sum(n))
## # A tibble: 2 × 3
## scientist_work n p
## <chr> <int> <dbl>
## 1 Benefits 80000 0.8
## 2 Doesn't benefit 20000 0.2
samp1 <- global_monitor %>%
sample_n(50)
samp1
## # A tibble: 50 × 1
## scientist_work
## <chr>
## 1 Doesn't benefit
## 2 Benefits
## 3 Benefits
## 4 Benefits
## 5 Benefits
## 6 Benefits
## 7 Benefits
## 8 Benefits
## 9 Benefits
## 10 Benefits
## # … with 40 more rows
table(samp1$scientist_work)
##
## Benefits Doesn't benefit
## 42 8
Exercise 1. Describe the distribution of responses in this sample. How does it compare to the distribution of responses in the population. Hint: Although the sample_n function takes a random sample of observations (i.e. rows) from the dataset, you can still refer to the variables in the dataset with the same names. Code you presented earlier for visualizing and summarizing the population data will still be useful for the sample, however be careful to not label your proportion p since you’re now calculating a sample statistic, not a population parameters. You can customize the label of the statistics to indicate that it comes from the sample.
samp_monitor <- tibble(samp1 = c(rep("Benefits", 37), rep("Doesn't benefit", 13)))
samp_monitor
## # A tibble: 50 × 1
## samp1
## <chr>
## 1 Benefits
## 2 Benefits
## 3 Benefits
## 4 Benefits
## 5 Benefits
## 6 Benefits
## 7 Benefits
## 8 Benefits
## 9 Benefits
## 10 Benefits
## # … with 40 more rows
ggplot(samp_monitor, aes(x = samp1)) +
geom_bar() +
labs(
x = "", y = "",
title = "Do you believe that the work scientists do benefit people like you?"
) +
coord_flip()
samp1 <- ames %>%
sample_n(50)
Exercise 2. Would you expect the sample proportion to match the sample proportion of another student’s sample? Why, or why not? If the answer is no, would you expect the proportions to be somewhat different or very different? Ask a student team to confirm your answer.
=> Not necessarily, because this is a relatively small random sample of the proportion. I am expecting such samples to form a distribution of their own.
Exercise 3. Take a second sample, also of size 50, and call it samp2. How does the sample proportion of samp2 compare with that of samp1? Suppose we took two more samples, one of size 100 and one of size 1000. Which would you think would provide a more accurate estimate of the population proportion?
samp2 <- global_monitor %>%
sample_n(50)
table(samp2$scientist_work)
##
## Benefits Doesn't benefit
## 45 5
=> I would expect the larger sample to more accurately reflect the greater population, since the sample itself more closely look like the population as the size increases.
Exercise 4. How many elements are there in sample_props50? Describe the sampling distribution, and be sure to specifically note its center. Make sure to include a plot of the distribution in your answer.
sample_props50 <- global_monitor %>%
infer::rep_sample_n(size = 50, reps = 15000, replace = TRUE) %>%
count(scientist_work) %>%
mutate(p = n / sum(n)) %>%
filter(scientist_work == "Doesn't benefit")
sample_props50
## # A tibble: 14,999 × 4
## # Groups: replicate [14,999]
## replicate scientist_work n p
## <int> <chr> <int> <dbl>
## 1 1 Doesn't benefit 17 0.34
## 2 2 Doesn't benefit 8 0.16
## 3 3 Doesn't benefit 14 0.28
## 4 4 Doesn't benefit 10 0.2
## 5 5 Doesn't benefit 11 0.22
## 6 6 Doesn't benefit 14 0.28
## 7 7 Doesn't benefit 16 0.32
## 8 8 Doesn't benefit 16 0.32
## 9 9 Doesn't benefit 10 0.2
## 10 10 Doesn't benefit 7 0.14
## # … with 14,989 more rows
Exercise 5. To make sure you understand how sampling distributions are built, and exactly what the rep_sample_n function does, try modifying the code to create a sampling distribution of 25 sample proportions from samples of size 10, and put them in a data frame named sample_props_small. Print the output. How many observations are there in this object called sample_props_small? What does each observation represent?
sample_props_small <- global_monitor %>%
infer::rep_sample_n(size = 10, reps = 25, replace = TRUE) %>%
count(scientist_work) %>%
mutate(p = n / sum(n)) %>%
filter(scientist_work == "Doesn't benefit")
sample_props_small
## # A tibble: 22 × 4
## # Groups: replicate [22]
## replicate scientist_work n p
## <int> <chr> <int> <dbl>
## 1 1 Doesn't benefit 2 0.2
## 2 3 Doesn't benefit 1 0.1
## 3 4 Doesn't benefit 2 0.2
## 4 5 Doesn't benefit 3 0.3
## 5 6 Doesn't benefit 1 0.1
## 6 7 Doesn't benefit 1 0.1
## 7 8 Doesn't benefit 4 0.4
## 8 9 Doesn't benefit 4 0.4
## 9 10 Doesn't benefit 1 0.1
## 10 11 Doesn't benefit 2 0.2
## # … with 12 more rows
=> There are 25 observations in the sample_props_small object, each one representing the proportion of “Doesn’t benefit” replies in a sample of size 10.
Exercise 6. Use the app below to create sampling distributions of proportions of Doesn’t benefit from samples of size 10, 50, and 100. Use 5,000 simulations. What does each observation in the sampling distribution represent? How does the mean, standard error, and shape of the sampling distribution change as the sample size increases? How (if at all) do these values change if you increase the number of simulations? (You do not need to include plots in your answer.)
=> The “standard deviation” of the sample mean decreases with sample size. The “mean” stays approximately consistent.
Exercise 7. Take a sample of size 15 from the population and calculate the proportion of people in this sample who think the work scientists do enhances their lives. Using this sample, what is your best point estimate of the population proportion of people who think the work scientists do enchances their lives?
samp3 <- global_monitor %>%
sample_n(15)
table(samp3$scientist_work)
##
## Benefits Doesn't benefit
## 11 4
Exercise 8. Since you have access to the population, simulate the sampling distribution of proportion of those who think the work scientists do enchances their lives for samples of size 15 by taking 2000 samples from the population of size 15 and computing 2000 sample proportions. Store these proportions in as sample_props15. Plot the data, then describe the shape of this sampling distribution. Based on this sampling distribution, what would you guess the true proportion of those who think the work scientists do enchances their lives to be? Finally, calculate and report the population proportion.
sample_props15 <- global_monitor %>%
infer::rep_sample_n(size = 15, reps = 2000, replace = TRUE) %>%
count(scientist_work) %>%
mutate(p = n /sum(n)) %>%
filter(scientist_work == "Benefits")
sample_props15
## # A tibble: 2,000 × 4
## # Groups: replicate [2,000]
## replicate scientist_work n p
## <int> <chr> <int> <dbl>
## 1 1 Benefits 11 0.733
## 2 2 Benefits 14 0.933
## 3 3 Benefits 11 0.733
## 4 4 Benefits 11 0.733
## 5 5 Benefits 10 0.667
## 6 6 Benefits 10 0.667
## 7 7 Benefits 14 0.933
## 8 8 Benefits 11 0.733
## 9 9 Benefits 12 0.8
## 10 10 Benefits 11 0.733
## # … with 1,990 more rows
mean(sample_props15$p)
## [1] 0.7982667
Exercise 9. Change your sample size from 15 to 150, then compute the sampling distribution using the same method as above, and store these proportions in a new object called sample_props150. Describe the shape of this sampling distribution and compare it to the sampling distribution for a sample size of 15. Based on this sampling distribution, what would you guess to be the true proportion of those who think the work scientists do enchances their lives?
sample_props150 <- global_monitor %>%
infer::rep_sample_n(size = 150, reps = 2000, replace = TRUE) %>%
count(scientist_work) %>%
mutate(p = n /sum(n)) %>%
filter(scientist_work == "Benefits")
sample_props150
## # A tibble: 2,000 × 4
## # Groups: replicate [2,000]
## replicate scientist_work n p
## <int> <chr> <int> <dbl>
## 1 1 Benefits 123 0.82
## 2 2 Benefits 126 0.84
## 3 3 Benefits 129 0.86
## 4 4 Benefits 112 0.747
## 5 5 Benefits 122 0.813
## 6 6 Benefits 118 0.787
## 7 7 Benefits 122 0.813
## 8 8 Benefits 121 0.807
## 9 9 Benefits 115 0.767
## 10 10 Benefits 133 0.887
## # … with 1,990 more rows
mean(sample_props150$p)
## [1] 0.7989433
Exercise 10. Of the sampling distributions from 2 and 3, which has a smaller spread? If you’re concerned with making estimates that are more often close to the true value, would you prefer a sampling distribution with a large or small spread?
sd(sample_props15$p) > sd(sample_props150$p)
## [1] TRUE
=> We would prefer the larger sample size.
Rpubs => https://rpubs.com/gunduzhazal/815671