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)Every two years, the Centers for Disease Control and Prevention conduct the Youth Risk Behavior Surveillance System (YRBSS) survey, where it takes data from high schoolers (9th through 12th grade), to analyze health patterns. You will work with a selected group of variables from a random sample of observations during one of the years the YRBSS was conducted.
Load the yrbss data set into your workspace.
data('yrbss', package='openintro')There are observations on 13 different variables, some categorical and some numerical. The meaning of each variable can be found by bringing up the help file:
?yrbssRemember that you can answer this question by viewing the data in the data viewer or by using the following command:
glimpse(yrbss)## Rows: 13,583
## Columns: 13
## $ age <int> 14, 14, 15, 15, 15, 15, 15, 14, 15, 15, 15, 1~
## $ gender <chr> "female", "female", "female", "female", "fema~
## $ grade <chr> "9", "9", "9", "9", "9", "9", "9", "9", "9", ~
## $ hispanic <chr> "not", "not", "hispanic", "not", "not", "not"~
## $ race <chr> "Black or African American", "Black or Africa~
## $ height <dbl> NA, NA, 1.73, 1.60, 1.50, 1.57, 1.65, 1.88, 1~
## $ weight <dbl> NA, NA, 84.37, 55.79, 46.72, 67.13, 131.54, 7~
## $ helmet_12m <chr> "never", "never", "never", "never", "did not ~
## $ text_while_driving_30d <chr> "0", NA, "30", "0", "did not drive", "did not~
## $ physically_active_7d <int> 4, 2, 7, 0, 2, 1, 4, 4, 5, 0, 0, 0, 4, 7, 7, ~
## $ hours_tv_per_school_day <chr> "5+", "5+", "5+", "2", "3", "5+", "5+", "5+",~
## $ strength_training_7d <int> 0, 0, 0, 0, 1, 0, 2, 0, 3, 0, 3, 0, 0, 7, 7, ~
## $ school_night_hours_sleep <chr> "8", "6", "<5", "6", "9", "8", "9", "6", "<5"~
They are 13,583 cases and case represents a high schooler (9th through 12th grade)
You will first start with analyzing the weight of the participants in kilograms: weight.
Using visualization and summary statistics, describe the distribution of weights. The summary function can be useful.
summary(yrbss$weight)## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 29.94 56.25 64.41 67.91 76.20 180.99 1004
1004 Observations are missing from Weights
Next, consider the possible relationship between a high schooler’s weight and their physical activity. Plotting the data is a useful first step because it helps us quickly visualize trends, identify strong associations, and develop research questions.
First, let’s create a new variable physical_3plus, which will be coded as either “yes” if they are physically active for at least 3 days a week, and “no” if not.
yrbss <- yrbss %>%
mutate(physical_3plus = ifelse(yrbss$physically_active_7d > 2, "yes", "no"))physical_3plus and weight. Is there a relationship between these two variables? What did you expect and why?# Create a subset for physical_3plus and weight
yrbss_phys_weight <- yrbss %>%
filter(physical_3plus == "yes", weight != "NA")
yrbss_no_phys_weight <- yrbss %>%
filter(physical_3plus == "no", weight != "NA")
boxplot(yrbss_phys_weight$weight, yrbss_no_phys_weight$weight,
names = c("Weight for phys. active", "Weight for no phys. active"))As the box plots show, there is not much of the difference between the median of the two variables. My expectation was to see youth physically active to have a low average weight compared to no active ones but no a very significant difference because there are many factors that contribute in someone’s weight.
yrbss %>%
group_by(physical_3plus) %>%
summarise(mean_weight = mean(weight, na.rm = TRUE))## # A tibble: 3 x 2
## physical_3plus mean_weight
## <chr> <dbl>
## 1 no 66.7
## 2 yes 68.4
## 3 <NA> 69.9
There is an observed difference, but is this difference statistically significant? In order to answer this question we will conduct a hypothesis test.
summarize command above by defining a new variable with the definition n().Yes. The condition for inference is satisfied because the experiment met the neccessary condition below:
Independence: The samples observations are high schoolers and they are all independent of each others.
Random samples: The data came from a random sample or randomized experiment.
Approximately normal: The sampling distribution of \(\hat{p}\) needs to be approximately normal — needs at least 10 expected successes and 10 expected failures.
yrbss %>%
group_by(physical_3plus) %>%
summarise(n_weight = n())## # A tibble: 3 x 2
## physical_3plus n_weight
## <chr> <int>
## 1 no 4404
## 2 yes 8906
## 3 <NA> 273
\(H_{0}:\) The average weights for those who exercise at least times a week is the same to those who don’t.
\(H_{A}:\) The average weights for those who exercise at least times a week differ to those who don’t.
Next, we will introduce a new function, hypothesize, that falls into the infer workflow. You will use this method for conducting hypothesis tests.
But first, we need to initialize the test, which we will save as obs_diff.
obs_diff <- yrbss %>%
specify(weight ~ physical_3plus) %>%
calculate(stat = "diff in means", order = c("yes", "no"))Notice how you can use the functions specify and calculate again like you did for calculating confidence intervals. Here, though, the statistic you are searching for is the difference in means, with the order being yes - no != 0.
After you have initialized the test, you need to simulate the test on the null distribution, which we will save as null.
null_dist <- yrbss %>%
specify(weight ~ physical_3plus) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))Here, hypothesize is used to set the null hypothesis as a test for independence. In one sample cases, the null argument can be set to “point” to test a hypothesis relative to a point estimate.
Also, note that the type argument within generate is set to permute, whichis the argument when generating a null distribution for a hypothesis test.
We can visualize this null distribution with the following code:
ggplot(data = null_dist, aes(x = stat)) +
geom_histogram()null permutations have a difference of at least obs_stat?count_null <- null_dist %>%
filter(stat >= obs_diff)
count_null## Response: weight (numeric)
## Explanatory: physical_3plus (factor)
## Null Hypothesis: independence
## # A tibble: 0 x 2
## # ... with 2 variables: replicate <int>, stat <dbl>
None of these null permutations have a difference of at least obs_stat
Now that the test is initialized and the null distribution formed, you can calculate the p-value for your hypothesis test using the function get_p_value.
null_dist %>%
get_p_value(obs_stat = obs_diff, direction = "two_sided")## # A tibble: 1 x 1
## p_value
## <dbl>
## 1 0
This the standard workflow for performing hypothesis tests.
# Find a point estimate
point_estimate <- yrbss %>%
specify(weight ~ physical_3plus) %>%
calculate(stat = "diff in means" , order = c("yes", "no")) # Generate null distribution
ci_null_dist <- yrbss %>%
specify(weight ~ physical_3plus) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))# Confidence interval
ci_null_dist %>%
get_confidence_interval(point_estimate = point_estimate,
level = 0.95,type = "se")## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 1.13 2.42
We are 95% confident that the difference between the weights of those who exercise at least three times a week and those who don’t falls in (1.13, 2.41)
height) and interpret it in context.Avg_height <- yrbss %>%
specify(response = height) %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "mean") %>%
get_ci(level = 0.95)
Avg_height## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 1.69 1.69
We are 95% confident that the average heights in meter falls within (1.69, 1.693)
Avg_height2 <- yrbss %>%
specify(response = height) %>%
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "mean") %>%
get_ci(level = 0.90)
Avg_height2## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 1.69 1.69
90% confident that the average heights in meter falls in (1.690, 1.693)
The width of the interval when confident level was reduced to 90% compared with the former of 95% confident level is almost negligible. There was an insignificant slight different.
\(H_{0}:\) The average height for those who exercise at least times a week is the same to those who don’t.
\(H_{A}:\) The average height for those who exercise at least times a week differ to those who don’t.
# Find the point estimate
point_estimate2 <- yrbss %>%
specify(height ~ physical_3plus) %>%
calculate(stat = "diff in means" , order = c("yes", "no")) # Generate null distribution
null_dist2 <- yrbss %>%
specify(height ~ physical_3plus) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))null_dist2 %>%
get_p_value(obs_stat = point_estimate2,
direction = "two-sided")## # A tibble: 1 x 1
## p_value
## <dbl>
## 1 0
null_dist2 %>% visualise()null_dist2 %>%
get_p_value(obs_stat = point_estimate2,
direction = "two-sided")## # A tibble: 1 x 1
## p_value
## <dbl>
## 1 0
hours_tv_per_school_day there are.yrbss %>%
group_by(hours_tv_per_school_day) %>%
summarise(n = n())## # A tibble: 8 x 2
## hours_tv_per_school_day n
## <chr> <int>
## 1 <1 2168
## 2 1 1750
## 3 2 2705
## 4 3 2139
## 5 4 1048
## 6 5+ 1595
## 7 do not watch 1840
## 8 <NA> 338
There are 7 options excluding “NA”
Assumptions
Independence: The samples observations are high schoolers and they are all independent of each others.
Random samples: The data came from a random sample or randomized experiment.
Approximately normal: The sampling distribution of \(\hat{p}\) needs to be approximately normal — needs at least 10 expected successes and 10 expected failures.
Hypotheses
\(H_{0}:\) The average height for those who sleep 10+ hours is the same to the rest of the high schoolers.
\(H_{A}:\) The average height for those who sleep 10+ hours differ to the rest of the high schoolers.
Test
\(α=0.05\)
# Create a new var
yrbss <- yrbss %>%
mutate(sleeper = ifelse(yrbss$school_night_hours_sleep == "10+", "yes", "no"))# Check the mean for both cases
yrbss %>%
group_by(sleeper) %>%
summarise(mean_height = mean(height, na.rm = TRUE))## # A tibble: 3 x 2
## sleeper mean_height
## <chr> <dbl>
## 1 no 1.69
## 2 yes 1.68
## 3 <NA> 1.70
# Find a point estimate
point_estimate3 <- yrbss %>%
specify(height ~ sleeper) %>%
calculate(stat = "diff in means" , order = c("yes", "no")) # Generate null distribution
set.seed(0809)
null_dist <- yrbss %>%
specify(height ~ sleeper) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))# p_value
null_dist %>%
get_p_value(obs_stat = point_estimate3,
direction = "two-sided")## # A tibble: 1 x 1
## p_value
## <dbl>
## 1 0.066
# Confidence interval
null_dist %>%
get_confidence_interval(point_estimate = point_estimate,
level = 0.95,
type = "se")## # A tibble: 1 x 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 1.76 1.79
Looking at the confidence interval, We are 95% confident that the diffrence of the average height for those who sleep 10+ hours and the rest of the high schoolers falls in (-0.0239, 0.000725).
From the hypthesis test, we can see that the p_value is greater than 0.05. Hence, we fail to reject the null hypothesis