This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
devtools::document()- Generate/update documentation from roxygen2 commentsdevtools::check(cran = TRUE)- Run comprehensive CRAN checks (this is the gold standard)devtools::spell_check()- Check spelling in documentation and DESCRIPTIONurlchecker::url_check()- Validate all URLs in documentationdevtools::check_win_devel()- Check on Windows development versionrhub::check_for_cran()- Run multi-platform CRAN checks
devtools::test()- Run all teststestthat::test_local()- Run tests locallytestthat::test_file("tests/testthat/test_specific.R")- Run a single test file- Tests are located in
tests/testthat/
pkgdown::build_site()- Build the package website- Documentation follows CRAN guidelines strictly (see CRAN_guidance.md)
fmrireg is an R package for fMRI time series regression analysis with a layered architecture:
-
Event System (
R/event-classes.R,R/event_vector.R)event_factor: Categorical experimental eventsevent_variable: Continuous experimental variablesevent_matrix: Matrix-based events- Events are convolved with HRFs to create regressors
-
HRF System (
R/hrf.R,R/hrf-*.R)- Base
HRFclass with various implementations (gamma, gaussian, spline, etc.) - HRFs can be modified via decorators (lag, block, normalize)
- Supports custom basis sets and parametric variations
- Base
-
Model Hierarchy
baseline_model: Nuisance regressors (drift, motion, etc.)event_model: Experimental design (events + HRFs)fmri_model: Complete model (baseline + events)
-
Dataset Abstractions (
R/fmri_dataset.R)fmri_dataset: Base class for fMRI datamatrix_dataset: In-memory matrix datafmri_mem_dataset: Memory-mapped fMRI datalatent_dataset: Reduced dimensionality data
-
Model Fitting (
R/fmri_model.R,R/fmrilm.R)fmri_lm: Standard GLM fittingfmri_rlm: Robust GLM fitting- Supports different strategies: runwise, chunkwise, trial-wise
- C++ implementations for performance (mixed_solve, AR whitening)
-
Contrast System (
R/contrast.R)- Flexible contrast specification via formulas
- Support for F-contrasts, pairwise, polynomial contrasts
- S3 Object System: All generics defined in
R/all_generic.R - Builder Pattern: Models built incrementally (events → event_model → fmri_model)
- Strategy Pattern: Different fitting algorithms (OLS, robust, regularized)
- Decorator Pattern: HRF modifications (lag_hrf, block_hrf)
- C++ implementations via Rcpp for computationally intensive operations
- Parallelization via RcppParallel (configurable:
options(fmrireg.num_threads = N)) - Chunked processing for large datasets to manage memory
The package strictly follows CRAN guidelines:
- S3 generics are the primary documentation site
- Methods use
@rdnameto link to generic documentation - All examples must run quickly (< 5 seconds)
- Avoid modifying user options or par() without restoration
- See
CRAN_guidance.mdfor detailed guidelines
From data-raw/principles.md:
- Single source of truth for data representations
- Encapsulation over reconstruction
- Clear separation of concerns
- Functional composition preferred
- Object-oriented design with S3
- One way to do one thing
- Fail fast and locally with clear error messages
All AR (autoregressive) modeling functionality has been migrated to the specialized fmriAR package:
-
Dependencies Updated: ✓
- Added fmriAR to Imports and Remotes in DESCRIPTION
- fmriAR provides enhanced AR/ARMA modeling capabilities
-
Integration Adapter: ✓
- Created
R/fmriAR_adapter.Rfor seamless integration - Maps fmrireg configurations to fmriAR parameters
- Maintains backward compatibility
- Created
-
Legacy Code Removed: ✓
- Removed
R/fmri_ar_modeling.R,R/ar_utils.R - Removed
src/ar_whiten.cpp(C++ implementation) - Created compatibility layer for legacy function calls
- Removed
-
Benefits:
- Access to advanced features: ARMA models, multiscale pooling, better diagnostics
- Improved performance via optimized C++ implementation
- Reduced maintenance burden (~600 lines of code removed)
- Single source of truth for AR functionality
All existing fmri_lm() calls continue to work unchanged:
cor_struct = "ar1"→ delegates to fmriAR with p=1cor_struct = "ar2"→ delegates to fmriAR with p=2cor_struct = "arp"→ delegates to fmriAR with user-specified p- All options preserved:
exact_first,iter_gls,global, etc.
- Integration tests in
test_fmriAR_integration.R - All existing AR tests continue to pass
- Numerical results match within tolerance
All critical functionality tests are now passing after the following fixes:
-
Config structure mismatch: FIXED ✓
- Updated
solve_integrated_glmto handle both config structures (config$ar$structandconfig$ar_options$cor_struct) - Added proper config extraction in all pipeline functions
- Files modified:
fmri_lm_integrated_solver.R
- Updated
-
Effective df for robust models: FIXED ✓
- Corrected the formula to use sum of weights approach:
df = sum(weights) - p - File modified:
fmri_lm_effective_df.R
- Corrected the formula to use sum of weights approach:
-
Missing XtXinv in results: FIXED ✓
- Added XtXinv to all pipeline results for contrast computation
- Added dfres to core solver results
- Files modified:
fmri_lm_integrated_solver.R,fmri_lm_solver.R
All 6 critical tests now pass:
- ✓ AR whitening integration (now via fmriAR)
- ✓ Robust fitting
- ✓ Contrast computation
- ✓ Effective df calculations
- ✓ Bootstrap functionality
- ✓ Sandwich variance estimator
The refactored code is now ready for integration with the existing codebase.