What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?
count<- yrbss %>%
count(text_while_driving_30d)
count
## # 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?
data('yrbss', package='openintro')
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.
## Warning: You have given `type = "bootstrap"`, but `type` is expected to be
## `"draw"`. This workflow is untested and the results may not mean what you think
## they mean.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.0647 0.0774
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?
using the sample size The margin of error is approximately 0.08%
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.
well_sleep <- yrbss %>%
mutate(well_slept = ifelse(school_night_hours_sleep > 5, "yes","no"))
well_sleep %>%
specify(response = well_slept, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 1248 rows containing missing values.
## Warning: You have given `type = "bootstrap"`, but `type` is expected to be
## `"draw"`. This workflow is untested and the results may not mean what you think
## they mean.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.768 0.784
We can assert that with 95% confidence that students that got more than 5 hours of sleep is between .769 and .783
## find the sample size: I got 12335 after subtracting all the total of nas from the sum of the results..
n <- 12335
z <- 1.96
p <- seq(from = 0, to = 1, by = 0.01)
me <- z * sqrt(p * (1 - p)/n)
me
## [1] 0.000000000 0.001755917 0.002470669 0.003010461 0.003458216 0.003846213
## [7] 0.004191081 0.004502741 0.004787683 0.005050431 0.005294290 0.005521764
## [13] 0.005734802 0.005934960 0.006123500 0.006301466 0.006469730 0.006629030
## [19] 0.006780000 0.006923183 0.007059054 0.007188027 0.007310467 0.007426698
## [25] 0.007537007 0.007641650 0.007740857 0.007834834 0.007923768 0.008007827
## [31] 0.008087162 0.008161912 0.008232201 0.008298142 0.008359839 0.008417385
## [37] 0.008470864 0.008520354 0.008565922 0.008607632 0.008645540 0.008679694
## [43] 0.008710140 0.008736916 0.008760055 0.008779587 0.008795536 0.008807920
## [49] 0.008816755 0.008822052 0.008823817 0.008822052 0.008816755 0.008807920
## [55] 0.008795536 0.008779587 0.008760055 0.008736916 0.008710140 0.008679694
## [61] 0.008645540 0.008607632 0.008565922 0.008520354 0.008470864 0.008417385
## [67] 0.008359839 0.008298142 0.008232201 0.008161912 0.008087162 0.008007827
## [73] 0.007923768 0.007834834 0.007740857 0.007641650 0.007537007 0.007426698
## [79] 0.007310467 0.007188027 0.007059054 0.006923183 0.006780000 0.006629030
## [85] 0.006469730 0.006301466 0.006123500 0.005934960 0.005734802 0.005521764
## [91] 0.005294290 0.005050431 0.004787683 0.004502741 0.004191081 0.003846213
## [97] 0.003458216 0.003010461 0.002470669 0.001755917 0.000000000
The margin of error is 0.08%
health <- yrbss %>%
mutate(healthy = ifelse(physically_active_7d>1,"yes","no"))
health %>%
specify(response = healthy, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 273 rows containing missing values.
## Warning: You have given `type = "bootstrap"`, but `type` is expected to be
## `"draw"`. This workflow is untested and the results may not mean what you think
## they mean.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.758 0.772
We are 95% confident that people who exercise more than 1 day is between 75% and 77%
h <- health %>%
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 error is 0.08%.
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?
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")
The relationship between me and p is that when me grows p also grows proportionate or vice-versa. me seems to be the greatest when p is 0.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 sampling distribution at n=300 and p=0.1 appears to look uni modal and symmetric with the center at p=0.1
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.
As I kept n constant and changed p, the shape appears to look symmetric and the center is shifting towards the left, or the p_hat seems to increase as I increase p and the spread appears to be unimodal normal.
Now also change n. How does n appear to affect the distribution of p^?
As n increases the distribution of p^ seems to be symmetic and the single peak seems more promiment with a greater spread compared to when I decreased n it seems less symmetric and there wasnt that much of a spread
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 convincing evidence that hose who sleep 10+ hours every day are more likely to strength train every day of the week Alt hypothesis. There is convincing evidence that hose who sleep 10+ hours every day are more likely to strength train everyday
## should calculate margin of error:?
well_rested <- yrbss %>%
filter(school_night_hours_sleep == "10+")
well_rested <- well_rested %>%
mutate(stron_g = ifelse(strength_training_7d == 7,"yes","no"))
well_rested %>%
specify(response = stron_g, success = "yes") %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "prop") %>%
get_ci(level = 0.95)
## Warning: Removed 4 rows containing missing values.
## Warning: You have given `type = "bootstrap"`, but `type` is expected to be
## `"draw"`. This workflow is untested and the results may not mean what you think
## they mean.
## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 0.221 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.
The definition of a type 1 error is when you reject the null hypothesis even though you didn’t need to.
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.
Since we have no idea of what to expect for p I would sample around 1000 people to be safe.