Suppose Sophie, the editor of the student newspaper, is going to conduct a survey of students to determine the level of support for the current president of the students association. She needs to determine her prior distribution for p, the proportion of students who support the president. She decides her prior mean is 0.5, and her prior standard deviation is 0.15.

\[\begin{aligned}E(Y)&=\frac{a}{a+b}\\ &=0.5\\ a&=0.5(a+b)\\ &=0.5a+0.5b\\ a-0.5a&=0.5b\\ 0.5a&=0.5b\\ a&=b\end{aligned}\]

\[\begin{aligned}V(Y)&=\frac{ab}{(a+b)^{2}(a+b+1)}\\ &=0.15^2\\ \frac{a^2}{(2a)^{2}(2a+1)}&= 0.0225\\ \frac{a^2}{(8a)^{2}+(4a)^2}&=0.0225\\ \frac{1}{8a+4}&=0.0225\\ 8a&=\frac{1}{0.0225}-4\\ 8a&=\frac{364}{9}\\ a&\approx 5.06\end{aligned}\]

Therefore,

a=5.06
b=5.06
  1. Out of the 68 students that she polls, y=21 support the current president. Determine posterior distribution using the Beta(5.06,5.06) prior.
n=68
y=21
a_prime = y+a
a_prime
## [1] 26.06
b_prime=n-y+a
b_prime
## [1] 52.06
p <- seq(0, 1, length = 1000)
a <- 5.06
b <-5.06
s <- 21
f <- n-y
f
## [1] 47
prior <- dbeta(p, a, b)
post <- dbeta(p, a+s, b+f)
plot(p, post, type = "l", ylab = "Density", lty = 2, lwd = 3)
lines(p, prior, lty = 3, lwd = 3)
legend(.7, 4, c("Prior", "Posterior"),
       lty=c(3, 2), lwd = c( 3, 3))

d.Construct and interpret a 90% credible interval for p.

ProbBayes::beta_interval(0.9,c(a_prime, b_prime))

Problem 2 Suppose Sophie, the editor of the student newspaper, conducted a survey to 68 students to determine the level of support for the current president of the students association. Out of the 68 students that she polls, y=21 support the current president. She needs to determine her prior distribution for p, the proportion of students who support the president. Suppose it is known that 25th and 75th percentiles of a Beta(a,b) prior are 0.393 and 0.607, respectively.

n2=68
y2=21
beta.select(list(x=0.393,p=0.25),
            list(x=0.607,p=0.75))
## [1] 5.1 5.1
a2=5.1
b2=5.1

b.Find and plot the posterior distribution.

p <- seq(0, 1, length = 1000)
a <- 5.1
b <- 5.1
s <- 21
f <- n-y
f
## [1] 47
post <- dbeta(p, a+s, b+f)
plot(p, post, type = "l", ylab = "Density", lty = 2, lwd = 3)
legend(.7, 4, "Posterior",
       lty=3, lwd = 3)

c.Sophie claims that at least 85% of the students support the current president. Are we going to reject or not her claim? Support your answer.

a_prime2=a2+21
a_prime2
## [1] 26.1
b_prime2=n2-y2+a2
b_prime2
## [1] 52.1
ProbBayes::beta_area(lo = 0.85, hi = 1.0,
          shape_par = c(a_prime2,b_prime2))

Since \(P(p\ge0.85)\approx 0\), thus, the claim that at least 85% of the students support the current president is very unlikely, almost impossible to happen.

  1. Using the posterior distribution, how many students are expected to support the current president if the survey is given to a sample of 100 students?
predict_p <- rbeta(1000, a_prime2, b_prime2)
predict_y<- rbinom(1000, 100, predict_p)
predict=(mean(predict_y))
predict
## [1] 33.213

Therefore, approximately 34 of the students are expected to support the current president given a sample of 100 students.

e.Does the posterior distribution describe well the prediction in (d)? Explain

sim_p <- rbeta(1000, a_prime2, b_prime2)
sim_y <- rbinom(1000,100, sim_p)
hist(sim_y,xlab="Simulated Y", main = " ")
abline(v = predict, col = "red", lwd = 3, lty = 2)

Since the predicted value is around the center, we can say that the posterior distribution describe well the prediction in (d).