Week5 ProblemSet Solutions
Week5 ProblemSet Solutions
y Z oe 2 y Z oe 2 y Z oe 2
m m m
cade cade cade
EZ A EZ A EZ A
Part 1
Question 1 [Required]
98 been many questions regarding whether
There 5have or8 not usage of social media increases
3589 98
o e 23 8levels. A study was conducted to examine
anxiety o e 2the relationship between social media usageo e 2358
y Z y Z y Z
A c adem and student anxiety. The following A c dem are measures of anxiety levels of students
ascores A c adem on a
EZ Z
Monday. Higher scores indicate Ehigher anxiety; and, if a student used social mediaEZ more than
1 hour per day then their usage was categorized as “High”.
(a) Construct boxplots of anxiety_score for the two levels of social media usage. Write 2-3
sentences comparing the distributions of anxiety scores across the social media usage groups.
8 8
e 2 3589 e 2 3589 e
anxiety_data o ggplot(aes(y=social_media_usage, x=anxiety_score))
y Z%>% y Zo + y Zo
A c adem
geom_boxplot() A c adem Acad
em
EZ EZ EZ
Low
social_media_usage
98 98
2358 2358
y Zoe y Zoe y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
High
There indeed appears to be a clear difference in anxiety between “Low” and “High” social media
usage groups, with the median of the latter exceeding the 75th percentile of the former. For the
“High” social media usage group, the anxiety scores are much more concentrated around high
(>30) anxiety scores; whereas, for the “Low” social media usage group the anxiety scores are
much more highly 2 3 5898
variable, with the upper 50% of anxiety 2
scores3 898the “Low” social media
5in
Zoe
my overlapping
usage agroup or even exceeding those in athe m
“High”
e
y Zo social media usage group, and m y Zo
e
d e d e ca d e
E Z Acof the lower 50% of anxiety scores in theE“Low”
most Z Ac social media usage group being less than EZ A
those in the “High” social media usage group.
(b) Do these data support the claim that the median anxiety level is different for those who
use social media in high frequency compared to those who use social media in lower frequency?
(i) State the hypotheses you are testing (be sure to define any parameters you refer to).
358 98 98 98
oe 2“High” social media usage population H
For and 2358 social media usage population L with
oe “Low” oe 2
358
y Z
m medians 50%H and 50%L , respectively,adem y Z m y Z
cade c cade
EZ A EZ A EZ A
H0 : 50%H = 50%L HA : 50%H 6= 50%L
(ii) Look at the code below and write a few sentences explaining what the code inside the for loop is doing
and why.
# Note: including the .groups="drop" option in summarise() will suppress a friendly
# warning R prints otherwise
8 "`summarise()` ungrouping output (override
8 with
#`.groups` argument)".
e 2 3589 e 2 3589 e
y Zothe .groups="drop" option is optional,em
# Including y Zo y Zo
A c adem A c ad but you should include it if you Acad
em
Z
#don't
E want to see that warning. EZ EZ
test_stat <- anxiety_data %>% group_by(social_media_usage) %>%
summarise(medians = median(anxiety_score), .groups="drop") %>%
summarise(value = diff(medians))
test_stat <- [Link](test_stat)
test_stat
89 8 98 98
## [1]
235-4.57 358 358
Zoe Z oe 2 Z oe 2
c a d emy cade
m y
cade
m y
EZ A EZ A EZ A
2
98 98 98
o e 2358 oe 2
358
oe 2
358
y Z y Z y Z
A c adem cade
m
cade
m
EZ [Link](523) EZ A EZ A
repetitions <- 1000;
simulated_values <- rep(NA, repetitions)
for(i in 1:repetitions){
simdata <- anxiety_data %>% mutate(social_media_usage = sample(social_media_usage))
sim_value <- 23
simdata
5898 23 5898
m y Zoe m y Zoe
%>% group_by(social_media_usage) %>%
y Zo
e
cade = median(anxiety_score), .groups="drop")
cade em
Acad
summarise(medians %>%
EZ Asummarise(value = diff(medians)) EZ A EZ
150
98 98
2358 2358
y Zoe y Zoe y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
100
count
8 98 98
3589
250 358 358
y Z o e
y Z oe 2 y Z oe 2
A c adem cade
m
cade
m
EZ EZ A EZ A
−6 −3 0 3 6
58 98 median_diff 589 8
o e 23 o e 23 e
my Z my Z
em y Zo
# Z A cade
Calculate p-value Z A cade Acad
E E EZ
num_more_extreme <- sim %>% filter(abs(median_diff) >= abs(test_stat)) %>% summarise(n())
## [1] 0.009
98
358 in the for loop is first shuffling the e“High” 98 98
The 2code 2358 and “Low” social media usage group 2358
Zoe Zo Zoe
cademy cademy cademy
EZ A EZ A EZ A
3
98 98 98
o e 2358 o e 2358 o e 2358
y Z y Z y Z
A c adem A c adem A c adem
EZ EZmeans the outcomes for both groups are sampled
labels for each observation. This EZ from the
same population (i.e, our sample), or, equivalently, that group label doesn’t matter in terms
of what the median of the population of a sample will be. But this then means that the null
hypothesis is true for these simulated data. So the subsequently calculated (median) statistic
represents a draw from the sampling distribution of the median under the assumption that the
null hypothesis is true. And by repeatedly sampling in this manner we create a simulation of
the sampling distribution of the median under the null hypothesis.
8 8
3589
2 e 2 3589
(iii) Write a few
m y Zoesentences summarizing your conclusions. Be sure
m y Zointerpret
to the p-value carefully and to
m y Zo
e
ca d e
clearly address the research question. ca d e ca d e
EZ A EZ A EZ A
At a alpha=0.05 significance level we reject the null hypothesis that the median anxiety level
among “High” and “Low” social media usage groups is the same. The p-value of 0.009 rep-
resents “very strong” evidence against the null hypothesis. Further, the observed difference
indicates that individuals with “High” social media usage have MORE anxiety than individu-
als with “High” social media usage; however, in rejecting the null hypothesis in favor of the
alternative hypothesis, all we are “formally” claiming is that the groups have different medi-
8 other hand, since the evidence against the
589the 98 98
ans.23
o e
On
o e 2358 null hypothesis is “very strong”, it is o e 2358
y Zsensible to conclude that “High” social media y Zusage increases anxiety relative to “Low” y Z
social
A c adem media usage. A c adem A c adem
EZ EZ EZ
98 98
2358 2358
y Zoe y Zoe y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
98 98
2358 2358
y Zoe y Zoe y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
(b) In the context of this problem, describe what rejecting the null hypothesis would mean.
Convicting Someone with a “Guilty” sentencing.
(d) In the context of this problem, describe what a type II error would be.
A guilty defendant being set free.
98
8 problem, describe what a type I error 2would
35this 58 be. 98
e 2of
(e) In the context e 3 e
y Zo y Zo y Zo
An c adem defendant being wrongly convicted asAcbeing
innocent
A ademguilty. Acad
em
EZ EZ EZ
98 98
2358 2358
y Zoe y Zoe y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
98 8
3589available
2358
Airbag available No airbag
2
y Zoe oe
y Z62 y Zo
e
cadem Passenger Fatalities 45
ca d e m
Acad
em
EZ A Total number of Passengers EZ A
10,541 9,867 EZ
The code below creates a tidy data frame for this problem, using the R command rep. This function creates a
vector which replicates its first argument the number of times indicated by its second argument. For example,
the rep("hello", 5) creates a vector with 5 elements, each of which is “hello”. Run the code chunk below
to load a tidy tibble called data which you’ll use for the remainder of this question.
35 898 2358
98 358 98
oe 2
library(tidyverse)
y Z y Z o e
y Z oe 2
A c adem A c adem cade
m
EZ EZ
data <- tibble(group=c(rep("airbag",10541),rep("no_airbag",9867)), EZ A
outcome=c(rep("dead",45), rep("alive",10541-45),
rep("dead",62), rep("alive",9867-62)))
(a) State appropriate hypotheses to compare the proportions of deaths in cars with and without
airbags. Be sure to define any parameters you refer to in your hypotheses.
For cars with (W) 2and898
35 without (WO) airbags having chance ofe death 898
235 in a car crash of πW and
m y Zoe m y Zo y Zo
e
πW O , a
respectively,
de we consider the one-sided hypothesis
ade test em
EZ A
c
EZ A
c
EZ Acad
H0 : πW = πW O HA : πW < πW O
which means “as or more extreme under the null hypothesis” will only consider test statistics
sampled under the null hypothesis which are less than or equal to the observed test statistic.
Generally, one-sided hypothesis tests lead to smaller p-values since only half of the outcomes
that would be considered “as or more extreme” are actually counted as being so.
98 98 98
o e 2358 o e 2358 oe 2
358
y Z emy
Z y Z
A c adem (b) Carry out a hypothesis test for Athe
c adhypotheses stated in part (a).
cade
m
EZ EZ EZ A
[Link](523) # Replace the number in the parentheses with the 1st, 3rd, and 5th
# digits in your student number.
for(i in 1:repetitions){
simdata <- data %>% mutate(group = sample(group))
sim_value
98 <- simdata %>%
358 358 98 358 98
y Z oe 2 y Z oe 2 y Z oe 2
m m m
cade cade cade
EZ A EZ A EZ A
6
98 98 98
o e 2358 o e 2358 o e 2358
y Z y Z y Z
A c adem A c adem A c adem
EZ EZ
mutate(outcome = case_when(outcome=='dead'~1, outcome!='dead'~0)) %>% E
Z
group_by(group) %>%
summarise(prop = mean(outcome), .group="drop") %>%
summarise(value = diff(prop))
## [1] 0.027
98 98 98
#o 2358for scratch work if needed
space
e oe 2
358
oe 2
358
Z Z Z
cademy cade
m y
cade
m y
EZ A EZ A EZ A
(c) Based on your answer in part (b), would you reject the null hypothesis at the 0.1 significance
level?
Yes – the p-value is less than 0.1.
(d) Based on your answer in part (c), what kind of error did you possibly make?
8 8
If the decision to 2 3589 the null hypothesis is wrong then we e
reject have 89 a Type I Error.
235made
yZoe yZo y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
98 98
2358 2358
y Zoe y Zoe y Zo
e
em em em
EZ Acad EZ Acad EZ Acad
(d) Based on results from a survey of graduates from the University of Toronto, we would like
to compare the median salaries of graduates from the statistics and graduates of mathematics
programs.
H0 : Mstat = Mmath vs HA : Mstat 6= Mmath where Mstat is the median salary for graduates of
statistics programs and 8M is the median salary for graduates of 8
math programs.
3589 math
e 2 3589 e 2 e
e m y Zo e m y Zo emy Zo
ca d ca d Acad
EZ A EZ A EZ