Description Usage Arguments Examples
Create a single Markov Chain Monte Carlo simulation
1 |
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. |
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.