mcarray output from JAGS using a script?

Help
Drew Tyre
2013-06-13
2013-06-13
  • Drew Tyre

    Drew Tyre - 2013-06-13

    Hi all,

    This week I've started using a remote LINUX cluster to run my models (more processors!). I have successfully compiled JAGS from source and everything appears to be working. For simplicity, I thought I would run JAGS directly with a script file. All good, except that the only script command for getting samples (CODA) writes coda output. I've previously written all of my post-processing of results using mcarray objects, and I can't find any way to coerce an mcmc.list object into an mcarray object.

    So 1) is there a way to have JAGS run using a script (i.e. not using rjags) to produce a JAGS format file? or 2) has someone already cracked the mcmc.list -> mcarray coercion?

    Off to start working on (2)!

     
  • Drew Tyre

    Drew Tyre - 2013-06-13

    FWIW, here's what I came up with. The more I think about it, the more I think no one will ever want to do this, but ... oh well. Should have figured out how to use R on the cluster!

    as.mcarray.mcmc.list <- function(x){
      var.index <- dimnames(x[[1]])[[2]]
      vars <- strsplit(var.index,"\\[|\\]|,")
      var.names <- sapply(vars,function(xx)xx[1])
      var.names <- rle(var.names)
      start <- c(1,cumsum(var.names$lengths)+1)
      start <- start[-length(start)] # remove last one
      stopat <- c(cumsum(var.names$lengths))
      var.dims <- sapply(vars,function(xx)length(xx))-1
      var.dims <- var.dims[start]
      chain.length = dim(x[[1]])[1]
      result <- list()
    
      for (i in 1:length(start)){
        tmp <- switch(var.dims[i]+1,
                      array(c(x[[1]][,start[i]],x[[2]][,start[i]],x[[3]][,start[i]]),
                            dim=c(1,chain.length,3)),
                      aperm(array(c(x[[1]][,start[i]:stopat[i]],x[[2]][,start[i]:stopat[3]],x[[1]][,start[i]:stopat[i]]),
                                  dim=c(chain.length,var.names$length[i],3)),c(2,1,3)),
                      aperm(array(c(x[[1]][,start[i]:stopat[i]],x[[2]][,start[i]:stopat[3]],x[[1]][,start[i]:stopat[i]]),
                                  dim=c(chain.length,as.numeric(vars[[stopat[i]]][2:3]),3)),c(2,3,1,4)))
        class(tmp)<- "mcarray"
        names(dim(tmp)) <- switch(var.dims[i]+1,
                                  c("","iteration","chain"),
                                  c("","iteration","chain"),
                                  c("","","iteration","chain"))
        result[[var.names$values[i]]] <- tmp
      }
      return(result)
    }
    
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks