Search code examples
rwinbugswinbugs14

Automation of WinBUGS from R


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


Solution

  • 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"))
    }