Skip to content

Instantly share code, notes, and snippets.

@rjbgoudie
Created May 13, 2011 09:58
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/970279 to your computer and use it in GitHub Desktop.
Save rjbgoudie/970279 to your computer and use it in GitHub Desktop.
structmcmc: basic operation, discrete data
# Basic operation of structmcmc. See https://github.com/rbtgde/structmcmc
# Setup data frame
x1 <- factor(c("a", "a", "g", "c", "c", "a", "g", "a", "a"))
x2 <- factor(c(2, 2, 4, 3, 1, 4, 4, 4, 1))
x3 <- factor(c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE))
x <- data.frame(x1 = x1, x2 = x2, x3 = x3)
# Draw samples from the posterior using MC^3.
set.seed(1234)
initial <- bn(c(), c(), c())
priorgraph <- bn(c(), c(1), c(2))
prior <- priorGraph(priorgraph, 0.5)
mcmc <- posterior(data = x, method = "mc3", prior = prior,
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", prior = prior)
epexact <- ep(exact)
levelplot(epexact)
# Comparing multiple MCMC runs
mcmc2 <- posterior(data = x, method = "mc3", prior = prior,
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)))
# 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