Learn R Programming

dclone (version 0.9-0)

jags.fit: Fit JAGS/BUGS models with cloned data

Description

Convenient functions designed to work well with cloned data arguments.

Usage

jags.fit(data, params, model, inits = NULL, n.chains = 3, n.adapt = 1000,
n.update = 0, thin = 1, n.iter = 5000, ...)
bugs.fit(data, params, model, inits = NULL, format = c("mcmc.list", "bugs"), 
program = c("winbugs", "openbugs"), DIC = FALSE, dir = getwd(), ...)
## S3 method for class 'bugs':
as.mcmc.list(x, ...)

Arguments

data
A list containing the data.
params
Character vector of parameters to be samples.
model
Character string (name of the model file) or a function containing the model (see Examples).
inits
Optional specification of initial values in the form of a list or a function (see Initialization at jags.model). If NULL, initial values will be generated automatically. It is an erro
n.chains
Number of chains to generate.
n.adapt
Number of steps for adaptation.
n.update
Number of updates before iterations.
thin
Thinning value.
n.iter
Number of iterations.
format
Required output format. (Note that default behaviour of n.thin can cause problems when program = "openbugs" and format = "mcmc.list", thus n.thin = 1 is advised.)
program
The program to use, not case sensitive. winbugs calls the function bugs, openbugs calls the function openbugs and
DIC
Logical, if deviance information criterion should be computed. (Needed for efficiency reasons, since it is opposite to the bugs/openbugs defaults, but if format = "mcmc.list" deviance is silently ignored.)
dir
Identical to working.directory argument in the bugs/openbugs call for setting working directory. (Needed for stability, because openbugs cant't always handle its default NULL value (a bug?)
x
A fitted 'bugs' object.
...
Further arguments passed to coda.samples, and update.jags (e.g. the progress.bar argument) for jags.fit. For

Value

  • By default, an mcmc.list object. If data cloning is used via the data argument, summary(jags.fit) returns a modified summary containing scaled data cloning standard errors (scaled by sqrt(n.clones)), and $R_{hat}$ values (as returned by gelman.diag). bugs.fit returns a bugs object if format = "bugs". In this case, printing is not changed, but the number of clones used is attached as attribute and can be retrieved by the function nclones. The function as.mcmc.list.bugs converts a 'bugs' object into 'mcmc.list'. Default behaviour of n.thin can cause problems in conversion when program = "openbugs" and format = "mcmc.list", thus n.thin = 1 is advised.

encoding

UTF-8

See Also

jags.model, coda.samples dcsd, confint.mcmc.list.dc, coef.mcmc.list, quantile.mcmc.list, vcov.mcmc.list.dc

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)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")
#### fit the model with JAGS
regmod <- jags.fit(jdata, jpara, jfun, n.chains = 3)
## model summary
summary(regmod)
## data cloning
dcdata <- dclone(jdata, 5, multiply = "N")
dcmod <- jags.fit(dcdata, jpara, jfun, n.chains = 3)
summary(dcmod)
#### fitting with WinBUGS, bugs example
data(schools)
dat <- list(J = nrow(schools), y = schools$estimate, sigma.y = schools$sd)
bugs.model <- function(){
       for (j in 1:J){
         y[j] ~ dnorm (theta[j], tau.y[j])
         theta[j] ~ dnorm (mu.theta, tau.theta)
         tau.y[j] <- pow(sigma.y[j], -2)
       }
       mu.theta ~ dnorm (0.0, 1.0E-6)
       tau.theta <- pow(sigma.theta, -2)
       sigma.theta ~ dunif (0, 1000)
     }  
inits <- function(){
    list(theta=rnorm(nrow(schools), 0, 100), mu.theta=rnorm(1, 0, 100),
         sigma.theta=runif(1, 0, 100))
}
param <- c("mu.theta", "sigma.theta")
sim <- bugs.fit(dat, param, bugs.model, inits)
dat2 <- dclone(dat, 2, multiply="J")
sim2 <- bugs.fit(dat2, param, bugs.model)
#### fitting the model with OpenBUGS
sim3 <- bugs.fit(dat2, param, bugs.model, program="openbugs", n.thin=1)
#### fitting the model with JAGS
sim4 <- jags.fit(dat2, param, bugs.model)

Run the code above in your browser using DataLab