Description Usage Arguments Details Value Author(s) See Also Examples
View source: R/write.jags.model.R
Writes or removes a BUGS model file to or from the hard drive.
1 2 3 4 | write.jags.model(model, filename = "model.txt", digits = 5,
dir = getwd(), overwrite = getOption("dcoptions")$overwrite)
clean.jags.model(filename = "model.bug", dir = getwd())
custommodel(model, exclude = NULL, digits = 5)
|
model |
JAGS model to write onto the hard drive (see Example).
For |
digits |
Number of significant digits used in the output. |
filename |
Character, the name of the file to write/remove.
It can be a |
dir |
Optional argument for directory where to write or look for the file to remove. |
overwrite |
Logical, if |
exclude |
Numeric, lines of the model to exclude (see 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 up the JAGS
model file after estimating sessions,
ideally via the on.exit
function.
The function custommodel
can be used to exclude some lines
of the model. This is handy when there are variations of the same model.
write.jags.model
accepts results returned by custommodel
.
This is also the preferred way of including BUGS models into
R packages, because the function form often includes
undefined functions.
Use the %_%
operator if the model is a function and the model
contains truncation (I()
in WinBUGS, T()
in JAGS).
See explanation on help page of write.model
.
write.jags.model
invisibly returns the name of the file
that was written eventually (possibly including random string).
clean.jags.model
invisibly returns the result of
file.remove
(logical).
Original working directory is restored.
custommodel
returns an object of class 'custommodel',
which is a character vector.
Peter Solymos, solymos@ualberta.ca
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | ## Not run:
## 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 <- crossprod(t(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)
## End(Not run)
## let's customize this model
jfun2 <- structure(
c(" model { ",
" for (i in 1:n) { ",
" Y[i] ~ dpois(lambda[i]) ",
" Y[i] <- alpha[i] + inprod(X[i,], beta[1,]) ",
" log(lambda[i]) <- alpha[i] + inprod(X[i,], beta[1,]) ",
" alpha[i] ~ dnorm(0, 1/sigma^2) ",
" } ",
" for (j in 1:np) { ",
" beta[1,j] ~ dnorm(0, 0.001) ",
" } ",
" sigma ~ dlnorm(0, 0.001) ",
" } "),
class = "custommodel")
custommodel(jfun2)
## GLMM
custommodel(jfun2, 4)
## LM
custommodel(jfun2, c(3,5))
## deparse when print
print(custommodel(jfun2), deparse=TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.