require(rjags)
require(coda)
# simulatee data
set.seed(4444)
N <- 100
Mu <- 100
Sigma <- 15
y <- rnorm(n=N, mean=Mu, sd=Sigma)
jagsdata <- list(y=y)
jags.script <- "
model {
for (i in 1:length(y)) {
y[i] ~ dnorm(mu, tau)
}
mu ~ dnorm(0, 0.001)
sigma ~ dunif(0, 1000)
tau <- 1/sigma^2
}"
mod1 <- jags.model(textConnection(jags.script), data=jagsdata, n.chains=4,
n.adapt=1000)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph Size: 109
##
## Initializing model
##
update(mod1, 200) # burn in
mod1.samples <- coda.samples(model=mod1,
variable.names=c('mu', 'sigma'),
n.iter=1000)
plot(mod1.samples)
getAnywhere(plot.mcmc)
## A single object matching 'plot.mcmc' was found
## It was found in the following places
## registered S3 method for plot from namespace coda
## namespace:coda
## with value
##
## function (x, trace = TRUE, density = TRUE, smooth = FALSE, bwf,
## auto.layout = TRUE, ask = dev.interactive(), ...)
## {
## oldpar <- NULL
## on.exit(par(oldpar))
## if (auto.layout) {
## mfrow <- set.mfrow(Nchains = nchain(x), Nparms = nvar(x),
## nplots = trace + density)
## oldpar <- par(mfrow = mfrow)
## }
## for (i in 1:nvar(x)) {
## y <- mcmc(as.matrix(x)[, i, drop = FALSE], start(x),
## end(x), thin(x))
## if (trace)
## traceplot(y, smooth = smooth, ...)
## if (density) {
## if (missing(bwf))
## densplot(y, ...)
## else densplot(y, bwf = bwf, ...)
## }
## if (i == 1)
## oldpar <- c(oldpar, par(ask = ask))
## }
## }
## <environment: namespace:coda>
my.plot.mcmc <- function (x, trace = TRUE, density = TRUE, smooth = FALSE, bwf,
auto.layout = TRUE, ask = FALSE, parameters, ...)
{
oldpar <- NULL
on.exit(par(oldpar))
if (auto.layout) {
mfrow <- coda:::set.mfrow(Nchains = nchain(x), Nparms = nvar(x),
nplots = trace + density)
oldpar <- par(mfrow = mfrow)
}
for (i in 1:nvar(x)) {
y <- mcmc(as.matrix(x)[, i, drop = FALSE], start(x),
end(x), thin(x))
if (trace)
traceplot(y, smooth = smooth, ...)
if (density) {
if (missing(bwf)) {
densplot(y, ...); abline(v=parameters[i])
} else densplot(y, bwf = bwf, ...)
}
if (i == 1)
oldpar <- c(oldpar, par(ask = ask))
}
}
my.plot.mcmc(mod1.samples, parameters=c(Mu, Sigma))