【问题标题】:Coding a posterior distribution in R [closed]在 R 中编码后验分布
【发布时间】:2013-12-07 10:38:20
【问题描述】:

这可能是一个荒谬的问题,但我对 R 非常陌生(从 3 周前开始),但我正在运行 Gibbs 采样器并且我正在从非共轭分布中绘图。它设置为 Yi|mu ~ N(1,4^2)、mu~N(0,1) 和 sig^2~IG(2,1)。我已经对采样部分进行了编码,但是在对后验分布进行编码以创建要从中采样的数据时遇到了麻烦。到目前为止我所拥有的是:

dev.new() #####Posterior predictive density ( ppd[1:lx] )for data on the grid x (new line)
#
lx = 200 (new line)
x = seq( min(yy) - .1*(max(yy) - min(yy)), 
     max(yy) + .1*(max(yy) - min(yy)), len = lx )

dev.new()
hist( yy, prob=T )

ppd = rep( 0, lx )

for( ii in 1:lx )
{
    ##### enter the code here, 
    ### ppd[ ii ] = mean( dnorm( .....
 }

 lines( x, ppd, col=2, lwd=2 )

【问题讨论】:

    标签: r bayesian


    【解决方案1】:

    我不知道 Gibbs Sampler 是什么,只是搜索 google:
    似乎分发的代码可能是:

    gibbs<-function (n, rho) 
    {
            mat <- matrix(ncol = 2, nrow = n)
            x <- 0
            y <- 0
            mat[1, ] <- c(x, y)
            for (i in 2:n) {
                    x <- rnorm(1, rho * y, sqrt(1 - rho^2))
                    y <- rnorm(1, rho * x, sqrt(1 - rho^2))
                    mat[i, ] <- c(x, y)
            }
            mat
    }
    

    我认为来自同一页面的here 会找到你想要的完整代码 R。 在另一个page 中,您可能会找到更多解释和示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多