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.
# unique(yrbss$text_while_driving_30d )
count <- yrbss %>%
count(text_while_driving_30d == "0")
print (count)
## # A tibble: 3 × 2
## `text_while_driving_30d == "0"` n
## <lgl> <int>
## 1 FALSE 7873
## 2 TRUE 4792
## 3 NA 918
Insert your answer here
There are 4792 occurences within the category for the amount of days these students have texted while driving within the past 30 days
unique(yrbss$text_while_driving_30d)
## [1] "0" NA "30" "did not drive"
## [5] "1-2" "3-5" "20-29" "10-19"
## [9] "6-9"
data('yrbss', package='openintro')
no_helmet <- yrbss %>%
filter(helmet_12m == "never")
summary(no_helmet)
## age gender grade hispanic
## Min. :12.00 Length:6977 Length:6977 Length:6977
## 1st Qu.:15.00 Class :character Class :character Class :character
## Median :16.00 Mode :character Mode :character Mode :character
## Mean :16.07
## 3rd Qu.:17.00
## Max. :18.00
## NA's :16
## race height weight helmet_12m
## Length:6977 Min. :1.27 Min. : 31.75 Length:6977
## Class :character 1st Qu.:1.63 1st Qu.: 56.70 Class :character
## Mode :character Median :1.70 Median : 65.77 Mode :character
## Mean :1.70 Mean : 68.21
## 3rd Qu.:1.78 3rd Qu.: 76.66
## Max. :2.11 Max. :180.99
## NA's :422 NA's :422
## text_while_driving_30d physically_active_7d hours_tv_per_school_day
## Length:6977 Min. :0.000 Length:6977
## Class :character 1st Qu.:2.000 Class :character
## Mode :character Median :4.000 Mode :character
## Mean :4.127
## 3rd Qu.:7.000
## Max. :7.000
## NA's :147
## strength_training_7d school_night_hours_sleep
## Min. :0.000 Length:6977
## 1st Qu.:0.000 Class :character
## Median :3.000 Mode :character
## Mean :3.156
## 3rd Qu.:5.000
## Max. :7.000
## NA's :622
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
Insert your answer here
The proportion of people who have texted while driving every day in the past 30 days and never wear helmets is 0.0712 or % 7.12
Remember that you can use filter to limit the dataset to
just non-helmet wearers. Here, we will name the dataset
no_helmet.
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.0650 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 estimate is 0.016
n <- 1000
p <- .0712
me <- 2 * sqrt(p * (1 - p)/n)
print(me)
## [1] 0.01626414
Insert your answer here
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
Finding those who always wear helmet while driving 30 days
no_helmet2 <- yrbss %>%
filter(helmet_12m == "always")
no_helmet2
## # A tibble: 399 × 13
## age gender grade hispanic race height weight helmet_12m
## <int> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 18 male 12 not Black or African Americ… 1.8 107. always
## 2 18 male 11 not Black or African Americ… NA NA always
## 3 18 male 12 not White 1.78 100. always
## 4 16 female 11 not Black or African Americ… 1.55 72.6 always
## 5 18 female 12 not White 1.65 69.8 always
## 6 18 female 12 not White 1.63 72.6 always
## 7 18 male 12 not White 1.75 57.2 always
## 8 15 male 9 not White 1.8 68.0 always
## 9 15 male 9 not White 1.88 74.8 always
## 10 15 male 9 not White 1.65 57.6 always
## # ℹ 389 more rows
## # ℹ 5 more variables: text_while_driving_30d <chr>, physically_active_7d <int>,
## # hours_tv_per_school_day <chr>, strength_training_7d <int>,
## # school_night_hours_sleep <chr>
no_helmet2 <- no_helmet2 %>%
mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no"))
no_helmet2
## # A tibble: 399 × 14
## age gender grade hispanic race height weight helmet_12m
## <int> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 18 male 12 not Black or African Americ… 1.8 107. always
## 2 18 male 11 not Black or African Americ… NA NA always
## 3 18 male 12 not White 1.78 100. always
## 4 16 female 11 not Black or African Americ… 1.55 72.6 always
## 5 18 female 12 not White 1.65 69.8 always
## 6 18 female 12 not White 1.63 72.6 always
## 7 18 male 12 not White 1.75 57.2 always
## 8 15 male 9 not White 1.8 68.0 always
## 9 15 male 9 not White 1.88 74.8 always
## 10 15 male 9 not White 1.65 57.6 always
## # ℹ 389 more rows
## # ℹ 6 more variables: text_while_driving_30d <chr>, physically_active_7d <int>,
## # hours_tv_per_school_day <chr>, strength_training_7d <int>,
## # school_night_hours_sleep <chr>, text_ind <chr>
no_helmet2 %>%
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.0158 0.0554
Based on the data I am 95% confident that the true population lies within the range of 0.0185 and 0.0554
Finding those who did not ride while driving 30 days
no_helmet3<- yrbss %>%
filter(helmet_12m == "did not ride")
no_helmet3 <- no_helmet3 %>%
mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no"))
no_helmet3 %>%
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.0625 0.0779
Based on the data I am 95% confident that the true population lies within the range of 0.0620 and 0.0767
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?Insert your answer here
The value of p and the margin of error maximized at a 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 shape of distribution seems to be normally distributed, the spread looks approximately even and the center has the max proportion of the margin of error.
Insert your answer here As the p value changes while the sample size remains constant, the center of the the distribution seems to be at the exact location of the p value. The spread still seems to be somewhat uniform but it seems to be the closest to a normal distribution shape when it gets to 0.5.
Insert your answer here
The distribution also seems to get closer and closer to resembling a normal distribution as the sample size increases while the proportions remain constant
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
Hypothesis - I think that those that sleep 10+ hours a day are more likely to strength train everyday of the week
Testing my hypothesis below:
sleep_data <- yrbss %>%
filter(school_night_hours_sleep == "10+") %>%
mutate(text_ind = ifelse(strength_training_7d == "7", "yes", "no"))
sleep_data %>%
count(sleep_data$text_ind)
## # A tibble: 3 × 2
## `sleep_data$text_ind` n
## <chr> <int>
## 1 no 228
## 2 yes 84
## 3 <NA> 4
sleep_p_hat <- (84/(228+84))
sleep_p_hat
## [1] 0.2692308
The proportion of people who sleep and train 7 days a week is 0.269. Now checking the confidence interval.
sleep_data %>%
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.218 0.324
The range is between 0.224 - 0.324
Insert your answer here
The probability of incorrectly detecting a significant difference at a significance level of 0.05, is 5%.
Insert your answer here
In order to get a margin of error no greater than 1% we can use the formula to find out
ME = 1.96 SE ME = 1.96 * sqrt(p * (1 - p) / n)
Finding n by rearranging the equation be get
n = (1.96^2 * p * (1 - p)) / ME^2
Now since we want p we can use the P which has the highest margin of error which is 0.5 to get a conservative number
n = (1.96^2 * 0.5 * (1 - 0.5)) / (0.01^2) n = (3.8416 * 0.25) / 0.0001 n = 96,041
We would need 96,041 residents surveyed to ensure that the margin of error is no greater than 1% with a 95% confidence interval * * *