Fit a generic model using Metropolis-Hastings (MH)
Fit a generic model using Metropolis-Hastings (MH)
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.
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
Returns
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