0

および並列計算rstanを使用して簡単な例を実行しようとしています。次のコードは、下部近くでコメントアウトすると正常に完了しますが、そのままでは、への呼び出しでセッションが無期限にハングします。Mac OS 10.11.5 の R GUI で R バージョン 3.3.0 を使用しています。plyrdoMCregisterDoMC(cores = 2)Radply

編集:奇妙なことに、これはLinuxサーバーと私のMacR CMD BATCHでコマンドライン経由で動作します。ただし、R GUI はまだありません。

library(rstan)
library(doMC)
library(plyr)

model = stan_model(model_code = "
data {
  int k; // number of observations per subject
  real theta; // hierarchical mean of the mu's, estimated beforehand (empirical Bayes)
  vector[k] subject; // subject-specific data
}
parameters {
  real mu; // subject-specific mean
}
model { 
  mu ~ normal(theta, 1) ;
  for(i in 1:k)
    subject[i] ~ normal(mu, 1);
}")

single_subject = function(model, subject, theta){
  diverge = TRUE
  attempt = 1

  while (diverge) {
    print(paste("Attempt", attempt))
    .mcmc = sampling(model, pars = "mu", iter = 2000*attempt, thin = attempt, verbose = F,
                     data = list(k = length(subject), theta = theta, subject = subject))
    .summary = summary(.mcmc)$summary
    diverge = any(.summary[,"n_eff"] < 1000)
    attempt = attempt + 1
  }

  .summary["mu",]
}

n = 100 # number of subjects
k = 32 # number of observations per subject
dataset = matrix(rnorm(n*k), ncol = k)
theta = 0 # could be estimated from the data beforehand

start = proc.time()
out = adply(dataset, 1, function(subject) single_subject(model, subject, theta))
serial_time = proc.time() - start

start = proc.time()
registerDoMC(cores = 2)
out = adply(dataset, 1, 
  function(subject) single_subject(model, subject, theta),
  .parallel = T,
  .paropts = list(.export=c("single_subject", "model", "subject", "theta"), 
                  .packages="rstan"))
registerDoSEQ()
parallel_time = proc.time() - start

cat("Serial time:\n")
print(serial_time)

cat("Parallel time:\n")
print(parallel_time)

write.csv(out, "mu.csv")
4

0 に答える 0