Hazal Gunduz

Inference for categorical data

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.5     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.0.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(infer)

The Data

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.

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?

view(yrbss)
counts_each <- yrbss %>%
  count(text_while_driving_30d)
counts_each
## # A tibble: 9 × 2
##   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")
no_helmet <- no_helmet %>%
  mutate(text_ind_everyday = ifelse(text_while_driving_30d == "30", "yes", "no"))

no_helmet %>%
  count(text_ind_everyday)
## # A tibble: 3 × 2
##   text_ind_everyday     n
##   <chr>             <int>
## 1 no                 6040
## 2 yes                 463
## 3 <NA>                474

Inference on proportions

no_helmet %>%
  specify(response = text_ind_everyday, success = "yes") %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "prop") %>%
  get_ci(level = 0.95)
## Warning: Removed 474 rows containing missing values.
## Warning: You have given `type = "bootstrap"`, but `type` is expected to be
## `"draw"`. This workflow is untested and the results may not mean what you think
## they mean.
## # A tibble: 1 × 2
##   lower_ci upper_ci
##      <dbl>    <dbl>
## 1   0.0654   0.0770

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?

n <- 6503
z <- 1.96
p <- seq(from = 0, to = 1, by = 0.01)
se <- z * sqrt((p * (1 - p)) / n)
me <- z * se
me
##   [1] 0.000000000 0.004739940 0.006669347 0.008126467 0.009335141 0.010382503
##   [7] 0.011313444 0.012154742 0.012923917 0.013633182 0.014291457 0.014905502
##  [13] 0.015480580 0.016020887 0.016529835 0.017010237 0.017464449 0.017894467
##  [19] 0.018301996 0.018688506 0.019055277 0.019403428 0.019733945 0.020047700
##  [25] 0.020345468 0.020627942 0.020895742 0.021149426 0.021389495 0.021616404
##  [31] 0.021830562 0.022032342 0.022222080 0.022400083 0.022566629 0.022721969
##  [37] 0.022866332 0.022999924 0.023122932 0.023235525 0.023337852 0.023430049
##  [43] 0.023512235 0.023584513 0.023646976 0.023699701 0.023742752 0.023776183
##  [49] 0.023800033 0.023814331 0.023819096 0.023814331 0.023800033 0.023776183
##  [55] 0.023742752 0.023699701 0.023646976 0.023584513 0.023512235 0.023430049
##  [61] 0.023337852 0.023235525 0.023122932 0.022999924 0.022866332 0.022721969
##  [67] 0.022566629 0.022400083 0.022222080 0.022032342 0.021830562 0.021616404
##  [73] 0.021389495 0.021149426 0.020895742 0.020627942 0.020345468 0.020047700
##  [79] 0.019733945 0.019403428 0.019055277 0.018688506 0.018301996 0.017894467
##  [85] 0.017464449 0.017010237 0.016529835 0.016020887 0.015480580 0.014905502
##  [91] 0.014291457 0.013633182 0.012923917 0.012154742 0.011313444 0.010382503
##  [97] 0.009335141 0.008126467 0.006669347 0.004739940 0.000000000

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.

glimpse(yrbss)
## 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"…
yrbss %>%
  count(physically_active_7d, sort = TRUE)
## # A tibble: 9 × 2
##   physically_active_7d     n
##                  <int> <int>
## 1                    7  3622
## 2                    0  2172
## 3                    5  1728
## 4                    3  1451
## 5                    2  1270
## 6                    4  1265
## 7                    1   962
## 8                    6   840
## 9                   NA   273
yrbss %>%
  count(school_night_hours_sleep, sort = TRUE)
## # A tibble: 8 × 2
##   school_night_hours_sleep     n
##   <chr>                    <int>
## 1 7                         3461
## 2 8                         2692
## 3 6                         2658
## 4 5                         1480
## 5 <NA>                      1248
## 6 <5                         965
## 7 9                          763
## 8 10+                        316
exercise_time <- yrbss %>%
  filter(!is.na(physically_active_7d)) %>%
  mutate(exercise_everyday = ifelse(physically_active_7d < "1", "yes", "no"))
exercise_time %>%
  count(exercise_everyday)
## # A tibble: 2 × 2
##   exercise_everyday     n
##   <chr>             <int>
## 1 no                11138
## 2 yes                2172
exercise_time %>%
  specify(response = exercise_everyday, 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.157    0.170
n <- nrow(exercise_time)
z <- 1.96
se <- z*sqrt((p*(1-p)) / n)
me <- z*se
me
##   [1] 0.000000000 0.003313147 0.004661773 0.005680278 0.006525123 0.007257214
##   [7] 0.007907927 0.008495983 0.009033624 0.009529389 0.009989514 0.010418722
##  [13] 0.010820693 0.011198360 0.011554106 0.011889900 0.012207388 0.012507964
##  [19] 0.012792820 0.013062985 0.013319352 0.013562705 0.013793731 0.014013041
##  [25] 0.014221177 0.014418622 0.014605810 0.014783131 0.014950936 0.015109542
##  [31] 0.015259235 0.015400276 0.015532901 0.015657322 0.015773735 0.015882315
##  [37] 0.015983223 0.016076602 0.016162583 0.016241283 0.016312808 0.016377253
##  [43] 0.016434699 0.016485221 0.016528882 0.016565735 0.016595827 0.016619195
##  [49] 0.016635866 0.016645860 0.016649190 0.016645860 0.016635866 0.016619195
##  [55] 0.016595827 0.016565735 0.016528882 0.016485221 0.016434699 0.016377253
##  [61] 0.016312808 0.016241283 0.016162583 0.016076602 0.015983223 0.015882315
##  [67] 0.015773735 0.015657322 0.015532901 0.015400276 0.015259235 0.015109542
##  [73] 0.014950936 0.014783131 0.014605810 0.014418622 0.014221177 0.014013041
##  [79] 0.013793731 0.013562705 0.013319352 0.013062985 0.012792820 0.012507964
##  [85] 0.012207388 0.011889900 0.011554106 0.011198360 0.010820693 0.010418722
##  [91] 0.009989514 0.009529389 0.009033624 0.008495983 0.007907927 0.007257214
##  [97] 0.006525123 0.005680278 0.004661773 0.003313147 0.000000000

How does the proportion affect the margin of error?

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")

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)
me
##   [1] 0.000000000 0.006292853 0.008854377 0.010788883 0.012393547 0.013784049
##   [7] 0.015019987 0.016136914 0.017158088 0.018099724 0.018973666 0.019788886
##  [13] 0.020552372 0.021269697 0.021945387 0.022583180 0.023186203 0.023757104
##  [19] 0.024298148 0.024811288 0.025298221 0.025760435 0.026199237 0.026615785
##  [25] 0.027011109 0.027386128 0.027741665 0.028078461 0.028397183 0.028698432
##  [31] 0.028982753 0.029250641 0.029502542 0.029738863 0.029959973 0.030166206
##  [37] 0.030357866 0.030535226 0.030698534 0.030848015 0.030983867 0.031106269
##  [43] 0.031215381 0.031311340 0.031394267 0.031464265 0.031521421 0.031565804
##  [49] 0.031597468 0.031616451 0.031622777 0.031616451 0.031597468 0.031565804
##  [55] 0.031521421 0.031464265 0.031394267 0.031311340 0.031215381 0.031106269
##  [61] 0.030983867 0.030848015 0.030698534 0.030535226 0.030357866 0.030166206
##  [67] 0.029959973 0.029738863 0.029502542 0.029250641 0.028982753 0.028698432
##  [73] 0.028397183 0.028078461 0.027741665 0.027386128 0.027011109 0.026615785
##  [79] 0.026199237 0.025760435 0.025298221 0.024811288 0.024298148 0.023757104
##  [85] 0.023186203 0.022583180 0.021945387 0.021269697 0.020552372 0.019788886
##  [91] 0.018973666 0.018099724 0.017158088 0.016136914 0.015019987 0.013784049
##  [97] 0.012393547 0.010788883 0.008854377 0.006292853 0.000000000

=> The margin of error seems to increase as the population proportion increases. Once it reaches 50% it begins to decrease.

Exercise 6. Describe the sampling distribution of sample proportions at n=300 and p=0.1. Be sure to note the center, spread, and shape.

=> The distribution is normal ,unimodal, centered at 0.1, spread between 0.04 and 0.17.

Exercise 7. Keep n constant and change p. How does the shape, center, and spread of the sampling distribution vary as p changes. You might want to adjust min and max for the x-axis for a better view of the distribution.

=> To keep n constant, the shape is normal and spread increases as p goes up to 50%, then spread decreases when p reaches 100%.

Exercise 8. Now also change n. How does n appear to affect the distribution of p̂ ?

=> The sample distribution of population seems normal, unimodal with less spread as well as standard error decreases.

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.

sleep_time<- yrbss %>%
  filter(!is.na(school_night_hours_sleep)) %>%
  mutate(sleep_everyday = ifelse(school_night_hours_sleep == "10+", "yes", "no"))

sleep_time %>%
  count(sleep_everyday)
## # A tibble: 2 × 2
##   sleep_everyday     n
##   <chr>          <int>
## 1 no             12019
## 2 yes              316
sleep_time %>%
 specify(response = sleep_everyday, 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.0229   0.0284
n <- nrow(sleep_time)
z <- 1.96
se <- z * sqrt((p * (1 - p)) / n)

me<- z * se
me
##   [1] 0.000000000 0.003441598 0.004842511 0.005900503 0.006778103 0.007538577
##   [7] 0.008214519 0.008825373 0.009383859 0.009898845 0.010376809 0.010822657
##  [13] 0.011240213 0.011632522 0.012002060 0.012350873 0.012680670 0.012992899
##  [19] 0.013288799 0.013569438 0.013835745 0.014088533 0.014328516 0.014556328
##  [25] 0.014772533 0.014977634 0.015172079 0.015356275 0.015530586 0.015695340
##  [31] 0.015850837 0.015997347 0.016135113 0.016264358 0.016385285 0.016498075
##  [37] 0.016602894 0.016699894 0.016789208 0.016870960 0.016945258 0.017012201
##  [43] 0.017071874 0.017124355 0.017169708 0.017207991 0.017239250 0.017263523
##  [49] 0.017280840 0.017291222 0.017294682 0.017291222 0.017280840 0.017263523
##  [55] 0.017239250 0.017207991 0.017169708 0.017124355 0.017071874 0.017012201
##  [61] 0.016945258 0.016870960 0.016789208 0.016699894 0.016602894 0.016498075
##  [67] 0.016385285 0.016264358 0.016135113 0.015997347 0.015850837 0.015695340
##  [73] 0.015530586 0.015356275 0.015172079 0.014977634 0.014772533 0.014556328
##  [79] 0.014328516 0.014088533 0.013835745 0.013569438 0.013288799 0.012992899
##  [85] 0.012680670 0.012350873 0.012002060 0.011632522 0.011240213 0.010822657
##  [91] 0.010376809 0.009898845 0.009383859 0.008825373 0.008214519 0.007538577
##  [97] 0.006778103 0.005900503 0.004842511 0.003441598 0.000000000

=> We need to reject the null hypothesis. 95% confident that the proportion of students who sleeps 10+ hours is between 2.29% and 2.83% and me is 0.17%, where is 95 % confident that the proportion of students who will strength exercise is between 1.61% and 1.74% and me is 0.17% too.

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 probability 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.

=> There could be 5% chance of detecting the change. A type one error is a false positive. Typically what happens is when researchers incorrectly reject the true null hypothesis.They would report their findings and state that the finds are significant when they’re really insignificant.

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.

p <- 0.50
me <- 0.01 
z <- 1.96
se <- sqrt((p * (1 - p)) / n)

n <- ((z ^ 2) * (p * (1 - p))) / (me ^ 2)
n
## [1] 9604

=> 9604 people.

=> Rpubs https://rpubs.com/gunduzhazal/820788