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.
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.
## # 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
In the past 30 days 4792 students reported that they texted 0 days, 925 reported texting 1-2 days, 493 reported texting 3-5 days, 311 reported texting 6-9 days, 373 reported texting 10-19 days, 298 reported texting 20-29 days, 827 reported texting 30 days, and 4646 reported that they did not drive, and there’s no data for 918 student
Insert your answer here The proportion of people who texted while driving in the last 30 and never wore a helmet is 0.0664
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")
no_helmet_proportion <- no_helmet %>%
filter(text_while_driving_30d == "30") %>%
summarise(proportion = n() / nrow(no_helmet))
no_helmet_proportion## # A tibble: 1 × 1
## proportion
## <dbl>
## 1 0.0664
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.
I wish I scrolled down and saw this before I started working on the problem
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.0646 0.0775
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.
Insert your answer here
margin_of_error <- 2 * sqrt(no_helmet_proportion * (1 - no_helmet_proportion)/nrow(yrbss))
margin_of_error## proportion
## 1 0.004271474
0.004271474 is 0.004271474
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.Insert your answer here
no_helmet <- no_helmet %>%
mutate(low_tv = ifelse(hours_tv_per_school_day <= 6, "yes", "no"))
no_helmet %>%
drop_na(low_tv) %>% # Drop missing values
specify(response = low_tv, 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.872 0.887
no_helmet <- no_helmet %>%
mutate(black = ifelse(race == "Black or African American", "yes", "no"))
no_helmet %>%
drop_na(black) %>% # Drop missing values
specify(response = black, 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.293 0.317
Confidence Interval for less than 6 hour of TV per school day is 0.871 to 0.887. The Confidence Interval for Black or African American is 0.0267211 to 0.03349415
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:
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\)).
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?Insert your answer here The graph is in the shape of a parabola. As the population proportion increase, the margin of error increasing until it hit it’s highest p value of 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.
Insert your answer here The distribution is close to the graph. The center is near 0.1. The spread is from 0.05 to 0.15. The shape is more bell curve then the pervious graph.
Insert your answer here The shape and spread remained the same but the center location would shift based on the new p. Based on the previous labs this makes sense and it what should happen
Insert your answer here The spread got wider, center remained, and the shape was similar.
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.
Insert your answer here H0: There’s no significant correlation that people who sleep 10+ hours and strength training everyday of the week H1: High school students who sleep 10+ hours per day are more likely to strength train everyday of the week
sleep_students <- yrbss %>%
filter(school_night_hours_sleep == "10+")
sleep_students %>%
mutate(exercises_7d = ifelse(physically_active_7d == 7, "yes", "no")) %>%
drop_na(exercises_7d) %>%
specify(response = exercises_7d, 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.313 0.425
## [1] 0.1
## [1] 0.05
We have A 95% confidence interval between about .319-.419, we can say that between 32-42% of high school student reported that they sleep at least 10 hours and go to the gym everyday. With this data I would accept the alternative hypothesis, and reject the null hypothesis
Insert your answer here Since the significance level is 0.05 the probability of type 1 error is 5%
Insert your answer here
Z <- 1.96 # 95% confidence
p <- 0.5 # We don't know the P so we use this
ME <- 0.01 # 1% margin of error
(Z * Z * p * (1 - p)) / (ME * ME) ### [1] 9604
9604 * * *