fmriAR

Fast AR and ARMA prewhitening for fMRI GLM workflows. Estimate a noise model from residuals, apply the same filter to the design matrix and the data, then fit a standard linear model on the whitened series.

The C++ core (RcppArmadillo) keeps large-scale whitening fast. Estimation is run-aware and censor-aware: lag products never cross a run boundary or a scrubbed frame.

Installation

install.packages("fmriAR")

Development version:

# install.packages("remotes")
# remotes::install_github("bbuchsbaum/fmriAR")
install.packages("fmriAR", repos = c('https://bbuchsbaum.r-universe.dev', 'https://cloud.r-project.org'))

Documentation: https://bbuchsbaum.github.io/fmriAR/

Typical workflow

  1. Fit an initial OLS model and take residuals.
  2. Estimate an AR/ARMA plan with fit_noise().
  3. Whiten both X and Y with whiten_apply() (or do both steps with whiten()).
  4. Fit the linear model on the whitened matrices.
  5. Optionally compute sandwich standard errors and residual autocorrelation.
library(fmriAR)

# X: design (n x p), Y: data (n x voxels), runs: one label per timepoint
resid <- Y - X %*% qr.solve(X, Y)

plan <- fit_noise(
  resid,
  runs = runs,
  method = "ar",
  p = "auto",
  pooling = "global",
  design = X            # optional: undo residual-projection bias
)

xyw <- whiten_apply(plan, X, Y, runs = runs)
fit <- lm.fit(xyw$X, xyw$Y)
se  <- sandwich_from_whitened_resid(xyw$X, xyw$Y, beta = fit$coefficients)
ac  <- acorr_diagnostics(xyw$Y - xyw$X %*% fit$coefficients, runs = runs)

With censoring, the scrubbed rows stay in the whitened output and are listed in xyw$censor; drop them before fitting, e.g. lm.fit(xyw$X[-xyw$censor, ], xyw$Y[-xyw$censor, ]).

One-step shortcut (fits the plan from Y and X internally):

xyw <- whiten(X, Y, runs = runs, method = "ar", p = "auto")

What the package does

See vignette("fmriAR-introduction") and ?fit_noise for parcel pooling, ARMA, and AFNI examples.

Options

Validation

tools/validation/accuracy_benchmark.R (in the source repository) runs seeded accuracy, calibration, and timing scenarios through the exported API, so any two installed versions can be compared directly:

Rscript tools/validation/accuracy_benchmark.R <lib_path_or_-> results.rds

References