通过jags的R中的贝叶斯ANCOVA

时间:2014-03-31 18:05:31

标签: r jags

我正在尝试使用JAGS实现考虑到R中的异方差性的贝叶斯ANCOVA。然而,尽管经历了贝叶斯简单回归和ANOVA的几个教程,但我无法理解如何为JAGS准备文件。到目前为止,这是我的代码:

y1     = rexp(57, rate=0.8)   # dependent variable
x1     = hist(rbeta(57, 6, 2)) # continuous factor
x2     = rep(c(1, 2), 57/2)   # categorical factor
groups = 2
n      = 57
# list of variables
lddados <- list(g=groups, n=length(x), y=y, x1=x1, x2=x2)

sink('reglin.txt') # nome do arquivo aqui
cat('
    # model
    {
      for(i in 1:n){
        mu[i] = a0 + a[i] 
        y[i]  = a0 + x1*a[ x2[i] ] + ε[i]
      }

      priors
      y ~  dgamma(0.001,0.01)
      for(i in 1:n){
        inter[i] ~  dgamma(0.001,0.001)
        coef[i]  ~  dnorm(0.0,1.0E-

        likelihood
        got stuck...
      }
    }#------fim do modelo
')
sink()

1 个答案:

答案 0 :(得分:0)

我目前正在尝试使用rjags来安排ANCOVA ...

根据我的理解,我会测试这个(未经测试);

require(rjags)
require(coda)

model_string <- "
  model {
    for ( i in 1:n ){
      mu[i] <- a0 + a[x2[i]] + a3 * x1[i] # linear predictor
      y[i] ~ dnorm(mu[i], prec) # y is norm. dist.
   }

 #  priors
    a0 ~ dnorm(0, 1.0E-6) # intercept
    a[1] ~ dnorm(0, 1.0E-6) # effect of x1 at x2 level 1
    a[2] ~ dnorm(0, 1.0E-6) # effect of x1 at x2 level 2
    a3 ~ dnorm(0, 1.0E-6) # regression coefficient for x1 (covariate)
   prec ~ dgamma(0.001, 0.001) # precision (inverse of variance)

 }
"

# initial values for the mcmc 
inits_list <- list(a=0, b=c(0,0), prec=100)
# model, initial values and data in right format
jags_model <- jags.model(textConnection(model_string), data=data, inits=inits_list, n.adapt   = 500, n.chains = 3, quiet = T)
# burn-in
update(jags_model, 10000)
# run the mcmc chains using the coda package 
mcmc_samples <- coda.samples(jags_model, c("mu", "a", "a1", "a2", "prec"), n.iter = 100000)

告诉我它是否有效......

推荐书籍;麦卡锡M.贝叶斯生态学方法和Kruschke JK。做贝叶斯数据分析