plot_chain: Graph the first few steps of a MCMC chain

Description Usage Arguments Examples

Description

Graph the first few steps of a MCMC chain

Usage

1
plot_chain(density, chain, proposed)

Arguments

density

A ggplot2 object that shows a density function

chain

A vector of values of the chain

proposed

A vector of proposed values for the chain

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Target Distribution
df <- function(x){
   return(.7*dnorm(x, mean=2, sd=1) + .3*dnorm(x, mean=5, sd=1))
}
x <- seq(-3,12, length=200)
density.data <- data.frame(x=x, y=df(x))
density <- ggplot( density.data, aes(x=x, y=y)) +
   geom_line() +
   labs(y='f(x)', x='x')
density

# Define the proposal distribution
rproposal <- function(x.i){
  out <- x.i + runif(1, -2, 2)  # x.i + 1 observation from Uniform(-2, 2)
  return(out)
}

# First chain step
x <- 3;       # starting value for the chain
x.star <- 3   # initialize the vector of proposal values
x.star[2] <- rproposal( x[1] )
x.star[2]
if( df(x.star[2]) / df(x[1])  >  runif(1) ){
  x[2] <- x.star[2]
}else{
  x[2] <- x[1]
}

# Show the first proposed value
plot_chain(density, x, x.star)

# Take a few more steps in the chain
for( i in 2:10 ){
  x.star[i+1] <- rproposal( x[i] )
  if( df(x.star[i+1]) / df(x[i]) > runif(1) ){
    x[i+1] <- x.star[i+1]
  }else{
    x[i+1] <- x[i]
  }
}

# Show the first 10 steps of the chain
plot_chain(density, x, x.star)

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