Learn R Programming

dclone (version 1.0-0)

write.jags.model: Write and remove model file

Description

Writes or removes a BUGS model file to or from the hard drive.

Usage

write.jags.model(model, filename = "model.bug", dir = getwd(), n = 5)
clean.jags.model(filename = "model.bug", dir = getwd())

Arguments

model
JAGS model to write onto the hard drive (see Example).
filename
Character, the name of the file to write/remove.
dir
Optional argument for directory where to write or look for the file to remove.
n
Numeric, length of a random string to be attached to filename if filename already exists.

Value

  • write.jags.model invisibly returns the name of the file that was written eventually (possibly including random string). The function tries to avoid overwriting existing files. clean.jags.model invisibly returns the result of file.remove (logical). Original working directory is then restored.

encoding

UTF-8

Details

write.jags.model is built upon the function write.model of the R2WinBUGS package. clean.jags.model is built upon the function file.remove, and intended to be used internally to clean the JAGS model file up after estimating sessions, ideally via the on.exit function.

See Also

write.model, file.remove

Examples

Run this code
## simple regression example from the JAGS manual
jfun <- function() {
    for (i in 1:N) {
        Y[i] ~ dnorm(mu[i], tau)
        mu[i] <- alpha + beta * (x[i] - x.bar)
    }
    x.bar <- mean(x)
    alpha ~ dnorm(0.0, 1.0E-4)
    beta ~ dnorm(0.0, 1.0E-4)
    sigma <- 1.0/sqrt(tau)
    tau ~ dgamma(1.0E-3, 1.0E-3)
}
## data generation
set.seed(1234)
N <- 100
alpha <- 1
beta <- -1
sigma <- 0.5
x <- runif(N)
linpred <- model.matrix(~x) %*% c(alpha, beta)
Y <- rnorm(N, mean = linpred, sd = sigma)
## list of data for the model
jdata <- list(N = N, Y = Y, x = x)
## what to monitor
jpara <- c("alpha", "beta", "sigma")
## write model onto hard drive
jmodnam <- write.jags.model(jfun)
## fit the model
regmod <- jags.fit(jdata, jpara, jmodnam, n.chains = 3)
## cleanup
clean.jags.model(jmodnam)
## model summary
summary(regmod)

Run the code above in your browser using DataLab