library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts ---------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(infer)
data("yrbss")
What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?
4792 stated 0 days of texting while driving. Days 1-2 925 were reported, day 3 to 5 493 were reported, day 6 to 9 311 were reported, day 10-19 373 were reported, day 20-29 298 were reported.
count_each <-yrbss%>%
count(text_while_driving_30d)
count_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
What is the proportion of people who have texted while driving every day in the past 30 days and never wear helmets?
6040 reported testing while driving and never wore helmets in the past 30 days. 463 were helmets while testing and driving in the past 30 days.
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 x 2
## text_ind n
## <chr> <int>
## 1 no 6040
## 2 yes 463
## 3 <NA> 474
no_helmet %>%
specify(response = text_ind, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 474 rows containing missing values.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0650 0.0775
What is the margin of error for the estimate of the proportion of non-helmet wearers that have texted while driving each day for the past 30 days based on this survey?
ME = 0.004
n = 6977
z = 1.96
p <- seq(from = 0, to = 1, by = 0.01)
me <- 2 * sqrt(p * (1 - p)/n)
me = 0.004
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.
good_sleep <- yrbss%>%
mutate(slept_well = ifelse(school_night_hours_sleep > 5, "yes", "no"))
good_sleep%>%
specify(response = slept_well, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 1248 rows containing missing values.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.769 0.783
We are 95% confident that the proportion of students who slept well and didn’t sleep well is between 0.768 and 0.783.
no_tv <- yrbss %>%
mutate(did_not_watch_tv = ifelse(hours_tv_per_school_day == "do not watch", "yes", "no") )
no_tv %>%
specify(response = did_not_watch_tv, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 338 rows containing missing values.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.133 0.145
We are 95% confident that the proportion of students who did and did not watch tv is between 0.133 and 0.144.
n <- 1000
p <- seq(from = 0, to = 1, by = 0.01)
me <- 2 * sqrt(p * (1 - p)/n)
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")
Describe the relationship between 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?
The margin of error seems to increase as the population proportion increases. Once it reaches 50% it begins to decrease.
Describe the sampling distribution of sample proportions at n=300 and p=0.1. Be sure to note the center, spread, and shape
The distribution seems to be normal. The center is at 0.1 and the spread is between 0.04 and 0.17
Keep n constant and change p. How does the shape, center, and spread of the sampling distribution vary as p changes. You might want to adjust min and max for the x-axis for a better view of the distribution.
The distribution seems to be normal. The center is at 0.1 and the spread is between 0.05 and 0.16
Now also change n. How does n appear to affect the distribution of p-hat?
When n decrease the spread increase but increasing n makes the spread decrease.
Is there convincing evidence that those who sleep 10+ hours per day are more likely to strength train every day of the week? As always, write out the hypotheses for any tests you conduct and outline the status of the conditions for inference. If you find a significant difference, also quantify this difference with a confidence interval
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.
good_sleep <- yrbss %>%
filter(school_night_hours_sleep == "10+")
good_sleep <- good_sleep %>%
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)
## Warning: Removed 4 rows containing missing values.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.218 0.321
Let’s say there has been no difference in likeliness to strength train every day of the week for those who sleep 10+ hours. What is the probability that you could detect a change (at a significance level of 0.05) simply by chance? Hint: Review the definition of the Type 1 error.
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.
Suppose you’re hired by the local government to estimate the proportion of residents that attend a religious service on a weekly basis. According to the guidelines, the estimate must have a margin of error no greater than 1% with 95% confidence. You have no idea what to expect for p. How many people would you have to sample to ensure that you are within the guidelines? Hint: Refer to your plot of the relationship between p and margin of error. This question does not require using a dataset.
ME = 1.96 × SE=1.96 × sqrtp(1−p)/n
n = (0.3)2/(0.01/1.96)2
n= 3457