绘制R中的后验分布

时间:2017-07-29 07:33:09

标签: r ggplot2

我想用共轭先验

计算后验密度图

我有已知参数的数据(平均值= 30,sd = 10)

我有两个先验,一个具有已知参数的正态分布(均值= 10,sd = 5),另一个具有相同均值和sd的t分布,但是自由度4

我想要一张带有先前,数据和后验的密度图的图表?

你可以帮我解决这个问题的代码吗?

另外,在我看来,我的后验密度函数错误。到目前为止,这是我的代码

x=seq(from=-90, to=90, by= 1)
data=dnorm(x,mean=30,sd =10)
prior = dnorm(x,mean=10,sd =5)
posterior = dnorm(x,mean=10,sd =5)*dnorm(x,mean=30,sd =10) # prior* data  #Prior*data

plot(x,data , type="l", col="blue")
lines(x,prior, type="l", col="red")
lines(x,posterior , type="l", col="green")

1 个答案:

答案 0 :(得分:1)

您需要将两个分布相加而不是相乘。我附上一个在两个发行版之间使用相同权重的示例:

x <- seq(from = -90, to = 90, by = 1)
data <- dnorm(x, mean = 30, sd = 10)
prior <- dnorm(x, mean = 10, sd = 5)
posterior <- 0.5 * dnorm(x, mean = 10, sd = 5) + 0.5 * dnorm(x, mean = 30, sd = 10)

plot(x, prior, type = "l", col = "red")
lines(x, posterior, type = "l", col = "green")
lines(x, data , type = "l", col = "blue")

enter image description here

相关问题