Problem Set 8
Sagar Lamsal
1. (5 points)
Let 𝑋 and 𝑌 be independent random variables with 𝜒 ! distributions with 𝑛 and 𝑚 degrees
of freedom respectively. What is the distribution of 𝑋 + 𝑌?. (5 points)
The probability density function for 𝑋 is
𝑥 #/!%& 𝑒 %'/!
,𝑥>0
2#/! 𝛤(𝑛/2)
𝑦 )/!%& 𝑒 %*/!
𝑓( (𝑦) = #/!
,𝑥>0
2 𝛤(𝑛/2)
𝑓" (𝑥) =
Let 𝑍 = 𝑋 + 𝑌. We have:
,
𝑓+ (𝑥)
= (𝑓 ∗ 𝑔) = 8 𝑓" (𝑧 − 𝑦)𝑓( (𝑦)𝑑𝑦
,
#/!%& %(/%*)/!
(𝑧 − 𝑦)
𝑒
𝑦 )/!%& 𝑒 %*/!
×
𝑑𝑦
2#/! 𝛤(𝑛/2)
2#/! 𝛤(𝑛/2)
,
(𝑧 − 𝑦)#/!%& 𝑒 %(/%*)/! 𝑦 )/!%& × 𝑒 %*/!
=8
𝑑𝑦
2#/! 𝛤(𝑛/2) × 2)/! 𝛤(𝑚/2)
,
(𝑧 − 𝑦)#/!%& 𝑦 )/!%& 𝑒 %//!
= 8 #/!1)/!
𝑑𝑦
𝛤(𝑛/2) × 𝛤(𝑚/2)
- 2
&
(𝑧 − 𝑧𝑡)#/!%& (𝑧𝑡))/!%& 𝑒 %//!
= 8 #/!1)/!
𝑧𝑑𝑡
𝛤(𝑛/2) × 𝛤(𝑚/2)
- 2
& #/! )/!%& %//!
𝑧 𝑧
𝑒
(1 − 𝑡)#/!%& 𝑡 )/!%&
=8
𝑑𝑡 by change of variable 𝑦 = 𝑧𝑡
2#/!1)/! 𝛤(𝑛/2) × 𝛤(𝑚/2)
&
𝑧 #/!1)/!%& 𝑒 %//!
𝛤(𝑛/2 + 𝑚/2)(1 − 𝑡)#/!%& 𝑡 )/!%&
= #/!1)/!
8
𝑑𝑡
2
𝛤(𝑛/2 + 𝑚/2) 𝛤(𝑛/2) × 𝛤(𝑚/2)
=8
⏟
Beta(#/!,)/!)
=
𝑧 #/!1)/!%& 𝑒 %//!
2#/!1)/! 𝛤(𝑛/2 + 𝑚/2)
Thus, 𝑍 ∼ 𝜒 ! (𝑛 + 𝑚).
2.
2a.(10 points)
Perform a numerical experiment to estimate the (0.025,0.25, .0.5,0.75,0.975) quantiles of
the sample correlations between two iid samples of sizes 10, 20, 50, and 100 from the
standard Normal distribution. Display the quantiles in a table. Suggestion: for each
size,simulate the process of drawing a pair of samples calculating their correlation many
times then report the requested quantiles of the observed correlations.(5 points)
sizes <- c(10, 20, 50, 100)
qles <- c(0.025, 0.25, 0.5, 0.75, 0.975)
n <- sizes[1]
# samples of size n
cor_xy <- sapply(sizes, function(n){
x <- rnorm(n)
y <- rnorm(n)
# correlations
cor(x,y)
})
# Correlations
q_cor_xy <- quantile(cor_xy, qles) %>% data.frame() %>%
rownames_to_column("Quantile") %>%
rename("Correlation" = 2)
q_cor_xy
##
Quantile Correlation
## 1
2.5% -0.33613351
## 2
25% -0.20270865
## 3
50% -0.12335310
## 4
75% -0.06060892
## 5
97.5% 0.02796522
2b.(10 points)
Display the quantiles for each 𝑛 in a plot that has the value of 𝑛 on the x-axis, and the
correlation quantiles on the y-axis. Please use bars to indicate the range between the 0.25
and 0.75 quantile and the range between the 0.025 and 0.975 quantile. Please plot a point
at the 0.5 qunatile for each 𝑛. Please title the plot and label the axes with informative text.
You may find “geom_errorbar” useful. You can position ticks on the x-axis using
“scale_x_continuous(breaks=c(10,20,50,100))” for example.
# Display the quantiles for each $n$ in a plot that has the value of $n$ on
the
# x-axis, and the correlation quantiles on the y-axis
qa <- as.numeric(q_cor_xy$Correlation)
p <- q_cor_xy %>%
ggplot(aes(x = Quantile, y = Correlation)) + geom_col() +
geom_errorbar(aes(ymin = qa[2],
ymax = qa[4]),
col = "blue")
# indicate the range between the 0.25 and 0.75 quantile
p <- p + geom_errorbar(aes(ymin = qa[1], ymax = qa[5]), col = "red")
# and the range between the 0.025 and 0.975 quantile
p <- p + geom_point(aes(x = Quantile, qa[4])) +
theme_classic()
# plot title
p <- p + ggtitle("Sample correlations with interquantile\nranges and median
points")
p
3. (5 points each)
The data here are sampled from the full survey data according to the survey weights.
load("pew_sampled_data.RData")
3a.
The variable recording whether the respondent was a STEM worker or a non-STEM worker
is “WORKTYPE_FINAL”.
Please create a 2-way table that gives the proportion of respondents among Stem workers
that selected each response and the proportion of respondents among non-Stem workers
that selected each response.
For the purposes of table display, please shorten the responses to “too hard”, “too boring”,
“not useful”, “other”.
# 2-way table that gives the proportion of respondents among Stem workers
that
# selected each response and the proportion of respondents among non-Stem
# workers that selected each response
samp1 <- samp %>%
mutate(SCH7 = gsub(pattern = "They think these subjects are too hard",
replacement = "too hard", SCH7),
SCH7 = gsub(pattern = "They think these subjects are too boring",
replacement = "too boring", SCH7),
SCH7 = gsub(
pattern = "They think these subjects are not useful for their
careers",
replacement = "not useful", SCH7),
SCH7 = gsub(pattern = "Some other reason \\(please specify\\)",
replacement = "other", SCH7)
) %>% droplevels()
tab3a <- samp1 %>% select(SCH7, WORKTYPE_FINAL) %>% table()
tab3a
##
WORKTYPE_FINAL
## SCH7
STEM Worker Non-STEM Worker
##
not useful
45
74
##
other
32
36
##
Refused
5
4
##
too boring
41
31
##
too hard
170
150
3b.
Please do a 𝜒 ! test and a Fisher’s Exact Test to determine if the responses to “SCH7” are
independent of “WORKTYPE_FINAL”, and compare the results.
chisq.test(tab3a)
## Warning in chisq.test(tab3a): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: tab3a
## X-squared = 10.046, df = 4, p-value = 0.03966
fisher.test(tab3a)
##
## Fisher's Exact Test for Count Data
##
## data: tab3a
## p-value = 0.03726
## alternative hypothesis: two.sided
The null hypothesis (“SCH7” are independent of “WORKTYPE_FINAL”) is rejected
because the p-value is less than the level 5% in both tests. The only problem here is that the
𝜒 ! test throws warning Chi-squared approximation may be incorrect.
3c.
Please repeat 2b. omitting respondents who answered “refused”.
tab3c <- samp1 %>% select(SCH7, WORKTYPE_FINAL) %>% droplevels() %>%
filter(!SCH7 %in% "Refused") %>%
table()
tab3c
##
WORKTYPE_FINAL
## SCH7
STEM Worker Non-STEM Worker
##
not useful
45
74
##
other
32
36
##
too boring
41
31
##
too hard
170
150
# tests
chisq.test(tab3c)
##
## Pearson's Chi-squared test
##
## data: tab3c
## X-squared = 9.9261, df = 3, p-value = 0.0192
fisher.test(tab3c)
##
## Fisher's Exact Test for Count Data
##
## data: tab3c
## p-value = 0.01892
## alternative hypothesis: two.sided
Same thing except the change of the p-value in the Fisher exact test which is again less than
the level 5%. By removing the refused category, we notice that there is no more warning
from the 𝜒 ! test summary.
Harassment
3d.
Please create a subset of the data that omits responses from the small categories
“HARASS3”==“Refused”. The function “droplevels” applied to a factor variable will omit
levels that aren’t represented in the data.
Please run and report the result of an appropriate test of the null hypothesis that the
response to “HARASS3” is independent from the response to “PPGENDER” on this subset.
Please give a statistical interpretation of the results.
tab3d <- samp1 %>%
filter(!HARASS3 == "Refused") %>% select(HARASS3, PPGENDER) %>%
droplevels() %>% table()
# tests
chisq.test(tab3d)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tab3d
## X-squared = 10.843, df = 1, p-value = 0.0009919
fisher.test(tab3d)
##
## Fisher's Exact Test for Count Data
##
## data: tab3d
## p-value = 0.000785
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.2674558 0.7305750
## sample estimates:
## odds ratio
## 0.4457607
We have evidence from both tests to reject the null hypothesis.
3e.
Please restrict the data from 3a the respondents who report “Female” in the “PPGENDER”
variable. Please provide a table of the proportion of each category of “PPETHM” that
responds “Yes, I have experienced sexual harassment at work”. Please run and report the
result of an appropriate test of the null hypothesis that the response to “HARASS3” is
independent from the response to “PPETHM” on this subset. Please give a statistical
interpretation of the results.
tab3e <- samp1 %>%
filter(!HARASS3 == "Refused" & !PPGENDER == "Female") %>%
select(HARASS3, PPETHM) %>% droplevels() %>% table()
# tests
chisq.test(tab3e)
## Warning in chisq.test(tab3e): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: tab3e
## X-squared = 9.573, df = 4, p-value = 0.04827
fisher.test(tab3e)
##
## Fisher's Exact Test for Count Data
##
## data: tab3e
## p-value = 0.03765
## alternative hypothesis: two.sided
Again, from both test we fail to accept the null hypothesis according to p-values. Note that
the 𝜒 ! throw warnings and its p-value is very close to the threshold 0.05.