Learn R Programming

dynamite: Bayesian Modeling and Causal Inference for Multivariate Longitudinal Data

The dynamite R package provides an easy-to-use interface for Bayesian inference of complex panel (time series) data comprising of multiple measurements per multiple individuals measured in time via dynamic multivariate panel models (DMPM). The main features distinguishing the package and the underlying methodology from many other approaches are:

  • Support for regular time-invariant effects, group-level random effects, and time-varying effects modeled via Bayesian P-splines.
  • Joint modeling of multiple measurements per individual (multiple channels) based directly on the assumed data-generating process. Individual channels can be univariate or multivariate.
  • Support for various distributions: Currently Gaussian, Multivariate Gaussian, Student t, Categorical, Ordered, Multinomial, Poisson, Bernoulli, Binomial, Negative Binomial, Gamma, Exponential, and Beta distributions are available, and these can be combined arbitrarily in multichannel models.
  • Allows evaluating realistic long-term counterfactual predictions that take into account the dynamic structure of the model by efficient posterior predictive distribution simulation.
  • Transparent quantification of parameter and predictive uncertainty due to a fully Bayesian approach.
  • Various visualization methods including a method for drawing and producing a TikZ code of the directed acyclic graph (DAG) of the model structure.
  • User-friendly and efficient R interface with state-of-the-art estimation via Stan. Both rstan and cmdstanr backends are supported, with both parallel chains and within-chain parallelization.

The dynamite package is developed with the support of the Research Council of Finland grant 331817 (PREDLIFE). For further information on DMPMs and the dynamite package, see the related papers:

  • Helske J. and Tikka S. (2024). Estimating Causal Effects from Panel Data with Dynamic Multivariate Panel Models. Advances in Life Course Research, 60, 100617. (Journal version, SocArXiv preprint)
  • Tikka S. and Helske J. (2024). dynamite: An R Package for Dynamic Multivariate Panel Models. (arXiv preprint)

Installation

You can install the most recent stable version of dynamite from CRAN or the development version from R-universe by running one the following lines:

install.packages("dynamite")
install.packages("dynamite", repos = "https://ropensci.r-universe.dev")

Example

A single-channel model with time-invariant effect of z, time-varying effect of x, lagged value of the response variable y and a group-specific random intercepts:

set.seed(1)
library("dynamite")
gaussian_example_fit <- dynamite(
  obs(y ~ -1 + z + varying(~ x + lag(y)) + random(~1), family = "gaussian") +
    splines(df = 20),
  data = gaussian_example, time = "time", group = "id",
  iter = 2000, chains = 2, cores = 2, refresh = 0
)

Summary of the model:

print(gaussian_example_fit)
#> Model:
#>   Family   Formula                                       
#> y gaussian y ~ -1 + z + varying(~x + lag(y)) + random(~1)
#> 
#> Correlated random effects added for response(s): y
#> 
#> Data: gaussian_example (Number of observations: 1450)
#> Grouping variable: id (Number of groups: 50)
#> Time index variable: time (Number of time points: 30)
#> 
#> NUTS sampler diagnostics:
#> 
#> No divergences, saturated max treedepths or low E-BFMIs.
#> 
#> Smallest bulk-ESS: 661 (sigma_nu_y_alpha)
#> Smallest tail-ESS: 1058 (sigma_nu_y_alpha)
#> Largest Rhat: 1.003 (sigma_y)
#> 
#> Elapsed time (seconds):
#>         warmup sample
#> chain:1  4.215  2.563
#> chain:2  4.118  2.592
#> 
#> Summary statistics of the time- and group-invariant parameters:
#> # A tibble: 6 × 10
#>   variable      mean median      sd     mad     q5   q95  rhat ess_bulk ess_tail
#>   <chr>        <dbl>  <dbl>   <dbl>   <dbl>  <dbl> <dbl> <dbl>    <dbl>    <dbl>
#> 1 beta_y_z    1.97   1.97   0.0116  0.0112  1.95   1.99   1.00    2815.    1434.
#> 2 sigma_nu_y… 0.0944 0.0933 0.0114  0.0107  0.0780 0.114  1.00     661.    1058.
#> 3 sigma_y     0.198  0.198  0.00373 0.00362 0.192  0.204  1.00    2580.    1254.
#> 4 tau_alpha_y 0.212  0.205  0.0483  0.0432  0.146  0.301  1.00    1731.    1606.
#> 5 tau_y_x     0.364  0.355  0.0740  0.0648  0.266  0.494  1.00    2812.    1504.
#> 6 tau_y_y_la… 0.107  0.105  0.0219  0.0213  0.0781 0.148  1.00    2387.    1682.

Posterior estimates of time-varying effects:

plot(gaussian_example_fit, types = c("alpha", "delta"), scales = "free")

And group-specific intercepts (for first 10 groups):

plot(gaussian_example_fit, types = "nu", groups = 1:10)

Traceplots and density plots for time-invariant parameters:

plot(gaussian_example_fit, plot_type = "trace", types = "beta")

Posterior predictive samples for the first 4 groups (using the samples based on the posterior distribution of the model parameters and observed data on the first time point):

library("ggplot2")
pred <- predict(gaussian_example_fit, n_draws = 100)
pred |>
  dplyr::filter(id < 5) |>
  ggplot(aes(time, y_new, group = .draw)) +
  geom_line(alpha = 0.25) +
  # observed values
  geom_line(aes(y = y), colour = "tomato") +
  facet_wrap(~id) +
  theme_bw()

Visualizing the model structure as a DAG (a snapshot at time t):

plot(gaussian_example_fit, plot_type = "dag", show_covariates = TRUE)

For more examples, see the package vignettes and the blog post about dynamite.

Related packages

  • The dynamite package uses Stan via rstan and cmdstanr (see also https://mc-stan.org), which is a probabilistic programming language for general Bayesian modelling.
  • The brms package also uses Stan, and can be used to fit various complex multilevel models.
  • Regression modeling with time-varying coefficients based on kernel smoothing and least squares estimation is available in package tvReg. The tvem package provides similar functionality for gaussian, binomial and poisson responses with mgcv backend.
  • plm contains various methods to estimate linear models for panel data, e.g., fixed effect models.
  • lavaan provides tools for structural equation modeling, and as such can be used to model various panel data models as well.

Contributing

Contributions are very welcome, see CONTRIBUTING.md for general guidelines.

Copy Link

Version

Install

install.packages('dynamite')

Monthly Downloads

370

Version

1.5.6

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Santtu Tikka

Last Published

April 7th, 2025

Functions in dynamite (1.5.6)

get_code

Extract the Stan Code of the dynamite Model
get_diagnostics

Get the diagnostics of a Stan model fit
fitted.dynamitefit

Extract Fitted Values of a dynamite Model
dynamiteformula

Model Formula for dynamite
get_data

Extract the Model Data of the dynamite Model
dynamite

Estimate a Bayesian Dynamic Multivariate Panel Model
get_algorithm

Get the algorithm used in a Stan model fit
gaussian_example

Simulated Data of a Gaussian Response
gaussian_example_fit

Model Fit for the Simulated Data of a Gaussian Response
get_draws

Get the draws of a Stan model fit
get_parameter_types

Get Parameter Types of the dynamite Model
get_priors

Get Prior Definitions of a dynamite Model
get_parameter_dims

Get Parameter Dimensions of the dynamite Model
get_parameter_names

Get Parameter Names of the dynamite Model
get_model_code

Get the model code of a Stan model fit
get_nchains

Get the number of chains of a Stan model fit
get_ndraws

Get the number of draws of a Stan model fit
get_pars_oi

Get pars_oi of a Stan model fit
get_max_treedepth

Get the maximum treedepth of chains of a Stan model fit
get_elapsed_time

Get the elapsed time of a Stan model fit
hmc_diagnostics

HMC Diagnostics for a dynamite Model
mice.impute.lag

Compute Lagging Values of an Imputed Response
multichannel_example

Simulated Multivariate Panel Data
mice.impute.lead

Compute Leading Values of an Imputed Response
multichannel_example_fit

Model Fit for the Simulated Multivariate Panel Data
lags

Add Lagged Responses as Predictors to Each Channel of a dynamite Model
mcmc_diagnostics

Diagnostic Values of a dynamite Model
print.lfo

Print the results from the LFO
random_spec

Additional Specifications for the Group-level Random Effects of the DMPM
plot.dynamitefit

Plots for dynamitefit Objects
loo.dynamitefit

Approximate Leave-One-Out (LOO) Cross-validation
plot.dynamiteformula

Plot the Model Structure as a Directed Acyclic Graph (DAG)
plot.lfo

Diagnostic Plot for Pareto k Values from LFO
lfo

Approximate Leave-Future-Out (LFO) Cross-validation
splines

Define the B-splines Used for the Time-varying Coefficients of the Model.
update.dynamitefit

Update a dynamite Model
lfactor

Define a Common Latent Factor for the dynamite Model.
predict.dynamitefit

Predict Method for a dynamite Model
nobs.dynamitefit

Extract the Number of Observations Used to Fit a dynamite Model
ndraws.dynamitefit

Return the Number of Posterior Draws of a dynamitefit Object
categorical_example_fit

Model Fit for the Simulated Categorical Multivariate Panel Data
dynamite-deprecated

Deprecated Functions in the dynamite Package
as_draws_df.dynamitefit

Convert dynamite Output to draws_df Format
confint.dynamitefit

Credible Intervals for dynamite Model Parameters
as.data.table.dynamitefit

Extract Samples From a dynamitefit Object as a Data Table
dynamite-package

The dynamite Package
dynamice

Estimate a Bayesian Dynamic Multivariate Panel Model With Multiple Imputation
coef.dynamitefit

Extract Regression Coefficients of a dynamite Model
as.data.frame.dynamitefit

Extract Samples From a dynamitefit Object as a Data Frame
categorical_example

Simulated Categorical Multivariate Panel Data