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")Documentation: https://bbuchsbaum.github.io/fmriAR/
Typical workflow
- Fit an initial OLS model and take residuals.
- Estimate an AR/ARMA plan with
fit_noise(). - Whiten both
XandYwithwhiten_apply()(or do both steps withwhiten()). - Fit the linear model on the whitened matrices.
- 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)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
-
AR and ARMA plans.
method = "ar"selects order by BIC on a Yule–Walker fit (p = "auto").method = "arma"uses Hannan–Rissanen- on the run-mean residual series.
-
Pooling.
"global"(one filter),"run"(one per run), or"parcel"(one per parcel, with optional multiscale shrinkage viaparcel_sets). - Censoring. Pass motion-scrubbed frames as indices or a logical mask; they are dropped from estimation and treated as segment breaks when whitening.
-
Residual-bias correction. Autocovariance from GLM residuals is biased low (
E[ehat ehat'] = M Sigma M). Passdesign = Xtofit_noise()ornoise_acvf()to undo that bias (AR, global/run pooling). Cache the map withacvf_bias_matrix()when many datasets share a design. -
Noise scale on the plan. An
fmriAR_plannow storesgamma(voxel-scale autocovariance) andsigma2(innovation variance) per pooling unit, not just the AR/MA coefficients. -
Autocovariance without a model.
noise_acvf()returns the same run- and censor-aware covariancesfit_noise()uses internally, plus pair counts so you can see how much data backs each lag. -
AFNI-style restricted AR.
afni_restricted_plan()builds a plan from AFNI root parameters (Cox, 2012) for pipeline comparison.
See vignette("fmriAR-introduction") and ?fit_noise for parcel pooling, ARMA, and AFNI examples.
Options
-
options(fmriAR.max_threads = n)— cap OpenMP threads when the package is built with OpenMP (optional; off by default in the CRAN sources). -
options(fmriAR.use_cpp_hr = TRUE)— use the C++ Hannan–Rissanen estimator (default). SetFALSEto fall back to the R implementation.
References
- Hannan, E. J., & Rissanen, J. (1982). Recursive estimation of mixed autoregressive-moving average order. Biometrika, 69(1), 81–94. https://doi.org/10.1093/biomet/69.1.81
- Cox, R. W. (2012). AFNI: What, where, how? NeuroImage, 62(2), 743–747. https://doi.org/10.1016/j.neuroimage.2011.08.056