When you get error running your BUGS model from R, one option is to try a mock run of the model in OpenBUGS or WinBUGS itself. It can help you (via the cursor placement after you hit check model button) to locate problematic lines.
I did this with your BUGS model. I found problems in the definition of mn
, prec
and R
in the BUGS model. You can drop these as they are already defined in the data (which, looks like the appropriate place to define them). Once I dropped these from your BUGS model everything ran fine.
Note, to run a model in OpenBUGS you have to edit the format of your data, for example the script I ran was:
model{
#likelihood
for(j in 1 : Nf){
p1[j, 1:2 ] ~ dmnorm(gamma[1:2], T[1:2 ,1:2])
for (i in 1:2){
logit(p[j,i]) <- p1[j,i]
Y[j,i] ~ dbin(p[j,i],n)
}
}
#priors
gamma[1:2] ~ dmnorm(mn[1:2],prec[1:2 ,1:2])
expit[1] <- exp(gamma[1])/(1+exp(gamma[1]))
expit[2] <- exp(gamma[2])/(1+exp(gamma[2]))
T[1:2 ,1:2] ~ dwish(R[1:2 ,1:2], 2)
sigma2[1:2, 1:2] <- inverse(T[,])
rho <- sigma2[1,2]/sqrt(sigma2[1,1]*sigma2[2,2])
}
#data
list(Y=structure(.Data=c(1,11,6,1,8,5,1,25,13,1,1,13,1,8,22),.Dim=c(5,3)),
Nf=5, n=60, mn=c(-1.59,-2.44),
prec=structure(.Data=c(0.0001,0,0,0.0001),.Dim=c(2,2)),
R=structure(.Data=c(0.001,0,0,0.001),.Dim=c(2,2)))
#inits
list(gamma=c(0,0), T=structure(.Data=c(0.9,0,0,0.9),.Dim=c(2,2)))
where the data and inits need a bit work to convert from your R script.
A couple of other points: 1) I am not sure you have the right format for Y as it has 3 columns, your distribution only considers the first two (X and Y1). 2) you had an unnecessary set of curly brackets in the likelihood.
To run the code in BUGS via R you can use the following R syntax...
#BUGS code as a character string
bugs1<-
"model{
#likelihood
for(j in 1 : Nf){
p1[j, 1:2 ] ~ dmnorm(gamma[1:2], T[1:2 ,1:2])
for (i in 1:2){
logit(p[j,i]) <- p1[j,i]
Y[j,i] ~ dbin(p[j,i],n)
}
}
#priors
gamma[1:2] ~ dmnorm(mn[1:2],prec[1:2 ,1:2])
expit[1] <- exp(gamma[1])/(1+exp(gamma[1]))
expit[2] <- exp(gamma[2])/(1+exp(gamma[2]))
T[1:2 ,1:2] ~ dwish(R[1:2 ,1:2], 2)
sigma2[1:2, 1:2] <- inverse(T[,])
rho <- sigma2[1,2]/sqrt(sigma2[1,1]*sigma2[2,2])
}"
#write the BUGS code to a txt file in current working directory
writeLines(bugs1, "bugs1.txt")
#create data
Y<-data.frame(X=1,Y1=c(11,8,25,1,8),Y2=c(6,5,13,13,22))
#run BUGS from R
library("R2OpenBUGS")
mcmc1 <- bugs(data = list(Y=as.matrix(Y), Nf=5, n=60, mn=c(-1.59, -2.44),
prec=matrix(c(.0001,0,0,.0001),nrow=2,ncol=2),
R=matrix(c(.001,0,0,.001),nrow=2,ncol=2)),
inits = list(list(gamma=c(0,0), T=matrix(c(0.9,0,0,0.9),nrow=2,ncol=2))),
param = c("gamma", "sigma2"),
model = "bugs1.txt",
n.iter = 11000, n.burnin = 1000, n.chains = 1)
A couple of points to note here. 1) This uses OpenBUGS not WinBUGS. 2) If you use R2WinBUGS you might hit a trap if you are not running R (or Rstudio, or whatever you are using) as an administrator.
To run the above code a 1000 times you could put it within a loop, something like....
#create and write the BUGS code to a txt file in current working directory (outside the loop)
bugs1<-...
#loop
for(i in 1:1000){
Y <- read.csv(file=paste0("MVN",i,".csv"))
#run BUGS from R
library("R2OpenBUGS")
mcmc1 <- bugs(data = list(Y=as.matrix(Y), Nf=5, n=60, mn=c(-1.59, -2.44),
prec=matrix(c(.0001,0,0,.0001),nrow=2,ncol=2),
R=matrix(c(.001,0,0,.001),nrow=2,ncol=2)),
inits = list(list(gamma=c(0,0), T=matrix(c(0.9,0,0,0.9),nrow=2,ncol=2))),
param = c("gamma", "sigma2"),
model = "bugs1.txt",
n.iter = 11000, n.burnin = 1000, n.chains = 1)
#save mcmc
write.csv(mcmc1$sims.matrix,paste0("mcmc",i,".csv"))
}