简体   繁体   中英

Run simulation studies in R2WinBUGS for n datasets

I am having issues figuring out how to run some simulation studies using R2WinBUGS. The aim is to simulate n datasets (aiming for 1000, but starting with 10), and put them all into the R2WinBUGS code as a matrix so that when it ports over to WinBUGS, it will run the produce the estimates for the n datasets. Here is what I have currently have:

The Model:

model{
      alpha0 ~ dnorm(66.6, 0.01)
      alpha1 ~ dnorm(0.3, 0.01)
      alpha2 ~ dnorm(100, 0.01)
      alpha3 ~ dnorm(0.2, 0.01)
      beta0 ~ dnorm(35, 0.01)
      beta1 ~ dnorm(80, 0.01)
      tau ~ dgamma(0.3,1)

    for(k in  1:Ndat) {
      y[k] ~ dnorm(mu[k], tau)
      mu[k] <- ((alpha0/(1 + exp(-alpha1*(28-beta0)))) + (alpha2/(1 + exp(-alpha3*(28-beta1)))))
    }
}

The bugs code I use is:

grapedat.sim = bugs(data = list('Ndat' = Ndat, 'y' = p.y[,1]),inits,
                model.file="H:/R coding/R2WinBUGS/multsimt1.bug",
             parameters=c("alpha0","alpha1","alpha2","alpha3","beta0","beta1","tau"),
                n.chains=1,n.iter=8000,n.sim = 6000, 
n.burnin=2000,n.thin=1,
                bugs.directory="H:/WinBUGS14",
                codaPkg=FALSE,
                debug = T)

where Ndat is the number of datasets, py is a 13 xn matrix, and the inits are:

inits <- function(){
  list(alpha0=70, alpha1=0.4, tau=0.15, alpha2=105, alpha3=0.25,beta0 = 
    40, beta1 = 85)
  }

Any help?

In theory you can do this in a single BUGS model by having a second (outer) loop and then indexing y and mu as matrices, and your coefficients as vectors eg (untested):

model{
    for(d in 1:n){
      alpha0[d] ~ dnorm(66.6, 0.01)
      alpha1[d] ~ dnorm(0.3, 0.01)
      alpha2[d] ~ dnorm(100, 0.01)
      alpha3[d] ~ dnorm(0.2, 0.01)
      beta0[d] ~ dnorm(35, 0.01)
      beta1[d] ~ dnorm(80, 0.01)
      tau[d] ~ dgamma(0.3,1)

      for(k in  1:Ndat) {
        y[k,d] ~ dnorm(mu[k,d], tau[d])
        mu[k,d] <- ((alpha0[d]/(1 + exp(-alpha1[d]*(28-beta0[d])))) +
                    (alpha2[d]/(1 + exp(-alpha3[d]*(28-beta1[d])))))
      }
    }
}

This is doable for n=10 but will be very unwieldy for n=1000 so is generally not a good solution to your problem. Instead I would run each dataset/model independently, with the loop within R. If you are open to using JAGS instead of WinBUGS then you could check out the semi-automated solution for this in the runjags package - ie read section 2.9 of this article: https://www.jstatsoft.org/article/view/v071i09

Matt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM