R Under development (unstable) (2024-03-09 r86082 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # Requires rjags: > if(require('rjags')){ + + + library('runjags') + runjags.options(inits.warning=FALSE, rng.warning=FALSE, blockignore.warning=FALSE) + + library('parallel') + + testnum <- 1 + + themodel <- " + model{ + + for(i in 1:N){ + Y[i] ~ dnorm(true.y[i], precision) + true.y[i] <- (m * X[i]) + c + } + m ~ dunif(-1000,1000) + c ~ dunif(-1000,1000) + precision ~ dexp(1) + + #data# N, X + }" + + # Simulate the data + set.seed(1) + N <- 20 + X <- 1:N + Y <- rnorm(length(X), 2*X + 1, 1) + + # Some initial values to use for 2 chains: + + initfun <- function(chain){ + + # data is made available within this function when it + # is evaluated for each simulation: + stopifnot(length(data$X) == data$N) + + m <- c(-10,10)[chain] + c <- c(10,-10)[chain] + precision <- c(0.01,100)[chain] + + .RNG.seed <- chain + .RNG.name <- c("base::Super-Duper", + "base::Wichmann-Hill")[chain] + + return(list(m=m, c=c, precision=precision, + .RNG.seed=.RNG.seed, .RNG.name=.RNG.name)) + } + + # A simple function that removes (over-writes with NA) one datapoint at a time: + datafun <- function(s){ + simdata <- Y + simdata[s] <- NA + return(list(Y=simdata)) + } + + + # Set up a cluster to use with the parLapply method: + cat('Running study test number', testnum, '\n'); testnum <- testnum+1 + cl <- makeCluster(2) + # Call the simulations over the snow cluster: + results <- run.jags.study(simulations=4, model=themodel, datafunction=datafun, + targets=list(Y=Y, m=2, c=1), n.chains=2, inits=initfun, cl=cl) + + + m <- 'model{ + d[1] ~ dpois(mu) + d[2] ~ dpois(mu) + d[3] ~ dpois(mu) + mu ~ dgamma(1,1) + #monitor# mu + #data# d + }' + + ##### Can't test any more than 2 spawned processes on winbuilder ##### + + cat('Running study test number', testnum, '\n'); testnum <- testnum+1 + mu <- list(1,1) + d <- c(5, 4, 7) + jr <- run.jags(m, method='rjags', n.chains=2, inits=list(list(mu=1), list(mu=1)), silent.jags=TRUE) + # Drop 1 (would create 3 clusters except we pass it cl): + r <- drop.k(jr, dropvars='d', cl=cl) + stopCluster(cl) + + # Drop k (use lapply so we don't create a cluster with 4 nodes): + cat('Running study test number', testnum, '\n'); testnum <- testnum+1 + r <- drop.k(jr, dropvars='d', simulations=4, k=2, silent.jags=TRUE, parallel.method=lapply) + + cat("All study/drop-k checks passed\n") + + }else{ + cat("Note: the rjags package is not installed, so the study/drop-k tests were skipped\n") + } Loading required package: rjags Loading required package: coda Linked to JAGS 4.3.1 Loaded modules: basemod,bugs Running study test number 1 Starting a JAGS study at 17:54 Testing the model and data for simulation 2... Finished running the simulation The model runs OK Calling autorun.jags for 4 simulations... Using a PSOCK cluster with 2 nodes on host 'localhost' Finished running the simulations Finished summarising results Finished JAGS study at 17:54 (total time taken: 11.5 seconds) Running study test number 2 Finished running the simulation Starting a JAGS study at 17:54 Checking the supplied model definition (and data for simulation 3)... The model compiles OK Calling autorun.jags for 3 simulations... Using a PSOCK cluster with 2 nodes on host 'localhost' Finished running the simulations Finished summarising results Finished JAGS study at 17:54 (total time taken: 5.8 seconds) Running study test number 3 Starting a JAGS study at 17:54 Checking the supplied model definition (and data for simulation 3)... The model compiles OK Calling autorun.jags for 4 simulations... Finished running the simulations Finished summarising results Finished JAGS study at 17:54 (total time taken: 10.9 seconds) All study/drop-k checks passed Warning message: One or more target variables is non-stochastic for all of the (successful) simulation results > > > > > proc.time() user system elapsed 3.89 0.67 31.37