I am currently working on understanding Bayes regression models using JAGS. I have a data frame that look something like
#sample data frame
group <- rep(1:3, each = 4)
density <- rnorm(12, 1, 1)
diet <- round(rPareto(12,1,1))
dat <- as.data.frame(cbind(group,density, diet))
I want to run a Bayes regression model density vs. diet of each group. A predictor variable is diet and a response variable is density. My goal is to compare beta coefficients between groups. However, I am struggled with this issue. I filtered data from each group and run the Bayes regression model separately. This path worked when there was a small number of groups, but will not be a practical way to do when there are many groups.
I first filtered the data by group, scaled the data frame and created a list format that jags wants
dat <- dat %>% filter(group == 1)
scale_dat<-as.data.frame(sapply(dat[,c(1:3)],scale))
dataForJags <- list(den=scale_dat$density, diet=scale_dat$diet, N=length(scale_dat$den))
I then specify model and prior information
model<-"model{
for(i in 1:N){
den[i] ~ dnorm(mu[i], tau)
# identity
mu[i] <- int + beta1*diet[i]
}
tau ~ dgamma(0.1,0.1)
int ~ dnorm(0, 0.001)
beta1 ~ dnorm(0, 0.001)
}"
mod1 <- jags.model(textConnection(model),data= dataForJags,n.chains=2)
samples<-jags.samples(model= mod1,variable.names=c("beta1", "int","mu","tau"),n.iter=100000)
From here I was able to get beta coefficient for one group, but couldn't find an appropriate way to run the Bayes regression to all levels of group at once. Does anyone know how to do this?