
rsDCM is an R package that provides a robust and sparse method for group-level Dynamic Causal Modelling (DCM) of fMRI data, together with an R implementation of single-subject DCM ported from the MATLAB SPM25 toolbox.
From GitHub (development version):
# install.packages("remotes")
remotes::install_github("Kay202/rsDCM")From CRAN:
install.packages("rsDCM")The following is an example of using the main function rsDCM()
library(rsDCM)
# Real 48-subject NARPS group analysis. The inputs are precomputed
# subject-level DCM summaries (a posterior mean and covariance per subject).
data(narps_dcm)
N <- nrow(narps_dcm$eta)
p <- ncol(narps_dcm$eta)
# Per-parameter variance-component basis, and an intercept-only design.
V_list <- lapply(seq_len(p), function(k) { V <- matrix(0, p, p); V[k, k] <- 1; V })
X_G <- matrix(1, N, 1, dimnames = list(NULL, "intercept"))
# rsDCM fit
fit <- rsdcm(narps_dcm$eta, narps_dcm$Cp, X_G, V_list, verbose = FALSE)
rownames(fit$beta_mat) <- rownames(fit$inclusion) <- narps_dcm$parameter_names
# Group-level connections selected by the spike-and-slab prior (PIP > 0.5)
sel <- fit$inclusion[, 1] > 0.5
round(cbind(estimate = fit$beta_mat[sel, 1],
PIP = fit$inclusion[sel, 1]), 3)
# Subjects the Student-t weighting down-weights most
w <- rowMeans(matrix(fit$weights, N, p, byrow = TRUE))
narps_dcm$subject[order(w)][1:5]The following is an example of fitting original DCM using this package:
library(rsDCM)
# Three-region toy DCM shipped with the package (482 scans, one input)
data(toy_dcm)
# Invert. Takes roughly 75 seconds on this dataset.
fit <- dcm_estimate(toy_dcm)
# Posterior connectivity
round(fit$Ep$A, 3)
# Variational free energy (log-evidence proxy)
fit$FThe introductory vignette has more detail:
vignette("introduction", package = "rsDCM")User-facing entry points:
dcm_estimate(): full DCM inversion.dcm_fmri_priors(): build priors from adjacency arrays.dcm_nlsi_GN(): variational Laplace optimisation (lower-level).dcm_int(): bilinear-system integrator (lower-level).dcm_fx_fmri(), dcm_gx_fmri(): neural and BOLD equations.dcm_evidence(): AIC / BIC summary of a fit.rsdcm_options(): adjust runtime options (e.g. FD step).Group-level (Parametric Empirical Bayes):
dcm_peb_prepare(), dcm_peb_run(): fit a second- or third-level PEB.dcm_peb_of_pebs(): third-level PEB-of-PEBs over a directory of subject PEBs.dcm_peb_design(): build the between-subject design matrix.dcm_peb_files(), dcm_peb_load(): locate/load subject PEBs (.rds or MATLAB .mat).Group-level (robust and sparse, Student-t + pMOM):
rsdcm(): robust, sparse group DCM using Student-t subject weighting, a nonlocal product-moment (pMOM) spike-and-slab prior (inclusion probabilities), and ReML variance components.rsdcm_fit(): wrapper that assembles the group model from a list of dcm_estimate() fits.The narps_dcm dataset (48 subjects, from the openly shared NARPS data) is the runnable real-data example for rsdcm().
Lower-level utilities (dcm_vec, dcm_inv, dcm_logdet, …) are also exported and have their own help pages, so you can call them directly as dcm_vec(). They are marked @keywords internal, which only keeps them out of the package index. Access is not restricted, and the ::: operator is not needed for them.
rsDCM is a derivative work of the MATLAB SPM25 toolbox, distributed under GPL-2 by the Wellcome Centre for Human Neuroimaging. See LICENSE.note for the list of ported routines and the references.