I am trying to decide between linear regression models using the Bayes Factor in Jaggs through R. For simplicity's sake, I am working on the mtcars dataset and the models are:
(1) mpg on only intercept (2) mpg on wt (3) mpg on disp (4) mpg on wt and disp
I am utilizing flat priors and try to implement the model comparison with the following code, but run into some problems and errors:
reg.bf.model <- "model{
M ~ dcat(model.p[])
model.p[1] <- 0.25
model.p[2] <- 0.25
model.p[3] <- 0.25
model.p[4] <- 0.25
# Likelihood
for (i in 1:n){
y[i] ~ dnorm(mu[i,M],tau[M])
mu[i,1] <- alpha[1]
mu[i,2] <- alpha[2] + beta1[1] * wt[i]
mu[i,3] <- alpha[3] + beta1[2] * disp[i]
mu[i,4] <- alpha[4] + beta1[3] * disp[i]+ beta2* wt[i]
}
for (m in 1:4){
alpha[m] ~ dnorm(0,0.0001)
sigma2[m] <- (1/tau[m])
}
for (j in 1:3) {
beta1[j] ~ dnorm(0,0.0001)
}
beta2 ~ dnorm(0,0.0001)
tau[1] ~ dgamma(prior.shape1[M],prior.rate1[M])
tau[2] ~ dgamma(prior.shape2[M],prior.rate2[M])
tau[3] ~ dgamma(prior.shape3[M],prior.rate3[M])
tau[4] ~ dgamma(prior.shape4[M],prior.rate4[M])
}"
jags.data <- list(y=mtcars$mpg,n=nrow(mtcars),
wt = mtcars$wt, disp = mtcars$disp,
prior.shape1=c(0.0001,1),prior.rate1=c(0.0001,1),
prior.shape2=c(1,0.0001),prior.rate2=c(1,0.0001),
prior.shape3=c(1,0.0001),prior.rate3=c(1,0.0001),
prior.shape4=c(1,0.0001),prior.rate4=c(1,0.0001))
jags.bf <- jags.model(file=textConnection(reg.bf.model),
# inits=jags.inits,
data=jags.data, n.chains=3)
update(jags.bf, 100)
jags.bf.out <- coda.samples(jags.bf,
variable.names=c("alpha","beta1","beta2"
,"sigma2","M"),
n.iter=50000, thin=400)
But I get the following error messages:
"Error in update.jags(jags.bf, 100) : LOGIC ERROR:
SimpleRange::leftOffset. Index outside of allowed range
Please send a bug report to martyn_plummer@users.sourceenter code here
forge.net"
and
"Error in jags.samples(model, variable.names, n.iter, thin, type = "trace", : No valid monitors set In addition: Warning messages: 1: In FUN(X[[i]], ...) : Can't set monitor. No model!
2: In FUN(X[[i]], ...) : Can't set monitor. No model!
3: In FUN(X[[i]], ...) : Can't set monitor. No model!
4: In FUN(X[[i]], ...) : Can't set monitor. No model!
5: In FUN(X[[i]], ...) : Can't set monitor. No model!"
Any ideas or nudges in the right direction are greatly appreciated!