簡體   English   中英

R中通過貝葉斯的貝葉斯ANCOVA

[英]Bayesian ANCOVA in R via 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()

我目前正在嘗試使用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.貝葉斯生態學方法和克魯克JK。 做貝葉斯數據分析

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM