library(bgms)
data = Wenchuan[, 1:5]
head(data)Ordinal Network
This page has not yet passed technical or readability review.
An end-to-end analysis of ordinal data using the ordinal Markov random field. This example uses the Wenchuan earthquake PTSD dataset bundled with bgms.
Data
The Wenchuan dataset contains responses from 362 survivors of the 2008 Wenchuan earthquake on 17 posttraumatic stress items, scored on a five-point scale from “not at all” to “extremely”. We analyze a subset of the first five items.
Fit the model
Pass the data to bgm(). Because all variables are ordinal, bgms fits an ordinal MRF with spike-and-slab edge selection by default.
fit = bgm(data, seed = 1234)Posterior summaries
summary() returns posterior means, standard deviations, R-hat, and effective sample sizes for all partial association and threshold parameters. coef() provides posterior means and inclusion probabilities.
summary(fit)
coef(fit)Edge selection
The spike-and-slab prior assigns each edge a posterior inclusion probability. Values near 1 indicate strong evidence the edge is present; values near 0 indicate strong evidence the edge is absent; values near 0.5 are inconclusive.
coef(fit)$indicatorWhen the prior inclusion probability equals 0.5 (the default for a Bernoulli prior), inclusion probabilities convert directly to Bayes factors:
p = coef(fit)$indicator[1, 5]
BF_10 = p / (1 - p)
BF_10The reciprocal gives the Bayes factor in favor of exclusion:
1 / BF_10Interpretation
Threshold the inclusion probabilities at 0.5 to obtain the median probability network, then plot with qgraph (Epskamp et al., 2012).
library(qgraph)
median_probability_network = coef(fit)$pairwise
median_probability_network[coef(fit)$indicator < 0.5] = 0.0
qgraph(median_probability_network,
theme = "TeamFortress",
maximum = 1,
fade = FALSE,
color = c("#f0ae0e"), vsize = 10, repulsion = .9,
label.cex = 1, label.scale = "FALSE",
labels = colnames(data)
)Diagnostics
Check convergence with the R-hat and ESS columns from the pairwise summary table.
summary(fit)$pairwiseNUTS-specific diagnostics — E-BFMI, divergent transitions, tree depth, and warmup equilibration — are available via fit$nuts_diag:
fit$nuts_diag$summary
fit$nuts_diag$warmup_checkSee the MCMC Diagnostics guide for interpretation of these quantities.