simulate_dynamic_multinomial: Simulate a multinomial dynamic series

View source: R/simulate.R

simulate_dynamic_multinomialR Documentation

Simulate a multinomial dynamic series

Description

Generates K - 1 independent latent additive-log-ratio (ALR) processes (random walk or AR(1)), one per non-baseline category, and multinomial choice counts with known totals. The ALR series z_{t,k} = \log(p_{t,k} / p_{t,b}) share no parameters: sigma, rho and mu may each be a single value (recycled) or a vector of length K - 1 giving one value per non-baseline category.

Usage

simulate_dynamic_multinomial(
  n,
  sigma,
  trials,
  alr0 = c(0, 0),
  baseline = length(alr0) + 1L,
  rho = 1,
  mu = 0,
  offset = 0,
  categories = NULL,
  seed = NULL
)

Arguments

n

Number of observations.

sigma

Standard deviation(s) of the latent increments (Gaussian); length 1 or K - 1.

trials

Total count per period: a single number (recycled) or a length-n vector.

alr0

Numeric vector of length K - 1: the initial ALR values z_{1,k} of the non-baseline categories, in column order. Its length determines the number of categories K. Default c(0, 0) (three equally likely categories).

baseline

The baseline category: a column index in ⁠1..K⁠ of the returned count matrix, or one of the categories. Default K (last column). Pass the returned baseline to fit_dynamic_model() to fit the model on the same ALR scale as the simulation.

rho

AR(1) coefficient(s) of the latent processes z_{t,k} = \mu_k + \rho_k z_{t-1,k} + \varepsilon_{t,k}; length 1 or K - 1. Default 1 (random walks).

mu

Drift (random walk) / intercept (AR(1)) of the latent processes; length 1 or K - 1. Default 0.

offset

Known offset on the ALR scale: a scalar, a length K - 1 vector (one constant per non-baseline category) or an n \times (K - 1) matrix (columns aligned with the non-baseline categories). Default 0.

categories

Optional character vector of length K with the category labels (column names of the returned counts). Default "cat1", ..., "catK".

seed

Optional random seed. The previous state of the global random number generator is restored afterwards.

Value

A list with components y (an ⁠n x K⁠ matrix of counts with the baseline in column baseline), trials, alr (the ⁠n x (K - 1)⁠ latent ALR paths z_{t,k}), prob (the ⁠n x K⁠ matrix of category probabilities), offset, baseline (the column index) and categories.

Examples

sim <- simulate_dynamic_multinomial(n = 50, sigma = 0.15, trials = 200,
                                    alr0 = c(-0.5, 0.5), seed = 1)
head(sim$y)
colSums(sim$y)
# fit on the simulated ALR scale by passing the simulated baseline
fit <- fit_dynamic_model(sim$y, family = "multinomial", baseline = sim$baseline,
                         nsave = 200, nburn = 100, seed = 1)
summary(fit)$params

DynCount documentation built on Sept. 28, 2026, 5:10 p.m.