## ── 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
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?
# Within the past 30 days, 2566 students texted while driving 0 days, 515 students did 1-2 days, 281 students did 3-5 days, 175 students did 6-9 days, 207 students did 10-19 days, 180 students did 20-29 days & 463 students did 30 days. While 2116 students did not drive at all throughout the past 30 days & thus were incapable of texting while driving. What is the proportion of people who have texted while driving every day in the past 30 days and never wear helmets?
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?
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.
#The confidence interval for the category of whether or not the students were hispanic was 0.264, 0.287, & the associated margin of error is .011. In the context of the data this means that we are 95% confident that the proportion of hispanic students is somewhere between .264-.287, with a margin of error of .011.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?
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>
## # A tibble: 8 × 2
## # Groups: text_while_driving_30d [8]
## text_while_driving_30d n
## <chr> <int>
## 1 0 2566
## 2 1-2 515
## 3 10-19 207
## 4 20-29 180
## 5 3-5 281
## 6 30 463
## 7 6-9 175
## 8 did not drive 2116
no_helmet |>
filter(!is.na(text_ind)) |>
summarise(prop_text_no_helmet = mean(text_ind == "yes")) |>
pull()## [1] 0.07119791
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")no_helmet <- yrbss |>
filter(helmet_12m == "never") |>
mutate(text_ind = ifelse(hispanic == "hispanic", "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 14 female 9 not Black or African American NA NA never
## 3 15 female 9 hispanic Native Hawaiian or Other… 1.73 84.4 never
## 4 15 female 9 not Black or African American 1.6 55.8 never
## 5 14 male 9 not Black or African American 1.88 71.2 never
## 6 15 male 9 not Black or African American 1.75 63.5 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 |>
specify(response = text_ind, success = "yes") |>
generate(reps = 1000, type = "bootstrap") |>
calculate(stat = "prop") |>
get_ci()## # A tibble: 1 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.265 0.286