I've implement a model in OpenBUGs from the following data:
The data consists of 10 independent studies - 5 trials of 2 different medications (Med = 0 or 1) then a total of infected participants for that trial (infected) - and a Total of participants in the trial.
bugsData(list(y = df$Infected, n = 10, J = 5 , t = df$Total, x = df$Med) , file="Bugs_Data.txt")
My original model can written as:
Y[i]|mu[i],t[i] ~ Bin(mu[i], t[i]), i = 1,.....10
logit(mu[i]) = Beta0 + Beta1*x[i]
Which I'm able to implement in OpenBUGS like so:
model {
## LIKELIHOOD
for (i in 1:n) {
y[i] ~ dbin(mu[i], t[i])
logit(mu[i]) <- Beta0 + Beta1*(x[i])
}
## NORMAL PRIORS
Beta0 ~ dnorm(0, 0.0001)
Beta1 ~ dnorm(0, 0.0001)
}
However I then would like to implement a hierarchical model, in which the s(i) denotes the trial from column trial where observation i was taken:
Y[i]|mu[i],t[i] ~ Bin(mu[i], t[i]), i = 1,.....10
logit(mu[i]) = Beta0,s(i) + Beta1*x[i]
Beta0,j ~ Normal(mu_bo, sigma2_b0), j = 1,....5
I've attempted a model to the best of my ability with no success as of yet.
model {
## LIKELIHOOD
for (j in 1:J) {
for (i in 1:n) {
y[i] ~ dbin(mu[i], t[i])
logit(mu[i]) <- Beta0[j] + Beta1*(x[i])
Beta0[j] ~ dnorm(mu_b0, tau_b0)
}
}
## PRIORS
mu_b0 ~ dnorm(0, 0.0001)
tau_b0 ~ dgamma(0.0001, 0.0001)
sigma2_b0 <- 1 / tau_b0
Beta1 ~ dnorm(0, 0.0001)
}
The current model isn't able to compile with my data as is.
You need to supply a vector of length
n
which indexes what group data pointi
belongs to.group_vec
is of lengthn
, and it takes an integer of which group data pointi
belongs to. So, for example, if we had 21 datapoints and 3 groups.then we could construct
group_vec
asThis is called nested indexing, and it's a really common way to specify hierarchical models, you just supply it along with your data. In your case, you already have trial as an integer. So you could either make
group_vec
be the trial column in your data, or renamegroup_vec
in the model to beTrial
, whichever you prefer.