write.jagsfile: Write a complete JAGS model to a text file

Description Usage Arguments Value References See Also Examples

View source: R/read.jagsfile.R

Description

Writes the JAGS model, data, initial values and monitored variables etc to a file. The model can then be run using a call to link{run.jags} with the filename as the model argument.

Usage

1
2
write.jagsfile(runjags.object, file, remove.tags = TRUE, write.data = TRUE,
  write.inits = TRUE)

Arguments

runjags.object

a valid (but not necessarily updated) runjags object to be saved to file. No default.

file

a filename to which the model will be written. Note that any files already existing in that location will be overwritten with no warning (see new_unique for a way to generate unique filenames). No default.

remove.tags

should the runjags tags #data#, #inits#, #monitors#, #modules#, #factories#, #residual#, #fitted# and #response# be removed from the original model code before writing it to file? If left in, these may create conflicts with the tags automatically added to the new file.

write.data

should the data also be written to file? If FALSE, the model may not run from the file without specifying a new source of data.

write.inits

should the data also be written to file? If FALSE, the model may not run from the file without specifying new initial values.

Value

Returns the filename that the model was saved to (invisibly)

References

Lunn D, Jackson C, Best N, Thomas A, Spiegelhalter D (2012). The BUGS book: A practical introduction to Bayesian analysis. CRC press; and Matthew J. Denwood (2016). runjags: An R Package Providing Interface Utilities, Model Templates, Parallel Computing Methods and Additional Distributions for MCMC Models in JAGS. Journal of Statistical Software, 71(9), 1-25. doi:10.18637/jss.v071.i09

See Also

read.jagsfile and run.jags for the reverse operation

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Set up a model:
# y = m x + c, assuming normal observation errors for y:

# Simulate the data
X <- 1:100
Y <- rnorm(length(X), 2*X + 10, 1)

# Model in the JAGS format
model <- "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 and initial values in a named list format, 
# with explicit control over the random number
# generator used for each chain (optional): 
data <- list(X=X, Y=Y, N=length(X))
inits1 <- list(m=1, c=1, precision=1,
.RNG.name="base::Super-Duper", .RNG.seed=1)
inits2 <- list(m=0.1, c=10, precision=1,
.RNG.name="base::Wichmann-Hill", .RNG.seed=2)

## Not run: 
# Compile the model but don't update it (sample=0):
compiled <- run.jags(model=model, monitor=c("m", "c", "precision"), 
data=data, n.chains=2, inits=list(inits1,inits2), sample=0)

# Save the complete model to a file:
filepath <- write.jagsfile(compiled, file='model.txt')

# And run the model from the file:
results <- run.jags(filepath)


## End(Not run)

Example output

Loading required namespace: rjags
Compiling rjags model...
Compiling rjags model...
sh: 1: wc: Permission denied
sh: 1: cannot create /dev/null: Permission denied
Calling the simulation using the rjags method...
Adapting the model for 1000 iterations...
Burning in the model for 4000 iterations...
Running the model for 10000 iterations...
Simulation complete
Calculating summary statistics...
Calculating the Gelman-Rubin statistic for 3 variables....
Error in modeest::mlv(x, ...)$M : 
  $ operator is invalid for atomic vectors
In addition: Warning message:
In runjags.start(model = runjags.object$model, monitor = c(monitor,  :
  Unable to verify the version number of JAGS.  If any functions do not work as expected, you could try checking your JAGS installation for problems.
Finished running the simulation
Warning message:
In runjags.summaries(fullmcmclist = mcmc, thinnedmcmclist = thinnedmcmc,  :
  An unexpected error occured while calculating the mode

runjags documentation built on March 26, 2020, 9:24 p.m.