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)
What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?
table(yrbss$text_while_driving_30d)
##
## 0 1-2 10-19 20-29 3-5
## 4792 925 373 298 493
## 30 6-9 did not drive
## 827 311 4646
0 = 4792 1-2 = 925 3-5 = 493 6-9 = 311 10-19 = 373 20-29 = 298 30 = 827 did not drive = 4646
What is the proportion of people who have texted while driving every day in the past 30 days and never wear helmets?
table(yrbss$helmet_12m)
##
## always did not ride most of time never rarely sometimes
## 399 4549 293 6977 713 341
827/6977
## [1] 0.1185323
p-hat = 0.119
no_helmet <- yrbss %>%
filter(helmet_12m == "never")
no_helmet <- no_helmet %>%
mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no"))
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.0647 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 = .006
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.
no_helmet2 <- yrbss %>%
filter(helmet_12m == "always")
no_helmet2 <- no_helmet2 %>%
mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no"))
no_helmet2 %>%
specify(response = text_ind, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 20 rows containing missing values.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0185 0.0528
We are 95% confident that the true proportion of those who always wear a helmet who text and drive is between .018 and .053.
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 %>%
specify(response = text_ind, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 324 rows containing missing values.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0618 0.0776
We are 95% confident that the true proportion of those who did not ride who text and drive is between .062 and .077.
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")
### Exercise 5
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 increases as the population proportion approaches .50 from both the left and the right. The margin of error is maximized when the population proportion is .5
Describe the sampling distribution of sample proportions at n=300 and p=0.1. Be sure to note the center, spread, and shape.
The center is .1 and the spread is .017. The shape is mostly normal
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 center is .5 and the spread is .029. The shape is mostly normal
Now also change n. How does n appear to affect the distribution of p^?
the center is .5, the spread s .05 and the shape is mostly normal
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.
table(yrbss$school_night_hours_sleep)
##
## <5 10+ 5 6 7 8 9
## 965 316 1480 2658 3461 2692 763
hrsleep <- yrbss %>%
filter(school_night_hours_sleep == "10+")
hrsleep <- hrsleep %>%
mutate(strength = ifelse(strength_training_7d == "7", "yes", "no"))
hrsleep %>%
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.221 0.321
Ho: There is no difference in strength training by people who sleep 10 hours on a school day and those who don’t
Ha: Those who sleep 10+ hours per day are more likely to strength train every day of the week
We are 95% confident that the true proportion of those who sleep 10+ hours a day who strength train every day of the week is between .221 and .314
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.
A type one error is rejecting the null when it is actually true.
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.
You would need a n= 1000