Get startedGet started for free

Posterior of proportions

Suppose that in the clinical trial, 12 out of 20 patients with the standard drug experience relief, and 17 out of 20 patients with the new drug get relief.

Here the posterior distributions for the two proportions will be independent where pS is \(beta(4.91 + 12, 3.38 + 8)\) and pN is \(beta(4.91 + 17, 3.38 + 3)\).

Note that in each case, we add the number of successes (those with relief) and number of failures (those who did not get relief) to the prior shape parameters to get the posterior beta parameters.

A simulated sample of 1000 from this posterior of (pS, pN) is displayed in the figure to the right.

Now it's your turn to explore the posterior density of the two proportions!

Remember: the prior assumes that pS, pN are independent with each assigned a uniform (i.e. \(beta(1, 1)\)) prior.

This exercise is part of the course

Beginning Bayes in R

View Course

Exercise instructions

A few vectors have been made available in your workspace. s1f1 and s2f2 represent the number of successes and number of failures for each drug. pS_prior and pN_prior represent the beta shape parameters for the uniform priors for the proportions.

  • Compute the vectors of beta shape parameters of the posteriors for pS and pN. Store the results as pS_shape and pN_shape, respectively.
  • Using rbeta(), simulate 1000 values from the posterior of (pS, pN).
  • Construct a scatterplot of the posterior distribution by passing sim_pS and sim_pN to plot().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define the number of successes and number of failures: s1f1, s2f2
s1f1 <- c(12, 8)
s2f2 <- c(17, 3)

# Find the prior beta shape parameters for pS and pN:
pS_prior <- c(1, 1)
pN_prior <- c(1, 1)

# Find the posterior beta shape parameters for pS: pS_shape


# Find the posterior beta shape parameters for pN: pN_shape


# Simulate 1000 draws from the posterior: sim_pS, sim_pN
sim_pS <- ___
sim_pN <- ___

# Construct a scatterplot of the posterior
Edit and Run Code