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
.
Insert your answer here
## Rows: 13,583
## Columns: 13
## $ age <int> 14, 14, 15, 15, 15, 15, 15, 14, 15, 15, 15, 1…
## $ gender <chr> "female", "female", "female", "female", "fema…
## $ grade <chr> "9", "9", "9", "9", "9", "9", "9", "9", "9", …
## $ hispanic <chr> "not", "not", "hispanic", "not", "not", "not"…
## $ race <chr> "Black or African American", "Black or Africa…
## $ height <dbl> NA, NA, 1.73, 1.60, 1.50, 1.57, 1.65, 1.88, 1…
## $ weight <dbl> NA, NA, 84.37, 55.79, 46.72, 67.13, 131.54, 7…
## $ helmet_12m <chr> "never", "never", "never", "never", "did not …
## $ text_while_driving_30d <chr> "0", NA, "30", "0", "did not drive", "did not…
## $ physically_active_7d <int> 4, 2, 7, 0, 2, 1, 4, 4, 5, 0, 0, 0, 4, 7, 7, …
## $ hours_tv_per_school_day <chr> "5+", "5+", "5+", "2", "3", "5+", "5+", "5+",…
## $ strength_training_7d <int> 0, 0, 0, 0, 1, 0, 2, 0, 3, 0, 3, 0, 0, 7, 7, …
## $ school_night_hours_sleep <chr> "8", "6", "<5", "6", "9", "8", "9", "6", "<5"…
# Amount of days the students texted while driving within past 30 days. Ignore the ones who didn't drive.
yrbss %>%
count(text_while_driving_30d) %>%
filter(text_while_driving_30d > 0 & text_while_driving_30d != "did not drive") %>%
mutate(p_textdrive = n /sum(n)) %>%
arrange(desc(p_textdrive))
## # A tibble: 6 × 3
## text_while_driving_30d n p_textdrive
## <chr> <int> <dbl>
## 1 1-2 925 0.287
## 2 30 827 0.256
## 3 3-5 493 0.153
## 4 10-19 373 0.116
## 5 6-9 311 0.0964
## 6 20-29 298 0.0923
Insert your answer here
yrbss %>%
count(text_while_driving_30d, helmet_12m) %>%
filter(text_while_driving_30d == 30) %>%
mutate(p_textdrive = n /sum(n)) %>%
arrange(desc(p_textdrive))
## # A tibble: 7 × 4
## text_while_driving_30d helmet_12m n p_textdrive
## <chr> <chr> <int> <dbl>
## 1 30 never 463 0.560
## 2 30 did not ride 295 0.357
## 3 30 rarely 29 0.0351
## 4 30 always 13 0.0157
## 5 30 <NA> 13 0.0157
## 6 30 sometimes 10 0.0121
## 7 30 most of time 4 0.00484
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
.
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 %>%
drop_na(text_ind) %>%
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.0652 0.0777
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.
Insert your answer here
# Margin of error = z * σ/sqrt(n) where z= z-score, σ = standard deviation of population, n = sample size.
# For a 95% confidence interval, the z-score is 1.96.
# For the non-helmet wearers, the sample is 6977 from the original 13583 yrbss dataset.
# Of all those non-helmet wearers, 463 drove every day.
no_helmet_drive_everyday <- yrbss %>%
filter(helmet_12m == "never" & text_while_driving_30d == "30")
ci_lower1 <- 0.065
ci_upper1 <- 0.077
error_margin1 <- (ci_upper1 - ci_lower1) / 2
error_margin1
## [1] 0.006
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. Interpret
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.Insert your answer here
# How many non-helmet wearers are active at least 4 days a week
no_helmet_active <- no_helmet %>%
mutate(text_ind = ifelse(physically_active_7d >= 4, "yes", "no"))
no_helmet_active %>%
drop_na(text_ind) %>%
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.583 0.607
## [1] 0.0115
# Margin of error is 0.0115
#-------------------------------------------------------------------------------
# How many non-helmet wearers get at least 7 hours of sleep a night
no_helmet_sleep <- no_helmet %>%
mutate(text_ind = ifelse(school_night_hours_sleep >= 7, "yes", "no"))
no_helmet_sleep %>%
drop_na(text_ind) %>%
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.546 0.570
## [1] 0.0125
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?Insert your answer here
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.
Insert your answer here
Insert your answer here
Insert your answer here
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
.
Insert your answer here
# All categories at first
yrbss %>%
count(school_night_hours_sleep, strength_training_7d) %>%
#filter(text_while_driving_30d > 0 & text_while_driving_30d != "did not drive") %>%
mutate(p_strength_training_7d = n /sum(n)) %>%
group_by(school_night_hours_sleep) %>%
arrange(school_night_hours_sleep) %>%
print(n = 75)
## # A tibble: 72 × 4
## # Groups: school_night_hours_sleep [8]
## school_night_hours_sleep strength_training_7d n p_strength_training_7d
## <chr> <int> <int> <dbl>
## 1 10+ 0 100 0.00736
## 2 10+ 1 17 0.00125
## 3 10+ 2 31 0.00228
## 4 10+ 3 31 0.00228
## 5 10+ 4 18 0.00133
## 6 10+ 5 23 0.00169
## 7 10+ 6 8 0.000589
## 8 10+ 7 84 0.00618
## 9 10+ NA 4 0.000294
## 10 5 0 500 0.0368
## 11 5 1 138 0.0102
## 12 5 2 163 0.0120
## 13 5 3 175 0.0129
## 14 5 4 97 0.00714
## 15 5 5 135 0.00994
## 16 5 6 44 0.00324
## 17 5 7 211 0.0155
## 18 5 NA 17 0.00125
## 19 6 0 822 0.0605
## 20 6 1 223 0.0164
## 21 6 2 294 0.0216
## 22 6 3 323 0.0238
## 23 6 4 216 0.0159
## 24 6 5 276 0.0203
## 25 6 6 99 0.00729
## 26 6 7 372 0.0274
## 27 6 NA 33 0.00243
## 28 7 0 946 0.0696
## 29 7 1 303 0.0223
## 30 7 2 367 0.0270
## 31 7 3 428 0.0315
## 32 7 4 325 0.0239
## 33 7 5 390 0.0287
## 34 7 6 153 0.0113
## 35 7 7 521 0.0384
## 36 7 NA 28 0.00206
## 37 8 0 664 0.0489
## 38 8 1 179 0.0132
## 39 8 2 260 0.0191
## 40 8 3 316 0.0233
## 41 8 4 253 0.0186
## 42 8 5 331 0.0244
## 43 8 6 140 0.0103
## 44 8 7 525 0.0387
## 45 8 NA 24 0.00177
## 46 9 0 172 0.0127
## 47 9 1 59 0.00434
## 48 9 2 77 0.00567
## 49 9 3 88 0.00648
## 50 9 4 71 0.00523
## 51 9 5 85 0.00626
## 52 9 6 33 0.00243
## 53 9 7 175 0.0129
## 54 9 NA 3 0.000221
## 55 <5 0 380 0.0280
## 56 <5 1 77 0.00567
## 57 <5 2 99 0.00729
## 58 <5 3 88 0.00648
## 59 <5 4 65 0.00479
## 60 <5 5 72 0.00530
## 61 <5 6 23 0.00169
## 62 <5 7 154 0.0113
## 63 <5 NA 7 0.000515
## 64 <NA> 0 48 0.00353
## 65 <NA> 1 16 0.00118
## 66 <NA> 2 14 0.00103
## 67 <NA> 3 19 0.00140
## 68 <NA> 4 14 0.00103
## 69 <NA> 5 21 0.00155
## 70 <NA> 6 13 0.000957
## 71 <NA> 7 43 0.00317
## 72 <NA> NA 1060 0.0780
# Only those who get at least 10 hours of sleep
yrbss %>%
count(school_night_hours_sleep, strength_training_7d) %>%
filter(school_night_hours_sleep == "10+" & strength_training_7d >= 0) %>%
mutate(p_strength_training_7d = n /sum(n)) %>%
arrange(strength_training_7d)
## # A tibble: 8 × 4
## school_night_hours_sleep strength_training_7d n p_strength_training_7d
## <chr> <int> <int> <dbl>
## 1 10+ 0 100 0.321
## 2 10+ 1 17 0.0545
## 3 10+ 2 31 0.0994
## 4 10+ 3 31 0.0994
## 5 10+ 4 18 0.0577
## 6 10+ 5 23 0.0737
## 7 10+ 6 8 0.0256
## 8 10+ 7 84 0.269
# Only those who workout at least once a week and sleep 10+ hours
yrbss %>%
count(school_night_hours_sleep, strength_training_7d) %>%
filter(school_night_hours_sleep == "10+" & strength_training_7d >= 1) %>%
mutate(p_strength_training_7d = n /sum(n)) %>%
arrange(strength_training_7d)
## # A tibble: 7 × 4
## school_night_hours_sleep strength_training_7d n p_strength_training_7d
## <chr> <int> <int> <dbl>
## 1 10+ 1 17 0.0802
## 2 10+ 2 31 0.146
## 3 10+ 3 31 0.146
## 4 10+ 4 18 0.0849
## 5 10+ 5 23 0.108
## 6 10+ 6 8 0.0377
## 7 10+ 7 84 0.396
# Only those who workout everyday, regardless of sleep.
yrbss %>%
count(school_night_hours_sleep, strength_training_7d) %>%
filter(strength_training_7d == 7) %>%
mutate(p_strength_training_7d = n /sum(n)) %>%
arrange(desc(n))
## # A tibble: 8 × 4
## school_night_hours_sleep strength_training_7d n p_strength_training_7d
## <chr> <int> <int> <dbl>
## 1 8 7 525 0.252
## 2 7 7 521 0.250
## 3 6 7 372 0.178
## 4 5 7 211 0.101
## 5 9 7 175 0.0839
## 6 <5 7 154 0.0739
## 7 10+ 7 84 0.0403
## 8 <NA> 7 43 0.0206
#-------------------------------------------------------------------------------
# Let's look from confidence interval perspective.
# Get those who sleep 10+ hours
sleep10 <- yrbss %>%
filter(school_night_hours_sleep == "10+")
# Those who workout everyday
sleep10 <- sleep10 %>%
mutate(text_ind_str7 = ifelse(strength_training_7d == "7", "yes", "no"))
sleep10 %>%
drop_na(text_ind_str7) %>%
specify(response = text_ind_str7, 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.221 0.321
Insert your answer here
Insert your answer here
p <- 0.5 # Use p = 0.5 as this corresponds to the greatest margin of error.
me <- 0.01
z <- 1.96 # The Z-Score for a 95% confidence interval
se <- sqrt((p*(1-p))/n)
n <- ((z^2)*(p*(1-p)))/(me^2)
n
## [1] 9604