library(tidyverse)
library(openintro)
library(infer)
set.seed(9999)
What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?
data("yrbss")
Counts <-yrbss%>%
count(text_while_driving_30d)
Counts
## # 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
head(yrbss)
## # A tibble: 6 × 13
## age gender grade hispanic race height weight helmet_12m
## <int> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 14 female 9 not Black or African American NA NA never
## 2 14 female 9 not Black or African American NA NA never
## 3 15 female 9 hispanic Native Hawaiian or Other… 1.73 84.4 never
## 4 15 female 9 not Black or African American 1.6 55.8 never
## 5 15 female 9 not Black or African American 1.5 46.7 did not r…
## 6 15 female 9 not Black or African American 1.57 67.1 did not r…
## # ℹ 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>
What is the proportion of people who have “text_while_driving_30d” and helmet_12m (never) ?
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"))
# Calculate the proportion
no_helmet %>%filter(text_while_driving_30d =="30" & helmet_12m =="never") %>%nrow()/nrow(no_helmet)
## [1] 0.0663609
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?
prop <-yrbss %>%filter(text_while_driving_30d =="30" & yrbss$helmet_12m =="never") %>%nrow()/nrow(yrbss)
Error_Margin <-1.96*sqrt(prop*(1-prop)/nrow(yrbss))
Error_Margin
## [1] 0.003051546
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.
n1 <-yrbss %>%filter(physically_active_7d > 3 & strength_training_7d >3) %>%nrow()
n = nrow(yrbss)
p <- n1/n
se <- sqrt(p * (1 - p) / nrow(yrbss))
z_score <- qnorm((1 + 0.95) / 2)
me <- z_score * se
lower <- p - me
upper <- p + me
# Print the confidence interval
cat("Confidence Interval: (", lower, ", ", upper, ")\n")
## Confidence Interval: ( 0.3129189 , 0.3286183 )
The confidence interval for the proportion of the population who do physical activity and strength training more than 3 times a week (success) is 0.3129189 and 0.3286183 that the true population proportion of individuals that work-out three times per week is between 31.3% and 32.9%.
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 margin of error is maximum when the population proportion is around 0.5 at a specific sample size. As the population proportion moves away from 0.5 in either direction, the margin of error decreases, that shows estimate becomes more precise and reliable.
Describe the sampling distribution of sample proportions at n = 300 and p = 0.1. Be sure to note the center, spread, and shape.
n <- 300 # Sample size
p <- 0.1 # Population proportion
reps <- 1000
# Simulate sampling distribution
sample_proportions <- replicate(reps, rbinom(1,n,p))
# Mean of the sampling distribution
center <- mean(sample_proportions)
cat("Center (Mean):", center, "\n")
## Center (Mean): 30.018
# Spread (standard deviation) of the sampling distribution
spread <- sd(sample_proportions)
cat("Spread (Standard Deviation):", spread, "\n")
## Spread (Standard Deviation): 5.219283
# PLot a histogram to visualize the shape of the sampling distribution
hist(sample_proportions, breaks = 25, main = "Sampling Distribution of Sample Proportions",
xlab = "Sample Proportions", col = "grey")
cat(" Shape is approximately normal (central limit theorem applies for n = 300)\n")
## Shape is approximately normal (central limit theorem applies for n = 300)
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.
n <- 300 # Sample size
p <- 0.01 # Population proportion
reps <- 1000
# Simulate sampling distribution
sample_proportions <- replicate(reps, rbinom(1,n,p))
# Mean of the sampling distribution
center <- mean(sample_proportions)
cat("Center (Mean):", center, "\n")
## Center (Mean): 3.042
# Spread (standard deviation) of the sampling distribution
spread <- sd(sample_proportions)
cat("Spread (Standard Deviation):", spread, "\n")
## Spread (Standard Deviation): 1.708554
# PLot a histogram to visualize the shape of the sampling distribution
hist(sample_proportions, breaks = 25, main = "Sampling Distribution of Sample Proportions",
xlab = "Sample Proportions", col = "grey")
cat(" Shape looks more right skewed for new p value is 10 times less than p)\n")
## Shape looks more right skewed for new p value is 10 times less than p)
Now also change n. How does n appear to affect the distribution of p̂?
n <- 600 # Sample size
p <- 0.01 # Population proportion
reps <- 1000
# Simulate sampling distribution
sample_proportions <- replicate(reps, rbinom(1,n,p))
# PLot a histogram to visualize the shape of the sampling distribution
hist(sample_proportions, breaks = 25, main = "Sampling Distribution of Sample Proportions",
xlab = "Sample Proportions", col = "grey")
cat(" Shape looks getting closer to normalized for p̂ = p and n=600 )\n")
## Shape looks getting closer to normalized for p̂ = p and n=600 )
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.
n1 <-yrbss %>%filter(school_night_hours_sleep >= 10 & strength_training_7d > 6) %>%nrow()
n = nrow(yrbss)
p <- n1/n
se <- sqrt(p * (1 - p) / nrow(yrbss))
z_score <- qnorm((1 + 0.95) / 2)
me <- z_score * se
lower <- p - me
upper <- p + me
# Print the confidence interval
cat("Confidence Interval is : (", lower, ", ", upper, ")\n")
## Confidence Interval is : ( 0.1331795 , 0.144815 )
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.
In hypothesis testing, a Type I error occurs when the null hypothesis H0 is incorrectly rejected that there is no actual difference in the significance level (α = 0.05) of the threshold for this error. So, the probability of detecting Type I error is 0.05 or 5%.
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 <- 0.01
p <- 0.5
z_score <- qnorm((1 + 0.95) / 2)
sample_size <- (z_score^2 * p * (1 - p)) / (ME^2)
# Print sample size that round up to whole number
cat("Sample Size of people is:", ceiling(sample_size))
## Sample Size of people is: 9604