通过jags在R的Bayes ANCOVA

通过jags在R的Bayes ANCOVA,r,jags,R,Jags,我正在尝试使用JAGS实现一个贝叶斯ANCOVA,它考虑了R中的异方差性。然而,尽管已经阅读了几篇贝叶斯简单回归和方差分析的教程,我还是不明白如何为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 gro

我正在尝试使用JAGS实现一个贝叶斯ANCOVA,它考虑了R中的异方差性。然而,尽管已经阅读了几篇贝叶斯简单回归和方差分析的教程,我还是不明白如何为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()
y1=rexp(57,比率=0.8)#因变量
x1=历史(rbeta(57,6,2))#连续因子
x2=代表(c(1,2),57/2)#分类因子
组=2
n=57
#变量列表

lddados我目前正在自己用RJAG试用ANCOVA

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

require(rjags)
要求(尾声)

你是不是想用R来运行JAG?在这种情况下,看看R2jags或rjags包可能会有所帮助。我还建议使用
runjags
包。它对我来说似乎更方便,更重要的是,它可以并行运行。谢谢你的建议。然而,我的问题是编写贝叶斯ancova模型,而不是使用包。我已经对R2JAG有了一定的了解,在这些软件包的教程或互联网上的其他教程中都找不到如何编写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)