| metrop {mcmc} | R Documentation |
Markov chain Monte Carlo for continuous random vector using a Metropolis algorithm.
metrop(obj, initial, nbatch, blen = 1, nspac = 1, scale = 1, outfun,
debug = FALSE, ...)
obj |
an R function that evaluates the log unnormalized probability
density of the desired equilibrium distribution of the Markov chain.
First argument is the state vector of the Markov chain. Other arguments
arbitrary and taken from the |
initial |
a real vector, the initial state of the Markov chain. |
nbatch |
the number of batches. |
blen |
the length of batches. |
nspac |
the spacing of iterations that contribute to batches. |
scale |
controls the proposal step size. If scalar or
vector, the proposal is |
outfun |
controls the output. If a function, then the batch means
of |
debug |
if |
... |
additional arguments for |
Runs a “random-walk” Metropolis algorithm, terminology introduced
by Tierney (1994), with multivariate normal proposal
producing a Markov chain with equilibrium distribution having a specified
unnormalized density. Distribution must be continuous. Support of the
distribution is the support of the density specified by argument obj.
The initial state must satisfy obj(state, ...) > - Inf.
Description of a complete MCMC analysis (Bayesian logistic regression)
using this function can be found in the vignette demo
(../doc/demo.pdf).
Suppose the function coded by the log unnormalized function (either
obj or obj$lud) is actually a log unnormalized density,
that is, if w denotes that function, then exp(w) integrates
to some value strictly between zero and infinity. Then the metrop
function always simulates a reversible, Harris ergodic Markov chain having
the equilibrium distribution with this log unnormalized density.
The chain is not guaranteed to be geometrically ergodic. In fact it cannot
be geometrically ergodic if the tails of the log unnormalized density are
sufficiently heavy. The morph.metrop function deals with this
situation.
an object of class "mcmc", subclass "metropolis",
which is a list containing at least the following components:
accept |
fraction of Metropolis proposals accepted. |
batch |
|
initial |
value of argument |
final |
final state of Markov chain. |
initial.seed |
value of |
final.seed |
value of |
time |
running time of Markov chain from |
lud |
the function used to calculate log unnormalized density,
either |
nbatch |
the argument |
blen |
the argument |
nspac |
the argument |
outfun |
the argument |
Description of additional output when debug = TRUE can be
found in the vignette debug (../doc/debug.pdf).
If outfun is missing or not a function, then the log unnormalized
density can be defined without a ... argument and that works fine.
One can define it starting ludfun <- function(state) and that works
or ludfun <- function(state, foo, bar), where foo and bar
are supplied as additional arguments to metrop.
If outfun is a function, then both it and the log unnormalized
density function can be defined without ... arguments if they
have exactly the same arguments list and that works fine. Otherwise it
doesn't work. Start the definitions ludfun <- function(state, foo)
and outfun <- function(state, bar) and you get an error about
unused arguments. Instead start the definitions
ludfun <- function(state, foo, ...)
and outfun <- function(state, bar, ...), supply
foo and bar as additional arguments to metrop,
and that works fine.
In short, the log unnormalized density function and outfun need
to have ... in their arguments list to be safe. Sometimes it works
when ... is left out and sometimes it doesn't.
Of course, one can avoid this whole issue by always defining the log
unnormalized density function and outfun to have only one argument
state and use global variables (objects in the R global environment) to
specify any other information these functions need to use. That too
follows the R way. But some people consider that bad programming practice.
Tierney, L. (1994) Markov chains for exploring posterior distributions (with discussion). Annals of Statistics 22 1701–1762.
h <- function(x) if (all(x >= 0) && sum(x) <= 1) return(1) else return(-Inf) out <- metrop(h, rep(0, 5), 1000) out$accept # acceptance rate too low out <- metrop(out, scale = 0.1) out$accept # acceptance rate o. k. (about 25 percent) plot(out$batch[ , 1]) # but run length too short (few excursions from end to end of range) out <- metrop(out, nbatch = 1e4) out$accept plot(out$batch[ , 1]) hist(out$batch[ , 1])