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.
library(tidyverse)
library(openintro)
library(infer)To create your new lab report, in RStudio, go to New File -> R Markdown… Then, choose From Template and then choose Lab Report for OpenIntro Statistics Labs from the list of templates.
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.
view(yrbss)counts_each<- yrbss %>%
count(text_while_driving_30d)
counts_each## # A tibble: 9 x 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
Remember that you can use filter to limit the dataset to just non-helmet wearers. Here, we will name the dataset no_helmet.
no_helmet <- yrbss %>%
filter(helmet_12m == "never")%>%
filter(!is.na(text_while_driving_30d))%>%
mutate(text_ind_everyday = ifelse(text_while_driving_30d == "30", "yes", "no"))
no_helmet%>%
count(text_ind_everyday)## # A tibble: 2 x 2
## text_ind_everyday n
## * <chr> <int>
## 1 no 6040
## 2 yes 463
no_helmet <- yrbss %>%
filter(helmet_12m == "never")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"))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 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0652 0.0773
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.
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
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.yrbss %>%
count(physically_active_7d, sort=TRUE)## # A tibble: 9 x 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 x 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 x 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 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.156 0.169
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
CI - 15.7% and 16.9%.
sleep_time<- yrbss %>%
filter(!is.na(school_night_hours_sleep)) %>%
mutate(sleep_everyday = ifelse(school_night_hours_sleep == "<5", "yes", "no"))
sleep_time %>%
count(sleep_everyday)## # A tibble: 2 x 2
## sleep_everyday n
## * <chr> <int>
## 1 no 11370
## 2 yes 965
sleep_time %>%
specify(response = sleep_everyday, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0735 0.0828
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
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:
n <- 1000The 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\)).
p <- seq(from = 0, to = 1, by = 0.01)
me <- 2 * sqrt(p * (1 - p)/n)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?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
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")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 sampling distribution is normal ,unimodal, centered at 0.1, spread between 0.01 and 0.14
To keep n constant, the shape is normal and spread increases as p goes up to 50%, then spread decreases when p reaches 100%.
The sample distribution of population appears to be normal, unimodal with much less spread as well as standard error decreases
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.
exercise_time<- yrbss %>%
filter(!is.na(strength_training_7d)) %>%
mutate(exercise_everyday = ifelse(strength_training_7d == "7", "yes", "no"))
exercise_time %>%
count(exercise_everyday)## # A tibble: 2 x 2
## exercise_everyday n
## * <chr> <int>
## 1 no 10322
## 2 yes 2085
exercise_time %>%
specify(response = exercise_everyday, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.162 0.175
n <- nrow(exercise_time)
z <- 1.96
se <- z*sqrt((p*(1-p))/n)
me<- z * se
me## [1] 0.000000000 0.003431598 0.004828439 0.005883358 0.006758407 0.007516671
## [7] 0.008190649 0.008799728 0.009356591 0.009870081 0.010346656 0.010791209
## [13] 0.011207551 0.011598720 0.011967185 0.012314984 0.012643822 0.012955144
## [19] 0.013250185 0.013530008 0.013795541 0.014047594 0.014286880 0.014514031
## [25] 0.014729607 0.014934111 0.015127992 0.015311652 0.015485457 0.015649733
## [31] 0.015804778 0.015950861 0.016088227 0.016217097 0.016337672 0.016450134
## [37] 0.016554649 0.016651367 0.016740422 0.016821936 0.016896018 0.016962767
## [43] 0.017022267 0.017074595 0.017119816 0.017157988 0.017189156 0.017213359
## [49] 0.017230625 0.017240977 0.017244427 0.017240977 0.017230625 0.017213359
## [55] 0.017189156 0.017157988 0.017119816 0.017074595 0.017022267 0.016962767
## [61] 0.016896018 0.016821936 0.016740422 0.016651367 0.016554649 0.016450134
## [67] 0.016337672 0.016217097 0.016088227 0.015950861 0.015804778 0.015649733
## [73] 0.015485457 0.015311652 0.015127992 0.014934111 0.014729607 0.014514031
## [79] 0.014286880 0.014047594 0.013795541 0.013530008 0.013250185 0.012955144
## [85] 0.012643822 0.012314984 0.011967185 0.011598720 0.011207551 0.010791209
## [91] 0.010346656 0.009870081 0.009356591 0.008799728 0.008190649 0.007516671
## [97] 0.006758407 0.005883358 0.004828439 0.003431598 0.000000000
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 x 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 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0229 0.0285
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 reject the null hypothesis. There is 95% confident that the proportion of students who sleeps 10+ hours is between 16.1% and 17.4% and me is 0.075%, where there is 95 % confident that the proportion of students who will strenght exersice is between 2.25% and 2.85% and me is 0.075% as well.
Type 1 Error is rejecting the null hypothesis when HNull is actually true
set up p 50% percent because the me was the greatest, so by that I would have to sample 9604 people.
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