| dynamite {dynamite} | R Documentation |
Estimate a Bayesian Dynamic Multivariate Panel Model
Description
Fit a Bayesian dynamic multivariate panel model (DMPM) using Stan for
Bayesian inference. The dynamite package supports a wide range of
distributions and allows the user to flexibly customize the priors for the
model parameters. The dynamite model is specified using standard R formula
syntax via dynamiteformula(). For more information and examples,
see 'Details' and the package vignettes.
The formula method returns the model definition as a quoted expression.
Information on the estimated dynamite model can be obtained via
print including the following: The model formula, the data, the smallest
effective sample sizes, largest Rhat and summary statistics of the
time- and group-invariant model parameters.
The summary() method provides statistics of the posterior samples of the
model; this is an alias of as.data.frame.dynamitefit() with
summary = TRUE.
Usage
dynamite(
dformula,
data,
time,
group = NULL,
priors = NULL,
backend = "rstan",
verbose = TRUE,
verbose_stan = FALSE,
stanc_options = list("O0"),
threads_per_chain = 1L,
grainsize = NULL,
custom_stan_model = NULL,
debug = NULL,
...
)
## S3 method for class 'dynamitefit'
formula(x, ...)
## S3 method for class 'dynamitefit'
print(x, full_diagnostics = FALSE, ...)
## S3 method for class 'dynamitefit'
summary(object, ...)
Arguments
dformula |
[ |
data |
[ |
time |
[ |
group |
[ |
priors |
[ |
backend |
[ |
verbose |
[ |
verbose_stan |
[ |
stanc_options |
[ |
threads_per_chain |
[ |
grainsize |
[ |
custom_stan_model |
[ |
debug |
[ |
... |
For |
x |
[ |
full_diagnostics |
By default, the effective sample size (ESS) and Rhat
are computed only for the time- and group-invariant parameters
( |
object |
[ |
Details
The best-case scalability of dynamite in terms of data size should be
approximately linear in terms of number of time points and and number of
groups, but as wall-clock time of the MCMC algorithms provided by Stan can
depend on the discrepancy of the data and the model (and the subsequent
shape of the posterior), this can vary greatly.
Value
dynamite returns a dynamitefit object which is a list containing
the following components:
-
stanfit
Astanfitobject, seerstan::sampling()for details. -
dformulas
A list ofdynamiteformulaobjects for internal use. -
data
A processed version of the inputdata. -
data_name
Name of the input data object. -
stan
Alistcontaining various elements related to Stan model construction and sampling. -
group_var
Name of the variable defining the groups. -
time_var
Name of the variable defining the time index. -
priors
Data frame containing the used priors. -
backend
Either"rstan"or"cmdstanr"indicating which package was used in sampling. -
call
Original function call as an object of classcall.
formula returns a quoted expression.
print returns x invisibly.
summary returns a data.frame.
References
Santtu Tikka and Jouni Helske (2023). dynamite: An R Package for Dynamic
Multivariate Panel Models. arXiv preprint,
https://arxiv.org/abs/2302.01607.
Jouni Helske and Santtu Tikka (2022). Estimating Causal Effects from Panel Data with Dynamic Multivariate Panel Models. SocArxiv preprint, https://osf.io/preprints/socarxiv/mdwu5/.
See Also
Model fitting
dynamice(),
get_priors(),
update.dynamitefit()
Model formula construction
dynamiteformula(),
lags(),
lfactor(),
random_spec(),
splines()
Model outputs
as.data.frame.dynamitefit(),
as.data.table.dynamitefit(),
as_draws_df.dynamitefit(),
coef.dynamitefit(),
confint.dynamitefit(),
get_code(),
get_data(),
get_parameter_dims(),
get_parameter_names(),
get_parameter_types(),
ndraws.dynamitefit(),
nobs.dynamitefit()
Examples
data.table::setDTthreads(1) # For CRAN
# Please update your rstan and StanHeaders installation before running
# on Windows
if (!identical(.Platform$OS.type, "windows")) {
fit <- dynamite(
dformula = obs(y ~ -1 + varying(~x), family = "gaussian") +
lags(type = "varying") +
splines(df = 20),
gaussian_example,
"time",
"id",
chains = 1,
refresh = 0
)
}
data.table::setDTthreads(1) # For CRAN
formula(gaussian_example_fit)
data.table::setDTthreads(1) # For CRAN
print(gaussian_example_fit)
data.table::setDTthreads(1) # For CRAN
summary(gaussian_example_fit,
types = "beta",
probs = c(0.05, 0.1, 0.9, 0.95)
)