2

I have a question related to the R Code which calls BUGS. I have run the model in WinBUGS and it runs fine giving me the expected results. Below is the automation code used when I had single outcome or univariate data for Y’s. Now I want to use it for multiple outcomes. I tried a different way of reading the data. There are 2 simulations for testing which are read from csv files.Not sure where to specify in the code so that the same process can be repeated for 2 outcomes instead of one. setwd("C://Tina/USB_Backup_042213/Testing/CSV")

  matrix=NULL
  csvs <- paste("MVN", 1:2, ".csv", sep="")
 for(i in 1:length(csvs)){
 matrix[[i]] <- read.csv(file=csvs[i], header=T)
 print(matrix[[i]])
  }
   Y1 Y2
 1 11  6
 2  8  5
 3 25 13
 4  1 13
 5  8 22
   Y1 Y2
 1  9  1
 2  7  9
 3 25 13
 4  1 18
 5  9 12
library("R2WinBUGS")

bugs.output <- list()
for(sim in 1:2){
    Y <-(matrix[[sim]])
    bugs.output[sim] <- 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))),
    model.file="M-LN_model_trial.txt",
    parameters.to.save = c("p","rho","sigma2"),
  n.chains=1, n.iter=12000, n.burnin=5000, debug=TRUE,
  bugs.directory="C://Tina/USB_Backup_042213/winbugs14/WinBUGS14",
  working.directory=NULL)
   }

Warning messages: 1: In bugs.output[sim] <- bugs(data = list(Y = as.matrix(Y), Nf = 5, : number of items to replace is not a multiple of replacement length 2: In bugs.output[sim] <- bugs(data = list(Y = as.matrix(Y), Nf = 5, : number of items to replace is not a multiple of replacement length

user1560215
  • 227
  • 1
  • 13
  • it's not officially forbidden, but please reconsider cross-posting to SO and r-help http://article.gmane.org/gmane.comp.lang.r.general/300245 ... it wastes effort. – Ben Bolker Sep 28 '13 at 15:56
  • 2
    can we see the BUGS model? i imagine that it will need to be set up to handle multivariate data (and this is most likely where your problem is). – guyabel Sep 28 '13 at 16:01
  • @ Ben Bolker, Will reconsider next time. I wasnt sure if the listservs and stackexchange have the same audience. – user1560215 Sep 28 '13 at 16:10
  • 1
    Not really related but... is this honestly how your code is indented? – Dason Oct 01 '13 at 16:11

1 Answers1

3

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"))
}
guyabel
  • 8,014
  • 6
  • 57
  • 86
  • @gjable,Thankyou for your response. The model ran fine with BUGS even earlier as I had mentioned. There was an error in this window when I pasted the code. I didnot mention mn, prec and R in code as they were listed under data. The problem is in how R is reading in the datafile. I have edited my code above for R and if I use your model code I still get the trap error. I am looking for some guidance in the R code as I am not so familiar with R calling WinBUGS. – user1560215 Oct 04 '13 at 13:57
  • It runs fine with 1 dataset, I just tested it. But how can I automate this for 1000 datastes. The first part is that how do I read in the 1000 simulated datasets? Thanks for all the help. – user1560215 Oct 04 '13 at 14:35
  • @user1560215 updated answer with R code. i take it that you have some sort of sensible named simulated data sets, like MVN1.csv, MVN2.csv,...,MVN1000.csv – guyabel Oct 04 '13 at 15:05
  • I do have the datasets named as MVN1.csv, MVN2.csv...where the 1st column denotes the simulation no and the 2nd and 3 rd columns are data for Y1 and Y2. In your code above, instead of saving the mcmc1, how could i subscript the mcmc1 so that I can retrieve the output of each run by typing mcmc1[1] for the first and so on.. Now if I just do mcmc1, I get the output from the 2nd run. Thanks – user1560215 Oct 04 '13 at 15:23
  • @user1560215 that is a bad idea. if you look at the structure of `str(mcmc1)` you will see it has lots of components, so keeping a 1000 of these will swamp your memory. if you really want to do it though you need to set up a empty list and then fill it in the loop.... http://stackoverflow.com/questions/18534526/create-list-of-lists-iteratively/18535210#18535210 – guyabel Oct 04 '13 at 15:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38624/discussion-between-user1560215-and-gjabel) – user1560215 Oct 04 '13 at 15:35