In this lab, you will investigate the ways in which the statistics from a random sample of data can serve as point estimates for population parameters. We’re interested in formulating a sampling distribution of our estimate in order to learn about the properties of the estimate, such as its distribution.
Setting a seed: We will take some random samples and build sampling distributions in this lab, which means you should set a seed at the start of your lab. If this concept is new to you, review the lab on probability.
In this lab, we will explore and visualize the data using the tidyverse suite of packages. We will also use the infer package for resampling.
Let’s load the packages.
A 2019 Gallup report states the following:
The premise that scientific progress benefits people has been embodied in discoveries throughout the ages – from the development of vaccinations to the explosion of technology in the past few decades, resulting in billions of supercomputers now resting in the hands and pockets of people worldwide. Still, not everyone around the world feels science benefits them personally.
The Wellcome Global Monitor finds that 20% of people globally do not believe that the work scientists do benefits people like them. In this lab, you will assume this 20% is a true population proportion and learn about how sample proportions can vary from sample to sample by taking smaller samples from the population. We will first create our population assuming a population size of 100,000. This means 20,000 (20%) of the population think the work scientists do does not benefit them personally and the remaining 80,000 think it does.
global_monitor <- tibble(
scientist_work = c(rep("Benefits", 80000), rep("Doesn't benefit", 20000))
)The name of the data frame is global_monitor and the
name of the variable that contains responses to the question “Do you
believe that the work scientists do benefit people like you?” is
scientist_work.
We can quickly visualize the distribution of these responses using a bar plot.
ggplot(global_monitor, aes(x = scientist_work)) +
geom_bar() +
labs(
x = "", y = "",
title = "Do you believe that the work scientists do benefit people like you?"
) +
coord_flip() We can also obtain summary statistics to confirm we constructed the data frame correctly.
## # A tibble: 2 × 3
## scientist_work n p
## <chr> <int> <dbl>
## 1 Benefits 80000 0.8
## 2 Doesn't benefit 20000 0.2
In this lab, you have access to the entire population, but this is rarely the case in real life. Gathering information on an entire population is often extremely costly or impossible. Because of this, we often take a sample of the population and use that to understand the properties of the population.
If you are interested in estimating the proportion of people who
don’t think the work scientists do benefits them, you can use the
sample_n command to survey the population.
This command collects a simple random sample of size 50 from the
global_monitor dataset, and assigns the result to
samp1. This is similar to randomly drawing names from a hat
that contains the names of all in the population. Working with these 50
names is considerably simpler than working with all 100,000 people in
the population.
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.# Summary table for samp1 with sample proportions (p_hat)
samp1 %>%
count(scientist_work) %>%
mutate(p_hat = n / sum(n))## # A tibble: 2 × 3
## scientist_work n p_hat
## <chr> <int> <dbl>
## 1 Benefits 37 0.74
## 2 Doesn't benefit 13 0.26
# Bar plot for samp1
ggplot(samp1, aes(x = scientist_work)) +
geom_bar() +
labs(x = "", y = "", title = "Sample (n = 50) vs population pattern") +
coord_flip()In this sample of 50, about r samp1 %>% count(scientist_work) %>% mutate(p_hat = n/sum(n)) %>% filter(scientist_work==“Doesn’t benefit”) %>% pull(p_hat) %>% round(2) report “Doesn’t benefit.” The population rate is 0.20, so this sample is close but not identical — which is expected due to random sampling variability.
If you’re interested in estimating the proportion of all people who do not believe that the work scientists do benefits them, but you do not have access to the population data, your best single guess is the sample mean.
## # A tibble: 2 × 3
## scientist_work n p_hat
## <chr> <int> <dbl>
## 1 Benefits 37 0.74
## 2 Doesn't benefit 13 0.26
Depending on which 50 people you selected, your estimate could be a bit above or a bit below the true population proportion of 0.26. In general, though, the sample proportion turns out to be a pretty good estimate of the true population proportion, and you were able to get it by sampling less than 1% of the population.
No. Each simple random sample differs by chance, so p-hat will vary. With p=0.20and n=50, the standard error is √(p(1-p)/n)=√(0.20×0.80/50)≈0.056. So most samples should fall within about ±2×SE ≈ ±0.11 of 0.20 (roughly 0.09 to 0.31). We expect somewhat different, but not wildly different, proportions.
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)
samp2 %>%
count(scientist_work) %>%
mutate(p_hat = n / sum(n))## # A tibble: 2 × 3
## scientist_work n p_hat
## <chr> <int> <dbl>
## 1 Benefits 42 0.84
## 2 Doesn't benefit 8 0.16
# Create sampling distribution of p-hat with reps = 15,000, n = 50
# Note: sampling distributions are built with replacement.
sample_props50 <- global_monitor %>%
rep_sample_n(size = 50, reps = 15000, replace = TRUE) %>%
mutate(is_no = scientist_work == "Doesn't benefit") %>%
group_by(replicate) %>%
summarise(p_hat = mean(is_no), .groups = "drop")
# Quick numeric summary of the sampling distribution
sample_props50 %>%
summarise(mean_p_hat = mean(p_hat), sd_p_hat = sd(p_hat))## # A tibble: 1 × 2
## mean_p_hat sd_p_hat
## <dbl> <dbl>
## 1 0.199 0.0569
# Histogram of the sampling distribution of p-hat (n=50)
ggplot(sample_props50, aes(x = p_hat)) +
geom_histogram(bins = 30, color = "white") +
labs(
title = "Sampling distribution of p-hat (n = 50, reps = 15,000)",
x = "Sample proportion (p-hat) of 'Doesn’t benefit'",
y = "Count"
)samp2’s p-hat is close to, but not exactly the same as, samp1’s p-hat — normal sampling variation. Between samples of n = 50, 100, and 1000, the larger n gives a more accurate estimate (smaller standard error). So n = 1000 would be the most accurate.
Not surprisingly, every time you take another random sample, you
might get a different sample proportion. It’s useful to get a sense of
just how much variability you should expect when estimating the
population mean this way. The distribution of sample proportions, called
the sampling distribution (of the proportion), can help you
understand this variability. In this lab, because you have access to the
population, you can build up the sampling distribution for the sample
proportion by repeating the above steps many times. Here, we use R to
take 15,000 different samples of size 50 from the population, calculate
the proportion of responses in each sample, filter for only the
Doesn’t benefit responses, and store each result in a vector
called sample_props50. Note that we specify that
replace = TRUE since sampling distributions are constructed
by sampling with replacement.
# Build p-hat for "Doesn't benefit" for EACH replicate (n = 50, reps = 15,000)
sample_props50 <- global_monitor %>%
rep_sample_n(size = 50, reps = 15000, replace = TRUE) %>%
group_by(replicate) %>% # <- group by sample
count(scientist_work, name = "n") %>% # counts per replicate & category
mutate(p_hat = n / sum(n)) %>% # p-hat within each replicate
filter(scientist_work == "Doesn't benefit") %>%
ungroup() %>%
select(replicate, p_hat)And we can visualize the distribution of these proportions with a histogram.
emp_mean <- mean(sample_props50$p_hat)
emp_sd <- sd(sample_props50$p_hat)
theo_se <- sqrt(0.20 * 0.80 / 50)
tibble(empirical_mean = emp_mean,
empirical_sd = emp_sd,
theoretical_se = theo_se)## # A tibble: 1 × 3
## empirical_mean empirical_sd theoretical_se
## <dbl> <dbl> <dbl>
## 1 0.200 0.0563 0.0566
Next, you will review how this set of code works.
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.**How many elements? sample_props50 has 15,000 rows, one for each replicate (sample) of size 50; each row is a sample proportion p̂ for “Doesn’t benefit.”
Center: The distribution is centered near 0.20 (the true population proportion), showing p̂ is an unbiased estimator of p.
Spread/shape: The spread (SE) is about √[0.2×0.8/50] ≈ 0.0566, and the histogram is roughly bell-shaped due to the CLT for proportions.**
The idea behind the rep_sample_n function is
repetition. Earlier, you took a single sample of size
n (50) from the population of all people in the population.
With this new function, you can repeat this sampling procedure
rep times in order to build a distribution of a series of
sample statistics, which is called the sampling
distribution.
Note that in practice one rarely gets to build true sampling distributions, because one rarely has access to data from the entire population.
Without the rep_sample_n function, this would be
painful. We would have to manually run the following code 15,000
times
global_monitor %>%
sample_n(size = 50, replace = TRUE) %>%
count(scientist_work) %>%
mutate(p_hat = n /sum(n)) %>%
filter(scientist_work == "Doesn't benefit")## # A tibble: 1 × 3
## scientist_work n p_hat
## <chr> <int> <dbl>
## 1 Doesn't benefit 8 0.16
as well as store the resulting sample proportions each time in a separate vector.
Note that for each of the 15,000 times we computed a proportion, we did so from a different sample!
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 %>%
rep_sample_n(size = 10, reps = 25, replace = TRUE) %>%
group_by(replicate) %>%
count(scientist_work, name = "n") %>%
mutate(p_hat = n / sum(n)) %>%
filter(scientist_work == "Doesn't benefit") %>%
ungroup() %>%
select(replicate, p_hat)
sample_props_small## # A tibble: 22 × 2
## replicate p_hat
## <int> <dbl>
## 1 1 0.1
## 2 2 0.2
## 3 3 0.6
## 4 4 0.1
## 5 5 0.1
## 6 6 0.3
## 7 8 0.1
## 8 9 0.3
## 9 10 0.2
## 10 12 0.1
## # ℹ 12 more rows
## [1] 22
sample_props_small has 25 observations, one p̂ per replicate. Each observation represents the sample proportion of “Doesn’t benefit” computed from one random sample of size 10.
Mechanics aside, let’s return to the reason we used the
rep_sample_n function: to compute a sampling distribution,
specifically, the sampling distribution of the proportions from samples
of 50 people.
The sampling distribution that you computed tells you much about estimating the true proportion of people who think that the work scientists do doesn’t benefit them. Because the sample proportion is an unbiased estimator, the sampling distribution is centered at the true population proportion, and the spread of the distribution indicates how much variability is incurred by sampling only 50 people at a time from the population.
In the remainder of this section, you will work on getting a sense of the effect that sample size has on your sampling distribution.
**Each observation in the sampling distribution is a sample proportion p̂ from one random sample.
As sample size n increases (10 → 50 → 100):
The mean stays near 0.20 (unbiased).
The standard error decreases roughly as √[p(1−p)/n], so the distribution gets narrower (more precise).
The shape becomes more normal (by CLT) as n grows.
As you increase the number of simulations (reps):
The histogram looks smoother, and your empirical mean/SE estimates become more stable,
but the true mean and true SE don’t change — you’re just estimating them more accurately.**
So far, you have only focused on estimating the proportion of those you think the work scientists doesn’t benefit them. Now, you’ll try to estimate the proportion of those who think it does.
Note that while you might be able to answer some of these questions using the app, you are expected to write the required code and produce the necessary plots and summary statistics. You are welcome to use the app for exploration.
set.seed(1234) # keep results reproducible
samp15 <- global_monitor %>%
sample_n(15)
# sample proportion p-hat for "Benefits"
q7_table <- samp15 %>%
count(scientist_work, name = "n") %>%
mutate(p_hat = n / sum(n))
q7_table## # A tibble: 2 × 3
## scientist_work n p_hat
## <chr> <int> <dbl>
## 1 Benefits 11 0.733
## 2 Doesn't benefit 4 0.267
q7_p_hat_benefit <- q7_table %>%
filter(scientist_work == "Benefits") %>%
pull(p_hat)
q7_p_hat_benefit## [1] 0.7333333
The point estimate (sample proportion) for “Benefits” from n=15 is r round(q7_p_hat_benefit, 2). This is our best single-number estimate of the population proportion for “Benefits.”
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.set.seed(1234)
sample_props15 <- global_monitor %>%
rep_sample_n(size = 15, reps = 2000, replace = TRUE) %>%
group_by(replicate) %>%
count(scientist_work, name = "n") %>%
mutate(p_hat = n / sum(n)) %>%
filter(scientist_work == "Benefits") %>%
ungroup() %>%
select(replicate, p_hat)
# numeric summary
q8_stats <- sample_props15 %>%
summarise(mean = mean(p_hat),
sd = sd(p_hat))
q8_stats## # A tibble: 1 × 2
## mean sd
## <dbl> <dbl>
## 1 0.800 0.105
ggplot(sample_props15, aes(x = p_hat)) +
geom_histogram(binwidth = 0.04, color = "white") +
geom_vline(xintercept = 0.80, linetype = "dashed") +
labs(
title = "Sampling distribution of p̂ for 'Benefits' (n = 15, reps = 2000)",
x = "p̂ (Benefits)", y = "Count"
)The sampling distribution is roughly unimodal and centered near r round(q8_stats\(mean, 2), close to the true population proportion of 0.80. Its spread (SE) is about r round(q8_stats\)sd, 3). Population proportion: from the full population, p(Benefits)=80,000/100,000=0.80.
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?set.seed(1234)
sample_props150 <- global_monitor %>%
rep_sample_n(size = 150, reps = 2000, replace = TRUE) %>%
group_by(replicate) %>%
count(scientist_work, name = "n") %>%
mutate(p_hat = n / sum(n)) %>%
filter(scientist_work == "Benefits") %>%
ungroup() %>%
select(replicate, p_hat)
# numeric summary
q9_stats <- sample_props150 %>%
summarise(mean = mean(p_hat),
sd = sd(p_hat))
q9_stats## # A tibble: 1 × 2
## mean sd
## <dbl> <dbl>
## 1 0.800 0.0335
ggplot(sample_props150, aes(x = p_hat)) +
geom_histogram(binwidth = 0.01, color = "white") +
geom_vline(xintercept = 0.80, linetype = "dashed") +
labs(
title = "Sampling distribution of p̂ for 'Benefits' (n = 150, reps = 2000)",
x = "p̂ (Benefits)", y = "Count"
)he distribution for n=150 is tighter (smaller SD: r round(q9_stats\(sd, 3)) and more bell-shaped than for n=15. It’s still centered near r round(q9_stats\)mean, 2) ≈ 0.80, which matches the true proportion.
The sampling distribution with n=150 has the smaller spread (smaller standard error). If we want estimates that are more often close to the true value, we prefer a sampling distribution with a small spread (i.e., larger sample size). * * *