#
# Chapter 5 -- Bayesian Computation With R
# Monte Carlo Method for Computing Integrals
#
# Remove all objects just to be safe
#
rm(list=ls(all=TRUE))
#
library(LearnBayes)
#
# From pages 22 - 26 on Sleep times of college students:
# The proportion of heavy sleepers = y ~ Beta(3.26+11, 7.19+16)
# Where there are 27 students, 11 sleep a sufficient number
# of hours and 16 do not.
#
# The Likelihood was a Beta and the Prior on the Proportion
# was also a Beta. Hence the Posterior was Beta(14.26, 23.19).
#
# Note that from page 25 the 90% interval for the proportion of
# heavy sleepers was (0.2562364, 0.5129274). The mean of this
# interval = 0.3845819.
#
# We wish to forecast the probability that two students from a
# future sample will be heavy sleepers. This is equivalent
# to predicting the value of p^2. This is done by drawing a
# large sample from the Posterior distribution and calculating
# the mean and variance of p_hat^2.
#
p <- rbeta(1000, 14.26, 23.19)
est <- mean(p^2)
se <- sd(p^2)/sqrt(1000)
forecast <- c(est,se)
#
# forecast
# [1] 0.146635480 0.001799525