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.
data('yrbss', package='openintro')
no_helmet <- yrbss %>%
filter(helmet_12m == "never")
no_helmet <- yrbss %>%
filter(helmet_12m == "never")
no_helmet <- no_helmet %>%
mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no"))
no_helmet %>%
count(text_ind)
## # A tibble: 3 × 2
## text_ind n
## <chr> <int>
## 1 no 6040
## 2 yes 463
## 3 <NA> 474
# get the proportion
na.omit(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.0691 0.0840
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 %>%
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 %>%
drop_na(text_ind) %>% # Drop missing values
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.0777
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.
The margin of error for the estinate of the proportion of non-helmet is approximately 0.0126%
n <- 6040
z <- 1.96
p <- seq(from = 0, to = 1, by = 0.01)
me <- 2 * sqrt(p * (1 - p)/n)
me
## [1] 0.000000000 0.002560526 0.003602795 0.004389934 0.005042863 0.005608650
## [7] 0.006111546 0.006566017 0.006981527 0.007364673 0.007720275 0.008051983
## [13] 0.008362642 0.008654517 0.008929451 0.009188965 0.009434332 0.009666629
## [19] 0.009886776 0.010095570 0.010293700 0.010481772 0.010660318 0.010829809
## [25] 0.010990664 0.011143257 0.011287924 0.011424964 0.011554650 0.011677226
## [31] 0.011792915 0.011901917 0.012004414 0.012100572 0.012190540 0.012274455
## [37] 0.012352440 0.012424607 0.012491056 0.012551879 0.012607157 0.012656962
## [43] 0.012701358 0.012740403 0.012774146 0.012802628 0.012825884 0.012843944
## [49] 0.012856827 0.012864552 0.012867125 0.012864552 0.012856827 0.012843944
## [55] 0.012825884 0.012802628 0.012774146 0.012740403 0.012701358 0.012656962
## [61] 0.012607157 0.012551879 0.012491056 0.012424607 0.012352440 0.012274455
## [67] 0.012190540 0.012100572 0.012004414 0.011901917 0.011792915 0.011677226
## [73] 0.011554650 0.011424964 0.011287924 0.011143257 0.010990664 0.010829809
## [79] 0.010660318 0.010481772 0.010293700 0.010095570 0.009886776 0.009666629
## [85] 0.009434332 0.009188965 0.008929451 0.008654517 0.008362642 0.008051983
## [91] 0.007720275 0.007364673 0.006981527 0.006566017 0.006111546 0.005608650
## [97] 0.005042863 0.004389934 0.003602795 0.002560526 0.000000000
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.
we use 95% of confidence interval so between 77.6% and 79.4 % student can get more than 5 hours of sleep
well_sleep <- yrbss %>%
mutate(well_slept = ifelse(school_night_hours_sleep > 5, "yes","no"))
na.omit(well_sleep) %>%
specify(response = well_slept, 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.777 0.794
The margin of error between 0.777 and 0.794 is 0.017
health_yrbss <- yrbss %>%
mutate(healthy = ifelse(physically_active_7d>1,"yes","no"))
na.omit(health_yrbss) %>%
specify(response = healthy, 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.766 0.783
h <- health_yrbss %>%
filter(!is.na(healthy)) %>%
count(healthy)
size <- 13310
z <- 1.96
p <- seq(from = 0, to = 1, by = 0.01)
me <- z * sqrt(p * (1 - p)/size)
me
## [1] 0.000000000 0.001690381 0.002378456 0.002898101 0.003329145 0.003702660
## [7] 0.004034657 0.004334685 0.004608992 0.004861933 0.005096691 0.005315675
## [13] 0.005520762 0.005713449 0.005894952 0.006066276 0.006228259 0.006381614
## [19] 0.006526949 0.006664788 0.006795588 0.006919747 0.007037618 0.007149511
## [25] 0.007255702 0.007356440 0.007451944 0.007542414 0.007628029 0.007708950
## [31] 0.007785324 0.007857284 0.007924949 0.007988430 0.008047824 0.008103222
## [37] 0.008154705 0.008202348 0.008246216 0.008286369 0.008322861 0.008355741
## [43] 0.008385051 0.008410827 0.008433103 0.008451906 0.008467259 0.008479181
## [49] 0.008487687 0.008492786 0.008494485 0.008492786 0.008487687 0.008479181
## [55] 0.008467259 0.008451906 0.008433103 0.008410827 0.008385051 0.008355741
## [61] 0.008322861 0.008286369 0.008246216 0.008202348 0.008154705 0.008103222
## [67] 0.008047824 0.007988430 0.007924949 0.007857284 0.007785324 0.007708950
## [73] 0.007628029 0.007542414 0.007451944 0.007356440 0.007255702 0.007149511
## [79] 0.007037618 0.006919747 0.006795588 0.006664788 0.006526949 0.006381614
## [85] 0.006228259 0.006066276 0.005894952 0.005713449 0.005520762 0.005315675
## [91] 0.005096691 0.004861933 0.004608992 0.004334685 0.004034657 0.003702660
## [97] 0.003329145 0.002898101 0.002378456 0.001690381 0.000000000
The margin of error is between between 0.765 and 0.783 is 0.018 ## How does the proportion affect the 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 <- 1000
The 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?When me grows p also grows proportionate untill it reach maximum value when p is 0.5 then begin to decrease. The margin of error is maximized when p = 0.5.
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 at n=300 and p=0.1 appears to look uni modal and symmetric with the center at p=0.1
as continues to moves the peak will continue to move slightly to the left
Changing n with p = 0.01 means the peak will stay symmetric . as you increase p, the peak will slightly move the left.
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.
sleep_well <- yrbss %>%
filter(school_night_hours_sleep == "10+")
sleep_well <- sleep_well %>%
mutate(stron_g = ifelse(strength_training_7d == 7,"yes","no"))
na.omit(sleep_well) %>%
specify(response = stron_g, 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.190 0.316
the definition of type I error suggest below: If the test results suggest that the data do not provide convincing evidence for the alternative hypothesis (H_a), we stick with the null hypothesis (H_o). If they do, then we reject the null hypothesis in favor of the alternative. Type I Error suggest rejecting the null hypothesis when it is true. and Type II Error rejecting the null hypothesis when it is true.
I would suggest 9604 for p = 0.05 margin of error and z = 1.96 and me = 0.01
p <- 0.5 # Use p = 0.5
me <- 0.01
z <- 1.96 # The Z-Score for a 95% confidence interval
se <- sqrt((p*(1-p))/n)
n <- ((z^2)*(p*(1-p)))/(me^2)
n
## [1] 9604