Skip to content

Instantly share code, notes, and snippets.

@khakieconomics
Created May 19, 2017 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khakieconomics/a7ed85100581cec6c0eeaf2ce806291b to your computer and use it in GitHub Desktop.
Save khakieconomics/a7ed85100581cec6c0eeaf2ce806291b to your computer and use it in GitHub Desktop.
Plotting posterior predictive density
library(ggplot2); library(dplyr)
aa <- data_frame(a = rnorm(30, 2, .1), b = rnorm(30, .5, .05), sigma = rnorm(30, 1, .1))
g <- ggplot(data.frame(x = c(-1, 5.5)), aes(x))
for(i in 1:nrow(aa)) {
g <- g +
stat_function(fun = dnorm, args = list(mean = c(aa$a[i] + aa$b[i]), sd = aa$sigma[i]), alpha = 0.3)
}
g +
geom_linerange(data = data.frame(ymin = 0, ymax = max(dnorm(1, aa$a + aa$b, aa$sigma)), x = 1), aes(x= x, ymin = ymin, ymax = ymax)) +
geom_point(data= data.frame(x = rep(1, nrow(aa) +1), y = c(0, dnorm(1, aa$a + aa$b, aa$sigma))), aes(x = x, y = y), colour = "red") +
xlab("y") +
ylab("Density") +
ggtitle("We can evaluate at the actual outcome\nover posterior draws", subtitle = "f(y | x, a, b, s)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment