MCMC: Create a single Markov Chain Monte Carlo simulation

Description Usage Arguments Examples

Description

Create a single Markov Chain Monte Carlo simulation

Usage

1
MCMC(df, start, rprop, dprop = NULL, N = 1000)

Arguments

df

A function that is the target distribution

start

The starting point for the MCMC

rprop

A function that takes the current chain step and proposes a new value

dprop

The density function of the proposal distribution. If NULL, it is assumed to be symetric.

N

How many chain steps to calculate.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Target Distribution
df <- function(x){
   return(.7*dnorm(x, mean=2, sd=1) + .3*dnorm(x, mean=5, sd=1))
}

# Proposal distribution
rprop <- function(x){ x + runif(1, -2, 2)}
dprop <- function(x.star, x){ dunif(x.star-x, -2, 2) }

# Create 1 chain
chain1 <- MCMC(df, start=4, rprop, dprop, N=1000)
trace_plot(chain1)

# Create several chains
chain2 <- MCMC(df, start= 8, rprop, dprop, N=1000)
chain3 <- MCMC(df, start=-3, rprop, dprop, N=1000)
chain4 <- MCMC(df, start=-6, rprop, dprop, N=1000)
chains <- cbind(chain1, chain2, chain3, chain4)
trace_plot(chains)

dereksonderegger/STA578 documentation built on May 15, 2019, 3:58 a.m.