Get startedGet started for free

Simulating a Beta prior

Suppose you're running in an election for public office. Let \(p\) be your underlying support, the proportion of voters that plan to vote for you. Based on past polls, your prior model of \(p\) is captured by a Beta distribution with shape parameters 45 and 55.

You will approximate the Beta(45, 55) prior using random samples from the rbeta() function. This function takes three arguments: sample size (n) and two shape parameters (shape1,shape2). Subsequently, you will construct a density plot of the samples using ggplot(). This function takes two arguments: the data set containing the samples and, within aes(), the variable to be plotted on the x axis. The density plot layer is added using geom_density().

This exercise is part of the course

Bayesian Modeling with RJAGS

View Course

Exercise instructions

  • Use rbeta() to sample 10,000 draws from Beta(45, 55). Assign the output to prior_A.
  • The prior_sim data frame includes the prior_A sample. Apply ggplot() to prior_sim to construct a density plot of the prior samples.

Hands-on interactive exercise

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

# Sample 10000 draws from Beta(45,55) prior
prior_A <- rbeta(n = ___, shape1 = ___, shape2 = ___)

# Store the results in a data frame
prior_sim <- data.frame(prior_A)

# Construct a density plot of the prior sample
ggplot(prior_sim, aes(x = ___)) + 
    geom_density()
Edit and Run Code