Skip to content

Instantly share code, notes, and snippets.

@rjbgoudie
Last active May 28, 2019 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjbgoudie/974390 to your computer and use it in GitHub Desktop.
Save rjbgoudie/974390 to your computer and use it in GitHub Desktop.
structmcmc: basic operation, normal data
# Basic operation of structmcmc. See https://github.com/rbtgde/structmcmc
# Setup data frame
x1 <- rnorm(20)
x2 <- rnorm(20)
x3 <- rnorm(20)
x <- matrix(c(x1, x2, x3), ncol = 3)
net <- bn(integer(0), 1, 2)
# Draw samples from the posterior using MC^3.
set.seed(1234)
initial <- bn(c(), c(), c())
mcmc <- posterior(data = x, method = "mc3", logScoreFUN = logScoreNormalFUN(),
nSamples = 10000, nBurnin = 1000, initial = initial)
# Compute and plot estimated edge probabilities
epmcmc <- ep(mcmc)
levelplot(epmcmc)
# Exact evaluation by exhaustive enumeration
exact <- posterior(x, "exact", logScoreFUN = logScoreNormalFUN())
epexact <- ep(exact)
levelplot(epexact)
# Comparing multiple MCMC runs
mcmc2 <- posterior(data = x, method = "mc3", logScoreFUN = logScoreNormalFUN(),
nSamples = 10000, nBurnin = 1000, initial = initial)
epmcmc2 <- ep(mcmc2)
levelplot(epmcmc2)
# Compare the final edge probabilities between runs
splom(bnpostmcmc.list(mcmc, mcmc2))
levelplot(ep.list(exact = epexact, mcmc = epmcmc))
# View how the cumulative edge probabilities change as samples are drawn.
xyplot(cumep(bnpostmcmc.list(mcmc, mcmc2)))
# View how the moving averaging edge probabilities change as samples are drawn.
xyplot(mwep(bnpostmcmc.list(mcmc, mcmc2), method = "offline"))
# Cumulative total variance distance
exactgp <- gp(exact)
xyplot(cumtvd(exactgp, bnpostmcmc.list(mcmc, mcmc2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment