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:
?yrbssWhat are the cases in this data set? How many cases are there in our sample?
Q1: The high school 9th to 12th grade students are cases. There are 13,583 cases in the data set.
Remember 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.
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
How many observations are we missing weights from?
Q2: There are 1004 missing in weight and 9476 in the whole data set.
sum(is.na(yrbss))## [1] 9476
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"))Make a side-by-side boxplot of physical_3plus and
weight. Is there a relationship between these two
variables? What did you expect and why?
Q3: There is very similar output on both variables. I was expecting larger gap in between this two groups.
yrbss_q3 <- yrbss %>% na.exclude()
ggplot()+geom_boxplot(data =yrbss_q3 , mapping = aes(x=physical_3plus,y = weight))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.
Are all conditions necessary for inference satisfied? Comment on
each. You can compute the group sizes with the summarize
command above by defining a new variable with the definition
n().
Q4: Conditions we need for inference are random, normal and independent. According to the data likely are normal distributed.
ggplot()+geom_blank()+geom_histogram(data=yrbss_q3,aes(x=weight,y=after_stat(density)),fill="darkblue")+
geom_density(data=yrbss_q3,aes(x=weight),color="green")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> 273Write the hypotheses for testing if the average weights are different for those who exercise at least times a week and those who don’t.
Q5: H0: the average weights is not different between who
exercise at least 3 times a week and those who don’t.
HA: the average weights is different between who exercise at least 3
times a week and 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 %>%
na.exclude() %>%
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 %>%
na.exclude()%>%
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?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
null_dist %>%
visualize()+
shade_p_value(obs_stat = obs_diff, direction = "two_sided")This the standard workflow for performing hypothesis tests.
Construct and record a confidence interval for the difference between the weights of those who exercise at least three times a week and those who don’t, and interpret this interval in context of the data.
Q7:
yrbss %>%
group_by(physical_3plus) %>%
summarise(sd_weight = sd(weight, na.rm = TRUE))## # A tibble: 3 Ă— 2
## physical_3plus sd_weight
## <chr> <dbl>
## 1 no 17.6
## 2 yes 16.5
## 3 <NA> 17.6
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
active_mean <- 68.44847
not_active_mean <-66.67389
active_sd<-16.47832
not_active_sd<-17.63805
rnum_active<-8906
rnum_not_active<-4404
upper_ci_not_active= not_active_mean+ 1.96 * (not_active_sd/sqrt(rnum_not_active))
lower_ci_not_active=not_active_mean-1.96 * (not_active_sd/sqrt(rnum_not_active))
upper_ci_active= active_mean + 1.96 * (active_sd/sqrt(rnum_active))
lower_ci_active=active_mean - 1.96 * (active_sd/sqrt(rnum_active))
c("Student with active",lower_ci_active,upper_ci_active)## [1] "Student with active" "68.1062324499207" "68.7907075500793"
c("Student with not active",lower_ci_not_active,upper_ci_not_active)## [1] "Student with not active" "66.152955661764"
## [3] "67.194824338236"Calculate a 95% confidence interval for the average height in
meters (height) and interpret it in context.
Q8: Within 95% CI the height range is between 1.689m and 1.693m.
height_mean <- mean(yrbss$height, na.rm = TRUE)
height_sd <- sd(yrbss$height, na.rm = TRUE)
sample_height <- nrow(yrbss)
height_upper <- height_mean + 1.96*(height_sd/sqrt(sample_height))
height_lower <- height_mean - 1.96*(height_sd/sqrt(sample_height))
c("the height range is between",height_lower,height_upper)## [1] "the height range is between" "1.68948022416186"
## [3] "1.69300169013976"Calculate a new confidence interval for the same parameter at the 90% confidence level. Comment on the width of this interval versus the one obtained in the previous exercise.
Q9: From 95% to 90%, range did not change much nearly same.
height_upper_2 <- height_mean + 1.645*(height_sd/sqrt(sample_height))
height_lower_2 <- height_mean - 1.645*(height_sd/sqrt(sample_height))
c("the height range is between",height_lower_2,height_upper_2)## [1] "the height range is between" "1.68976319910651"
## [3] "1.6927187151951"
c("the different compare with 95%",abs(height_lower_2 - height_lower),
abs(height_upper_2 - height_upper))## [1] "the different compare with 95%" "0.000282974944652459"
## [3] "0.000282974944652459"Conduct a hypothesis test evaluating whether the average height is different for those who exercise at least three times a week and those who don’t.
Q10:
H0: the average height is not different between who exercise
at least 3 times a week and those who don’t.
HA: the average height different between who exercise at least 3 times a
week and those who don’t.
.
obs_diff_height <- yrbss %>%
na.exclude()%>%
specify(height ~ physical_3plus) %>%
calculate(stat = "diff in means", order = c("yes", "no"))
set.seed(45698)
null_dist_height <- yrbss %>%
na.exclude()%>%
specify(height ~ physical_3plus) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in means", order = c("yes", "no"))
visualize(null_dist_height) +
shade_p_value(obs_stat = obs_diff_height, direction = "two_sided")null_dist_height %>%
get_p_value(obs_stat = obs_diff_height, direction = "two_sided")## # A tibble: 1 Ă— 1
## p_value
## <dbl>
## 1 0
yrbss %>%
group_by(physical_3plus) %>%
summarise(height_sd_q10 = sd(height, na.rm = TRUE))## # A tibble: 3 Ă— 2
## physical_3plus height_sd_q10
## <chr> <dbl>
## 1 no 0.103
## 2 yes 0.103
## 3 <NA> 0.107
yrbss %>%
group_by(physical_3plus) %>%
summarise(height_mean_q10 = mean(height, na.rm = TRUE))## # A tibble: 3 Ă— 2
## physical_3plus height_mean_q10
## <chr> <dbl>
## 1 no 1.67
## 2 yes 1.70
## 3 <NA> 1.71
not_active_mean_q10<-1.6655
active_mean_q10<-1.7032
not_active_sd_q10<-0.1028
active_sd_q10<-0.1032
upper_ci_not_active_q10 = not_active_mean_q10+ 1.96 * (not_active_sd_q10/sqrt(rnum_not_active))
lower_ci_not_active_q10 = not_active_mean_q10-1.96 * (not_active_sd_q10/sqrt(rnum_not_active))
upper_ci_active_q10 = active_mean_q10 + 1.96 * (active_sd_q10/sqrt(rnum_active))
lower_ci_active_q10 = active_mean_q10 - 1.96 * (active_sd_q10/sqrt(rnum_active))
c("Student with active",lower_ci_active_q10,upper_ci_active_q10)## [1] "Student with active" "1.70105664344617" "1.70534335655383"
c("Student with not active",lower_ci_not_active_q10,upper_ci_not_active_q10)## [1] "Student with not active" "1.66246383387219"
## [3] "1.66853616612781"Now, a non-inference task: Determine the number of different
options there are in the dataset for the
hours_tv_per_school_day there are.
Q11: There are 7 options for TV hours per school day.
yrbss %>%
group_by(hours_tv_per_school_day)%>%
summarise(n=n())## # A tibble: 8 Ă— 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> 338Come up with a research question evaluating the relationship between height or weight and sleep. Formulate the question in a way that it can be answered using a hypothesis test and/or a confidence interval. Report the statistical results, and also provide an explanation in plain language. Be sure to check all assumptions, state your \(\alpha\) level, and conclude in context.
Q12: H0 Not difference between students sleep 8 hours and whose are not sleep 8 hours. HA There is difference between.
library(reshape2)
yrbss.melt<-melt(yrbss[,c('height','school_night_hours_sleep')],
id='height',na.rm = TRUE)
#yrbss.melt
grand.mean=mean(yrbss.melt$height,na.rm=T)
ggplot(yrbss.melt,aes(x=value,y=height))+geom_boxplot()+
geom_point(alpha=0.1,color='blue')+xlab('Sleeping_hour')+
geom_hline(yintercept=grand.mean,color='red')yrbss.melt <- yrbss.melt %>%
mutate(sleep_7hour = ifelse(yrbss.melt$value > 7, "yes", "no"),na.rm=T)
#yrbss.meltlibrary(psych)
yrbss.melt.tab <- describeBy(yrbss.melt$height,
group = yrbss.melt$sleep_7hour,mat = T, skew=F)
yrbss.melt.tab## item group1 vars n mean sd min max range se
## X11 1 no 1 8271 1.689770 0.1044631 1.27 2.11 0.84 0.001148640
## X12 2 yes 1 3210 1.693171 0.1050022 1.27 2.06 0.79 0.001853301
ggplot(yrbss.melt,aes(x=sleep_7hour,y=height))+
geom_boxplot()+geom_point(data =yrbss.melt.tab,
aes(x=group1,y=mean),color='blue',size=3)+
geom_hline(yintercept = grand.mean,color='red')ggplot(yrbss.melt,aes(x=height,color=sleep_7hour))+geom_density()
From here we could use SE for difference in sleep over 7 hours and
calculate 95% CI.
sd_no7hour<-0.1044631
sd_yes7hour<-0.1050022
n_no7hour <-8271
n_yes7hour <-3210
se_diff<-sqrt(sd_no7hour^2/n_no7hour + sd_yes7hour^2/n_yes7hour)
mean_no7hour <-1.689770
mean_yes7hour<-1.693171
upper_output= (mean_yes7hour-mean_no7hour)+1.96*se_diff
lower_output= (mean_yes7hour-mean_no7hour)-1.96*se_diff
c("the difference in between is",lower_output,upper_output)## [1] "the difference in between is" "-0.000872562799923633"
## [3] "0.00767456279992361"
Therefore, 0 is within the difference with 95% CI we are not reject null hypothesis. That means there is not difference in between student have 7 housr sleep and those whose don’t sleep 7 hours.