knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  fig.align = "center",
  fig.width = 9, 
  fig.height = 6
)
#' @srrstats {G1.1,  G1.2}

dynamite: Bayesian Modeling and Causal Inference for Multivariate Longitudinal Data

Project Status: Active – The project has reached a stable, usable state and is being actively developed. R-CMD-check Codecov test coverage Status at rOpenSci Software Peer Review dynamite status badge dynamite CRAN badge

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:

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:

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:

library("dynamite")
ggplot2::theme_set(ggplot2::theme_bw())
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
)
set.seed(1)
library("dynamite")
gaussian_example_fit <- update(
  gaussian_example_fit,
  iter = 2000, warmup = 1000, thin = 1,
  chains = 2, cores = 2, refresh = 0
)

Summary of the model:

print(gaussian_example_fit)

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

Contributing

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



santikka/dynamite documentation built on April 17, 2025, 11:47 a.m.