| fit_dynamic_model | R Documentation |
Fits a GMRF state-space model in which a latent trajectory z_t evolves
as either a first-order random walk (latent_dynamics = "rw", the default)
or a stationary AR(1) process (latent_dynamics = "ar1"), and the
observations are linked to it through a Poisson (log link), binomial
(logit link) or multinomial (additive-log-ratio link) observation model.
See DynCount-package for an overview.
fit_dynamic_model(
y,
family = c("poisson", "binomial", "multinomial"),
trials = NULL,
innovations = c("gaussian", "t", "mixture", "sv"),
latent_dynamics = c("rw", "ar1"),
include_mu = FALSE,
zeros = c("none", "inflated", "missing"),
zero_inflation = FALSE,
prior = dynamic_prior(),
nsave = 4000,
nburn = 1000,
thin = 1,
horizon = 0L,
forecast_trials = NULL,
forecast_offset = NULL,
offset = NULL,
baseline = "largest",
verbose = FALSE,
seed = NULL
)
y |
For the Poisson and binomial families a numeric vector of
non-negative integer observations (counts, or numbers of successes). For
the multinomial family an |
family |
Observation model, |
trials |
For |
innovations |
Distribution of the latent increments, one of
|
latent_dynamics |
Latent state evolution: |
include_mu |
Logical; include a scalar |
zeros |
Zero handling for the Poisson and binomial families: |
zero_inflation |
A single |
prior |
A |
nsave |
Number of posterior draws to keep. Default |
nburn |
Number of burn-in iterations. Default |
thin |
Thinning interval: one draw is kept every |
horizon |
Forecast horizon |
forecast_trials |
For the binomial and multinomial families, the number
of trials (binomial) or the total count per period (multinomial) over the
forecast horizon (length 1, recycled, or length |
forecast_offset |
Known offset over the forecast horizon. For the
Poisson and binomial families a vector of length 1 (recycled) or
|
offset |
Optional known per-observation offset on the linear-predictor
scale. For the Poisson family (length 1 or |
baseline |
Multinomial family only: the baseline category, given as a
column index or a column name of |
verbose |
Logical; print a progress bar. Default |
seed |
Optional random seed for reproducibility. The previous state of the global random number generator is restored after fitting. |
Estimation. The model is estimated by Metropolis-within-Gibbs
MCMC. The latent states are updated one site at a time by adaptive
random-walk Metropolis steps that use their Gaussian Markov random field
(GMRF) full conditionals. States without an observation (the initial
state, structural zeros, zero-total rows) are drawn exactly from their
Gaussian full conditionals. The
innovation parameters, the drift/intercept \mu and the AR(1)
coefficient \rho are updated by Gibbs steps (with a Metropolis step
for the Student-t degrees of freedom).
Multinomial family. With family = "multinomial", y is an
n \times K matrix of category counts (rows = time, columns =
categories, K \ge 2); the row totals N_t are treated as known
trials. One category b is the baseline, and the remaining
K - 1 categories each get their own latent additive-log-ratio (ALR)
series z_{t,k} = \log(p_{t,k} / p_{t,b}), so that
y_t \sim \mathrm{Multinomial}(N_t, p_t), \qquad
p_{t,k} = \frac{\exp(o_{t,k} + z_{t,k})}{1 + \sum_{j \ne b}
\exp(o_{t,j} + z_{t,j})}, \qquad p_{t,b} = \frac{1}{1 + \sum_{j \ne b}
\exp(o_{t,j} + z_{t,j})},
with known offsets o_{t,k} (zero by default). The chosen latent
dynamics, innovation structure and drift/intercept setting apply to every
ALR series, but no parameters are shared across categories. Each
series has its own innovation variance (and, where relevant, degrees of
freedom, mixture components or volatility path), its own \rho and
\mu, and its own copy of prior. The series are coupled only
through the multinomial likelihood, and each series is updated with the
other categories held at their current values. A row with
N_t = 0 carries no information about the shares and is handled like
a missing observation. Zero inflation is not available for this family
(zeros must be "none"). Running time grows linearly in K - 1.
Zero handling. Under zeros = "inflated" a latent gate decides,
for each observed zero, whether it is structural (gate closed) or an
ordinary sampling zero produced by the Poisson/binomial process (gate
open); the gate-open probability is a single parameter that is constant
over time. See structural_zero_prob(). Under zeros = "missing" the
observed zeros are treated as missing values.
Forecasting. Forecasts are obtained by forward simulation from the
posterior draws, so they can be computed after fitting for any horizon with
forecast.dynamic_fit(). Setting horizon = H additionally simulates an
H-step forecast right after sampling and stores it in the fit (under the
same seed).
Reproducibility. With a seed, the sampler and the fit-time
forecast run with that seed, and the previous state of the global random
number generator is restored afterwards.
An object of class "dynamic_fit": a list with three elements.
drawsPosterior draws only. For the Poisson and binomial families a list of matrices/vectors with one row (or element) per stored draw:
zLatent states aligned with the observations
(draws x n).
z0The initial latent state, one period before the first
observation, which carries the init_mean / init_var prior.
sig2Variance of the increment leading into each
z_t (draws x n); the first column is the increment from
z0.
fitted, yrepUnconditional fitted means and
posterior predictive replicates; under zeros = "inflated" they
include the zero-inflation gate.
fitted_open, yrep_openTheir conditional-on-gate-open counterparts: the latent-implied mean, and a replicate drawn straight from the observation model.
gate, pi_openZero-inflation gate indicators and
gate-open probability; NULL unless zeros = "inflated".
rho, muAR(1) coefficient (1 under the random walk)
and drift/intercept (0 unless include_mu = TRUE).
innov_varA representative innovation variance whose
definition depends on innovations: the estimated constant
increment variance \sigma^2 for "gaussian"; the marginal
increment variance for "t" (\sigma^2 \nu / (\nu - 2)) and
"mixture" (\sigma^2 \sum_h \eta_h \sigma_h^2); and the
average of the per-increment variances \exp(h_t) over the
in-sample transitions for "sv". For "t", if a draw has
\nu \le 2 (possible only when df_min <= 2), the undefined
variance factor is replaced by the convention 3.
scaleThe overall innovation scale \sigma^2
(NULL for "sv").
nuStudent-t degrees of freedom (NULL unless
innovations = "t").
mix_weight, mix_varMixture weights and component
variances (\sigma^2 \sigma_h^2), draws x mix_components
(NULL unless innovations = "mixture"). The components are
exchangeable and not identified individually. Within each draw
they are stored in order of increasing variance, which is a
labelling convention rather than an identification, so
per-component summaries are meaningful only if the components
are clearly separated. Label-invariant quantities, such as
innov_var and the forecasts, are unaffected.
sv_mu, sv_phi, sv_sigmaLevel, persistence and
volatility of the AR(1) log-variance process (NULL unless
innovations = "sv").
forecast_z, forecast_yLatent and response forecasts
when horizon >= 1 (NULL otherwise); forecast_y is
unconditional, i.e. includes the gate.
Use yrep, not yrep_open, for posterior predictive checks; without
zero inflation the conditional and unconditional pairs are identical.
See predict.dynamic_fit().
For the multinomial family the same components carry an extra
trailing category dimension, named by the category labels:
latent quantities (z, sig2, forecast_z, mix_weight,
mix_var) are arrays with one slice per non-baseline category, and
z0, rho, mu, innov_var, scale, nu and the sv_*
parameters are draws x (K - 1) matrices; response-scale quantities
(fitted – the expected counts N_t p_{t,k} –, yrep,
forecast_y, and the additional fitted_prob / forecast_prob
holding the category shares) are draws x time x K arrays including
the baseline, in the original column order. fitted_open,
yrep_open, gate and pi_open are NULL.
dataThe observed inputs: y, trials (the row totals for the
multinomial family), the series length n, and the resolved
per-observation offset. For the multinomial family also K, the
categories (column labels), and the baseline index and
baseline_name.
specThe model and MCMC specification: family, innovations,
latent_dynamics, include_mu, zeros, prior, nsave, nburn,
thin, horizon, and the forecast-period inputs forecast_offset /
forecast_trials used for the stored forecast (NULL when not
applicable).
dynamic_prior(), forecast.dynamic_fit(), predict.dynamic_fit(),
plot_fitted(), structural_zero_prob(), simulate_dynamic_multinomial()
sim <- simulate_dynamic_poisson(n = 60, sigma = 0.2, log_rate0 = 2, seed = 1)
fit <- fit_dynamic_model(sim$y, family = "poisson", nsave = 300, nburn = 200,
seed = 1)
summary(fit)
forecast(fit, horizon = 5)
# multinomial choice counts: three categories, baseline chosen automatically
simm <- simulate_dynamic_multinomial(n = 40, sigma = 0.15, trials = 100,
alr0 = c(-1, -0.5), seed = 2)
fitm <- fit_dynamic_model(simm$y, family = "multinomial",
nsave = 200, nburn = 100, seed = 2)
fitm
summary(fitm)$params
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.