mh: Fit a generic model using Metropolis-Hastings (MH)

Description Usage Arguments Value Elements for hmclearn objects Available logPOSTERIOR functions Author(s) Examples

View source: R/mcmc_functions.R

Description

This function runs the MH algorithm on a generic model provided the logPOSTERIOR function. All parameters specified within the list param are passed to these the posterior function.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
mh(
  N,
  theta.init,
  qPROP,
  qFUN,
  logPOSTERIOR,
  nu = 0.001,
  varnames = NULL,
  param = list(),
  chains = 1,
  parallel = FALSE,
  ...
)

Arguments

N

Number of MCMC samples

theta.init

Vector of initial values for the parameters

qPROP

Function to generate proposal

qFUN

Probability for proposal function. First argument is where to evaluate, and second argument is the conditional parameter

logPOSTERIOR

Function to calculate and return the log posterior given a vector of values of theta

nu

Single value or vector parameter passed to qPROP or qFUN for the proposal density

varnames

Optional vector of theta parameter names

param

List of additional parameters for logPOSTERIOR and glogPOSTERIOR

chains

Number of MCMC chains to run

parallel

Logical to set whether multiple MCMC chains should be run in parallel

...

Additional parameters for logPOSTERIOR

Value

Object of class hmclearn

Elements for hmclearn objects

N

Number of MCMC samples

theta

Nested list of length N of the sampled values of theta for each chain

thetaCombined

List of dataframes containing sampled values, one for each chain

r

NULL for Metropolis-Hastings

theta.all

Nested list of all parameter values of theta sampled prior to accept/reject step for each

r.all

NULL for Metropolis-Hastings

accept

Number of accepted proposals. The ratio accept / N is the acceptance rate

accept_v

Vector of length N indicating which samples were accepted

M

NULL for Metropolis-Hastings

algorithm

MH for Metropolis-Hastings

varnames

Optional vector of parameter names

chains

Number of MCMC chains

Available logPOSTERIOR functions

linear_posterior

Linear regression: log posterior

logistic_posterior

Logistic regression: log posterior

poisson_posterior

Poisson (count) regression: log posterior

lmm_posterior

Linear mixed effects model: log posterior

glmm_bin_posterior

Logistic mixed effects model: log posterior

glmm_poisson_posterior

Poisson mixed effects model: log posterior

Author(s)

Samuel Thomas samthoma@iu.edu, Wanzhu Tu wtu@iu.edu

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Linear regression example
set.seed(521)
X <- cbind(1, matrix(rnorm(300), ncol=3))
betavals <- c(0.5, -1, 2, -3)
y <- X%*%betavals + rnorm(100, sd=.2)

f1_mh <- mh(N = 3e3,
         theta.init = c(rep(0, 4), 1),
         nu <- c(rep(0.001, 4), 0.1),
         qPROP = qprop,
         qFUN = qfun,
         logPOSTERIOR = linear_posterior,
         varnames = c(paste0("beta", 0:3), "log_sigma_sq"),
         param=list(y=y, X=X), parallel=FALSE, chains=1)

summary(f1_mh, burnin=1000)

hmclearn documentation built on Oct. 23, 2020, 8:04 p.m.