Updating by Bayes' rule
Suppose Madison constructs a prior for proportion of female college students who believe they are overweight, where the possible values are contained in P
and the respective probabilities in Prior
:
P <- c(0.5, 0.6, 0.7, 0.8, 0.9)
Prior <- c(0.3, 0.3, 0.2, 0.1, 0.1)
Imagine that a sample of n = 20
female students is selected and y = 16
of these students consider themselves to be overweight. The likelihood of 16 so-called "successes" in a sample of size 20 is given by the binomial density function:
dbinom(16, size = 20, prob = P)
where P
is the binomial probability of success.
Now you can compute Madison's posterior probabilities for the unknown proportion P
!
The vector of proportions P
and the vector of probabilities Prior
are both available in your workspace.
Remember: you can use the bayesian_crank()
function to implement Bayes' rule!
This exercise is part of the course
Beginning Bayes in R
Exercise instructions
- Use the
dbinom()
function to compute the vector of likelihoods. Store the result inLikelihood
. - Create a Bayesian data frame called
bayes_df
with variablesP
,Prior
, andLikelihood
. - Use the
bayesian_crank()
function to compute the posterior probabilities. Save the result inbayes_df
and print it to the console. - Pass
bayes_df
to theprior_post_plot()
function to graph the prior and posterior probabilities contained inbayes_df
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define the values of the proportion: P
P <- c(0.5, 0.6, 0.7, 0.8, 0.9)
# Define Madison's prior: Prior
Prior <- c(0.3, 0.3, 0.2, 0.1, 0.1)
# Compute the likelihoods: Likelihood
# Create Bayes data frame: bayes_df
# Compute and print the posterior probabilities: bayes_df
# Graphically compare the prior and posterior