在R中编码后验分布
这可能是一个荒谬的问题,但我对R非常陌生(从3周前开始),但我正在运行一个吉布斯取样器,我从一个非共轭分布绘制。它被设置为Yi | mu~N(1,4^2)、mu~N(0,1)和sig^2~IG(2,1)。我已经对采样部分进行了编码,但是我在编码后验分布以创建要采样的数据时遇到了问题。到目前为止,我得到的是:在R中编码后验分布,r,bayesian,R,Bayesian,这可能是一个荒谬的问题,但我对R非常陌生(从3周前开始),但我正在运行一个吉布斯取样器,我从一个非共轭分布绘制。它被设置为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
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 )
我不知道吉布斯取样器是什么,只是在搜索:
似乎发行版的代码可能是:
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
}
吉布斯