2013-11-22 50 views
-1

这可能是一个荒谬的问题,但我对R(3周前开始)非常陌生,但我运行的是Gibbs采样器,并且我从非共轭分布中绘制。它被设置为Yi | mu〜N(1,4^2),mu〜N(0,1)和sig^2〜IG(2,1)。我有采样部分编码,但我无法编码后验分布以创建要采样的数据。我至今是:在R中编码后验分布

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) 

回答

1

我不知道是什么吉布斯采样器,但只是在寻找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你可能会发现一些更多的解释和例子