Learn R Programming

bgms (version 0.1.6.3)

simulate.bgms: Simulate Data from a Fitted bgms Model

Description

Generates new observations from the Markov Random Field model using the estimated parameters from a fitted bgms object.

Usage

# S3 method for bgms
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"),
  ...
)

Value

If method = "posterior-mean": A matrix with nsim rows and p columns containing simulated observations.

If method = "posterior-sample": A list of matrices, one per posterior draw, each with nsim rows and p columns.

Arguments

object

An object of class bgms.

nsim

Number of observations to simulate. Default: 500.

seed

Optional random seed for reproducibility.

method

Character string specifying which parameter estimates to use:

"posterior-mean"

Use posterior mean parameters (faster, single simulation).

"posterior-sample"

Sample from posterior draws, producing one dataset per draw (accounts for parameter uncertainty). This method uses parallel processing when cores > 1.

ndraws

Number of posterior draws to use when method = "posterior-sample". If NULL, uses all available draws.

iter

Number of Gibbs iterations for equilibration before collecting samples. Default: 1000.

cores

Number of CPU cores for parallel execution when method = "posterior-sample". Default: parallel::detectCores().

display_progress

Character string specifying the type of progress bar. Options: "per-chain", "total", "none". Default: "per-chain".

...

Additional arguments (currently ignored).

Details

This function uses the estimated interaction and threshold parameters to generate new data via Gibbs sampling. When method = "posterior-sample", parameter uncertainty is propagated to the simulated data by using different posterior draws. Parallel processing is available for this method via the cores argument.

See Also

predict.bgms for computing conditional probabilities, simulate_mrf for simulation with user-specified parameters.

Examples

Run this code
# \donttest{
# Fit a model
fit <- bgm(x = Wenchuan[, 1:5], chains = 2)

# Simulate 100 new observations using posterior means
new_data <- simulate(fit, nsim = 100)

# Simulate with parameter uncertainty (10 datasets)
new_data_list <- simulate(fit, nsim = 100, method = "posterior-sample", ndraws = 10)

# Use parallel processing for faster simulation
new_data_list <- simulate(fit, nsim = 100, method = "posterior-sample",
                          ndraws = 100, cores = 2)
# }

Run the code above in your browser using DataLab