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)
library(ggplot2)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"…
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. - mean: 67.93 - median: 64.41 - distribution of weigth is uni modal, close to a normal distribution but skews slightly right
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
qplot(yrbss$weight,
geom="histogram",
binwidth = 3,
main = "Histogram for Weight",
xlab = "weight",
fill=I("blue"),
col=I("red"),
alpha=I(.2)
)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?ggplot(yrbss, aes(x=weight, y=physical_3plus)) +
geom_boxplot() +
geom_point(alpha=0.2, color='blue') +
xlab('weight') +
ylab('work out 3 or more d/pw')The box plots show how the medians of the two distributions compare, but we can also compare the means of the distributions using the following to first group the data by the physical_3plus variable, and then calculate the mean weight in these groups using the mean function while ignoring missing values by setting the na.rm argument to TRUE.
yrbss %>%
group_by(physical_3plus) %>%
summarise(mean_weight = mean(weight, na.rm = TRUE))## # A tibble: 3 × 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().yrbss %>%
group_by(physical_3plus) %>%
summarise(n = n())## # A tibble: 3 × 2
## physical_3plus n
## <chr> <int>
## 1 no 4404
## 2 yes 8906
## 3 <NA> 273
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?null_dist %>%
mutate(diff_abs = abs(stat)) %>%
filter(obs_diff$stat < diff_abs) ## Response: weight (numeric)
## Explanatory: physical_3plus (factor)
## Null Hypothesis: independence
## # A tibble: 0 × 3
## # … with 3 variables: replicate <int>, stat <dbl>, diff_abs <dbl>
summary(null_dist)## replicate stat
## Min. : 1.0 Min. :-1.145660
## 1st Qu.: 250.8 1st Qu.:-0.222776
## Median : 500.5 Median :-0.004444
## Mean : 500.5 Mean : 0.002432
## 3rd Qu.: 750.2 3rd Qu.: 0.227438
## Max. :1000.0 Max. : 1.002531
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 × 1
## p_value
## <dbl>
## 1 0
This the standard workflow for performing hypothesis tests.
y1_df <- yrbss %>% filter(physical_3plus == "yes" ) %>% drop_na(weight)
y2_df <- yrbss %>% filter(physical_3plus == "no" ) %>% drop_na(weight)
s1 <- sd(y1_df$weight)
n1 <- count(y1_df)$n
x1 <- mean(y1_df$weight)
s2 <- sd(y2_df$weight)
n2 <- count(y2_df)$n
x2 <- mean(y2_df$weight)
df <- min(n1-1, n2-1)
SE <- sqrt(s1^2/n1 + s2^2/n2)
PE <- x1 - x2
(CI <- c(PE-1.96*SE ,PE+1.96*SE))## [1] 1.124821 2.424348
height) and interpret it in context.h_df <- yrbss %>% drop_na(height)
s <- sd(h_df$height)
n <- count(h_df)$n
x <- mean(h_df$height)
SE <- s / sqrt(n)
PE <- x
z <- round(-qnorm((1-.90)/2),2)
(CI <- c(PE-z*SE ,PE+z*SE))## [1] 1.689710 1.692772
z <- round(-qnorm((1-.90)/2),2)
(CI <- c(PE-z*SE ,PE+z*SE))## [1] 1.689710 1.692772
obs_diff <- yrbss %>%
specify(height ~ physical_3plus) %>%
calculate(stat = "diff in means", order = c("yes", "no"))
null_dist <- yrbss %>%
specify(height ~ physical_3plus) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))
null_dist %>% get_p_value(obs_stat = obs_diff, direction = "two_sided")## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
null_dist %>% get_ci(level = 0.95, point_estimate = obs_diff )## # A tibble: 1 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 -0.00390 0.00389
hours_tv_per_school_day there are.yrbss %>% select(hours_tv_per_school_day) %>% distinct() %>% summarize(n())## # A tibble: 1 × 1
## `n()`
## <int>
## 1 8
-H0 - There is no difference in weight for those who are over 1.73 meters and those who are under -H1 - There is a difference in weight for those who are over 1.73 meters and those who are under
Conditions
qplot(yrbss$weight,
geom="histogram",
binwidth = 3,
main = "Histogram for Weight",
xlab = "weight",
fill=I("blue"),
col=I("red"),
alpha=I(.2)
)yrbss <- yrbss %>%
mutate( height1.73 = ifelse(height >= 1.73, "yes", "no"))
obs_diff <- yrbss %>%
specify(weight ~ height1.73) %>%
calculate(stat = "diff in means", order = c("yes", "no"))
null_dist <- yrbss %>%
specify(weight ~ physical_3plus) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))
null_dist %>% get_p_value(obs_stat = obs_diff, direction = "two_sided")## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
null_dist %>% get_ci(level = 0.95, point_estimate = obs_diff )## # A tibble: 1 × 2
## lower_ci upper_ci
## <dbl> <dbl>
## 1 -0.625 0.683