Methods

Methods for inspecting, summarizing, and using bgms and bgmCompare model fits. Both are S7 classes; methods use S3 dispatch for compatibility with base R generics.

print

print(x, ...)
Argument Description
x An object of class bgms or bgmCompare.
... Ignored.

Returns x invisibly.

summary

summary(object, ...)
Argument Description
object An object of class bgms or bgmCompare.
... Currently ignored.

Returns a summary object containing:

  • main — Data frame of main-effect summaries (mean, sd, MCSE, ESS, Rhat).
  • pairwise — Data frame of partial association summaries.
  • indicator — Data frame of inclusion indicator summaries (if edge/difference selection enabled).

For bgmCompare, summaries are split into baseline and difference components.

coef

Extract posterior mean coefficients.

coef(object, ...)
Argument Description
object An object of class bgms or bgmCompare.
... Ignored.

Returns (by class):

  • bgms: main, pairwise, and (if available) indicator.
  • bgmCompare: main_effects_raw, pairwise_effects_raw, main_effects_groups, pairwise_effects_groups, and indicators.

predict

Compute conditional probability distributions for one or more variables given the observed values of other variables in the data.

predict(object, newdata, variables = NULL,
        type = c("probabilities", "response"),
        method = c("posterior-mean", "posterior-sample"),
        ndraws = NULL, seed = NULL, ...)

predict(object, newdata, group, variables = NULL,
        type = c("probabilities", "response"),
        method = c("posterior-mean"), ...)
Argument Description
object An object of class bgms or bgmCompare.
newdata A matrix or data frame matching the variables used in fitting.
group Integer group index for bgmCompare only (required there).
variables Character names, integer indices, or NULL (all variables).
type "probabilities" or "response".
method bgms: "posterior-mean" or "posterior-sample"; bgmCompare: "posterior-mean".
ndraws For bgms with method = "posterior-sample".
seed Optional random seed.

Return shape depends on model type:

  • Ordinal: probabilities per category or predicted categories.
  • GGM: conditional means/sds or conditional means.
  • Mixed: combined discrete and continuous outputs.

For bgmCompare, outputs use group-specific parameters (baseline plus group differences).

Example

# Predict probabilities for the first observation's variable 1
# given its observed values on variables 2-17
predict(fit, newdata = Wenchuan[1, , drop = FALSE], variables = 1)
$intrusion
         cat_0     cat_1      cat_2       cat_3        cat_4
[1,] 0.3683497 0.5816389 0.04811667 0.001882903 1.181429e-05
# Predict responses (most likely category) for first 3 observations
predict(fit, newdata = Wenchuan[1:3, ], variables = 1:3, type = "response")
     intrusion dreams flash
[1,]         1      0     0
[2,]         1      0     0
[3,]         2      1     1

simulate

Generate new observations from a fitted model.

simulate(object, nsim = 500, seed = NULL,
         method = c("posterior-mean", "posterior-sample"),
         ndraws = NULL, iter = 1000,
         cores = parallel::detectCores(),
         display_progress = c("per-chain", "total", "none"), ...)

simulate(object, nsim = 500, seed = NULL, group,
         method = c("posterior-mean"), iter = 1000, ...)
Argument Description
object An object of class bgms or bgmCompare.
nsim Number of observations to simulate. Default: 500.
seed Optional random seed.
group Integer group index for bgmCompare only (required there).
method bgms: "posterior-mean" or "posterior-sample"; bgmCompare: "posterior-mean".
ndraws For bgms with method = "posterior-sample".
iter Gibbs iterations for equilibration. Default: 1000.
cores, display_progress Used for bgms with method = "posterior-sample".

Returns:

  • bgms, posterior-mean: matrix (nsim x p).
  • bgms, posterior-sample: list of matrices (one per draw).
  • bgmCompare: matrix (nsim x p) for the selected group.

Example

# Fit a model
fit = bgm(Wenchuan, seed = 1234)
# Simulate 100 new observations from the posterior mean network
sim_data = simulate(fit, nsim = 100, seed = 42)
head(sim_data)
     intrusion dreams flash upset physior avoidth avoidact amnesia lossint
[1,]         2      2     3     2       4       2        1       3       1
[2,]         3      3     2     1       1       2        1       0       1
[3,]         1      1     1     0       0       1        1       0       0
[4,]         1      0     0     0       0       0        1       0       3
[5,]         2      1     1     2       1       3        3       1       1
[6,]         1      0     1     3       2       2        1       0       0
     distant numb future sleep anger concen hyper startle
[1,]       1    0      1     3     2      3     1       3
[2,]       1    1      2     3     2      1     1       1
[3,]       0    0      0     0     1      0     0       0
[4,]       1    0      1     1     1      1     1       1
[5,]       1    1      0     1     2      1     1       1
[6,]       0    0      1     2     3      0     0       2

simulate_mrf

Simulate observations from a Markov Random Field using user-specified parameters (no fitted model needed).

simulate_mrf(
  num_states,
  num_variables,
  num_categories,
  pairwise,
  main,
  variable_type = "ordinal",
  baseline_category,
  iter = 1000,
  seed = NULL
)
Argument Description
num_states Number of observations to generate.
num_variables Number of variables in the MRF.
num_categories Integer or integer vector. Number of response categories on top of the base category (1 = binary).
pairwise Symmetric matrix of partial associations. For continuous variables, this is the precision matrix (must be positive definite).
main Main-effect parameters. For ordinal variables: a matrix of category thresholds. For continuous variables: a means vector.
variable_type "ordinal", "blume-capel", or "continuous". Can be a vector for mixed types (ordinal/Blume-Capel only).
baseline_category Integer vector of baseline categories for Blume–Capel variables.
iter Gibbs iterations for equilibration (ordinal only). Default: 1000.
seed Optional random seed.

Returns a (num_states x num_variables) matrix of simulated observations.

For ordinal and Blume-Capel variables, the Gibbs sampler generates observations from full conditional distributions. For continuous variables, observations are drawn directly from \(N(\mu, \Omega^{-1})\).

Example

# Simulate from a 3-variable binary MRF with specified parameters
pairwise = matrix(c(
   0.0,  0.5, -0.3,
   0.5,  0.0,  0.4,
  -0.3,  0.4,  0.0
), nrow = 3, byrow = TRUE)

main = matrix(c(0.0, -0.5, 0.2), nrow = 3, ncol = 1)

sim_data = simulate_mrf(
 num_states = 500,
 num_variables = 3,
 num_categories = c(1, 1, 1),
 pairwise = pairwise,
 main = main,
 seed = 123
)
head(sim_data)
     [,1] [,2] [,3]
[1,]    1    1    0
[2,]    1    1    1
[3,]    1    1    1
[4,]    1    1    1
[5,]    0    1    1
[6,]    1    0    0

See also

bgm(), bgmCompare(), extractor functions