22 Assignment 3
2021-09-07
22.1 Setup
::opts_chunk$set(echo = TRUE, comment = "#>", dpi = 300)
knitr
for (f in list.files(here::here("src"), pattern = "R$", full.names = TRUE)) {
source(f)
}
library(glue)
library(tidyverse)
theme_set(theme_bw() + theme(plot.title = element_text(hjust = 0.5)))
set.seed(748)
22.2 Exercise 1. Inference for normal mean and deviation
A factory produces car windshields and we have sample of data from testing their hardness. Assume the observations follow a normal distribution with unknown standard deviation. Also use an uninformative prior \(p(\mu, \sigma) \propto \sigma^{-1}\).
<- read_data("windshieldy1.txt")
windshieldy <- as.numeric(windshieldy)
windshieldy <- c(13.357, 14.928, 14.896, 14.820) windshieldy_test
a) What can you say about the unknown \(\mu\)? Summarize your results using Bayesian point estimate (i.e. \(E(\mu|y)\)), a posterior interval (95%), and plot the density.
The point estimate and 95% CI for \(\mu\) can be calculated using the \(t\)-distribution and scaling the result according to the following equation for BDA3 (pg. 66):
\[ \frac{\mu - \bar{y}}{s / \sqrt{n}} | y \sim t_{n-1} \] where
\[ s = \frac{1}{n-1} \Sigma(y - \bar{y})^2 \]
and \(n\) is the total number of data points.
<- function(a) {
calc_s_statistic <- (1 / (length(a) - 1)) * sum((a - mean(a))^2)
s_sqr return(sqrt(s_sqr))
}
<- function(data, q = 0.5) {
mu_point_est <- data
y <- length(y)
n <- mean(y)
y_bar <- calc_s_statistic(y)
s <- qt(q, df = n - 1)
t_prob return(t_prob * s / sqrt(n) + y_bar)
}
stopifnot(close_to(mu_point_est(data = windshieldy_test), 14.5))
mu_point_est(data = windshieldy)
#> [1] 14.61122
<- function(data, prob = 0.95) {
mu_interval <- (1 - prob) / 2.0
lower_q <- 1 - lower_q
upper_q return(c(mu_point_est(data, lower_q), mu_point_est(data, upper_q)))
}
stopifnot(all(close_to(
mu_interval(data = windshieldy_test, prob = 0.95),
c(13.3, 15.7),
epsilon = 0.1
)))mu_interval(data = windshieldy)
#> [1] 13.47808 15.74436
The probability density function of \(\mu\) can be estimated using the same equations. The PDF is plotted below.
<- function(mu, data) {
mu_density <- data
y <- length(y)
n <- mean(y)
y_bar <- calc_s_statistic(y)
s <- (mu - y_bar) / (s / sqrt(n))
mu_trans <- dt(mu_trans, df = n - 1)
d return(d)
}
<- seq(11.5, 17.5, 0.01)
mus <- purrr::map_dbl(mus, ~ mu_density(.x, windshieldy))
mu_dens plot_dist(
mus,
mu_dens,xlab = "mu",
ylab = "probability",
main = "posterior distribution of mu"
)
The PDF for \(\sigma\) can be calculated analytically from equation 3.5 in BDA (pg. 65).
\[
\sigma^2 | y \sim \text{Inv-}\chi^2(n-1, s^2)
\]
where \(n\) and \(s\) have the same description as above.
The \(\text{Inv-}\chi^2(\nu, s^2)\) distribution is the scaled inverse chi-squared distribution parameterized by the degrees of freedom \(\nu\) and scale \(s\) (BDA3, Appendix A, pg. 578).
I used the function dinvchisq()
from the package ‘LaplacesDemon’ to calculate the probabilities of values of \(\sigma^2\).
The PDF for \(sigma^2\) is plotted below.
<- function(x, data) {
sigma_probability <- length(data) - 1
nu <- calc_s_statistic(data)
s <- LaplacesDemon::dinvchisq(x = x, df = nu, scale = s)
d return(d)
}
<- seq(0, 10.0, 0.01)
sigmas <- sigmas[2:length(sigmas)]
sigmas <- purrr::map_dbl(sigmas, ~ sigma_probability(.x, windshieldy))
sigma_dens plot_dist(
sigmas,
sigma_dens,xlab = "sigma^2",
ylab = "probability",
main = "posterior distribution of sigma^2"
)
b) What can you say about the hardness of the next windshield coming from the production line before actually measuring the hardness? Summarize your results using Bayesian point estimate, a predictive interval (95%), and plot the density.
Random values of \(\mu\) can be sampled from the \(t\)-distribution (and transformed as explained above) and samples for \(\sigma^2\) can be sampled from the \(\text{Inv-}\chi^2\) distribution. Another option for sampling \(\sigma^2\) is to first sample values \(X\) from the \(\chi_\nu^2\) distribution and transform them by \(\nu s^2 / X\) (BDA3, Appendix A, pg. 583).
The posterior predictive sampling procedure was to sample 1,000 random values for \(\mu\) and \(\sigma\) then sample a random value from a normal distribution described with each pair of the random values.
<- function(n, data) {
random_mu <- data
y <- length(y)
nu <- mean(y)
y_bar <- calc_s_statistic(y)
s <- rt(n, df = nu)
r return(r * s / sqrt(nu) + y_bar)
}
<- function(n, data) {
scaled_rinvchiq <- length(data) - 1
nu <- calc_s_statistic(data)
s <- LaplacesDemon::rinvchisq(n = n, df = nu, scale = s)
theta return(theta)
}
<- 1e4
n <- scaled_rinvchiq(n, data = windshieldy)
r_sigmas_sqrd <- random_mu(n, data = windshieldy)
r_mus <- rnorm(n, mean = r_mus, sd = sqrt(r_sigmas_sqrd)) y_tildes
The plot below shows 100 of the randomly created normal distributions form the sampled values of \(\mu\) and \(\sigma\).
<- function(mu, sigma, a = 10, b = 20, stepsize = 0.1) {
get_densities_over_x <- seq(a, b, stepsize)
x <- dnorm(x, mean = mu, sd = sigma)
y return(tibble(x, y))
}
<- 100
plot_n tibble(
sigma = head(sqrt(r_sigmas_sqrd), n = plot_n),
mu = head(r_mus, n = plot_n)
%>%
) mutate(
grp = glue("mu = {round(mu, 2)}, sigma = {round(sigma, 2)}"),
dist_data = purrr::map2(mu, sigma, get_densities_over_x)
%>%
) unnest(dist_data) %>%
ggplot(aes(x = x, y = y)) +
geom_line(aes(group = grp), alpha = 0.25) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.02))) +
labs(
x = "windshield hardness",
y = "probability",
title = "Posterior distributions for the hardness of windshield"
)
The density of the posterior samples for \(\mu\) and \(\sigma\) is shown in the plot below.
<- tibble(
post_pred_df mu = r_mus, sigma = sqrt(r_sigmas_sqrd), y_tilde = y_tildes
)
%>%
post_pred_df ggplot(aes(x = mu, y = sigma)) +
geom_point(alpha = 0.5, size = 0.2) +
geom_density2d() +
scale_x_continuous(expand = expansion(mult = c(0.02, 0.02))) +
scale_y_continuous(expand = expansion(mult = c(0.02, 0.02))) +
labs(title = "Posterior samples for distribution parameters")
Finally, the following is the posterior predictive distribution.
%>%
post_pred_df add_column(grp = "y_tilde") %>%
bind_rows(tibble(y_tilde = windshieldy, grp = "y")) %>%
ggplot(aes(x = y_tilde)) +
geom_rug(aes(x = y), data = tibble(y = windshieldy)) +
geom_density(aes(color = grp)) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.02))) +
scale_color_brewer(type = "qual", palette = "Set1") +
labs(
x = "windshield hardness",
y = "density",
color = NULL,
title = "Posterior predicitive distribution"
)
Using the functions created above, I can address the specific tests provided with the question. It would be better to provide an analytic solution by integrating over the probability densities of \(\sigma\) and \(\mu\), but these sampling-based solutions are pretty close.
<- function(data, n) {
post_pred_sample <- random_mu(n, data = data)
mu <- sqrt(scaled_rinvchiq(n, data = data))
sigma ::map2_dbl(mu, sigma, ~ rnorm(1, mean = .x, sd = .y))
purrr
}
<- function(data, n = 1e6, iters = 5) {
mu_pred_point_est lapply(
seq(1, iters),
function(x) {
mean(post_pred_sample(data = data, n = n))
}%>%
) unlist() %>%
mean()
}
<- function(data, prob, n = 1e7) {
mu_pred_interval <- (1.0 - prob) / 2.0
lower_q <- 1.0 - lower_q
upper_q <- post_pred_sample(data = data, n = n)
post_pred_samples quantile(post_pred_samples, c(lower_q, upper_q))
}
stopifnot(close_to(
mu_pred_point_est(data = windshieldy_test), 14.5,
epsilon = 0.1
))stopifnot(all(close_to(
mu_pred_interval(data = windshieldy_test, prob = 0.95),
c(11.8, 17.2),
epsilon = 0.5
)))
mu_pred_point_est(data = windshieldy)
#> [1] 14.61259
mu_pred_interval(data = windshieldy, prob = 0.95)
#> 2.5% 97.5%
#> 11.61276 17.61140
22.3 Exercise 2. Inference for the difference between proportions
An experiment was performed to estimate the effect of beta-blockers on mortality of cardiac patients. A group of patients was randomly assigned to treatment and control groups: out of 674 patients receiving the control, 39 died, and out of 680 receiving the treatment, 22 died. Assume that the outcomes are independent and binomially distributed, with probabilities of death of \(p_0\) and \(p_1\) under the control and treatment, respectively. Set up a non-informative or weakly informative prior distribution on \((p_0,p_1)\).
a) Summarize the posterior distribution for the odds ratio, \(\frac{p_1 / (1-p_1)}{p_0 / (1-p_0)}\). Compute the point estimate, a posterior interval (95%), and plot the histogram.
priors: \(p_0, p_1 \sim \text{Beta}(2, 5)\)
<- seq(0, 1, 0.01)
x <- dbeta(x, 2, 5)
p plot_dist(
x = x,
y = p,
xlab = "proportion",
ylab = "probability",
main = "Prior distribution for p0, p1 ~ Beta(2, 5)"
)
likelihoods:
- \(y_0|p_0, n_0 \sim \text{Binomial}(p_0, n_0)\)
- \(y_1|p_1, n_1 \sim \text{Binomial}(p_1, n_1)\)
posteriors:
- \(p_0|y_0, n_0 \sim \text{Beta}(y_0 + 2, n_0 - y_0 + 5)\)
- \(p_1|y_1, n_1 \sim \text{Beta}(y_1 + 2, n_1 - y_1 + 5)\)
# Calculate the odds ratio given two values or vectors.
<- function(p0, p1) {
posterior_odds_ratio <- (p1 / (1 - p1)) / (p0 / (1 - p0))
or return(or)
}
# Calculate a point estimate (mean) for the OR.
<- function(p0, p1) {
posterior_odds_ratio_point_est return(mean(posterior_odds_ratio(p0, p1)))
}
# Find probability intervals for the posterior OR.
<- function(p0, p1, prob = 0.95) {
posterior_odds_ratio_interval <- (1.0 - prob) / 2.0
lower_q <- 1.0 - lower_q
upper_q <- posterior_odds_ratio(p0, p1)
or return(quantile(or, c(lower_q, upper_q)))
}
I can check my calculations and implementation using the test data provided with the exercise.
# test data
set.seed(4711)
<- rbeta(100000, 5, 95)
p0 <- rbeta(100000, 10, 90)
p1 stopifnot(close_to(
posterior_odds_ratio_point_est(p0 = p0, p1 = p1), 2.676,
epsilon = 0.01
))stopifnot(all(close_to(
posterior_odds_ratio_interval(p0 = p0, p1 = p1, prob = 0.9),
c(0.875, 6.059),
epsilon = 0.01
)))
Finally, I can address the question using the following values: \(n_0= 674\), \(y_0 = 39\), \(n_1=680\), and \(y_1 = 22\). I used a weakly informative prior \(\text{Beta}(2, 5)\) I first drew samples from the posteriors for \(p_0\) and \(p_1\) then used those to calculate the posterior for the odds ratio.
<- 674
n0 <- 39
y0 <- 680
n1 <- 22
y1
<- 2
prior_a <- 5
prior_b
<- function(prior_a, prior_b, y, n, draws = 1e6) {
sample_posterior_p <- prior_a + y
a <- prior_b + n - y
b return(rbeta(draws, a, b))
}
<- sample_posterior_p(prior_a, prior_b, y = y0, n = n0)
p0_post <- sample_posterior_p(prior_a, prior_b, y = y1, n = n1)
p1_post <- posterior_odds_ratio(p0_post, p1_post) post_or
The posterior distributions of \(p_0\) and \(p_1\) are shown below. From this plot, it appears that \(p_1\) is likely lower than \(p_0\) and we should expect an \(OR < 1\).
plot_two_distributions(p0_post, "p0", p1_post, "p1") +
labs(
x = "parameter value",
y = "density",
color = "parameter",
fill = "parameter"
)
The posterior distribution for the odds ratio is plotted below and it is shifted primarily to values less than 1.
plot_single_distribution(post_or) +
labs(x = "odds ratio", y = "density")
b) Discuss the sensitivity of your inference to your choice of prior density with a couple of sentences.
# Non-informative priors
<- 1
prior_a <- 1
prior_b
<- function(prior_a, prior_b, y, n, draws = 1e6) {
sample_posterior_p <- prior_a + y
a <- prior_b + n - y
b return(rbeta(draws, a, b))
}
<- sample_posterior_p(prior_a, prior_b, y = y0, n = n0)
p0_post_noinfo <- sample_posterior_p(prior_a, prior_b, y = y1, n = n1)
p1_post_noinfo <- posterior_odds_ratio(p0_post_noinfo, p1_post_noinfo)
post_or_noinfo
plot_single_distribution(post_or_noinfo)
plot_two_distributions(post_or, "weakly info.", post_or_noinfo, "non-info.") +
labs(x = "odds ratio", y = "density", color = "prior", fill = "prior")
The difference in influence on the posterior inference between a weakly informative prior \(\text{Beta}(2, 5)\) and non-informative, uniform prior \(\text{Beta}(1, 1)\) is negligible. The large number of data points overwhelms the prior’s influence.
This can be tested by experimenting with a very strong prior \(\text{Beta}(1000, 5)\), as shown below.
# Strong priors
<- 1000
prior_a <- 5
prior_b
<- function(prior_a, prior_b, y, n, draws = 1e6) {
sample_posterior_p <- prior_a + y
a <- prior_b + n - y
b return(rbeta(draws, a, b))
}
<- sample_posterior_p(prior_a, prior_b, y = y0, n = n0)
p0_post_strong <- sample_posterior_p(prior_a, prior_b, y = y1, n = n1)
p1_post_strong <- posterior_odds_ratio(p0_post_strong, p1_post_strong)
post_or_strong
plot_two_distributions(post_or, "weakly info.", post_or_strong, "strong") +
labs(x = "odds ratio", y = "density", color = "prior", fill = "prior")
22.4 Exercise 3. Inference for the difference between normal means
Consider a case where the same factory has two production lines for manufacturing car windshields. Independent samples from the two production lines were tested for hardness. We assume that the samples have unknown standard deviations \(\sigma_1\) and \(\sigma_2\). Use uninformative or weakly informative priors.
<- read_data("windshieldy1.txt") %>% as.numeric()
windshieldy1 <- read_data("windshieldy2.txt") %>% as.numeric() windshieldy2
a) What can you say about \(\mu_d = \mu_1 - \mu_2\)?
I calculated \(\mu_1\) and \(\mu_2\) as in Exercise 1. The posterior distributions for these parameters is plotted below along with the actual observed values along the x-axis.
<- seq(11.5, 17.5, 0.01)
mus <- purrr::map_dbl(mus, ~ mu_density(.x, windshieldy1))
mu1_dens <- purrr::map_dbl(mus, ~ mu_density(.x, windshieldy2)) mu2_dens
<- tibble(
data_tibble prod_line = c(rep("1", length(windshieldy1)), rep("2", length(windshieldy2))),
y = c(windshieldy1, windshieldy2)
)
tibble(
mu = c(mus, mus),
prob = c(mu1_dens, mu2_dens),
prod_line = c(rep("1", length(mu1_dens)), rep("2", length(mu2_dens)))
%>%
) ggplot() +
geom_line(aes(x = mu, y = prob, color = prod_line)) +
geom_rug(aes(x = y, color = prod_line), data = data_tibble, size = 1.2) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.02))) +
labs(x = "windshield hardness", y = "probability", color = "production line")
As before, I can sample from the posteriors of \(\mu_1\) and \(\mu_2\) and use these simulated values for further calculations, in this case \(\mu_d = \mu_1 - \mu_2\). The distribution for \(\mu_d\) is plotted below.
<- 1e6
n <- random_mu(n, data = windshieldy1)
mu1_draws <- random_mu(n, data = windshieldy2)
mu2_draws <- mu1_draws - mu2_draws
mud_draws
plot_single_distribution(mud_draws) +
theme(axis.title.x = ggtext::element_markdown()) +
labs(x = "µ<sub>d</sub>", y = "probability density")
Using the simulated posterior, I can get an estimate for the expected value of \(\mu_d\) as the mean of the posterior distribution.
glue(
"point estimate for µ_d as mean of posterior = {round(mean(mud_draws), 3)}"
)
#> point estimate for µ_d as mean of posterior = -1.21
I can also use the simulated values to get 95% CI of the posterior.
<- round(quantile(mud_draws, c(0.025, 0.975)), 3)
mud_95ci glue("95% CI of posterior for µ_d: {mud_95ci[[1]]}, {mud_95ci[[2]]}")
#> 95% CI of posterior for µ_d: -2.432, 0.012
Lastly, I can calculate the probability that \(\mu_1\) is less than \(\mu_2\), i.e. \(\mu_d < 0\).
<- mean(mud_draws < 0)
prob_mud_neg glue("Pr(µ_1 < µ_2) = {round(prob_mud_neg, 3)}")
#> Pr(µ_1 < µ_2) = 0.974
b) Given the model used, what is the probability that the means are exactly the same (\(\mu_1 = \mu_2\))? Explain your reasoning.
The probability that \(\mu_1 = \mu_2\) is technically 0 because we cannot compute the probability of individual values. Instead, we can define a region of practical equivalence (ROPE) that represents a range of values where we would say the values are effectively the same. Ideally, we would ask someone with domain expertise to devise a region based on the real-world meaning of the values, but, in this example with windshield hardness, that is not possible. Thus, I will define the ROPE to be 1% of the standard deviation of the observed hardness measurements: \(0.01 \times \sigma(y_1, y_2)\).
<- sd(c(windshieldy1, windshieldy2)) * 0.01
rope rope
#> [1] 0.01278151
<- mean((mud_draws > -rope) & (mud_draws < rope))
prob_rope glue("probability that µ_d is within the ROPE: {round(prob_rope, 3)}")
#> probability that µ_d is within the ROPE: 0.002
sessionInfo()
#> R version 4.1.2 (2021-11-01)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur 10.16
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices datasets utils methods base
#>
#> other attached packages:
#> [1] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4
#> [5] readr_2.0.1 tidyr_1.1.3 tibble_3.1.3 ggplot2_3.3.5
#> [9] tidyverse_1.3.1 glue_1.4.2
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.7 lubridate_1.7.10 here_1.0.1
#> [4] clisymbols_1.2.0 assertthat_0.2.1 rprojroot_2.0.2
#> [7] digest_0.6.27 utf8_1.2.2 R6_2.5.0
#> [10] cellranger_1.1.0 backports_1.2.1 reprex_2.0.1
#> [13] evaluate_0.14 httr_1.4.2 highr_0.9
#> [16] pillar_1.6.2 rlang_0.4.11 readxl_1.3.1
#> [19] rstudioapi_0.13 jquerylib_0.1.4 rmarkdown_2.10
#> [22] labeling_0.4.2 gridtext_0.1.4 munsell_0.5.0
#> [25] broom_0.7.9 compiler_4.1.2 modelr_0.1.8
#> [28] xfun_0.25 pkgconfig_2.0.3 htmltools_0.5.1.1
#> [31] ggtext_0.1.1 tidyselect_1.1.1 bookdown_0.24
#> [34] fansi_0.5.0 crayon_1.4.1 tzdb_0.1.2
#> [37] dbplyr_2.1.1 withr_2.4.2 MASS_7.3-54
#> [40] grid_4.1.2 jsonlite_1.7.2 gtable_0.3.0
#> [43] lifecycle_1.0.0 DBI_1.1.1 magrittr_2.0.1
#> [46] scales_1.1.1 cli_3.0.1 stringi_1.7.3
#> [49] farver_2.1.0 renv_0.14.0 LaplacesDemon_16.1.6
#> [52] fs_1.5.0 xml2_1.3.2 bslib_0.2.5.1
#> [55] ellipsis_0.3.2 generics_0.1.0 vctrs_0.3.8
#> [58] RColorBrewer_1.1-2 tools_4.1.2 markdown_1.1
#> [61] hms_1.1.0 parallel_4.1.2 yaml_2.2.1
#> [64] colorspace_2.0-2 isoband_0.2.5 rvest_1.0.1
#> [67] knitr_1.33 haven_2.4.3 sass_0.4.0