Someone said a while ago that MCMC is an embarrassingly parallel (computing) problem. I wanted to try some R implementations of parallel computing for BUGS a few months ago when I was running some models with large data sets. I tried the function jags.parallel() in the R2jags package. It works for some models but fails for somewhat more complicated ones. This week, I learned a new package dclone and tried a toy problem. I have used this setup on several models and all worked out well.
packages(snow)
packages(rjags)
packages(dclone)
bugs.in <- function(chains=3){
x <- 1:100
y <- 3 + 5 * x + rnorm(100,0, 0.25)
bugs.ini <- list()
bugs.data <- list(n=100,y=y,x=x)
for (i in 1:chains)
bugs.ini [[i]] <- list(
beta=rnorm(2), sigma=runif(1))
bugs.para <- c("beta", "sigma")
return(list(data=bugs.data, inits=bugs.ini,
para=bugs.para, n.chains=chains,
model=function() {
for (i in 1:n){
y[i] ~ dnorm(mu[i], tau)
mu[i] <- beta[1] + beta[2]* x[i]
}
for (j in 1:2){
beta[j] ~ dnorm(0,0.0001)
}
tau <- pow(sigma, -2)
sigma ~ dunif(0,10)
}))
}
input.to.bugs <- bugs.in()
n.chains <- input.to.bugs$n.chains
n.adapt <- 10000
n.update <- 10000
n.iter <- 10000
n.keep <- 1000
thin <- max(c(1,floor((n.iter)*n.chains/n.keep)))
date()
cl <- makeCluster(3, type = "SOCK")
m2 <- jags.parfit(cl, data = input.to.bugs$data,
params = input.to.bugs$para,
model = input.to.bugs$model,
inits = input.to.bugs$inits,
n.adapt = n.adapt, n.update = n.update,
n.iter = n.iter, thin = thin, n.chains = n.chains)
date()
stopCluster(cl)
Subscribe to:
Post Comments (Atom)
Log or not log
LOGorNOTLOG.html Log or not log, that is the question May 19, 2018 In 2014 I taught a special topics class on statistical i...
-
Statistics is More Than \( P \)-values and AIC Statistics is More Than \( P \)-values and AIC Introduction The cont...
-
even if you believe that she is not worthy of your talent. Recently, I served as the advisor of a graduate student at Duke University. It...
-
The second edition of EESwithR is coming in fall 2016. I added one new chapter to the book and it is posted as an example chapter on githu...
1 comment:
dclone is easy to set up but the data list needs to include the clone size k appropriately especially for some time series data. Peter has done a great job for the coding.
Post a Comment