Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
This assignment is related to Lecture 2 and BDA3 Chapters 1 and 2. You may find an additional discussion about choosing priors in a blog post by Andrew Gelman.
Reading instructions:
Algae status is monitored in 274 sites at Finnish lakes and rivers. The observations for the 2008 algae status at each site are presented in the dataset algae
in the aaltobda
package (‘0’: no algae, ‘1’: algae present).
Loading the library and the data.
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Let \(\pi\) be the probability of a monitoring site having detectable blue-green algae levels and \(y\) the observations in algae
. Use a binomial model for the observations \(y\) and a \(Beta(2,10)\) prior for binomial model parameter \(\pi\) to formulate a Bayesian model. Here it is not necessary to derive the posterior distribution for \(\pi\) as it has already been done in the book and it suffices to refer to that derivation. Also, it is not necessary to write out the distributions; it is sufficient to use label-parameter format, e.g. \(Beta(\alpha,\beta)\).
Your task is to perform Bayesian inference for a binomial model and answer questions based on it.
Keep the below name and format for the functions to work with markmyassignment
:
Keep the below name and format for the function to work with markmyassignment
:
Make prior sensitivity analysis by testing a couple of different reasonable priors and plot the different posteriors. Try to summarise the results by one or two sentences.
Amend the code below to help you.
# Beta distribution density function
beta_density <- function(x, shape1, shape2) {
dbeta(x, shape1, shape2)
}
# Binomial likelihood density function
binomial_likelihood <- function(x, n, k) {
choose(n, k) * x^k * (1 - x)^(n - k)
}
# Parameters for the Beta prior distributions
priors <- list(c(#alpha, #beta), c(#alpha, #beta), c(#alpha, #beta)) # Define three different priors here priors, (alpha, beta)
# Parameters for the binomial likelihood (number of trials and successes)
n <- # number of trials
k <- # number of successes
# Create a sequence of x values from 0 to 1
x <- seq(0, 1, length.out = 100)
# Create a data frame with densities
densities_list <- lapply(priors, function(prior) {
alpha_prior <- prior[1]
beta_prior <- prior[2]
alpha_posterior <- alpha_prior + k
beta_posterior <- beta_prior + n - k
data.frame(
x = rep(x, 2),
density = c(beta_density(x, alpha_prior, beta_prior),
beta_density(x, alpha_posterior, beta_posterior)),
distribution = factor(rep(c("Prior", "Posterior"), each = length(x))),
prior = paste("Prior: Alpha =", alpha_prior, "Beta =", beta_prior)
)
})
df <- bind_rows(densities_list)
# Plot the densities using ggplot2
ggplot(df, aes(x = x, y = density, color = distribution)) +
geom_line(size = 1) +
facet_wrap(~ prior, scales = "fixed", nrow = 3) +
labs(title = "Prior and Posterior Densities for Different Priors",
x = TeX("$\\theta$"),
y = "Density",
color = "Distribution") +
theme_minimal()