贝叶斯简单线性回归Gibbs采样与伽马先验

时间:2015-12-17 08:23:39

标签: r

请帮帮我。

我正在Gibbs内部进行Metopolis_hasting以产生具有静态分布的Markov Chian,其等于观察到的y的(β,phi)的联合条件分布。其中y的模型是简单线性回归,phi是1 / sigma ^ 2。 phi的完整条件分布是gamma(shape = shape_0 + n / 2,rate = rate_0 + 0.5 * sum((y $ yb [1] -b [1] * y $ x)^ 2))其中shape_0和rate_0是phi的先验分布(跟随γ)

这是我的代码:

y <- read.table("...",header = T)
n <- 50
shape_0 <- 10
rate_0 <- 25


  shape <- shape_0+n/2

   mcmc <- function (n = 10){
       X <- matrix(0,n,3)
       b <- c(5,2)
       phi <- 0.2 
       X[1,] <- c(b,phi)
       count1 <- 0
       count2 <- 0
      for (i in 2:n){

      phi_new <- rnorm(1,phi,1) #generate new phi candidate 
      rate <- rate_0 + 0.5*sum((y$y-b[1]-b[1]*y$x)^2)

      prob1 <- min(dgamma(phi_new,shape = shape, 
       rate =   rate)/dgamma(phi,shape = shape, rate = rate),1)
      ##here is where I run into trouble, dgamma(phi_new,shape = shape, 
       ##rate =   rate) 
       ##and dgamma(phi,shape = shape, rate = rate) both gives 0

     u <- runif(1)
      if (prob1>u)
      {X[i,3] <- phi_new; count1=count1+1}
        else  {X[i,3] <-phi}

       phi <- X[i,3]

 ....}

我知道我应该对precision参数使用log变换,但我不确定如何去做。 log(dgamma(phi_new,shape = shape,rate = rate))将返回-inf。

非常感谢您的帮助。

0 个答案:

没有答案