In this lab, we will explore and visualize the data using the tidyverse suite of packages, and perform statistical inference using infer. The data can be found in the companion package for OpenIntro resources, openintro.
Let’s load the packages.
You will be analyzing the same dataset as in the previous lab, where
you delved into a sample from the Youth Risk Behavior Surveillance
System (YRBSS) survey, which uses data from high schoolers to help
discover health patterns. The dataset is called yrbss.
4792 people reported 0, 925 people reported 1-2 days, 493 people reported 3-5 days, 311 people reported 6-9 days, 373 people reported 10-19 days, 298 people reported 20-29 days and 827 people reported 30 days
## # A tibble: 9 × 3
## text_while_driving_30d n p
## <chr> <int> <dbl>
## 1 0 4792 0.353
## 2 1-2 925 0.0681
## 3 10-19 373 0.0275
## 4 20-29 298 0.0219
## 5 3-5 493 0.0363
## 6 30 827 0.0609
## 7 6-9 311 0.0229
## 8 did not drive 4646 0.342
## 9 <NA> 918 0.0676
6040 people have not texted while driving in 30 days while never wearing helmets, while 463 people or 6.64% of people have texted while driving in the last 30 days while never wearing helmets
Remember that you can use filter to limit the dataset to
just non-helmet wearers. Here, we will name the dataset
no_helmet.
Also, it may be easier to calculate the proportion if you create a
new variable that specifies whether the individual has texted every day
while driving over the past 30 days or not. We will call this variable
text_ind.
no_helmet <- no_helmet %>%
mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no")) %>%
filter(!is.na(text_ind))
value <- no_helmet %>%
count(text_ind) %>%
mutate( p = n / sum(n))
value## # A tibble: 2 × 3
## text_ind n p
## <chr> <int> <dbl>
## 1 no 6040 0.929
## 2 yes 463 0.0712
When summarizing the YRBSS, the Centers for Disease Control and Prevention seeks insight into the population parameters. To do this, you can answer the question, “What proportion of people in your sample reported that they have texted while driving each day for the past 30 days?” with a statistic; while the question “What proportion of people on earth have texted while driving each day for the past 30 days?” is answered with an estimate of the parameter.
The inferential tools for estimating population proportion are analogous to those used for means in the last chapter: the confidence interval and the hypothesis test.
no_helmet %>%
specify(response = text_ind, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)## # A tibble: 1 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0649 0.0775
Note that since the goal is to construct an interval estimate for a
proportion, it’s necessary to both include the success
argument within specify, which accounts for the proportion
of non-helmet wearers than have consistently texted while driving the
past 30 days, in this example, and that stat within
calculate is here “prop”, signaling that you are trying to
do some sort of inference on a proportion.
The margin of error for the estimate of the proportion of non-helmet wearers that have texted while driving each day for the past 30 days based on this survey is 0.065% margin of error
## [1] 0.0065
infer package, calculate confidence intervals
for two other categorical variables (you’ll need to decide which level
to call “success”, and report the associated margins of error. Interpet
the interval in context of the data. It may be helpful to create new
data sets for each of the two countries first, and then use these data
sets to construct the confidence intervals.we are 95% confident that the proportion of people in the data set that actually watch tv is exactly 86.1% with a 0% margin of error. We are also 95% confident that the proportion of non-helmet wearers that are African American are between 29.3% and 31.7% with a 1.2% margin of error
## calc confidence interval for hours of tv per school day
hours <- yrbss
hours %>%
count(hours_tv_per_school_day, sort=TRUE) %>%
mutate(p = n /sum(n))## # A tibble: 8 × 3
## hours_tv_per_school_day n p
## <chr> <int> <dbl>
## 1 2 2705 0.199
## 2 <1 2168 0.160
## 3 3 2139 0.157
## 4 do not watch 1840 0.135
## 5 1 1750 0.129
## 6 5+ 1595 0.117
## 7 4 1048 0.0772
## 8 <NA> 338 0.0249
# Create a new variable "hours_tv_school for those who watch or dont watch tv at all"
yrbss <- yrbss %>%
mutate(hours_tv_school = ifelse(str_trim(hours_tv_per_school_day) != "do not watch", "yes", "no")) %>%
filter(!is.na(hours_tv_school))
# Calculate the proportion
hours_proportion <- yrbss %>%
specify(response = hours_tv_school, success = "yes") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95, type = "percentile")
## calc the margin of error
lower_ci1 <- 0.861
upper_ci1 <- 0.861
me1 <- (upper_ci1 - lower_ci1) / 2
me1## [1] 0
# the proportion of non-helmet wearers and are Black or African American
no_helmet <- no_helmet %>%
mutate(race_or_not = ifelse(str_trim(race) == "Black or African American", "yes", "no")) %>%
filter(!is.na(race_or_not))
no_helmet %>%
specify(response = race_or_not, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)## # A tibble: 1 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.286 0.309
## calc the margin of error
lower_ci2 <- 0.293
upper_ci2 <- 0.317
me2 <- (upper_ci2 - lower_ci2) / 2 # margin of error
me2## [1] 0.012
Imagine you’ve set out to survey 1000 people on two questions: are you at least 6-feet tall? 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? Wrong! 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:
The first step is to make a variable p that is a
sequence from 0 to 1 with each number incremented by 0.01. You can then
create a variable of the margin of error (me) associated
with each of these values of p using the familiar
approximate formula (\(ME = 2 \times
SE\)).
Lastly, you can plot the two variables against each other to reveal
their relationship. To do so, we need to first put these variables in a
data frame that you can call in the ggplot function.
dd <- data.frame(p = p, me = me)
ggplot(data = dd, aes(x = p, y = me)) +
geom_line() +
labs(x = "Population Proportion", y = "Margin of Error")p and
me. Include the margin of error vs. population proportion
plot you constructed in your answer. For a given sample size, for which
value of p is margin of error maximized?The margin of error is not a constant value but changes depending on the population proportion. As the proportion reaches the median which is 50%, the margin of error reaches its peak which is around 0.0118, and as it reaches either extreme, the margin of error decreases. For a given sample size, the margin of error is maximized when the population proportion is equal to .5 or 50%
We have 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 you wonder: what’s so special about the number 10?
The short answer is: nothing. You could argue that you would be fine with 9 or that you 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.
You 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.
The distribution looks normal with the shape having a similar look to a bell curve and the center of the graph being around 0.11 and the spread being between 0.05 to 0.12
As I increase the populations proportions towards 0.5, the graph begins to look more and more like a normally distributed graph, and as I increase or decrease the proportion away from 0.5 the graph begins to look less like a normally distributed graph with a bell curve.
As I increase the value of n, the graph still remains normal
For some of the exercises below, you will conduct inference comparing
two proportions. In such cases, you have a response variable that is
categorical, and an explanatory variable that is also categorical, and
you are comparing the proportions of success of the response variable
across the levels of the explanatory variable. This means that when
using infer, you need to include both variables within
specify.
I am 95% confident that the proportion of students who sleep 10+ hours is between 3.25 and 5.03% with a 0.89% margin of error. Therefore there is no convincing evidence that those who sleep 10+ hours per day are more likely to strength train every day of the week
## calc people who train every day of the week
train_7_days_a_week <- yrbss %>%
filter(strength_training_7d == 7)
train_7_days_a_week <- train_7_days_a_week %>%
mutate(sleep_over_10 = ifelse(school_night_hours_sleep == "10+", "yes", "no")) %>%
filter(!is.na(sleep_over_10))
train_7_days_a_week %>%
specify(response = sleep_over_10, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)## # A tibble: 1 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0330 0.0498
## calculate the margin of error
lower_ci3 <- 0.0325
upper_ci3 <- 0.0503
me3 <- (upper_ci3 - lower_ci3) / 2 # margin of error
me3## [1] 0.0089
a Type 1 error in this situation is equal to the significance level 0.05, which means the probability of the null hypothesis not happening is 5%, which isn’t that significant
I would need a sample size of about 936 people to, with 95% confidence, to estimate estimate the proportion ofresidents that attend a religious service on a weekly basis
# used previous graph to choose a proportion with a low margin of error
type1 <- 0.05
p <- 0.025
z <- qnorm(1 - type1 / 2)
me <- 0.01
n <- (z^2 * p * (1 - p)) / me^2
n## [1] 936.3556