knitr::opts_chunk$set(eval = TRUE, message = FALSE, warning = FALSE)
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:
?yrbss
Insert your answer here
Remember that you can answer this question by viewing the data in the data viewer or by using the following command:
glimpse(yrbss)
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)
Insert your answer here
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?Insert your answer here
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))
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()
.Insert your answer here
Insert your answer here
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 %>% drop_na(physical_3plus) %>% 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 %>% drop_na(physical_3plus) %>% 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
?Insert your answer here
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")
This the standard workflow for performing hypothesis tests.
height
) and interpret it in context.Insert your answer here
Insert your answer here
Insert your answer here
hours_tv_per_school_day
there are.Insert your answer here
Insert your answer here
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.