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.
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.
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:
Theh cases are essentialy observations. There are 13583 cases in this datatset
Remember that you can answer this question by viewing the data in the data viewer or by using the following command:
## 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.
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 29.94 56.25 64.41 67.91 76.20 180.99 1004
1004
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.
physical_3plus and
weight. Is there a relationship between these two
variables? What did you expect and why?physical_3plus_weight <- yrbss %>% drop_na()
physical_3plus_weight %>% ggplot(aes(x = physical_3plus, y = weight)) + geom_boxplot()The boxplots are showing that the cases with 3plus days of physical activity are actually slightly heavier than those with less than 3. Which goes against what i thought the case would be. I expected physical activity and weight to be negatively correlated.
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.
## # 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().The necessary conditions are met. The data is independent and the smaple size is normal
yrbss %>%
group_by(physical_3plus) %>%
summarise(mean_weight = mean(weight, na.rm = TRUE), count=n())## # A tibble: 3 x 3
## physical_3plus mean_weight count
## <chr> <dbl> <int>
## 1 no 66.7 4404
## 2 yes 68.4 8906
## 3 <NA> 69.9 273
**H_null = There’s no difference between The average weights for those who exercise at least 3 times per week and Those who don’t.
H_alt = There is a difference between The average weights for those who exercise at least 3 times per week and those who don’t. Students who exercise at least 3 times per week have a different average weight than 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 %>%
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:
null permutations have a difference
of at least obs_stat?Insert your answer here
print(paste0('The total null permutations that have a difference of at least obs_stat is: ', nrow(null_dist %>% filter(stat >= obs_diff)),' cases.'))## [1] "The total null permutations that have a difference of at least obs_stat is: 0 cases."
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.
## # A tibble: 1 x 1
## p_value
## <dbl>
## 1 0
This the standard workflow for performing hypothesis tests.
yrbss %>%
group_by(physical_3plus) %>%
summarise(mean_weight = mean(weight, na.rm = TRUE), sd_weight = sd(weight, na.rm = TRUE), count = n())## # A tibble: 3 x 4
## physical_3plus mean_weight sd_weight count
## <chr> <dbl> <dbl> <int>
## 1 no 66.7 17.6 4404
## 2 yes 68.4 16.5 8906
## 3 <NA> 69.9 17.6 273
c_weight <- yrbss %>%
group_by(physical_3plus) %>%
summarise(mean_weight = mean(weight, na.rm = TRUE), sd_weight = sd(weight, na.rm = TRUE), count = n())t <- 1.96
#Confidence Interval for those who exercise at least three times a week
uci_exercise3plus <- c_weight$mean_weight[2] + t*(c_weight$sd_weight[2]/sqrt(c_weight$count[2]))
lci_exercise3plus <- c_weight$mean_weight[2] - t*(c_weight$sd_weight[2]/sqrt(c_weight$count[2]))
#Confidence Interval for who do not exercise at least three times a week
uci_noexercise <- c_weight$mean_weight[1] + t*(c_weight$sd_weight[1]/sqrt(c_weight$count[1]))
lci_noexercise <- c_weight$mean_weight[1] - t*(c_weight$sd_weight[1]/sqrt(c_weight$count[1]))the 95% confidence interval of students who exercise 3 times a week is between 68.1062 and 68.7907. The interval for those who do not exercise is 66.1530 and 67.1948 * * *
height) and interpret it in context.data_tb <- as.data.frame(table(yrbss$height))
freqncy <- sum(data_tb$Freq)
m_height <- mean(yrbss$height, na.rm = TRUE)
sd_height <- sd(yrbss$height, na.rm = TRUE)
mx_height <- max(yrbss$height, na.rm = TRUE)
n_height <- yrbss %>%
summarise(freqncy = table(height)) %>%
summarise(n = sum(freqncy, na.rm = TRUE))
u_height <- m_height + t * (sd_height/sqrt(n_height))
l_height <- m_height - t * (sd_height/sqrt(n_height))the confidence interval for height is an interval of 1.6931 and 1.6894
nu_height <- m_height + 1.645 * (sd_height/sqrt(n_height))
nl_height <- m_height - 1.645 * (sd_height/sqrt(n_height))**90% CI interval is 1.6928 and 1.6897*
exercise average height 1.703, dont exercise av aheight 1.666
exercise3plus <- yrbss %>%
filter(physical_3plus == "yes") %>%
select(height) %>%
na.omit()
noexercise <- yrbss %>%
filter(physical_3plus == "no") %>%
select(height) %>%
na.omit()
mean_exercise3plus <- mean(exercise3plus$height)
mean_noexercise <- mean(noexercise$height)hours_tv_per_school_day
there are.Insert your answer here
## # A tibble: 8 x 2
## hours_tv_per_school_day `n()`
## <chr> <int>
## 1 1 1750
## 2 2 2705
## 3 3 2139
## 4 4 1048
## 5 5+ 1595
## 6 <1 2168
## 7 do not watch 1840
## 8 <NA> 338
Insert your answer here