在 R 中绘制似然函数、先验分布和后验分布

时间:2021-02-27 01:45:17

标签: r plot bayesian

所以对于我的问题,有 n=10,000 人参与了临床试验。每个人在接种疫苗后都有感染病毒的概率 θp。我们得到 θp ∈ (0,1) .

我已经确定我的似然函数是 Y∼Binomial(n,θp) ,而我的先验是

Beta(α,β) ,从而给我一个等于 Beta(A,B) 的后验分布,其中 A=Y+α 和 B=n−Y+β。

我想在一个图表上绘制所有这些。这是我尝试过的:

# 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, data , type = "l",  col = "blue")
lines(success, prior, type = "l",  col = "red")
lines(success, posterior, type = "l",  col = "green")

但是,当我绘制这些函数时,我将其视为错误:

<块引用>

xy.coords(x, y, xlabel, ylabel, log) 中的错误:'x' 和 'y' 长度不同

任何帮助解决这个问题并使我的情节工作将不胜感激。谢谢!我还是 R 的新手,绘图是我仍然熟悉的东西。我以为我通过确保它们仍然在我标记为“成功”的域上绘制而使一切正常,但我想不是吗?我只是不确定错误来自哪里。再次感谢!

0 个答案:

没有答案