Recent posts by Tim Handleyhttps://sourceforge.net/u/thandle2/Recent posts by Tim HandleyenThu, 15 Nov 2012 16:18:45 -0000Running JAGS with snowfallhttps://sourceforge.net/p/mcmc-jags/discussion/610036/thread/585b0e4c/I've done just that thing via Rjags and snow. So I can't comment on R2jags or snowfall, but I can offer the example Rjags/snow code that I wrote for myself as a proof of concept, and that ought to be pretty similar to what you're trying to do.
Say you have a model file called "lm.model" that describes a simple, straight-line, least-squares model:
model{
m~dnorm(0,10)
b~dnorm(0,10)
sd~dunif(0,10)
tau<-pow(sd,-2)
yhat <- x*m+b
for(obsj in 1:N)
{ y.obs[obsj] ~ dnorm(yhat[obsj], tau) }
}
Then the following R code will fit that model in two different ways:
1) by running n.chains chains on one core
2) by running one chain in each of n.chains cores:
library(snow)
library(rjags)
##Describe the toy data
N = 1e3
m.tru = 1
b.tru = 1
sd.tru= 1
##Create toy data
x = seq(0,1,length.out=N)
y.tru = m.tru*x + b.tru
y.obs = rnorm(N, y.tru, sd.tru)
##Describe MCMC
n.iter = 10e3
n.chains = 2
n.thin=2
##Run n.chains for n.iter on one core
simple.start.time = proc.time()
lm.model = jags.model("lm.model", data=list(x=x,y.obs=y.obs, N=N),
n.chains=n.chains, n.adapt=100)
samples = coda.samples(lm.model, c("m","b","sd"), n.iter=n.iter, thin=n.thin)
simple.end.time = proc.time()
simple.dtime = simple.end.time - simple.start.time
##Run n.chains for n.iter, with one chain on each of n.chains cores
##Need a wrapper for the fitting process.
##1) The snow function 'clusterApply' requires
##functions with just one argument. 2) Also, for reasons that I don't understand,
##JAGS requires that both jags.model and coda.samples be run under a single call
##to clusterApply.
##Note that it's important to specify a different random number seed for each worker.
coda.samples.wrapper <- function(j)
{
temp.model = jags.model("lm.model",
inits=list(.RNG.name="base::Wichmann-Hill",
.RNG.seed=j),
data=list(x=x,y.obs=y.obs, N=N), n.chains=1, n.adapt=100)
coda.samples(temp.model, c("m","b","sd"), n.iter=n.iter, thin=n.thin)
}
snow.start.time = proc.time()
cl <- makeCluster(n.chains, "SOCK")
##Make sure the rjags library is loaded in each worker
clusterEvalQ(cl, library(rjags))
##Send data to workers, then fit models. One disadvantage of this
##parallelization is that you lose the ability to watch the progress bar.
clusterExport(cl, list("x","y.obs","N","n.iter","n.thin"))
par.samples = clusterApply(cl, 1:n.chains, coda.samples.wrapper)
##Reorganize 'par.samples' so that it is recognizeable as an 'mcmc.list' object
for(i in 1:length(par.samples)) { par.samples[[i]] <- par.samples[[i]][[1]] }
class(par.samples) <- "mcmc.list"
stopCluster(cl)
snow.end.time = proc.time()
snow.dtime = snow.end.time - snow.start.time
##Compare times. The worker processes run outside of R's accounting, and so
##the time that they spend computing stuff is given by elapsed time
##rather than user or system time.
simple.dtime
snow.dtime
simple.dtime/snow.dtime
Tim HandleyThu, 15 Nov 2012 16:18:45 -0000https://sourceforge.netfd6af0f84dad0290563c2bb8f99fb9b62c232ea1sparse representationhttps://sourceforge.net/p/mcmc-jags/discussion/610036/thread/f35584bb/I think that JAGS allows you to specify which elements to monitor. So if your matrix is sparse, then in your list of parameters to monitor, you can list just those elements that you care about. e.g. c("matrix1[5,6], matrix1[12,4]"). If there is a relatively long set of important elements, you could use something like R to algorithmically construct a vector of strings that describes the set of parameters to monitor.
Also note that you can specify a "thinning" number. This lets you store samples at regular intervals, e.g. every 100th sample, every 1000th sample.
In combination, these two things should help.Tim HandleyFri, 19 Oct 2012 18:49:11 -0000https://sourceforge.net277e8a7105b6d78f7da477d293fdf51d8e2bd23aWikiPage Home modified by Tim Handleyhttps://sourceforge.net/u/thandle2/wiki/Home/Welcome to your wiki!
This is the default page, edit it as you see fit. To add a new page simply reference it within brackets, e.g.: [SamplePage].
The wiki uses [Markdown](/u/thandle2/wiki/markdown_syntax/) syntax.
[[project_admins]]
[[download_button]]
Tim HandleyTue, 24 Jan 2012 17:44:24 -0000https://sourceforge.neteaa5d3fba918602e2e1fe55687bd64a604a55e2d