STAT 223 Assignment 6
Due April 30, 2024 at 11:59pm
A study was conducted investigating the performance of a newly implemented student orientation program.
This program was implemented at 9 different colleges. Students in the study were given a test on their
understanding of their school’s policy before and after the training, and the after-before score was calculated,
and this will be considered the “orientation effect” that we are interested in investigating. In the table below,
we have the mean orientation effect at each of the 9 colleges, along with the standard error of the estimate.
We will takeσ
j
to be known since sample sizes are large enough.
College ̄y
j
σ
j
A31 17
B14 12
C20 13
D17 9
E-1 12
F10 10
G58 11
H32 17
I5 14
Using this data, we setup the following model specifications:
̄y
j
|θ
j
,σ
j
∼N(θ
j
,σ
2
j
)
θ
j
|μ,τ∼N(μ,τ
2
)
p(μ,τ) =p(τ)∝c
This leads to the following posterior distributions (refer to page 50 of the notes forVandˆμ:
θ
j
|μ,τ,y∼N
̄y
j
σ
2
j
+
μ
τ
2
1
σ
2
j
+
1
τ
2
,
1
1
σ
2
j
+
1
τ
2
μ|τ,y∼N
P
J
j=1
̄y
j
σ
2
j
+τ
2
P
J
j=1
1
σ
2
j
+τ
2
,
1
P
J
j=1
1
σ
2
j
+τ
2
τ|y∝V
1/2
J
Y
j=1
(σ
2
j
+τ
2
)
−1/2
e
−
( ̄y
j
−ˆμ)
2
2(σ
2
j
+τ
2
)
1
1. We will write a Metropolis-Hastings algorithm to draw samples from the posterior ofτ|y.
a. Begin by writing a function for calculating the unnormalized posterior:
p <-function(tau, y, sigma){
J <-length(y)
dens <-vector(length=J)
mu.hat <-sum(y/(sigmaˆ2+tauˆ2))/sum(1/(sigmaˆ2+tauˆ2))
V <- 1/(sum(1/(sigmaˆ2+tauˆ2)))
for(jin1:J){
dens[j] <-### fill this part in
}
dens <-prod(dens)*Vˆ(1/2)
return(dens)
}
b. Write a functionQto sample from a normal distribution truncated at 0 with mean equal to the
current value ofτand letting the variance be defined as an argument in the function. Also write
a functionqdensto evaluate this proposal distribution. You will want to use thertruncnorm()
anddtruncnorm()functions within thetruncnormlibrary in R for this question.
c. Write a Metropolis-Hastings algorithm for this problem.
d. Using an initial value ofτ= 10, drawn.iter=100000samples from the posterior distribution
ofτ|y, using a proposal variance of 60. Make sure you set your seed for reproducibility. Use
set.seed(123).
e. Use the draws ofτ|yto draw 100000 samples of theμ|τ,y.
f. Use the draws ofτ|yandμ|τ,yto draw 100000 samples of eachθ
j
|μ,τ,y.
2. Report the posterior mean and 95% posterior interval for each variable. Present the results in a nice
table.
3. Plot the posterior means ofθ
1
,...,θ
J
against the sample means, along with the 95% posterior intervals.
Gibbs
As an alternative to the method above, suppose we sample directly from the full conditional posteriors: This
leads to the following full conditional posterior distributions:
θ
j
|μ,τ,y∼N
̄y
j
σ
2
j
+
μ
τ
2
1
σ
2
j
+
1
τ
2
,
1
1
σ
2
j
+
1
τ
2
μ|θ
1
,...,θ
J
,τ,y∼N
̄
θ,
τ
2
J
τ
2
|θ
1
,...,θ
J
,μ,y∼IG
J−1
2
,
P
J
j=1
(θ
j
−μ)
2
2
!
4. Use a Gibbs sampler to draw 100000 samples from these full conditional posterior distributions. Report
the posterior mean and 95% posterior interval for each variable. Present the results in a nice table.
2