RLab

Load Packages

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata
library(infer)

The Data

Exercise 1

What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?

yrbss |>
  group_by(text_while_driving_30d) |>
  count()
# A tibble: 9 × 2
# Groups:   text_while_driving_30d [9]
  text_while_driving_30d     n
  <chr>                  <int>
1 0                       4792
2 1-2                      925
3 10-19                    373
4 20-29                    298
5 3-5                      493
6 30                       827
7 6-9                      311
8 did not drive           4646
9 <NA>                     918

Exercise 2

What is the proportion of people who have texted while driving every day in the past 30 days and never wear helmets?

no_helmet <- yrbss |>
  filter(helmet_12m == "never") |>
  mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no")) |>
  filter(!is.na(text_ind))
head(no_helmet)
# A tibble: 6 × 14
    age gender grade hispanic race                      height weight helmet_12m
  <int> <chr>  <chr> <chr>    <chr>                      <dbl>  <dbl> <chr>     
1    14 female 9     not      Black or African American  NA      NA   never     
2    15 female 9     hispanic Native Hawaiian or Other…   1.73   84.4 never     
3    15 female 9     not      Black or African American   1.6    55.8 never     
4    16 male   9     not      Black or African American   1.68   74.8 never     
5    14 male   9     not      Black or African American   1.73   73.5 never     
6    15 male   9     not      Black or African American   1.83   67.6 never     
# ℹ 6 more variables: text_while_driving_30d <chr>, physically_active_7d <int>,
#   hours_tv_per_school_day <chr>, strength_training_7d <int>,
#   school_night_hours_sleep <chr>, text_ind <chr>
no_helmet |>
  summarise(prop_text_no_helmet = mean(text_ind == "yes")) |>
  pull()
[1] 0.07119791

Inference on Proportions

Exercise 3

What is 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?

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.0773
me <- 1.96 * sqrt(0.07119791 * (1-0.07119791)/6503)
me
[1] 0.006250207

Exercise 4

Using the 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.

gender1 <- yrbss |>
  mutate(gender_var = ifelse(gender == "female", "yes", "no")) |>
  filter(!is.na(gender_var)) 
gender1
# A tibble: 13,571 × 14
     age gender grade hispanic race                     height weight helmet_12m
   <int> <chr>  <chr> <chr>    <chr>                     <dbl>  <dbl> <chr>     
 1    14 female 9     not      Black or African Americ…  NA      NA   never     
 2    14 female 9     not      Black or African Americ…  NA      NA   never     
 3    15 female 9     hispanic Native Hawaiian or Othe…   1.73   84.4 never     
 4    15 female 9     not      Black or African Americ…   1.6    55.8 never     
 5    15 female 9     not      Black or African Americ…   1.5    46.7 did not r…
 6    15 female 9     not      Black or African Americ…   1.57   67.1 did not r…
 7    15 female 9     not      Black or African Americ…   1.65  132.  did not r…
 8    14 male   9     not      Black or African Americ…   1.88   71.2 never     
 9    15 male   9     not      Black or African Americ…   1.75   63.5 never     
10    15 male   10    not      Black or African Americ…   1.37   97.1 did not r…
# ℹ 13,561 more rows
# ℹ 6 more variables: text_while_driving_30d <chr>, physically_active_7d <int>,
#   hours_tv_per_school_day <chr>, strength_training_7d <int>,
#   school_night_hours_sleep <chr>, gender_var <chr>
gender1 |>
  specify(response = gender_var, 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.480    0.496
gender1 |>
  summarise(prop_text_gender1 = mean(gender_var == "yes")) |>
  pull()
[1] 0.4878786
me <- 1.96 * sqrt(0.4878786 * (1 - 0.4878786)/13571)
me
[1] 0.008409932
# Interpretation

# We are 95% confident that the true difference in proportions of female and not female youth of the yrbss dataset is between 47.911% and 49.687%. 
not_hispanic <- yrbss |>
  mutate(hispanic_var = ifelse(hispanic == "not", "yes", "no")) |>
  filter(!is.na(hispanic_var)) 
not_hispanic
# A tibble: 13,352 × 14
     age gender grade hispanic race                     height weight helmet_12m
   <int> <chr>  <chr> <chr>    <chr>                     <dbl>  <dbl> <chr>     
 1    14 female 9     not      Black or African Americ…  NA      NA   never     
 2    14 female 9     not      Black or African Americ…  NA      NA   never     
 3    15 female 9     hispanic Native Hawaiian or Othe…   1.73   84.4 never     
 4    15 female 9     not      Black or African Americ…   1.6    55.8 never     
 5    15 female 9     not      Black or African Americ…   1.5    46.7 did not r…
 6    15 female 9     not      Black or African Americ…   1.57   67.1 did not r…
 7    15 female 9     not      Black or African Americ…   1.65  132.  did not r…
 8    14 male   9     not      Black or African Americ…   1.88   71.2 never     
 9    15 male   9     not      Black or African Americ…   1.75   63.5 never     
10    15 male   10    not      Black or African Americ…   1.37   97.1 did not r…
# ℹ 13,342 more rows
# ℹ 6 more variables: text_while_driving_30d <chr>, physically_active_7d <int>,
#   hours_tv_per_school_day <chr>, strength_training_7d <int>,
#   school_night_hours_sleep <chr>, hispanic_var <chr>
not_hispanic |>
  specify(response = hispanic_var, 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.736    0.751
not_hispanic |>
  summarise(prop_text_not_hispanic = mean(hispanic_var == "yes")) |>
  pull()
[1] 0.743559
me <- 1.96 * sqrt(0.743559 * (1 - 0.743559)/13352)
me
[1] 0.007406864
# Interpretation

# We are 95% confident that the true difference in proportions of non-hispanic and hispanic youth of the yrbss dataset is between 73.637% and 75.038%. 

How does the proportion affect the margin of error?

Exercise 5

Describe the relationship between 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?

n <- 1000
p <- seq(from = 0, to = 1, by = 0.01)
me <- 2 * sqrt(p * (1 - p)/n)

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 is maximized when the population proportion is 0.50. 

More Practice

Exercise 9

Is there convincing evidence that those who sleep 10+ hours per day are more likely to strength train every day of the week? As always, write out the hypotheses for any tests you conduct and outline the status of the conditions for inference. If you find a significant difference, also quantify this difference with a confidence interval.

# Ho: There is no evidence those who sleep 10+ hours per day are more likely to strength train every day of the week.
# Ha: There is evidence those who sleep 10+ hours per day are more likely to strength train every day of the week.

# Compare the proportions of those who strength train 7 days of the week and sleep 10+ hours vs those who sleep less

sleep1 <- yrbss |>
  mutate(longer_sleep = ifelse(school_night_hours_sleep == "10+", "yes", "no"))  |>
  filter(!is.na(longer_sleep)) 

Exercise 10

Let’s say there has been no difference in likeliness to strength train every day of the week for those who sleep 10+ hours. What is the probablity that you could detect a change (at a significance level of 0.05) simply by chance? Hint: Review the definition of the Type 1 error.

Exercise 11

Suppose you’re hired by the local government to estimate the proportion of residents that attend a religious service on a weekly basis. According to the guidelines, the estimate must have a margin of error no greater than 1% with 95% confidence. You have no idea what to expect for p . How many people would you have to sample to ensure that you are within the guidelines? Hint: Refer to your plot of the relationship between p and margin of error. This question does not require using a dataset.