Help Plotting the Likelihood Function, Prior Distribution, and Posterior Distribution

Hi all,

So for my problem, there are n = 10,000 people involved in a clinical trial. Each has probability \theta_p of getting a virus after receiving a vaccine. We are given that \theta_p \in (0,1).

I've determined my likelihood function to be Y \sim Binomial(n, \theta_p), and my prior to be
Beta(\alpha, \beta), thus giving me a posterior distribution equal to Beta(A,B) where A = Y + \alpha, and B = n - Y + \beta.

I want to plot all these on a single graph. Here is what I have tried:

# Creating likelihood function
n <- 10000
success <- 0:n
likelihood <- dbinom(success, size = n, prob = .5) # p = 0.5 chosen WLOG

# Creating the prior distribution
p <- seq(0,1, length = n)
alpha <- 1 # shape parameters alpha and beta  chosen arbitrarily to be equal to 1 
beta <- 1
prior <- dbeta(success, shape1 = alpha, shape2 = beta)

# Creating the posterior distribution
Y <- 7000 # chosen WLOG
posterior <- dbeta(success, shape1 = (Y + alpha), shape2 = (n - Y + beta))

# Making the plots
plot(success, likelihood , type = "l",  col = "blue")
lines(success, prior, type = "l",  col = "red")
lines(success, posterior, type = "l",  col = "green")

However, when I plot these functions, I'm getting this as an error:

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

Any help fixing this issue and getting my plots to work would be greatly appreciated. Thank you!

I am not getting that error but you are passing bad values to dbeta. The beta distribution only has support over [0, 1]. If you set \theta to 0.5, then your prior is a constant, right? The prior is p(\theta).

2 Likes

The first argument to dbeta has to be in the interval [0,1].

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.