开始使用免费开始使用

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().

本练习是课程的一部分

Bayesian Modeling with RJAGS

查看课程

练习说明

  • 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.

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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()
编辑并运行代码