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)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.
Answer:
count_text <- yrbss|>
count(text_while_driving_30d, sort = TRUE)
print(count_text)## # A tibble: 9 × 2
## text_while_driving_30d n
## <chr> <int>
## 1 0 4792
## 2 did not drive 4646
## 3 1-2 925
## 4 <NA> 918
## 5 30 827
## 6 3-5 493
## 7 10-19 373
## 8 6-9 311
## 9 20-29 298
Answer:
no_helmet <- yrbss|>
filter(helmet_12m =="never")|>
select(text_while_driving_30d)|>
filter(text_while_driving_30d=="30")
remaining <- yrbss|>
filter(helmet_12m=="never")|>
select(text_while_driving_30d)|>
filter(!is.na(text_while_driving_30d))
count_nohelmet <- count(no_helmet)
proportion <- (count(no_helmet)/(count(remaining)-count(no_helmet)))*100
print(proportion)## n
## 1 7.665563
print(count_nohelmet)## # A tibble: 1 × 1
## n
## <int>
## 1 463
Remember that you can use filter to limit the dataset to
just non-helmet wearers. Here, we will name the dataset
no_helmet.
data('yrbss', package='openintro')
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 %>%
filter(!is.na(text_while_driving_30d))%>%
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 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0647 0.0772
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.
Answer:
n <- nrow(no_helmet)
z <- 1.96
p <- seq(from = 0, to = 1, by = 0.01)
se <- z*sqrt((p*(1-p))/n)
me<- z * se
data <- data.frame(p=p,me=me)
ggplot(data=data,aes(x=p,y=me))+geom_line()+labs(x="Proportion",y="Margin of error")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.Answer:
lets go back to the data set and pick up other two variables
head(yrbss)## # A tibble: 6 × 13
## age gender grade hispa…¹ race height weight helme…² text_…³ physi…⁴ hours…⁵
## <int> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <int> <chr>
## 1 14 female 9 not Blac… NA NA never 0 4 5+
## 2 14 female 9 not Blac… NA NA never <NA> 2 5+
## 3 15 female 9 hispan… Nati… 1.73 84.4 never 30 7 5+
## 4 15 female 9 not Blac… 1.6 55.8 never 0 0 2
## 5 15 female 9 not Blac… 1.5 46.7 did no… did no… 2 3
## 6 15 female 9 not Blac… 1.57 67.1 did no… did no… 1 5+
## # … with 2 more variables: strength_training_7d <int>,
## # school_night_hours_sleep <chr>, and abbreviated variable names ¹hispanic,
## # ²helmet_12m, ³text_while_driving_30d, ⁴physically_active_7d,
## # ⁵hours_tv_per_school_day
Looking at the column names I will try to calculate the confidence interval for “physically_active_7d” and “strength_training_7d”.
physically_active_7d:
Calculating confidence interval:
set.seed(1122)
active_time<- yrbss %>%
filter(!is.na(physically_active_7d)) %>%
mutate(exercise_everyday = ifelse(physically_active_7d =="7", "yes", "no"))
active_time %>%
count(exercise_everyday)## # A tibble: 2 × 2
## exercise_everyday n
## <chr> <int>
## 1 no 9688
## 2 yes 3622
active_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.265 0.280
Margin of error:
n <- nrow(active_time)
z <- 1.96
se <- z*sqrt((p*(1-p))/n)
me_a <- z * se
me_a## [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
plot of margin of error against population proportion:
dd_a <- data.frame(p = p, me_a = me_a)
ggplot(data = dd_a, aes(x = p, y = me_a))+
geom_line()+
labs(x = "Population Proportion", y = "Margin of Error")strength_training_7d:
Confidence interval:
set.seed(1122)
strength<- yrbss %>%
filter(!is.na(strength_training_7d)) %>%
mutate(train_everyday = ifelse(strength_training_7d=="7", "yes", "no"))
strength %>%
count(train_everyday)## # A tibble: 2 × 2
## train_everyday n
## <chr> <int>
## 1 no 10322
## 2 yes 2085
strength %>%
specify(response = train_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.161 0.175
Margin of error:
n <- nrow(strength)
z <- 1.96
se <- z*sqrt((p*(1-p))/n)
me_s <- z * sePlotting margin of error against population proportion:
dd_s <- data.frame(p = p, me_s = me_s)
ggplot(data = dd_a, aes(x = p, y = me_s)) +
geom_line() +
labs(x = "Population Proportion", y = "Margin of Error")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?Answer:
Looking at the graph above between p and me
the relationship mimics the parabolic curve. The margin of error
me initially increases with the increment of populations
proportion p and hits the maximum at p=.5. The
me falls back to zero as we cross the population proportion
from .5 toward 1. at p=1, me=0.
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.
Answer:
The sampling distribution has a bell shape and follows normal distribution. The distribution is centered at 0.1 with spread from around .04 to .14.
Answer: 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 center changes with the value of \(p\).
Answer:
As we increase the value of \(n\) the spread of the distribution narrows down and the distribution remains normal with lower standard error.
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.
Answer:
Null Hypothesis: There is no difference in strength training between students that sleep more than 10+ hours and those who don’t. Alternative: There is a difference in strength training between students that sleep more than 10+ hours and those who don’t. We are 95% confident that the student proportion of those students that sleep more than 10+ hours are between 0.221 and 0.321.
set.seed(1122)
good_sleep <- yrbss %>%
filter(school_night_hours_sleep == "10+")
good_sleep <- good_sleep %>%
filter(!is.na(strength_training_7d))%>%
mutate(strength = ifelse(strength_training_7d == "7", "yes", "no"))
good_sleep %>%
specify(response = strength, 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.218 0.317
n <- nrow(good_sleep)
z <- 1.96
se <- z*sqrt((p*(1-p))/n)
me_n<- z * se
me_n## [1] 0.00000000 0.02163976 0.03044829 0.03710063 0.04261873 0.04740036
## [7] 0.05165048 0.05549136 0.05900295 0.06224103 0.06524633 0.06804970
## [13] 0.07067516 0.07314188 0.07546544 0.07765867 0.07973233 0.08169554
## [19] 0.08355607 0.08532065 0.08699511 0.08858456 0.09009351 0.09152592
## [25] 0.09288536 0.09417497 0.09539758 0.09655575 0.09765177 0.09868770
## [31] 0.09966542 0.10058663 0.10145286 0.10226552 0.10302587 0.10373506
## [37] 0.10439413 0.10500403 0.10556562 0.10607965 0.10654681 0.10696773
## [43] 0.10734294 0.10767292 0.10795809 0.10819880 0.10839535 0.10854797
## [49] 0.10865686 0.10872214 0.10874389 0.10872214 0.10865686 0.10854797
## [55] 0.10839535 0.10819880 0.10795809 0.10767292 0.10734294 0.10696773
## [61] 0.10654681 0.10607965 0.10556562 0.10500403 0.10439413 0.10373506
## [67] 0.10302587 0.10226552 0.10145286 0.10058663 0.09966542 0.09868770
## [73] 0.09765177 0.09655575 0.09539758 0.09417497 0.09288536 0.09152592
## [79] 0.09009351 0.08858456 0.08699511 0.08532065 0.08355607 0.08169554
## [85] 0.07973233 0.07765867 0.07546544 0.07314188 0.07067516 0.06804970
## [91] 0.06524633 0.06224103 0.05900295 0.05549136 0.05165048 0.04740036
## [97] 0.04261873 0.03710063 0.03044829 0.02163976 0.00000000
g_sleep<-data.frame(p=p,me_n=me_n)
ggplot(data = g_sleep, aes(x = p, y = me_n)) +
geom_line() +
labs(x = "Population Proportion", y = "Margin of Error")We can reject null hypothesis in favor of alternate
Answer:
There would be a 5% chance of detecting a change. A type 1 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.
Answer:
m_e <- .01
p <- .5 # for maximum value of margin of error
c <- 1.96
n <- ((c * c)*(p)*(1-p))/(m_e * m_e)
n## [1] 9604