ku-awdc / runjags

An R package implementing interface utilities, model templates, parallel computing methods and additional distributions for MCMC models in JAGS
GNU General Public License v2.0
4 stars 2 forks source link

keep.jags.files = "/path/to/runjagsfiles" creates directory but does not write files in it #1

Open sjcowtan opened 2 years ago

sjcowtan commented 2 years ago

I'm running inside a docker environment (hence the /home/rstudio) and trying to specify a location to store the JAGS output files using keep.jags.files, but unfortunately it creates the folder but then gives the error "Error in setwd(temp.directory) : cannot change working directory", so all I get is an empty directory. If I include an explicit setwd() and use keep.jags.files = TRUE then it creates the runjagsfile and populates it, but that isn't an ideal solution. Toy example below.

library(runjags)

### Example from 
### http://www.jkarreth.net/files/Lab3-4_JAGS-BUGS.html#fitting_bayesian_models_using_runjags
# Create predictors
n.sim <- 100; set.seed(123)
x1 <- rnorm(n = n.sim, mean = 5, sd = 2)
x2 <- rbinom(n.sim, size = 1, prob = 0.3)
e <- rnorm(n = n.sim, mean = 0, sd = 1)

# Outcome variable from coeffs b1 and b2 and intercept a
b1 <- 1.2
b2 <- -3.1
a <- 1.5
y <- a + b1 * x1 + b2 * x2 + e

# Simulated data in dataframe
sim_df <- data.frame(y, x1, x2)

# Convert to list
sim_ls <- as.list(sim_df)

# Add number of observations
sim_ls$N <- nrow(sim_df)

# Convert format for runjags
sim_runjags <- dump.format(sim_ls)

# Set up initial values
inits <- list(
  inits1 = list(alpha = 1, beta1 = 1, beta2 = 1),
  inits2 = list(alpha = 0, beta1 = 0, beta2 = 0),
  inits3 = list(alpha = -1, beta1 = -1, beta2 = -1)
)

# Specifying a folder name in keep.jags.files doesn't work; this does (with keep.jags.files = TRUE) but
# that's not the solution I want
#setwd("~/diss-code-r4/thesis_analysis/run_jags")

# Run jags
toy_example_runjags <- run.jags(
  model = "~/diss-code-r4/thesis_analysis/run_jags/toy_example.bug",
  monitor = c("alpha", "beta1", "beta2"),
  data = sim_runjags,
  inits = inits,
  burnin = 1000,
  sample = 5000,
  keep.jags.files = "/home/rstudio/diss-code-r4/thesis_analysis/run_jags/runjagsfiles"
)

# Output
print(toy_example_runjags)