| Title: | Design Matrix Construction for fMRI Analysis |
|---|---|
| Description: | Constructs and inspects design matrices for functional magnetic resonance imaging (fMRI) analyses. Provides a formula interface for specifying event-related designs with flexible hemodynamic response function (HRF) bases, parametric modulators, and trialwise models. Includes facilities for baseline/nuisance regressors, contrast specification, and utilities for naming, validation, and visualization. Intended to support the model-building stage prior to statistical fitting. Implements methods described in Friston et al. (1995) <doi:10.1006/nimg.1995.1007> and Lindquist (2008) <doi:10.1214/09-STS282>. |
| Authors: | Bradley Buchsbaum [aut, cre, cph] |
| Maintainer: | Bradley Buchsbaum <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.6.0 |
| Built: | 2026-06-02 21:53:29 UTC |
| Source: | https://github.com/bbuchsbaum/fmridesign |
Generates a baselinespec for modeling low-frequency drift in fMRI time series.
baseline( degree = 1, basis = c("constant", "poly", "bs", "ns"), name = NULL, intercept = c("runwise", "global", "none") )baseline( degree = 1, basis = c("constant", "poly", "bs", "ns"), name = NULL, intercept = c("runwise", "global", "none") )
degree |
Number of basis terms per image block (ignored for "constant"). |
basis |
Type of basis ("constant", "poly", "bs", or "ns"). |
name |
Optional name for the term. |
intercept |
Type of intercept to include ("runwise", "global", or "none"). |
A baselinespec list instance.
baseline(degree = 3, basis = "bs")baseline(degree = 3, basis = "bs")
Builds a baseline model to account for noise and non-event-related variance. This model may include a drift term, a block intercept term, and nuisance regressors.
baseline_model( basis = c("constant", "poly", "bs", "ns"), degree = 1, sframe, intercept = c("runwise", "global", "none"), nuisance_list = NULL, nuisance_check = c("warn", "error", "drop", "none"), na_action = c("drop", "zero", "median") )baseline_model( basis = c("constant", "poly", "bs", "ns"), degree = 1, sframe, intercept = c("runwise", "global", "none"), nuisance_list = NULL, nuisance_check = c("warn", "error", "drop", "none"), na_action = c("drop", "zero", "median") )
basis |
Character; type of basis function ("constant", "poly", "bs", or "ns"). |
degree |
Integer; degree of the spline/polynomial function. |
sframe |
A sampling_frame object. |
intercept |
Character; whether to include an intercept ("runwise", "global", or "none").
Ignored when |
nuisance_list |
Optional list of nuisance matrices or data frames (one per fMRI block). |
nuisance_check |
Character; how to handle nuisance diagnostics. |
na_action |
Character; how to handle |
An object of class "baseline_model".
sframe <- fmrihrf::sampling_frame(blocklens = c(100, 100), TR = 2) bmod <- baseline_model(basis = "bs", degree = 3, sframe = sframe) bmod_global <- baseline_model(basis = "bs", degree = 3, sframe = sframe, intercept = "global") bmod_nointercept <- baseline_model(basis = "bs", degree = 3, sframe = sframe, intercept = "none") stopifnot(ncol(design_matrix(bmod)) == 8)sframe <- fmrihrf::sampling_frame(blocklens = c(100, 100), TR = 2) bmod <- baseline_model(basis = "bs", degree = 3, sframe = sframe) bmod_global <- baseline_model(basis = "bs", degree = 3, sframe = sframe, intercept = "global") bmod_nointercept <- baseline_model(basis = "bs", degree = 3, sframe = sframe, intercept = "none") stopifnot(ncol(design_matrix(bmod)) == 8)
Extract baseline terms
## S3 method for class 'baseline_model' baseline_terms(x, ...) baseline_terms(x, ...)## S3 method for class 'baseline_model' baseline_terms(x, ...) baseline_terms(x, ...)
x |
The object. |
... |
Additional arguments. |
A named list of baseline term objects.
sframe <- fmrihrf::sampling_frame(blocklens = 6, TR = 1) bmod <- baseline_model(sframe = sframe) baseline_terms(bmod)sframe <- fmrihrf::sampling_frame(blocklens = 6, TR = 1) bmod <- baseline_model(sframe = sframe) baseline_terms(bmod)
Generates the _b## suffix for HRF basis functions.
basis_suffix(j, nb)basis_suffix(j, nb)
j |
Integer vector of basis indices (1-based). |
nb |
Total number of basis functions. |
Character vector of suffixes (e.g., _b01, _b02).
basis_suffix(1:3, 5) basis_suffix(1:10, 10)basis_suffix(1:3, 5) basis_suffix(1:10, 10)
Returns a block variable that is constant over the span of a scanning run.
block(x)block(x)
x |
The block variable. |
An object of class "blockspec".
block(run)block(run)
Creates a generator function for use with the hrf_fun parameter in hrf().
The generator produces boxcar HRFs where each event's duration determines
the boxcar width.
boxcar_hrf_gen(normalize = TRUE, min_duration = 0.1)boxcar_hrf_gen(normalize = TRUE, min_duration = 0.1)
normalize |
Logical; whether to normalize the boxcar HRF. Default TRUE. |
min_duration |
Numeric; minimum duration to use (prevents zero-width boxcars). Default 0.1. |
A function that takes an event data frame and returns a list of HRF objects.
weighted_hrf_gen() for weighted impulse HRFs
trial_data <- data.frame( onset = c(0, 10, 25), duration = c(2, 5, 3), condition = c("A", "B", "A"), run = 1 ) sf <- fmrihrf::sampling_frame(blocklens = 50, TR = 2) emod <- event_model( onset ~ hrf(condition, hrf_fun = boxcar_hrf_gen()), data = trial_data, block = ~run, sampling_frame = sf, durations = trial_data$duration ) print(emod)trial_data <- data.frame( onset = c(0, 10, 25), duration = c(2, 5, 3), condition = c("A", "B", "A"), run = 1 ) sf <- fmrihrf::sampling_frame(blocklens = 50, TR = 2) emod <- event_model( onset ~ hrf(condition, hrf_fun = boxcar_hrf_gen()), data = trial_data, block = ~run, sampling_frame = sf, durations = trial_data$duration ) print(emod)
Generate the B-spline basis matrix for a polynomial spline.
BSpline(x, degree)BSpline(x, degree)
x |
a numeric vector at which to evaluate the spline. Missing values are not allowed in x |
degree |
the degree of the piecewise polynomial |
an BSpline list instance
x_vals <- seq(0, 1, length.out = 6) bs_obj <- BSpline(x_vals, degree = 3) dim(bs_obj$y) bs_obj$namex_vals <- seq(0, 1, length.out = 6) bs_obj <- BSpline(x_vals, degree = 3) dim(bs_obj$y) bs_obj$name
Extract cells from a design object
## S3 method for class 'baseline_model' cells(x, drop.empty = TRUE, ...) cells(x, drop.empty = TRUE, ...) ## S3 method for class 'event' cells(x, drop.empty = TRUE, ...) ## S3 method for class 'event_term' cells(x, drop.empty = TRUE, ...) ## S3 method for class 'covariate_convolved_term' cells(x, ...)## S3 method for class 'baseline_model' cells(x, drop.empty = TRUE, ...) cells(x, drop.empty = TRUE, ...) ## S3 method for class 'event' cells(x, drop.empty = TRUE, ...) ## S3 method for class 'event_term' cells(x, drop.empty = TRUE, ...) ## S3 method for class 'covariate_convolved_term' cells(x, ...)
x |
The object to extract cells from. |
drop.empty |
Logical indicating whether to drop empty cells (default: TRUE). |
... |
Additional arguments (e.g., exclude_basis for convolved_term method). |
A data.frame/tibble of cells (categorical combinations) relevant to x.
sframe <- fmrihrf::sampling_frame(blocklens = 6, TR = 1) bmod <- baseline_model(sframe = sframe) head(cells(bmod))sframe <- fmrihrf::sampling_frame(blocklens = 6, TR = 1) bmod <- baseline_model(sframe = sframe) head(cells(bmod))
Convenience helper to quickly flag highly correlated regressors.
check_collinearity(X, threshold = 0.9)check_collinearity(X, threshold = 0.9)
X |
A numeric design matrix (or an |
threshold |
Absolute correlation above which a pair is flagged. Default 0.9. |
A list with elements: ok (logical), pairs (data.frame with
offending pairs and their correlations). Invisibly returns the same list.
# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emodel <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Check for multicollinearity res <- check_collinearity(design_matrix(emodel), threshold = 0.95) if (!res$ok) print(res$pairs)# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emodel <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Check for multicollinearity res <- check_collinearity(design_matrix(emodel), threshold = 0.95) if (!res$ok) print(res$pairs)
Inspects a block-wise nuisance regressor list before it is added to a
baseline model. The check is run per block and compares nuisance columns
against the baseline terms that would be constructed from basis, degree,
and intercept.
check_nuisance( nuisance_list, sframe, basis = c("constant", "poly", "bs", "ns"), degree = 1, intercept = c("runwise", "global", "none"), tol = sqrt(.Machine$double.eps), duplicate_threshold = 1 - sqrt(.Machine$double.eps), na_action = c("drop", "zero", "median") )check_nuisance( nuisance_list, sframe, basis = c("constant", "poly", "bs", "ns"), degree = 1, intercept = c("runwise", "global", "none"), tol = sqrt(.Machine$double.eps), duplicate_threshold = 1 - sqrt(.Machine$double.eps), na_action = c("drop", "zero", "median") )
nuisance_list |
A list of numeric matrices or data frames, one per block. |
sframe |
A sampling frame. |
basis, degree, intercept
|
Baseline model settings used to construct the comparison baseline terms. |
tol |
Numeric tolerance passed to QR rank checks. |
duplicate_threshold |
Absolute correlation threshold used to flag duplicate or near-duplicate columns. |
na_action |
Character; how to handle |
A nuisance_check object with ok, problems, by_block, and
normalized nuisance_list elements.
Drops nuisance columns that are non-finite, zero-variance, or fail to increase QR rank after the block's baseline terms and earlier nuisance columns. Column order is respected, so when two columns are aliased the earlier column is kept.
clean_nuisance( nuisance_list, sframe, basis = c("constant", "poly", "bs", "ns"), degree = 1, intercept = c("runwise", "global", "none"), tol = sqrt(.Machine$double.eps), duplicate_threshold = 1 - sqrt(.Machine$double.eps), na_action = c("drop", "zero", "median") )clean_nuisance( nuisance_list, sframe, basis = c("constant", "poly", "bs", "ns"), degree = 1, intercept = c("runwise", "global", "none"), tol = sqrt(.Machine$double.eps), duplicate_threshold = 1 - sqrt(.Machine$double.eps), na_action = c("drop", "zero", "median") )
nuisance_list |
A list of numeric matrices or data frames, one per block. |
sframe |
A sampling frame. |
basis, degree, intercept
|
Baseline model settings used to construct the comparison baseline terms. |
tol |
Numeric tolerance passed to QR rank checks. |
duplicate_threshold |
Absolute correlation threshold used to flag duplicate or near-duplicate columns. |
na_action |
Character; how to handle |
A list with nuisance_list and report elements. Pass
result$nuisance_list to baseline_model().
Define a contrast by directly targeting design matrix columns using regex patterns. This is useful for contrasts involving continuous variables or specific basis functions.
column_contrast(pattern_A, pattern_B = NULL, name, where = NULL)column_contrast(pattern_A, pattern_B = NULL, name, where = NULL)
pattern_A |
A character string containing a regex pattern to identify the columns for the positive (+) part of the contrast. |
pattern_B |
Optional character string containing a regex pattern for the
negative (-) part (for A-B type contrasts). If NULL, creates a contrast testing
the average of columns matching |
name |
A character string name for the contrast (mandatory). |
where |
Currently unused for column_contrast, but kept for API consistency. |
This contrast type operates by finding design matrix columns whose names match
the provided patterns (pattern_A, pattern_B). It calculates weights such that
the average effect of the 'A' columns is compared to the average effect of the
'B' columns (or baseline if pattern_B is NULL). Weights are assigned as +1/nA
for 'A' columns and -1/nB for 'B' columns, ensuring the contrast sums to zero
if both A and B groups are present.
Use standard R regex syntax for the patterns. Remember to escape special
characters (e.g., \\[, \\., \\*).
A column_contrast_spec object containing the specification.
# Test the main effect of a continuous modulator 'RT' # Assumes RT is a column name, e.g., from columns(Scale(RT)) cc1 <- column_contrast(pattern_A = "^z_RT$", name = "Main_RT") # Compare Condition.A vs Condition.B for the 'RT' modulator effect # Assumes condition names like "Condition.A_z_RT", "Condition.B_z_RT" cc2 <- column_contrast(pattern_A = "^Condition\\.A_z_RT$", pattern_B = "^Condition\\.B_z_RT$", name = "CondA_vs_CondB_for_RT") # Test a specific basis function (e.g., basis spline #3) # Assumes column names like "TermName_Condition.Tag_b03" cc3 <- column_contrast(pattern_A = "_b03$", name = "Basis_3_Effect")# Test the main effect of a continuous modulator 'RT' # Assumes RT is a column name, e.g., from columns(Scale(RT)) cc1 <- column_contrast(pattern_A = "^z_RT$", name = "Main_RT") # Compare Condition.A vs Condition.B for the 'RT' modulator effect # Assumes condition names like "Condition.A_z_RT", "Condition.B_z_RT" cc2 <- column_contrast(pattern_A = "^Condition\\.A_z_RT$", pattern_B = "^Condition\\.B_z_RT$", name = "CondA_vs_CondB_for_RT") # Test a specific basis function (e.g., basis spline #3) # Assumes column names like "TermName_Condition.Tag_b03" cc3 <- column_contrast(pattern_A = "_b03$", name = "Basis_3_Effect")
Extract columns
## S3 method for class 'Scale' columns(x, ...) ## S3 method for class 'ScaleWithin' columns(x, ...) ## S3 method for class 'RobustScale' columns(x, ...) columns(x, ...)## S3 method for class 'Scale' columns(x, ...) ## S3 method for class 'ScaleWithin' columns(x, ...) ## S3 method for class 'RobustScale' columns(x, ...) columns(x, ...)
x |
The object. |
... |
Additional arguments. |
Character vector of column names produced by the object.
bs_basis <- BSpline(seq(0, 1, length.out = 5), degree = 3) columns(bs_basis)bs_basis <- BSpline(seq(0, 1, length.out = 5), degree = 3) columns(bs_basis)
event_term to a per-condition basis listA lightweight wrapper around convolve() that post-processes the
resulting design matrix into a named list of T x d matrices - one per
experimental condition ("base condition tag"). This keeps all of the
heavy lifting inside fmrireg while exposing a minimal, pipe-friendly API
that can be used anywhere a condition -> basis split is required (e.g. for
CFALS).
condition_basis_list( x, hrf, sampling_frame, ..., output = c("condition_list", "matrix") )condition_basis_list( x, hrf, sampling_frame, ..., output = c("condition_list", "matrix") )
x |
An |
hrf |
An |
sampling_frame |
A |
... |
Further arguments passed on to
|
output |
Either "matrix" (default) for the ordinary design matrix or "condition_list" for the split-by-condition list. |
A numeric matrix or a named list of matrices, depending on
output.
term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) sf <- fmrihrf::sampling_frame(blocklens = 30, TR = 1) condition_basis_list(term, fmrihrf::HRF_SPMG1, sf)term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) sf <- fmrihrf::sampling_frame(blocklens = 30, TR = 1) condition_basis_list(term, fmrihrf::HRF_SPMG1, sf)
Map Display and Canonical Condition Names
condition_map(x, drop.empty = TRUE, expand_basis = FALSE, ...)condition_map(x, drop.empty = TRUE, expand_basis = FALSE, ...)
x |
The object to inspect. |
drop.empty |
Logical whether to drop empty conditions (default: TRUE). |
expand_basis |
Logical whether to expand basis functions (default: FALSE). |
... |
Additional arguments. |
A tibble mapping display names to canonical names.
term <- event_term( list( category = factor(c("face", "scene", "face")), attention = factor(c("attend", "attend", "ignore")) ), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) condition_map(term)term <- event_term( list( category = factor(c("face", "scene", "face")), attention = factor(c("attend", "attend", "ignore")) ), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) condition_map(term)
Extract conditions from a design object
conditions( x, drop.empty = TRUE, expand_basis = FALSE, style = c("canonical", "display"), ... ) ## S3 method for class 'event_term' conditions( x, drop.empty = TRUE, expand_basis = FALSE, style = c("canonical", "display"), ... )conditions( x, drop.empty = TRUE, expand_basis = FALSE, style = c("canonical", "display"), ... ) ## S3 method for class 'event_term' conditions( x, drop.empty = TRUE, expand_basis = FALSE, style = c("canonical", "display"), ... )
x |
The object to extract conditions from. |
drop.empty |
Logical whether to drop conditions with no events (default: TRUE). |
expand_basis |
Logical whether to expand basis functions (default: FALSE). |
style |
Naming style. |
... |
Additional arguments. |
A character vector of condition names.
term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) conditions(term) conditions(term, style = "display") conditions(term, expand_basis = TRUE)term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) conditions(term) conditions(term, style = "display") conditions(term, expand_basis = TRUE)
Construct method
## S3 method for class 'baselinespec' construct(x, model_spec, ...) ## S3 method for class 'covariatespec' construct(x, model_spec, sampling_frame = NULL, ...) construct(x, ...)## S3 method for class 'baselinespec' construct(x, model_spec, ...) ## S3 method for class 'covariatespec' construct(x, model_spec, sampling_frame = NULL, ...) construct(x, ...)
x |
The object. |
model_spec |
A model specification object (used by some methods). For baselinespec: typically a sampling_frame or list containing one. For hrfspec/covariatespec: contains data and other model information. |
... |
Additional arguments. |
sampling_frame |
A sampling_frame object (used by covariatespec method). |
A constructed object; return type depends on method.
sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) drift_spec <- baseline(degree = 2, basis = "poly") construct(drift_spec, sframe)sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) drift_spec <- baseline(degree = 2, basis = "poly") construct(drift_spec, sframe)
Define a linear contrast using a formula expression.
contrast(form, name, where = NULL)contrast(form, name, where = NULL)
form |
A formula describing the contrast. |
name |
A character label for the contrast. |
where |
An expression defining the subset over which the contrast is applied (default: NULL). |
A list containing the contrast specification.
# A minus B contrast using display labels contrast(~ A - B, name="A_B") # With subsetting contrast(~ A - B, name="A_B_block1", where = ~ block == 1)# A minus B contrast using display labels contrast(~ A - B, name="A_B") # With subsetting contrast(~ A - B, name="A_B_block1", where = ~ block == 1)
contrast objectDrives the unified post-processing pipeline: expands the base mask across
basis functions (when applicable), applies basis filtering, attaches the
standard contrast list class, and returns an object compatible with
downstream contrast_weights.event_model().
contrast_from_mask(mask, spec, term, classes = character())contrast_from_mask(mask, spec, term, classes = character())
mask |
Output of |
spec |
The originating contrast spec (stored on the result). |
term |
The |
classes |
Character vector of extra S3 classes prepended to the
default |
Custom contrast types implement contrast_mask() and call this driver.
A contrast object.
contrast_mask() is the extension point for new contrast types: it
returns a base contrast matrix with one row per base condition (no
basis expansion) and one column per contrast column. The unified driver
contrast_from_mask() handles basis expansion, basis filtering and
packaging into a contrast object compatible with
contrast_weights.event_model().
contrast_mask(x, term, ...)contrast_mask(x, term, ...)
x |
A |
term |
An |
... |
Additional arguments passed to methods. |
To register a custom contrast type:
Define a constructor that produces a list with class
c("my_spec", "contrast_spec", "list").
Implement contrast_mask.my_spec() returning the base weights.
Implement contrast_weights.my_spec() as a one-liner:
contrast_weights.my_spec <- function(x, term, ...) {
contrast_from_mask(contrast_mask(x, term, ...), x, term)
}
The built-in spec classes (pair_contrast_spec, oneway_contrast_spec,
poly_contrast_spec, ...) implement contrast_weights() directly, so
they do not require contrast_mask() methods.
A list with elements:
weights : numeric matrix, n_base_conditions x n_contrast_cols,
row names = base condition names.
condnames : character vector of base condition names.
Construct a list of contrast_spec objects.
contrast_set(...)contrast_set(...)
... |
A variable-length list of contrast_spec objects. |
A list of contrast_spec objects with class "contrast_set".
c1 <- contrast(~ A - B, name="A_B") c2 <- contrast(~ B - C, name="B_C") contrast_set(c1,c2)c1 <- contrast(~ A - B, name="A_B") c2 <- contrast(~ B - C, name="B_C") contrast_set(c1,c2)
Compute the contrast weights for a unit_contrast_spec object.
Compute the contrast weights for an oneway_contrast_spec object.
Compute the contrast weights for an interaction_contrast_spec object.
Compute the contrast weights for a poly_contrast_spec object.
Compute the contrast weights for a pair_contrast_spec object.
Compute contrast weights for a column_contrast_spec object by targeting
design matrix columns based on regex patterns.
Compute the contrast weights for a contrast_formula_spec object.
Compute the contrast weights for a contrast_diff_spec object.
Compute the contrast weights for each contrast specification within a contrast_set object.
## S3 method for class 'unit_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'oneway_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'interaction_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'poly_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'pair_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'column_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'contrast_formula_spec' contrast_weights(x, term, ...) ## S3 method for class 'contrast_diff_spec' contrast_weights(x, term, ...) ## S3 method for class 'contrast_set' contrast_weights(x, term, ...) contrast_weights(x, ...) ## S3 method for class 'convolved_term' contrast_weights(x, ...) ## S3 method for class 'event_model' contrast_weights(x, ...)## S3 method for class 'unit_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'oneway_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'interaction_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'poly_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'pair_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'column_contrast_spec' contrast_weights(x, term, ...) ## S3 method for class 'contrast_formula_spec' contrast_weights(x, term, ...) ## S3 method for class 'contrast_diff_spec' contrast_weights(x, term, ...) ## S3 method for class 'contrast_set' contrast_weights(x, term, ...) contrast_weights(x, ...) ## S3 method for class 'convolved_term' contrast_weights(x, ...) ## S3 method for class 'event_model' contrast_weights(x, ...)
x |
The object. |
term |
A term object against which weights should be computed. |
... |
Additional arguments. |
If the weight matrices returned by a contrast specification contain row names, these are matched to the column names of the corresponding term in the design matrix. This allows contrasts to target only a subset of term levels.
A list containing the term, name, weights, condition names, and contrast specification.
A list containing the term, name, weights, condition names, and contrast specification.
A list containing the term, name, weights, condition names, and contrast specification.
A list containing the term, name, weights, condition names, and contrast specification.
A list containing the term, name, weights, condition names, and contrast specification.
A list containing the contrast details:
term |
The original |
name |
The name of the contrast. |
weights |
A numeric matrix where rows correspond to the full design
matrix columns (from |
condnames |
Character vector of all potential expanded condition names from |
contrast_spec |
The original |
A list containing the term, name, weights, condition names, and contrast specification.
A list containing the term, name, weights, condition names, and contrast specification.
A named list where each element is the result of calling contrast_weights on the corresponding contrast_spec in the set. The list names are the names of the individual contrasts.
A named list of contrast weight objects or matrices.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrast_weights(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrast_weights(emod)
Extract contrasts
contrasts(x, ...)contrasts(x, ...)
x |
The object. |
... |
Additional arguments. |
A named list of contrast specifications.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrasts(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrasts(emod)
This function collects the contrast specifications defined for each term within the model and returns them as a named list.
## S3 method for class 'event_model' contrasts(x, ...)## S3 method for class 'event_model' contrasts(x, ...)
x |
An |
... |
Unused. |
A named list of contrast specifications.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrasts(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrasts(emod)
This accessor returns the list of contrast specifications attached to the term's originating hrfspec, if any.
## S3 method for class 'event_term' contrasts(x, ...)## S3 method for class 'event_term' contrasts(x, ...)
x |
An |
... |
Unused. |
A list of contrast specifications or NULL when none are defined.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrasts(terms(emod)[[1]])des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) cset <- contrast_set( diff = column_contrast(pattern_A = "cond.A", pattern_B = "cond.B", name = "diff") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) contrasts(terms(emod)[[1]])
Retrieve contrast specifications from an hrfspec
## S3 method for class 'hrfspec' contrasts(x, ...)## S3 method for class 'hrfspec' contrasts(x, ...)
x |
An |
... |
Unused. |
The list of contrast specifications attached to the hrfspec, or NULL.
condition <- factor(c("A", "B")) cset <- contrast_set( diff = column_contrast(pattern_A = "condition.A", pattern_B = "condition.B", name = "diff") ) spec <- hrf(condition, contrasts = cset) contrasts(spec)condition <- factor(c("A", "B")) cset <- contrast_set( diff = column_contrast(pattern_A = "condition.A", pattern_B = "condition.B", name = "diff") ) spec <- hrf(condition, contrasts = cset) contrasts(spec)
Convolve events with a hemodynamic response function
convolve( x, hrf, sampling_frame, drop.empty = TRUE, summate = TRUE, precision = 0.1, normalize = FALSE, ... ) ## S3 method for class 'event_term' convolve( x, hrf, sampling_frame, drop.empty = TRUE, summate = TRUE, precision = 0.3, normalize = FALSE, ... )convolve( x, hrf, sampling_frame, drop.empty = TRUE, summate = TRUE, precision = 0.1, normalize = FALSE, ... ) ## S3 method for class 'event_term' convolve( x, hrf, sampling_frame, drop.empty = TRUE, summate = TRUE, precision = 0.3, normalize = FALSE, ... )
x |
The events to convolve. |
hrf |
The hemodynamic response function. |
sampling_frame |
The sampling frame. |
drop.empty |
Logical indicating whether to drop columns with all zeros. |
summate |
Logical indicating whether to sum convolved signals. |
precision |
Numeric specifying the temporal precision for convolution. |
normalize |
Logical; if TRUE, each convolved regressor column is peak-normalized. Default FALSE. |
... |
Additional arguments. |
A matrix-like (often tibble) of convolved regressors.
term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 5, 10), blockids = c(1, 1, 1) ) sf <- fmrihrf::sampling_frame(blocklens = 20, TR = 1) conv <- convolve(term, fmrihrf::HRF_SPMG1, sf) names(conv)term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 5, 10), blockids = c(1, 1, 1) ) sf <- fmrihrf::sampling_frame(blocklens = 20, TR = 1) conv <- convolve(term, fmrihrf::HRF_SPMG1, sf) names(conv)
Convolves a HRF with a design matrix (one column per condition) to produce a list of regressors.
convolve_design(hrf, dmat, globons, durations, summate = TRUE, hrf_list = NULL)convolve_design(hrf, dmat, globons, durations, summate = TRUE, hrf_list = NULL)
hrf |
A function representing the HRF (used when hrf_list is NULL). |
dmat |
Design matrix (with named columns). |
globons |
Numeric vector of global onsets. |
durations |
Numeric vector of event durations. |
summate |
Logical; if TRUE, summate the convolved HRF (default: TRUE). |
hrf_list |
Optional list of HRF objects, one per event (row in dmat). When provided, allows per-onset HRF specification. The list is subsetted to match non-zero amplitude events for each condition. |
A list of regressors (one for each column).
hrf <- fmrihrf::HRF_SPMG1 dmat <- data.frame(A = c(1, 0, 1), B = c(0, 1, 0)) globons <- c(0, 10, 20) durations <- rep(0, 3) regs <- convolve_design(hrf, dmat, globons, durations) length(regs)hrf <- fmrihrf::HRF_SPMG1 dmat <- data.frame(A = c(1, 0, 1), B = c(0, 1, 0)) globons <- c(0, 10, 20) durations <- rep(0, 3) regs <- convolve_design(hrf, dmat, globons, durations) length(regs)
Compute correlation map
correlation_map(x, ...)correlation_map(x, ...)
x |
The object. |
... |
Additional arguments. |
A ggplot2 object visualizing regressor correlations.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) correlation_map(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) correlation_map(emod)
Generates a correlation heatmap of the columns in a baseline_model's design matrix.
## S3 method for class 'baseline_model' correlation_map( x, method = c("pearson", "spearman"), half_matrix = FALSE, absolute_limits = TRUE, ... )## S3 method for class 'baseline_model' correlation_map( x, method = c("pearson", "spearman"), half_matrix = FALSE, absolute_limits = TRUE, ... )
x |
A |
method |
Correlation method (e.g., "pearson", "spearman"). |
half_matrix |
Logical; if TRUE, display only the lower triangle of the matrix. |
absolute_limits |
Logical; if TRUE, set color scale limits from -1 to 1. |
... |
Additional arguments passed to internal plotting functions. |
A ggplot2 plot object.
sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) bmod <- baseline_model(sframe = sframe) if (requireNamespace("ggplot2", quietly = TRUE)) correlation_map(bmod)sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) bmod <- baseline_model(sframe = sframe) if (requireNamespace("ggplot2", quietly = TRUE)) correlation_map(bmod)
Creates a heatmap visualization of the correlation matrix between regressors in an event_model object.
## S3 method for class 'event_model' correlation_map(x, rotate_x_text = TRUE, ...)## S3 method for class 'event_model' correlation_map(x, rotate_x_text = TRUE, ...)
x |
An |
rotate_x_text |
Logical. Whether to rotate x-axis labels. Default is TRUE. |
... |
Additional arguments passed to geom_tile. |
A ggplot2 object showing the correlation matrix heatmap.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) correlation_map(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) correlation_map(emod)
Creates a covariate term that is added directly to the fMRI model without being convolved with a hemodynamic response function (HRF). This is useful for including nuisance variables, continuous covariates, or any other regressors that should not undergo HRF convolution.
covariate(..., data, id = NULL, prefix = NULL, subset = NULL)covariate(..., data, id = NULL, prefix = NULL, subset = NULL)
... |
A variable argument set of covariate names. |
data |
A data.frame containing the variables. |
id |
An optional identifier for the covariate term. |
prefix |
An optional prefix to add to the covariate names. |
subset |
Optional expression used to subset the covariate data. |
In fMRI analysis, some predictors should not be convolved with the HRF because they represent:
Continuous physiological measurements (e.g., heart rate, respiration)
Motion parameters from head movement correction
Scanner drift or other technical artifacts
Behavioral measures that directly correlate with BOLD signal
Global signal or other nuisance variables
The covariate term can be combined with standard HRF-convolved event terms in the same model. For example:
model <- event_model(onset ~ hrf(stimulus) + covariate(motion_x, motion_y, data = cov_data),
data = events, block = ~ 1, sampling_frame = sframe)
A list containing information about the covariate term with class 'covariatespec' that can be used within an event_model.
event_model() for creating complete fMRI models
hrf() for creating HRF-convolved event terms
# Add motion parameters as covariates motion_data <- data.frame( x = rnorm(100), # x translation y = rnorm(100) # y translation ) cv <- covariate(x, y, data = motion_data, prefix = "motion") # Combine with event model sframe <- sampling_frame(blocklens = c(100), TR = 2) # 50 events, strictly increasing onsets per block event_data <- data.frame( stimulus = factor(rep(c("A", "B"), 25)), onset = seq(0, by = 4, length.out = 50) ) # Full model with both HRF-convolved events and non-convolved covariates model <- event_model( onset ~ hrf(stimulus) + covariate(x, y, data = motion_data, id = "motion"), data = event_data, block = ~ 1, sampling_frame = sframe )# Add motion parameters as covariates motion_data <- data.frame( x = rnorm(100), # x translation y = rnorm(100) # y translation ) cv <- covariate(x, y, data = motion_data, prefix = "motion") # Combine with event model sframe <- sampling_frame(blocklens = c(100), TR = 2) # 50 events, strictly increasing onsets per block event_data <- data.frame( stimulus = factor(rep(c("A", "B"), 25)), onset = seq(0, by = 4, length.out = 50) ) # Full model with both HRF-convolved events and non-convolved covariates model <- event_model( onset ~ hrf(stimulus) + covariate(x, y, data = motion_data, id = "motion"), data = event_data, block = ~ 1, sampling_frame = sframe )
Returns a tibble with one row per column of the design matrix and structured metadata (term, condition, basis, role, run, etc.).
design_colmap(x, ...)design_colmap(x, ...)
x |
An object containing or producing a design matrix (e.g., event_model, baseline_model). |
... |
Additional arguments passed to methods. |
A tibble with per-column metadata.
# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Extract column metadata colmap <- design_colmap(emod) head(colmap) # Query specific columns colmap[colmap$condition == "A", ]# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Extract column metadata colmap <- design_colmap(emod) head(colmap) # Query specific columns colmap[colmap$condition == "A", ]
Returns a tibble with one row per column of the design matrix and structured metadata derived from existing package attributes and naming conventions. This avoids ad-hoc string parsing in user code and provides a consistent, queryable view of the design.
## S3 method for class 'event_model' design_colmap(x, ...)## S3 method for class 'event_model' design_colmap(x, ...)
x |
An object containing or producing a design matrix
(e.g., |
... |
Unused; reserved for future extensions. |
A tibble with columns:
col (integer): 1-based column index
name (character): column name
term_tag (character): term identifier (event tag or baseline term name)
term_index (integer): position within terms(x)
condition (character): condition/event label without basis suffix (if applicable)
run (integer): block/run id when the column is block-specific; NA if pooled
role (character): e.g., "task", "drift", "intercept", "nuisance"
model_source (character): "event" or "baseline"
basis_name (character): HRF or baseline basis identifier when available
basis_ix (integer): within-condition basis index (HRF component); NA if not applicable
basis_total (integer): total number of basis components for the column's term; NA if not applicable
basis_label (character): human-readable label for the basis component when known
pretty_name (character): concise, human-friendly label for display (e.g., "RT" or "RT_lag_02")
is_block_diagonal (logical): TRUE when the regressor is per-run/block
modulation_type (character): "amplitude", "parametric", or "covariate"
modulation_id (character): modulator identifier when applicable (e.g., "RT", "RT_by_group")
# Create event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Get column metadata colmap <- design_colmap(emod) head(colmap) # Query columns by condition colmap[colmap$condition == "A", ]# Create event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Get column metadata colmap <- design_colmap(emod) head(colmap) # Query columns by condition colmap[colmap$condition == "A", ]
Compute design map
design_map(x, ...)design_map(x, ...)
x |
The object. |
... |
Additional arguments. |
A ggplot2 object visualizing the design matrix.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) design_map(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) design_map(emod)
Produces a heatmap of all columns in the design matrix for a baseline_model object,
with rows corresponding to scans and columns corresponding to regressors. By default,
it draws horizontal lines separating runs (blocks), and rotates the column labels diagonally.
## S3 method for class 'baseline_model' design_map( x, block_separators = TRUE, rotate_x_text = TRUE, fill_midpoint = NULL, fill_limits = NULL, ... )## S3 method for class 'baseline_model' design_map( x, block_separators = TRUE, rotate_x_text = TRUE, fill_midpoint = NULL, fill_limits = NULL, ... )
x |
A |
block_separators |
Logical; if |
rotate_x_text |
Logical; if |
fill_midpoint |
Numeric or |
fill_limits |
Numeric vector of length 2 or |
... |
Additional arguments forwarded to |
A ggplot2 plot object.
sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) bmod <- baseline_model(sframe = sframe) if (requireNamespace("ggplot2", quietly = TRUE)) design_map(bmod)sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) bmod <- baseline_model(sframe = sframe) if (requireNamespace("ggplot2", quietly = TRUE)) design_map(bmod)
Creates a heatmap visualization of the design matrix for an event_model object.
## S3 method for class 'event_model' design_map( x, block_separators = TRUE, rotate_x_text = TRUE, fill_midpoint = NULL, fill_limits = NULL, ... )## S3 method for class 'event_model' design_map( x, block_separators = TRUE, rotate_x_text = TRUE, fill_midpoint = NULL, fill_limits = NULL, ... )
x |
An |
block_separators |
Logical. Whether to draw separators between blocks/runs. Default is TRUE. |
rotate_x_text |
Logical. Whether to rotate x-axis labels. Default is TRUE. |
fill_midpoint |
Numeric. Midpoint for color scale. If NULL, uses gradient scale. |
fill_limits |
Numeric vector of length 2. Limits for fill scale. |
... |
Additional arguments passed to geom_tile. |
A ggplot2 object showing the design matrix heatmap.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) design_map(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) design_map(emod)
Extract or construct a design matrix
## S3 method for class 'baseline_model' design_matrix(x, blockid = NULL, allrows = FALSE, ...) ## S3 method for class 'baseline_term' design_matrix(x, blockid = NULL, allrows = FALSE, ...) design_matrix(x, ...) ## S3 method for class 'event_model' design_matrix(x, blockid = NULL, ...) ## S3 method for class 'event_term' design_matrix(x, drop.empty = TRUE, ...)## S3 method for class 'baseline_model' design_matrix(x, blockid = NULL, allrows = FALSE, ...) ## S3 method for class 'baseline_term' design_matrix(x, blockid = NULL, allrows = FALSE, ...) design_matrix(x, ...) ## S3 method for class 'event_model' design_matrix(x, blockid = NULL, ...) ## S3 method for class 'event_term' design_matrix(x, drop.empty = TRUE, ...)
x |
The object to extract design matrix from. |
blockid |
Block ID(s) to extract (for baseline_term method). |
allrows |
Whether to return all rows (for baseline_term method). |
... |
Additional arguments. |
drop.empty |
Whether to drop empty columns (for event_term method). |
A matrix-like object (often tibble) with rows = scans, cols = regressors.
sframe <- fmrihrf::sampling_frame(blocklens = 6, TR = 1) bmod <- baseline_model(sframe = sframe) head(design_matrix(bmod))sframe <- fmrihrf::sampling_frame(blocklens = 6, TR = 1) bmod <- baseline_model(sframe = sframe) head(design_matrix(bmod))
Returns the col_metadata tibble attached to a design matrix produced by
event_model(). If the attribute is missing (e.g., on a manually
constructed matrix) returns NULL.
design_meta(x)design_meta(x)
x |
A design matrix or any object with a |
A tibble, or NULL.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) design_meta(emod)des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) design_meta(emod)
Generator for use with the hrf_fun parameter in hrf(). Creates per-onset
HRFs by convolving a base HRF with a boxcar of each event's duration, then
normalizing to peak = 1. This preserves the temporal shape of the
duration-modulated response while equalizing amplitude across events.
duration_hrf_gen(base = fmrihrf::HRF_SPMG1, min_duration = 0)duration_hrf_gen(base = fmrihrf::HRF_SPMG1, min_duration = 0)
base |
Base HRF object. Default |
min_duration |
Minimum duration (prevents zero-width events). Default 0. |
A function for use with hrf_fun in hrf().
boxcar_hrf_gen() for duration-based boxcar HRFs
# Events with variable durations trial_data <- data.frame( onset = c(0, 10, 25), duration = c(2, 5, 3), condition = c("A", "B", "A"), run = 1 ) sf <- fmrihrf::sampling_frame(blocklens = 50, TR = 2) emod <- event_model( onset ~ hrf(condition, hrf_fun = duration_hrf_gen()), data = trial_data, block = ~run, sampling_frame = sf, durations = trial_data$duration ) print(emod)# Events with variable durations trial_data <- data.frame( onset = c(0, 10, 25), duration = c(2, 5, 3), condition = c("A", "B", "A"), run = 1 ) sf <- fmrihrf::sampling_frame(blocklens = 50, TR = 2) emod <- event_model( onset ~ hrf(condition, hrf_fun = duration_hrf_gen()), data = trial_data, block = ~run, sampling_frame = sf, durations = trial_data$duration ) print(emod)
Extract elements from an object
elements(x, ...) ## S3 method for class 'event' elements(x, what = c("values", "labels"), transformed = TRUE, ...) ## S3 method for class 'event_term' elements(x, what = c("values", "labels"), ...)elements(x, ...) ## S3 method for class 'event' elements(x, what = c("values", "labels"), transformed = TRUE, ...) ## S3 method for class 'event_term' elements(x, what = c("values", "labels"), ...)
x |
The object to extract elements from. |
... |
Additional arguments. |
what |
Character string specifying what to extract: "values" for numeric/actual values, or "labels" for descriptive labels/names. |
transformed |
Logical indicating whether to return transformed values. Default is TRUE. |
Requested elements; structure depends on method (e.g., numeric values or labels).
# Create an event term with mixed categorical and continuous events term <- event_term( list( condition = factor(c("A", "B", "A", "B")), intensity = c(1.2, 0.8, 1.5, 0.9) ), onsets = c(0, 10, 20, 30), blockids = c(1, 1, 1, 1) ) # Extract values (actual numeric/factor codes) elements(term, what = "values") # Extract labels (descriptive names/levels) elements(term, what = "labels")# Create an event term with mixed categorical and continuous events term <- event_term( list( condition = factor(c("A", "B", "A", "B")), intensity = c(1.2, 0.8, 1.5, 0.9) ), onsets = c(0, 10, 20, 30), blockids = c(1, 1, 1, 1) ) # Extract values (actual numeric/factor codes) elements(term, what = "values") # Extract labels (descriptive names/levels) elements(term, what = "labels")
This is a user-facing wrapper around the internal event() constructor,
specifically for creating event sequences modulated by a basis set.
event_basis( basis, name = NULL, onsets, blockids = 1, durations = 0, subset = NULL )event_basis( basis, name = NULL, onsets, blockids = 1, durations = 0, subset = NULL )
basis |
A |
name |
Optional name for the event variable. If NULL, uses |
onsets |
Numeric vector of event onsets (seconds). |
blockids |
Numeric vector of block IDs. |
durations |
Numeric vector of event durations (seconds), or a scalar. |
subset |
Optional logical vector indicating which events to keep. If
provided, the vector must match |
An S3 object of class event and event_seq.
basis <- BSpline(1:21, 3) onsets <- seq(0, 20, length.out = 21) blockids <- rep(1, length(onsets)) ebasis <- event_basis(basis, onsets=onsets, blockids=blockids) print(ebasis) levels(ebasis)basis <- BSpline(1:21, 3) onsets <- seq(0, 20, length.out = 21) blockids <- rep(1, length(onsets)) ebasis <- event_basis(basis, onsets=onsets, blockids=blockids) print(ebasis) levels(ebasis)
Retrieve per-event condition assignments
event_conditions(x, drop.empty = FALSE, ...)event_conditions(x, drop.empty = FALSE, ...)
x |
The object of interest. |
drop.empty |
Logical; drop unused levels when |
... |
Additional arguments passed to methods. |
Typically a factor aligned with the events of x.
term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) event_conditions(term)term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) event_conditions(term)
This is a user-facing wrapper around the internal event() constructor,
specifically for creating categorical event sequences from factors or characters.
event_factor(fac, name, onsets, blockids = 1, durations = 0, subset = NULL)event_factor(fac, name, onsets, blockids = 1, durations = 0, subset = NULL)
fac |
A factor or something coercible to a factor. |
name |
Name of the event variable. |
onsets |
Numeric vector of event onsets (seconds). |
blockids |
Numeric vector of block IDs. |
durations |
Numeric vector of event durations (seconds), or a scalar. |
subset |
Optional logical vector indicating which events to keep. If
provided, the vector must match Column names are sanitized using |
An S3 object of class event and event_seq.
event_model, event_variable, event_matrix, event_basis
ef_onsets <- seq(1, 100, length.out = 6) efac <- event_factor(factor(c("a", "b", "c", "a", "b", "c")), "abc", onsets = ef_onsets, blockids = rep(1, length(ef_onsets))) print(efac) levels(efac)ef_onsets <- seq(1, 100, length.out = 6) efac <- event_factor(factor(c("a", "b", "c", "a", "b", "c")), "abc", onsets = ef_onsets, blockids = rep(1, length(ef_onsets))) print(efac) levels(efac)
This is a user-facing wrapper around the internal event() constructor,
specifically for creating continuous event sequences from numeric matrices.
event_matrix(mat, name, onsets, blockids = 1, durations = 0, subset = NULL)event_matrix(mat, name, onsets, blockids = 1, durations = 0, subset = NULL)
mat |
A numeric matrix of continuous event values (one row per event). |
name |
Name of the event variable. |
onsets |
Numeric vector of event onsets (seconds). |
blockids |
Numeric vector of block IDs. |
durations |
Numeric vector of event durations (seconds), or a scalar. |
subset |
Optional logical vector indicating which events to keep. If
provided, the vector must match If |
An S3 object of class event and event_seq.
mat <- matrix(rnorm(20), 10, 2, dimnames=list(NULL, c("Val1", "Val2"))) onsets <- seq(1, 100, length.out = 10) durations <- rep(1, 10) blockids <- rep(1, 10) eset <- event_matrix(mat, "eset", onsets, blockids, durations) print(eset) columns(eset) # Alias for levelsmat <- matrix(rnorm(20), 10, 2, dimnames=list(NULL, c("Val1", "Val2"))) onsets <- seq(1, 100, length.out = 10) durations <- rep(1, 10) blockids <- rep(1, 10) eset <- event_matrix(mat, "eset", onsets, blockids, durations) print(eset) columns(eset) # Alias for levels
This file contains the generic functions used throughout the fmridesign package. These generics define the interface for working with event models, baseline models, and related design components. Construct an event model
This is the main constructor for event_model objects. It unifies the previous
formula and list-based interfaces and uses a more efficient internal pipeline.
event_model( formula_or_list, data, block, sampling_frame, durations = 0, drop_empty = TRUE, precision = 0.3, parallel = FALSE, progress = FALSE, strict = FALSE, ... ) event_model( formula_or_list, data, block, sampling_frame, durations = 0, drop_empty = TRUE, precision = 0.3, parallel = FALSE, progress = FALSE, strict = FALSE, ... )event_model( formula_or_list, data, block, sampling_frame, durations = 0, drop_empty = TRUE, precision = 0.3, parallel = FALSE, progress = FALSE, strict = FALSE, ... ) event_model( formula_or_list, data, block, sampling_frame, durations = 0, drop_empty = TRUE, precision = 0.3, parallel = FALSE, progress = FALSE, strict = FALSE, ... )
formula_or_list |
Either a formula (e.g., |
data |
A |
block |
A formula (e.g., |
sampling_frame |
An object of class |
durations |
Numeric vector or scalar specifying event durations (seconds). Default is 0. |
drop_empty |
Logical indicating whether to drop empty events during term construction. Default is TRUE. |
precision |
Numeric precision for HRF sampling/convolution. Default is 0.3. |
parallel |
Logical compatibility shim reserved for future use. The
current implementation always evaluates terms sequentially; when set to
|
progress |
Logical indicating whether to show a progress bar during term realisation. Default is FALSE. |
strict |
Logical controlling the onset bounds check. When |
... |
Additional arguments (currently unused). |
This function creates an event-based fMRI regression model, represented as a data structure.
The columns in the resulting design matrix follow the naming convention:
term_tag + _ + condition_tag + _b## basis suffix
Where:
term_tag: The unique tag assigned to the hrf() term (see below).
condition_tag: Represents the specific factor level or continuous regressor within the term (e.g., condition.A, poly_RT_01, condition.A_task.go).
_b##: Optional suffix added for HRFs with multiple basis functions (e.g., _b01, _b02).
Each term in the model (typically defined by an hrf() call in a formula) gets a
unique term_tag. This tag is used as the prefix for all columns generated by that term.
Default Naming: If no explicit id (or name) is given in hrf(), the tag is derived
from the variable names (e.g., hrf(condition) -> condition, hrf(RT, acc) -> RT_acc).
Explicit Naming: Use id= within hrf() for an explicit tag (e.g., hrf(condition, id="CondMain")).
Sanitization: Dots (.) in tags are converted to underscores (_).
Clash Resolution: If multiple terms generate the same tag, # and a number are appended
to ensure uniqueness (e.g., condition, condition#1).
This consistent naming scheme replaces the previous compact and qualified styles.
An event_model object describing the task design.
An object of class c("event_model", "list") containing the terms, design matrix,
sampling frame, and other metadata.
des <- data.frame(onset = seq(0, 90, by=10), run = rep(1:2, each=5), cond = factor(rep(c("A","B"), 5)), mod = rnorm(10)) sframe <- fmrihrf::sampling_frame(blocklens=c(50, 60), TR=2) ev_model_form <- event_model(onset ~ hrf(cond) + hrf(mod, basis="spmg3"), data = des, block = ~run, sampling_frame = sframe) print(ev_model_form) head(design_matrix(ev_model_form)) spec1 <- hrf(cond) spec2 <- hrf(mod, basis = "spmg3") ev_model_list <- event_model(list(spec1, spec2), data = des, block = des$run, sampling_frame = sframe) print(ev_model_list)des <- data.frame(onset = seq(0, 90, by=10), run = rep(1:2, each=5), cond = factor(rep(c("A","B"), 5)), mod = rnorm(10)) sframe <- fmrihrf::sampling_frame(blocklens=c(50, 60), TR=2) ev_model_form <- event_model(onset ~ hrf(cond) + hrf(mod, basis="spmg3"), data = des, block = ~run, sampling_frame = sframe) print(ev_model_form) head(design_matrix(ev_model_form)) spec1 <- hrf(cond) spec2 <- hrf(mod, basis = "spmg3") ev_model_list <- event_model(list(spec1, spec2), data = des, block = des$run, sampling_frame = sframe) print(ev_model_list)
Extract event table
event_table(x, ...)event_table(x, ...)
x |
The object. |
... |
Additional arguments. |
A data.frame/tibble of event rows.
term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) event_table(term)term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) event_table(term)
Generates an event_term object which represents the combination of one
or more event sequences (e.g., a factor crossed with a numeric modulator).
It takes a list of variables (factors, numeric vectors, matrices, basis objects)
along with shared onsets, block IDs, and durations.
It uses the EV factory internally to create standardized event objects for each variable.
event_term(evlist, onsets, blockids, durations = 0, subset = NULL)event_term(evlist, onsets, blockids, durations = 0, subset = NULL)
evlist |
A named list of variables (factors, numeric, matrices, ParametricBasis objects). The names are used as variable identifiers within the term. |
onsets |
Numeric vector of onset times (in seconds). |
blockids |
Numeric vector of block numbers (non-decreasing integers). |
durations |
Numeric vector of event durations (seconds, default is 0).
Can be scalar (recycled) or vector matching length of |
subset |
Optional logical vector indicating which events to retain (applied before processing). |
A list object with class c("event_term", "event_seq"). Contains:
varname |
Concatenated variable names from |
events |
A named list of the processed |
subset |
The |
event_table |
A tibble representing the combinations of descriptive levels/names
for each event in the term, constructed using |
onsets |
Numeric vector of onsets (after processing/subsetting). |
blockids |
Numeric vector of block IDs (after processing/subsetting). |
durations |
Numeric vector of durations (after processing/subsetting). |
x1 <- factor(rep(letters[1:3], 10)) x2 <- factor(rep(1:3, each = 10)) onsets <- seq(1, 100, length.out = 30) blockids <- rep(1:3, each = 10) eterm <- event_term(list(Condition = x1, Group = x2), onsets = onsets, blockids = blockids) print(eterm) head(event_table(eterm)) levels(eterm) head(design_matrix(eterm)) term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) head(design_matrix(term))x1 <- factor(rep(letters[1:3], 10)) x2 <- factor(rep(1:3, each = 10)) onsets <- seq(1, 100, length.out = 30) blockids <- rep(1:3, each = 10) eterm <- event_term(list(Condition = x1, Group = x2), onsets = onsets, blockids = blockids) print(eterm) head(event_table(eterm)) levels(eterm) head(design_matrix(eterm)) term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) head(design_matrix(term))
Extract event terms
event_terms(x, ...)event_terms(x, ...)
x |
The object. |
... |
Additional arguments. |
A named list of event term objects.
# Create a simple experimental design des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Extract event terms (named list of event term objects) terms_list <- event_terms(emod) names(terms_list)# Create a simple experimental design des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Extract event terms (named list of event term objects) terms_list <- event_terms(emod) names(terms_list)
This is a user-facing wrapper around the internal event() constructor,
specifically for creating continuous event sequences from numeric vectors.
event_variable(vec, name, onsets, blockids = 1, durations = 0, subset = NULL)event_variable(vec, name, onsets, blockids = 1, durations = 0, subset = NULL)
vec |
Numeric vector representing continuous event values. |
name |
Name of the event variable. |
onsets |
Numeric vector of event onsets (seconds). |
blockids |
Numeric vector of block IDs. |
durations |
Numeric vector of event durations (seconds), or a scalar. |
subset |
Optional logical vector indicating which events to keep. If
provided, the vector must match |
An S3 object of class event and event_seq.
ev_onsets <- seq(1, 100, length.out = 6) evar <- event_variable(c(1, 2, 3, 4, 5, 6), "example_var", onsets = ev_onsets, blockids = rep(1, length(ev_onsets))) print(evar) is_continuous(evar)ev_onsets <- seq(1, 100, length.out = 6) evar <- event_variable(c(1, 2, 3, 4, 5, 6), "example_var", onsets = ev_onsets, blockids = rep(1, length(ev_onsets))) print(evar) is_continuous(evar)
Retrieve canonical event information
events(x, drop.empty = FALSE, ...)events(x, drop.empty = FALSE, ...)
x |
The object to summarise. |
drop.empty |
Logical; whether to drop empty conditions in the resulting factor. |
... |
Additional arguments passed to methods. |
A data.frame (or tibble) with onset, duration, block, and condition columns.
# Create an event term with condition factor term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) # Extract canonical event information evt_info <- events(term) print(evt_info)# Create an event term with condition factor term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) # Extract canonical event information evt_info <- events(term) print(evt_info)
Compute F-contrasts
Fcontrasts(x, ...) ## S3 method for class 'convolved_term' Fcontrasts(x, ...) ## S3 method for class 'event_model' Fcontrasts(x, ...)Fcontrasts(x, ...) ## S3 method for class 'convolved_term' Fcontrasts(x, ...) ## S3 method for class 'event_model' Fcontrasts(x, ...)
x |
The object. |
... |
Additional arguments. |
Row names of the contrast matrices can specify which levels of the term are tested. Any matching is done against the design matrix column names.
A named list of matrices with F-contrast weights.
des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) names(Fcontrasts(emod))des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) names(Fcontrasts(emod))
Generates the f## suffix for multi-column continuous events.
feature_suffix(j, nf)feature_suffix(j, nf)
j |
Integer vector of feature indices (1-based). |
nf |
Total number of features. |
Character vector of suffixes (e.g., f01, f02).
feature_suffix(1:3, 5)feature_suffix(1:3, 5)
Returns all function names that should be recognized in formulas from registered external packages.
get_all_external_hrf_functions()get_all_external_hrf_functions()
Character vector of function names
register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", formula_functions = "demo_hrf" ) get_all_external_hrf_functions()register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", formula_functions = "demo_hrf" ) get_all_external_hrf_functions()
Accepts either a single class name or a class vector (the typical output
of class(x)). Returns the first matching registration entry, walking
the class vector from most-specific to least-specific so a subclass's
registration takes precedence over its parent.
get_basis_entry(class_name)get_basis_entry(class_name)
class_name |
Character string or character vector of class names. |
Registration entry (a list) or NULL if no class is registered.
get_basis_entry("Poly")get_basis_entry("Poly")
Returns the function name(s) that should be recognized in formulas for a given external HRF specification class.
get_external_hrfspec_functions(spec_class)get_external_hrfspec_functions(spec_class)
spec_class |
Character string naming the class |
Character vector of function names, or NULL if not registered
register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", formula_functions = c("demo_hrf", "demo_trialwise") ) get_external_hrfspec_functions("demo_hrfspec")register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", formula_functions = c("demo_hrf", "demo_trialwise") ) get_external_hrfspec_functions("demo_hrfspec")
Get Information About a Registered External HRF Specification
get_external_hrfspec_info(spec_class)get_external_hrfspec_info(spec_class)
spec_class |
Character string naming the class |
A list with registration information, or NULL if not registered
register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", requires_external_processing = TRUE, formula_functions = "demo_hrf" ) get_external_hrfspec_info("demo_hrfspec")register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", requires_external_processing = TRUE, formula_functions = "demo_hrf" ) get_external_hrfspec_info("demo_hrfspec")
This function is to be used in formulas for fitting functions, e.g. onsets ~ hrf(fac1,fac2) ...
It captures the variables/expressions provided and packages them with HRF/contrast
information into an hrfspec object, which is then processed by event_model.
hrf( ..., basis = "spmg1", hrf_fun = NULL, onsets = NULL, durations = NULL, prefix = NULL, subset = NULL, precision = 0.3, nbasis = 1, contrasts = NULL, id = NULL, name = NULL, lag = 0, summate = TRUE, normalize = FALSE )hrf( ..., basis = "spmg1", hrf_fun = NULL, onsets = NULL, durations = NULL, prefix = NULL, subset = NULL, precision = 0.3, nbasis = 1, contrasts = NULL, id = NULL, name = NULL, lag = 0, summate = TRUE, normalize = FALSE )
... |
One or more variable names (bare or character) or expressions involving variables
present in the |
basis |
the impulse response function or the name of a pre-supplied function,
one of: "gamma", "spmg1", "spmg2", "spmg3", "bspline", "gaussian", "tent", "bs".
Can also be an |
hrf_fun |
optional per-onset HRF generator. Can be:
When |
onsets |
optional onsets override. If missing, onsets will be taken from the LHS of the main model formula. |
durations |
optional durations override. If missing, durations argument from |
prefix |
a character string that is prepended to the variable names and used to identify the term.
Can be used to disambiguate two |
subset |
an expression indicating the subset of 'onsets' to keep. |
precision |
sampling precision in seconds. |
nbasis |
number of basis functions – only used for hemodynamic response functions (e.g. bspline) that take a variable number of bases. |
contrasts |
one or more |
id |
a unique |
name |
Optional human-readable name for the term. |
lag |
a temporal offset in seconds which is added to onset before convolution |
summate |
whether impulse amplitudes sum up when duration is greater than 0. |
normalize |
logical; if TRUE, each convolved regressor column is peak-normalized
so that |
an hrfspec instance
form1 <- onsets ~ hrf(condition, basis="spmg1") form2 <- onsets ~ hrf(condition) + hrf(RT) form3 <- onsets ~ hrf(condition, RT, basis="spmg3") library(rlang) con1 <- pair_contrast(~ condition == "A", ~ condition == "B", name="AvB") form4 <- onsets ~ hrf(condition, Poly(RT, 2), contrasts=con1) form5 <- onsets ~ hrf(condition, hrf_fun = boxcar_hrf_gen()) form6 <- onsets ~ hrf(condition, hrf_fun = weighted_hrf_gen("sub_times", "sub_weights"))form1 <- onsets ~ hrf(condition, basis="spmg1") form2 <- onsets ~ hrf(condition) + hrf(RT) form3 <- onsets ~ hrf(condition, RT, basis="spmg3") library(rlang) con1 <- pair_contrast(~ condition == "A", ~ condition == "B", name="AvB") form4 <- onsets ~ hrf(condition, Poly(RT, 2), contrasts=con1) form5 <- onsets ~ hrf(condition, hrf_fun = boxcar_hrf_gen()) form6 <- onsets ~ hrf(condition, hrf_fun = weighted_hrf_gen("sub_times", "sub_weights"))
A basis that applies identity transform to a set of raw variables.
Ident(...)Ident(...)
... |
a list of variable names |
an instance of class Ident extending ParametricBasis
# Create identity basis from numeric vectors x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) ident_basis <- Ident(x, y) print(ident_basis$y)# Create identity basis from numeric vectors x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) ident_basis <- Ident(x, y) print(ident_basis$y)
Create an interaction contrast specification
interaction_contrast(A, name, where = NULL)interaction_contrast(A, name, where = NULL)
A |
A formula specifying the interaction contrast |
name |
The name of the contrast |
where |
An optional formula specifying the subset over which the contrast is computed. |
An interaction_contrast_spec object containing the specification for generating interaction contrast weights
oneway_contrast for main effects,
pair_contrast for pairwise comparisons
# Create an interaction contrast for factors A and B con <- interaction_contrast(~ A * B, name = "A_by_B") # Create an interaction contrast with a 'where' clause con <- interaction_contrast(~ A * B, name = "A_by_B", where = ~ block == 1)# Create an interaction contrast for factors A and B con <- interaction_contrast(~ A * B, name = "A_by_B") # Create an interaction contrast with a 'where' clause con <- interaction_contrast(~ A * B, name = "A_by_B", where = ~ block == 1)
Check if categorical
is_categorical(x, ...) ## S3 method for class 'event' is_categorical(x, ...)is_categorical(x, ...) ## S3 method for class 'event' is_categorical(x, ...)
x |
The object. |
... |
Additional arguments. |
Logical scalar indicating whether x is categorical.
# Create a categorical event from factor data cat_event <- event_factor( factor(c("faces", "houses", "faces", "houses")), name = "condition", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_categorical(cat_event) # Returns TRUE # Create a continuous event from numeric data cont_event <- event_variable( c(1.2, 0.8, 1.5, 0.9), name = "reaction_time", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_categorical(cont_event) # Returns FALSE # Event term with mixed types is considered categorical mixed_term <- event_term( list(condition = factor(c("A", "B", "A", "B")), modulator = c(1.1, 0.9, 1.2, 0.8)), onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_categorical(mixed_term) # Returns TRUE# Create a categorical event from factor data cat_event <- event_factor( factor(c("faces", "houses", "faces", "houses")), name = "condition", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_categorical(cat_event) # Returns TRUE # Create a continuous event from numeric data cont_event <- event_variable( c(1.2, 0.8, 1.5, 0.9), name = "reaction_time", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_categorical(cont_event) # Returns FALSE # Event term with mixed types is considered categorical mixed_term <- event_term( list(condition = factor(c("A", "B", "A", "B")), modulator = c(1.1, 0.9, 1.2, 0.8)), onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_categorical(mixed_term) # Returns TRUE
Check if continuous
is_continuous(x, ...) ## S3 method for class 'event' is_continuous(x, ...)is_continuous(x, ...) ## S3 method for class 'event' is_continuous(x, ...)
x |
The object. |
... |
Additional arguments. |
Logical scalar indicating whether x is continuous.
# Create a continuous event from numeric vector cont_event <- event_variable( c(1.2, 0.8, 1.5, 0.9), name = "reaction_time", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(cont_event) # Returns TRUE # Create a continuous event from matrix mat_event <- event_matrix( matrix(c(1.1, 0.9, 1.2, 0.8, 2.1, 1.9, 2.2, 1.8), nrow = 4), name = "coordinates", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(mat_event) # Returns TRUE # Categorical event is not continuous cat_event <- event_factor( factor(c("faces", "houses", "faces", "houses")), name = "condition", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(cat_event) # Returns FALSE # Event term with all continuous events cont_term <- event_term( list(rt = c(1.1, 0.9, 1.2, 0.8), accuracy = c(0.95, 0.87, 0.92, 0.88)), onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(cont_term) # Returns TRUE# Create a continuous event from numeric vector cont_event <- event_variable( c(1.2, 0.8, 1.5, 0.9), name = "reaction_time", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(cont_event) # Returns TRUE # Create a continuous event from matrix mat_event <- event_matrix( matrix(c(1.1, 0.9, 1.2, 0.8, 2.1, 1.9, 2.2, 1.8), nrow = 4), name = "coordinates", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(mat_event) # Returns TRUE # Categorical event is not continuous cat_event <- event_factor( factor(c("faces", "houses", "faces", "houses")), name = "condition", onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(cat_event) # Returns FALSE # Event term with all continuous events cont_term <- event_term( list(rt = c(1.1, 0.9, 1.2, 0.8), accuracy = c(0.95, 0.87, 0.92, 0.88)), onsets = c(0, 10, 20, 30), blockids = rep(1, 4) ) is_continuous(cont_term) # Returns TRUE
Check if a Class is a Registered External HRF Specification
is_external_hrfspec(x)is_external_hrfspec(x)
x |
An object or character string class name |
Logical indicating if the class is registered as an external HRF spec
register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg" ) is_external_hrfspec("demo_hrfspec")register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg" ) is_external_hrfspec("demo_hrfspec")
Returns a character vector of formatted labels for an event object,
using the Variable[Level] style for categorical events,
Variable[Index] for multi-column continuous events, or just
Variable for single continuous events.
Useful for getting consistent labels for individual event components.
This is distinct from levels() which returns the raw level names or column names.
Relies on the internal .level_vector helper function.
## S3 method for class 'event' labels(object, ...)## S3 method for class 'event' labels(object, ...)
object |
An object of class |
... |
Additional arguments (unused). |
A character vector of formatted labels, or character(0) if not applicable.
fac <- factor(rep(c("A", "B"), 3)) onsets <- 1:6 ev_fac <- event_factor(fac, "Condition", onsets, blockids = rep(1, length(onsets))) labels(ev_fac) # Should return c("Condition[A]", "Condition[B]") vals <- 1:6 ev_num <- event_variable(vals, "Modulator", onsets, blockids = rep(1, length(onsets))) labels(ev_num) # Should return "Modulator" mat <- matrix(1:12, 6, 2) colnames(mat) <- c("C1", "C2") ev_mat <- event_matrix(mat, "MatrixVar", onsets, blockids = rep(1, length(onsets))) labels(ev_mat) # Should return c("MatrixVar[1]", "MatrixVar[2]")fac <- factor(rep(c("A", "B"), 3)) onsets <- 1:6 ev_fac <- event_factor(fac, "Condition", onsets, blockids = rep(1, length(onsets))) labels(ev_fac) # Should return c("Condition[A]", "Condition[B]") vals <- 1:6 ev_num <- event_variable(vals, "Modulator", onsets, blockids = rep(1, length(onsets))) labels(ev_num) # Should return "Modulator" mat <- matrix(1:12, 6, 2) colnames(mat) <- c("C1", "C2") ev_mat <- event_matrix(mat, "MatrixVar", onsets, blockids = rep(1, length(onsets))) labels(ev_mat) # Should return c("MatrixVar[1]", "MatrixVar[2]")
Extract levels from various fmrireg objects. These methods extend the base R
levels generic to work with fmrireg-specific classes.
## S3 method for class 'Scale' levels(x, ...) ## S3 method for class 'ScaleWithin' levels(x, ...) ## S3 method for class 'RobustScale' levels(x, ...) ## S3 method for class 'event' levels(x, ...) ## S3 method for class 'event' columns(x, ...)## S3 method for class 'Scale' levels(x, ...) ## S3 method for class 'ScaleWithin' levels(x, ...) ## S3 method for class 'RobustScale' levels(x, ...) ## S3 method for class 'event' levels(x, ...) ## S3 method for class 'event' columns(x, ...)
x |
An object from which to extract levels. Can be:
|
... |
Additional arguments (currently unused). |
A character vector of levels or names, depending on the object type:
For categorical events: the factor levels
For continuous events: the column names (matrices) or variable name (vectors)
For scale objects: the variable name being scaled
columns(event): Alias for levels.event
# Create a categorical event fac_event <- event_factor( factor(c("A", "B", "A", "B")), name = "condition", onsets = c(1, 10, 20, 30), blockids = rep(1, 4) ) levels(fac_event) # Returns: c("A", "B") # Create a continuous event cont_event <- event_variable( c(1.2, 0.8, 1.5, 0.9), name = "reaction_time", onsets = c(1, 10, 20, 30), blockids = rep(1, 4) ) levels(cont_event) # Returns: "reaction_time"# Create a categorical event fac_event <- event_factor( factor(c("A", "B", "A", "B")), name = "condition", onsets = c(1, 10, 20, 30), blockids = rep(1, 4) ) levels(fac_event) # Returns: c("A", "B") # Create a continuous event cont_event <- event_variable( c(1.2, 0.8, 1.5, 0.9), name = "reaction_time", onsets = c(1, 10, 20, 30), blockids = rep(1, 4) ) levels(cont_event) # Returns: "reaction_time"
List All Registered External HRF Specifications
list_external_hrfspecs()list_external_hrfspecs()
A character vector of registered class names
register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg" ) list_external_hrfspecs()register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg" ) list_external_hrfspecs()
List Registered Parametric Basis Classes
list_registered_bases()list_registered_bases()
Character vector of registered class names.
list_registered_bases()list_registered_bases()
Superseded by conditions(x, style = "canonical").
longnames(x, ...)longnames(x, ...)
x |
The object. |
... |
Additional arguments. |
Character vector of long (fully qualified) names.
# Create a simple event term with one condition factor term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) longnames(term) # Returns: "condition.A" "condition.B" # Create event term with multiple factors term2 <- event_term( list( category = factor(c("face", "scene", "face")), attention = factor(c("attend", "attend", "ignore")) ), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) longnames(term2) # Returns: "category.face_attention.attend" # "category.scene_attention.attend" # "category.face_attention.ignore"# Create a simple event term with one condition factor term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) longnames(term) # Returns: "condition.A" "condition.B" # Create event term with multiple factors term2 <- event_term( list( category = factor(c("face", "scene", "face")), attention = factor(c("attend", "attend", "ignore")) ), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) longnames(term2) # Returns: "category.face_attention.attend" # "category.scene_attention.attend" # "category.face_attention.ignore"
Get number of basis functions from hrfspec
## S3 method for class 'hrfspec' nbasis(x, ...)## S3 method for class 'hrfspec' nbasis(x, ...)
x |
An hrfspec object |
... |
Additional arguments (unused) |
The number of basis functions
# Create hrfspec with canonical HRF (1 basis function) spec1 <- hrf(condition, basis = "spmg1") nbasis(spec1) # Create hrfspec with derivative HRF (2 basis functions) spec2 <- hrf(condition, basis = "spmg2") nbasis(spec2) # Create hrfspec with FIR basis (custom number of basis functions) spec3 <- hrf(condition, basis = "fir", nbasis = 10) nbasis(spec3)# Create hrfspec with canonical HRF (1 basis function) spec1 <- hrf(condition, basis = "spmg1") nbasis(spec1) # Create hrfspec with derivative HRF (2 basis functions) spec2 <- hrf(condition, basis = "spmg2") nbasis(spec2) # Create hrfspec with FIR basis (custom number of basis functions) spec3 <- hrf(condition, basis = "fir", nbasis = 10) nbasis(spec3)
Returns a nuisance term specification from a numeric matrix.
nuisance(x)nuisance(x)
x |
A matrix. |
An object of class "nuisancespec".
mat <- matrix(rnorm(10), nrow = 5) nuisance(mat)mat <- matrix(rnorm(10), nrow = 5) nuisance(mat)
Construct contrasts comparing each factor level against the average of the other levels.
one_against_all_contrast(levels, facname, where = NULL)one_against_all_contrast(levels, facname, where = NULL)
levels |
A vector of factor levels to be compared. |
facname |
A character string specifying the name of the factor containing the supplied levels. |
where |
An optional formula specifying the subset over which the contrast is computed. |
A contrast_set object containing contrasts comparing each factor level against the average of the other levels.
fac <- factor(rep(c("A", "B", "C"), 2)) con <- one_against_all_contrast(levels(fac), "fac")fac <- factor(rep(c("A", "B", "C"), 2)) con <- one_against_all_contrast(levels(fac), "fac")
Create a one-way contrast specification
oneway_contrast(A, name, where = NULL, basis = NULL, basis_weights = NULL)oneway_contrast(A, name, where = NULL, basis = NULL, basis_weights = NULL)
A |
A formula specifying the contrast |
name |
The name of the contrast |
where |
An optional formula specifying the subset over which the contrast is computed. |
basis |
NULL (default: use all basis functions), an integer vector specifying
which basis function indices to include, or |
basis_weights |
NULL (default: equal weights), or a numeric vector of weights to apply
to the selected basis functions. Must have the same length as |
A oneway_contrast_spec object that can be used to generate contrast weights
interaction_contrast for testing interactions,
pair_contrast for pairwise comparisons
# Create a one-way contrast for a factor 'basis' con <- oneway_contrast(~ basis, name = "Main_basis") # Create a one-way contrast with a 'where' clause con <- oneway_contrast(~ basis, name = "Main_basis", where = ~ block == 1) # Test only first two basis functions con <- oneway_contrast(~ condition, name = "Main_early", basis = 1:2)# Create a one-way contrast for a factor 'basis' con <- oneway_contrast(~ basis, name = "Main_basis") # Create a one-way contrast with a 'where' clause con <- oneway_contrast(~ basis, name = "Main_basis", where = ~ block == 1) # Test only first two basis functions con <- oneway_contrast(~ condition, name = "Main_early", basis = 1:2)
Construct a sum-to-zero contrast between two logical expressions. This function is particularly useful for comparing specific conditions or combinations of conditions.
pair_contrast(A, B, name, where = NULL, basis = NULL, basis_weights = NULL)pair_contrast(A, B, name, where = NULL, basis = NULL, basis_weights = NULL)
A |
A formula representing the first logical expression in the contrast. |
B |
A formula representing the second logical expression in the contrast. |
name |
A character string specifying the name of the contrast (mandatory). |
where |
An optional formula specifying the subset over which the contrast is computed. |
basis |
NULL (default: use all basis functions), an integer vector specifying
which basis function indices to include (e.g., |
basis_weights |
NULL (default: equal weights), or a numeric vector of weights to apply
to the selected basis functions. Must have the same length as |
The contrast is constructed as (A - B), where A and B are logical expressions that evaluate to TRUE/FALSE for each observation. The resulting contrast weights sum to zero.
When using multi-basis HRFs (e.g., bspline with 5 basis functions), the basis argument
allows you to test specific temporal components of the response. For example:
basis = 1: Test only the first basis function (often the canonical/early response)
basis = 2:3: Test the second and third basis functions together
basis = NULL or basis = "all": Test all basis functions (default behavior)
The basis_weights argument allows non-uniform weighting across selected basis functions:
basis_weights = c(.1, .2, .4, .2, .1): Gaussian-like emphasis on peak
basis_weights = c(1, 0, 0, 0, 0): Isolate first basis (equivalent to basis = 1)
Weights are applied within each condition, maintaining contrast sum-to-zero property
A pair_contrast_spec object containing:
A |
First logical expression |
B |
Second logical expression |
where |
Subsetting formula (if provided) |
basis |
Basis function specification (if provided) |
basis_weights |
Basis weights (if provided) |
name |
Contrast name |
pairwise_contrasts for all pairwise comparisons,
contrast_set for creating sets of contrasts
# Compare faces vs scenes (all basis functions) pair_contrast(~ category == "face", ~ category == "scene", name = "face_vs_scene") # Test only the second basis function (e.g., linear component for polynomial HRF) pair_contrast(~ category == "face", ~ category == "scene", basis = 2, name = "face_vs_scene_basis2") # Test early response components (first 3 basis functions) pair_contrast(~ category == "face", ~ category == "scene", basis = 1:3, name = "face_vs_scene_early") # Compare with subsetting pair_contrast(~ category == "face", ~ category == "scene", name = "face_vs_scene_block1", where = ~ block == 1) # Complex logical expressions pair_contrast(~ stimulus == "face" & emotion == "happy", ~ stimulus == "face" & emotion == "sad", name = "happy_vs_sad_faces")# Compare faces vs scenes (all basis functions) pair_contrast(~ category == "face", ~ category == "scene", name = "face_vs_scene") # Test only the second basis function (e.g., linear component for polynomial HRF) pair_contrast(~ category == "face", ~ category == "scene", basis = 2, name = "face_vs_scene_basis2") # Test early response components (first 3 basis functions) pair_contrast(~ category == "face", ~ category == "scene", basis = 1:3, name = "face_vs_scene_early") # Compare with subsetting pair_contrast(~ category == "face", ~ category == "scene", name = "face_vs_scene_block1", where = ~ block == 1) # Complex logical expressions pair_contrast(~ stimulus == "face" & emotion == "happy", ~ stimulus == "face" & emotion == "sad", name = "happy_vs_sad_faces")
Construct pairwise contrasts for all combinations of factor levels.
pairwise_contrasts(levels, facname, where = NULL, name_prefix = "con")pairwise_contrasts(levels, facname, where = NULL, name_prefix = "con")
levels |
A vector of factor levels to be compared. |
facname |
The name of the factor variable (column name in the design) these levels belong to. |
where |
An optional formula specifying the subset over which the contrast is computed. |
name_prefix |
A character string to prefix the generated contrast names (default: "con"). |
A contrast_set object containing pairwise contrasts for all combinations of factor levels.
# Assuming 'my_factor' is a column name pairwise_contrasts(c("A", "B", "C"), facname = "my_factor") pairwise_contrasts(c("A", "B", "C"), facname = "my_factor", name_prefix = "pair")# Assuming 'my_factor' is a column name pairwise_contrasts(c("A", "B", "C"), facname = "my_factor") pairwise_contrasts(c("A", "B", "C"), facname = "my_factor", name_prefix = "pair")
Generic function for plotting contrasts.
plot_contrasts(x, ...)plot_contrasts(x, ...)
x |
Object containing contrast information |
... |
Additional arguments passed to methods |
A plot object (typically ggplot2) displaying the contrasts. The exact type depends on the method used.
# Create example data des <- data.frame( onset = c(1, 3, 5, 7), cond = factor(c("A", "B", "A", "B")), run = c(1, 1, 1, 1) ) # Create sampling frame and event model sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) # Create contrast set cset <- contrast_set( main_A = unit_contrast(~ cond == "A", name = "A_vs_baseline"), diff = pair_contrast(~ cond == "A", ~ cond == "B", name = "A_vs_B") ) # Create event model with contrasts emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) # Plot the contrasts plot_contrasts(emod)# Create example data des <- data.frame( onset = c(1, 3, 5, 7), cond = factor(c("A", "B", "A", "B")), run = c(1, 1, 1, 1) ) # Create sampling frame and event model sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) # Create contrast set cset <- contrast_set( main_A = unit_contrast(~ cond == "A", name = "A_vs_baseline"), diff = pair_contrast(~ cond == "A", ~ cond == "B", name = "A_vs_B") ) # Create event model with contrasts emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) # Plot the contrasts plot_contrasts(emod)
Produces a heatmap of all contrasts defined for an event_model.
Rows = each contrast (or column of an F-contrast), columns = each regressor in
the full design matrix, and the fill color = the contrast weight.
## S3 method for class 'event_model' plot_contrasts( x, absolute_limits = FALSE, rotate_x_text = TRUE, scale_mode = c("auto", "diverging", "one_sided"), coord_fixed = TRUE, ... )## S3 method for class 'event_model' plot_contrasts( x, absolute_limits = FALSE, rotate_x_text = TRUE, scale_mode = c("auto", "diverging", "one_sided"), coord_fixed = TRUE, ... )
x |
An |
absolute_limits |
Logical; if |
rotate_x_text |
Logical; if |
scale_mode |
Character; 'auto', 'diverging', or 'one_sided' color scaling. |
coord_fixed |
Logical; if TRUE, use fixed aspect ratio. |
... |
Further arguments passed to |
A ggplot2 object (a heatmap).
# Create event model with contrasts des <- data.frame( onset = c(0, 10, 20, 30, 40, 50), run = 1, cond = factor(c("A", "B", "C", "A", "B", "C")) ) sframe <- fmrihrf::sampling_frame(blocklens = 60, TR = 1) cset <- contrast_set( A_vs_B = pair_contrast(~ cond == "A", ~ cond == "B", name = "A_vs_B"), B_vs_C = pair_contrast(~ cond == "B", ~ cond == "C", name = "B_vs_C") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) plot_contrasts(emod)# Create event model with contrasts des <- data.frame( onset = c(0, 10, 20, 30, 40, 50), run = 1, cond = factor(c("A", "B", "C", "A", "B", "C")) ) sframe <- fmrihrf::sampling_frame(blocklens = 60, TR = 1) cset <- contrast_set( A_vs_B = pair_contrast(~ cond == "A", ~ cond == "B", name = "A_vs_B"), B_vs_C = pair_contrast(~ cond == "B", ~ cond == "C", name = "B_vs_C") ) emod <- event_model(onset ~ hrf(cond, contrasts = cset), data = des, block = ~run, sampling_frame = sframe) plot_contrasts(emod)
Creates a line plot visualization of the predicted BOLD response for each regressor in an event_model object.
## S3 method for class 'event_model' plot( x, term_name = NULL, facet_threshold = Inf, label_mode = c("auto", "compact", "none"), max_labels = 30, abbrev_min = 10, strip_text_size = 8, block_x = c("global", "run"), facet_by_block = FALSE, show_block_bounds = TRUE, ... )## S3 method for class 'event_model' plot( x, term_name = NULL, facet_threshold = Inf, label_mode = c("auto", "compact", "none"), max_labels = 30, abbrev_min = 10, strip_text_size = 8, block_x = c("global", "run"), facet_by_block = FALSE, show_block_bounds = TRUE, ... )
x |
An |
term_name |
Character. Name of specific term to plot. If NULL, plots all terms. |
facet_threshold |
Integer. Switch to faceting when number of regressors exceeds this value. Default 6. |
label_mode |
Character. One of |
max_labels |
Integer. When |
abbrev_min |
Integer. Minimum length used by |
strip_text_size |
Numeric. Strip label text size when faceting with labels. Default 8. |
block_x |
Time axis to use for multi-run designs. |
facet_by_block |
Logical; if |
show_block_bounds |
Logical; if |
... |
Additional arguments (currently unused). |
This method attempts to keep labels readable when there are many
regressors (e.g., trial-wise designs) by switching to faceting and either
abbreviating or suppressing labels depending on thresholds. You can control
this behavior via label_mode, max_labels, and abbrev_min.
A ggplot2 object showing the predicted BOLD timecourses.
# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Plot all regressors plot(emod) # Plot specific term only plot(emod, term_name = "cond")# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Plot all regressors plot(emod) # Plot specific term only plot(emod, term_name = "cond")
Orthogonal polynomial expansion of a linear term based on poly
Poly(x, degree)Poly(x, degree)
x |
a numeric vector at which to evaluate the polynomial. Missing values are not allowed in x. |
degree |
the degree of the polynomial. Must be less than the number of unique points. |
an instance of class Poly extending ParametricBasis
# Create a 3rd degree polynomial basis x_vals <- c(1, 2, 3, 4, 5, 6) poly_basis <- Poly(x_vals, degree = 3) print(poly_basis$y)# Create a 3rd degree polynomial basis x_vals <- c(1, 2, 3, 4, 5, 6) poly_basis <- Poly(x_vals, degree = 3) print(poly_basis$y)
Create polynomial contrasts for testing trends across ordered factor levels. This is particularly useful for analyzing factors with a natural ordering (e.g., time, dose).
poly_contrast( A, name, where = NULL, degree = 1, value_map = NULL, basis = NULL, basis_weights = NULL )poly_contrast( A, name, where = NULL, degree = 1, value_map = NULL, basis = NULL, basis_weights = NULL )
A |
A formula specifying the ordered factor. |
name |
A character string identifying the contrast. |
where |
An optional formula for subsetting the data. |
degree |
An integer specifying the degree of the polynomial (default: 1). |
value_map |
An optional list mapping factor levels to numeric values. |
basis |
NULL (default: use all basis functions), an integer vector specifying
which basis function indices to include, or |
basis_weights |
NULL (default: equal weights), or a numeric vector of weights to apply
to the selected basis functions. Must have the same length as |
The function creates orthogonal polynomial contrasts up to the specified degree. These contrasts can test for linear, quadratic, cubic, and higher-order trends in the data. The value_map parameter allows for non-uniform spacing between levels.
A poly_contrast_spec object containing the specification for generating polynomial contrast weights.
oneway_contrast for categorical contrasts,
interaction_contrast for interaction effects
# Linear trend across time points pcon <- poly_contrast(~ time, name = "linear_time", degree = 1) # Cubic trend with custom spacing pcon <- poly_contrast(~ dose, name = "dose_cubic", degree = 3, value_map = list("low" = 0, "med" = 2, "high" = 5)) # Linear trend for only first basis function pcon <- poly_contrast(~ dose, name = "dose_linear_basis1", degree = 1, basis = 1)# Linear trend across time points pcon <- poly_contrast(~ time, name = "linear_time", degree = 1) # Cubic trend with custom spacing pcon <- poly_contrast(~ dose, name = "dose_cubic", degree = 3, value_map = list("low" = 0, "med" = 2, "high" = 5)) # Linear trend for only first basis function pcon <- poly_contrast(~ dose, name = "dose_linear_basis1", degree = 1, basis = 1)
Dispatch to the appropriate method for transforming new data according to a specific parametric basis.
## S3 method for class 'ParametricBasis' predict(object, newdata, ...) ## S3 method for class 'Standardized' predict(object, newdata, ...) ## S3 method for class 'Poly' predict(object, newdata, ...) ## S3 method for class 'BSpline' predict(object, newdata, ...) ## S3 method for class 'Ident' predict(object, newdata, ...) ## S3 method for class 'Scale' predict(object, newdata, ...) ## S3 method for class 'ScaleWithin' predict(object, newdata, newgroup, ...) ## S3 method for class 'RobustScale' predict(object, newdata, ...)## S3 method for class 'ParametricBasis' predict(object, newdata, ...) ## S3 method for class 'Standardized' predict(object, newdata, ...) ## S3 method for class 'Poly' predict(object, newdata, ...) ## S3 method for class 'BSpline' predict(object, newdata, ...) ## S3 method for class 'Ident' predict(object, newdata, ...) ## S3 method for class 'Scale' predict(object, newdata, ...) ## S3 method for class 'ScaleWithin' predict(object, newdata, newgroup, ...) ## S3 method for class 'RobustScale' predict(object, newdata, ...)
object |
ParametricBasis object. |
newdata |
Numeric vector to transform. |
... |
Additional arguments. |
newgroup |
Optional factor for group-dependent bases. |
A numeric matrix with transformed values (one column per basis component).
# Create polynomial basis from training data train_x <- 1:10 poly_basis <- Poly(train_x, degree = 2) # Predict on new data new_x <- c(5.5, 7.3, 11.2) predict(poly_basis, new_x) # Create scaling basis train_vals <- c(10, 20, 30, 40, 50) scale_basis <- Scale(train_vals) # Apply same scaling to new data new_vals <- c(15, 25, 35) predict(scale_basis, new_vals)# Create polynomial basis from training data train_x <- 1:10 poly_basis <- Poly(train_x, degree = 2) # Predict on new data new_x <- c(5.5, 7.3, 11.2) predict(poly_basis, new_x) # Create scaling basis train_vals <- c(10, 20, 30, 40, 50) scale_basis <- Scale(train_vals) # Apply same scaling to new data new_vals <- c(15, 25, 35) predict(scale_basis, new_vals)
Displays key information about the baseline model components and a preview of the design matrix.
Print a contrast set.
Print a contrast specification.
Print a contrast.
Print a polynomial contrast specification.
Print a contrast difference specification.
Provides a concise summary of an event object using cli.
Provides a concise summary of an event_model object using cli.
Provides a concise summary of an event_term object using cli.
## S3 method for class 'baseline_model' print(x, ...) ## S3 method for class 'contrast_set' print(x, ...) ## S3 method for class 'contrast_spec' print(x, ...) ## S3 method for class 'contrast' print(x, ...) ## S3 method for class 'poly_contrast_spec' print(x, ...) ## S3 method for class 'contrast_diff_spec' print(x, ...) ## S3 method for class 'event' print(x, ...) ## S3 method for class 'event_model' print(x, ...) ## S3 method for class 'fmri_term' print(x, ...) ## S3 method for class 'convolved_term' print(x, ...) ## S3 method for class 'event_term' print(x, ...)## S3 method for class 'baseline_model' print(x, ...) ## S3 method for class 'contrast_set' print(x, ...) ## S3 method for class 'contrast_spec' print(x, ...) ## S3 method for class 'contrast' print(x, ...) ## S3 method for class 'poly_contrast_spec' print(x, ...) ## S3 method for class 'contrast_diff_spec' print(x, ...) ## S3 method for class 'event' print(x, ...) ## S3 method for class 'event_model' print(x, ...) ## S3 method for class 'fmri_term' print(x, ...) ## S3 method for class 'convolved_term' print(x, ...) ## S3 method for class 'event_term' print(x, ...)
x |
An event_term object. |
... |
Additional arguments (unused). |
The input object, invisibly.
sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) bmod <- baseline_model(sframe = sframe) print(bmod)sframe <- fmrihrf::sampling_frame(blocklens = 5, TR = 1) bmod <- baseline_model(sframe = sframe) print(bmod)
Custom print and plot methods for sampling_frame objects (from fmrihrf).
The print method provides a concise summary focused on runs/blocks, TR and
total scans without mentioning lower-level evaluation precision. The plot
method visualizes runs over time.
## S3 method for class 'sampling_frame' print(x, ...) ## S3 method for class 'sampling_frame' plot(x, style = c("timeline", "grid"), show_ticks = FALSE, tick_every = 5, ...)## S3 method for class 'sampling_frame' print(x, ...) ## S3 method for class 'sampling_frame' plot(x, style = c("timeline", "grid"), show_ticks = FALSE, tick_every = 5, ...)
x |
A |
... |
Unused. |
style |
Plot style for |
show_ticks |
Logical; for |
tick_every |
Integer; draw a tick every |
For print(), returns x invisibly. For plot(), a ggplot object.
sf <- fmrihrf::sampling_frame(blocklens = c(60, 120), TR = 2) print(sf) plot(sf)sf <- fmrihrf::sampling_frame(blocklens = c(60, 120), TR = 2) print(sf) plot(sf)
Register a basis class so that the term-naming pipeline produces a
well-formed term tag (<prefix>_<varname>) and so that design-matrix
metadata identifies columns built from this basis as parametric.
Built-in bases (Poly, BSpline, Scale, Standardized,
ScaleWithin, RobustScale, Ident) are registered automatically.
register_basis( class_name, prefix = NULL, modulation = c("parametric", "amplitude"), description = NULL )register_basis( class_name, prefix = NULL, modulation = c("parametric", "amplitude"), description = NULL )
class_name |
Character string naming the S3 class extending
|
prefix |
Character string used as the term-tag prefix for terms whose
sole variable is |
modulation |
Character string describing the modulation kind for
columns generated by this basis. Defaults to |
description |
Optional human-readable description. |
Invisibly returns the registration entry.
# Register a custom orthogonal-polynomial basis with prefix "ortho" register_basis("OrthoPoly", prefix = "ortho")# Register a custom orthogonal-polynomial basis with prefix "ortho" register_basis("OrthoPoly", prefix = "ortho")
Register a new HRF specification class that can be used in event models. This allows external packages to extend fmridesign with their own HRF types.
register_hrfspec_extension( spec_class, package, convolved_class = NULL, requires_external_processing = FALSE, formula_functions = NULL )register_hrfspec_extension( spec_class, package, convolved_class = NULL, requires_external_processing = FALSE, formula_functions = NULL )
spec_class |
Character string naming the class to register |
package |
Character string naming the package providing the class |
convolved_class |
Optional character string naming the associated convolved term class |
requires_external_processing |
Logical indicating if this spec should be skipped during standard convolution (e.g., for AFNI terms that are processed externally) |
formula_functions |
Optional character vector of function names that should be recognised in formulas and mapped to this HRF specification class. |
Invisible NULL
register_hrfspec_extension( spec_class = "afni_hrfspec", package = "afnireg", convolved_class = "afni_hrf_convolved_term", requires_external_processing = TRUE, formula_functions = "afni_hrf" )register_hrfspec_extension( spec_class = "afni_hrfspec", package = "afnireg", convolved_class = "afni_hrf_convolved_term", requires_external_processing = TRUE, formula_functions = "afni_hrf" )
Convolve the event-term design matrix with an HRF and return the resulting regressors.
regressors(x, ...) ## S3 method for class 'event_term' regressors(x, hrf, sampling_frame, summate = FALSE, drop.empty = TRUE, ...)regressors(x, ...) ## S3 method for class 'event_term' regressors(x, hrf, sampling_frame, summate = FALSE, drop.empty = TRUE, ...)
x |
The object. |
... |
Additional arguments. |
hrf |
HRF function |
sampling_frame |
sampling_frame object |
summate |
Logical; sum HRF responses |
drop.empty |
Logical; drop empty conditions |
Character vector of regressor names for x.
# Create an event term with two conditions term <- event_term( list(condition = factor(c("A", "B", "A", "B"))), onsets = c(0, 10, 20, 30), blockids = c(1, 1, 1, 1) ) # Create a sampling frame for timing information sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 2) # Extract regressors convolved with canonical HRF reg <- regressors(term, hrf = fmrihrf::HRF_SPMG1, sampling_frame = sframe) names(reg) # Shows regressor names: "condition.A" "condition.B"# Create an event term with two conditions term <- event_term( list(condition = factor(c("A", "B", "A", "B"))), onsets = c(0, 10, 20, 30), blockids = c(1, 1, 1, 1) ) # Create a sampling frame for timing information sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 2) # Extract regressors convolved with canonical HRF reg <- regressors(term, hrf = fmrihrf::HRF_SPMG1, sampling_frame = sframe) names(reg) # Shows regressor names: "condition.A" "condition.B"
Determines if an HRF specification or convolved term should be handled by external tools rather than R's standard convolution.
requires_external_processing(x)requires_external_processing(x)
x |
An object to check |
Logical indicating if external processing is required
register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", requires_external_processing = TRUE ) requires_external_processing("demo_hrfspec")register_hrfspec_extension( spec_class = "demo_hrfspec", package = "demoPkg", requires_external_processing = TRUE ) requires_external_processing("demo_hrfspec")
Projects the data onto the orthogonal complement of the design, returning
residuals from an OLS fit. Specialized for event_model, baseline_model,
or a raw numeric design matrix.
residualize(x, data, cols = NULL, ...)residualize(x, data, cols = NULL, ...)
x |
A design object ( |
data |
A numeric vector/matrix/data.frame of observations Y with rows matching |
cols |
Optional integer or character vector selecting columns of the design to project out. |
... |
Additional arguments passed to methods. |
Residuals with the same dimensions as data.
# Simple example with a raw design matrix X <- cbind(1, 1:5) Y <- cbind(1:5, 2:6) R <- residualize(X, Y) dim(R)# Simple example with a raw design matrix X <- cbind(1, 1:5) Y <- cbind(1:5, 2:6) R <- residualize(X, Y) dim(R)
S3 methods for the residualize() generic. These methods project data
onto the orthogonal complement of a design, returning OLS residuals.
## S3 method for class 'matrix' residualize(x, data, cols = NULL, ...) ## S3 method for class 'event_model' residualize(x, data, cols = NULL, ...) ## S3 method for class 'baseline_model' residualize(x, data, cols = NULL, ...)## S3 method for class 'matrix' residualize(x, data, cols = NULL, ...) ## S3 method for class 'event_model' residualize(x, data, cols = NULL, ...) ## S3 method for class 'baseline_model' residualize(x, data, cols = NULL, ...)
x |
A design object: matrix, event_model, or baseline_model. |
data |
Numeric vector/matrix/data.frame of observations Y. |
cols |
Optional integer or character vector selecting columns to project out. |
... |
Additional arguments (currently unused). |
A numeric matrix of residuals with the same dimensions as data.
# Residualize with a raw matrix X <- cbind(1, 1:10) Y <- matrix(rnorm(20), ncol = 2) R <- residualize(X, Y) dim(R) # 10 x 2 # Residualize with an event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) Y_sim <- matrix(rnorm(40 * 2), ncol = 2) R_emod <- residualize(emod, Y_sim) dim(R_emod) # 40 x 2# Residualize with a raw matrix X <- cbind(1, 1:10) Y <- matrix(rnorm(20), ncol = 2) R <- residualize(X, Y) dim(R) # 10 x 2 # Residualize with an event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) Y_sim <- matrix(rnorm(40 * 2), ncol = 2) R_emod <- residualize(emod, Y_sim) dim(R_emod) # 40 x 2
Robust Scaling (Median/MAD)
RobustScale(x)RobustScale(x)
x |
numeric vector (NAs allowed) |
object of class c("RobustScale","ParametricBasis")
# Create a robust scale transformed basis using median and MAD x_vals <- c(1, 2, 3, 4, 100) # Note the outlier robust_basis <- RobustScale(x_vals) print(robust_basis$y) print(robust_basis$median) print(robust_basis$mad)# Create a robust scale transformed basis using median and MAD x_vals <- c(1, 2, 3, 4, 100) # Note the outlier robust_basis <- RobustScale(x_vals) print(robust_basis$y) print(robust_basis$median) print(robust_basis$mad)
Wraps make.names but allows control over dot replacement.
sanitize(x, allow_dot = TRUE)sanitize(x, allow_dot = TRUE)
x |
A character vector. |
allow_dot |
Logical, if |
A sanitized character vector.
sanitize("a.b c") sanitize("a.b c", allow_dot = FALSE)sanitize("a.b c") sanitize("a.b c", allow_dot = FALSE)
Z-score (global) basis
Scale(x)Scale(x)
x |
numeric vector (NAs allowed) |
object of class c("Scale","ParametricBasis")
# Create a z-score transformed basis x_vals <- c(1, 3, 5, 7, 9, 11) scale_basis <- Scale(x_vals) print(scale_basis$y) print(scale_basis$mean) print(scale_basis$sd)# Create a z-score transformed basis x_vals <- c(1, 3, 5, 7, 9, 11) scale_basis <- Scale(x_vals) print(scale_basis$y) print(scale_basis$mean) print(scale_basis$sd)
Z-score within groups
ScaleWithin(x, g)ScaleWithin(x, g)
x |
numeric vector |
g |
grouping factor / character / integer of same length as x |
An object of class ScaleWithin (a ParametricBasis).
# Create a within-group z-score transformed basis x_vals <- c(1, 2, 3, 10, 11, 12) groups <- c("A", "A", "A", "B", "B", "B") scale_within_basis <- ScaleWithin(x_vals, groups) print(scale_within_basis$y) print(scale_within_basis$means) print(scale_within_basis$sds)# Create a within-group z-score transformed basis x_vals <- c(1, 2, 3, 10, 11, 12) groups <- c("A", "A", "A", "B", "B", "B") scale_within_basis <- ScaleWithin(x_vals, groups) print(scale_within_basis$y) print(scale_within_basis$means) print(scale_within_basis$sds)
Superseded by conditions(x, style = "display").
shortnames(x, ...)shortnames(x, ...)
x |
The object. |
... |
Additional arguments. |
Character vector of short names.
# Create a simple event term with one condition factor term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) shortnames(term) # Returns: "A" "B" # Create event term with multiple factors term2 <- event_term( list( category = factor(c("face", "scene", "face")), attention = factor(c("attend", "attend", "ignore")) ), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) shortnames(term2) # Returns: "face:attend" "scene:attend" "face:ignore"# Create a simple event term with one condition factor term <- event_term( list(condition = factor(c("A", "B", "A"))), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) shortnames(term) # Returns: "A" "B" # Create event term with multiple factors term2 <- event_term( list( category = factor(c("face", "scene", "face")), attention = factor(c("attend", "attend", "ignore")) ), onsets = c(0, 10, 20), blockids = c(1, 1, 1) ) shortnames(term2) # Returns: "face:attend" "scene:attend" "face:ignore"
Generate a set of A-vs-B contrasts where A and B are adjacent, equally sized
and disjoint windows over an ordered factor. For window size k, contrast i
compares A = levels[i:(i+k-1)] against B = levels[(i+k):(i+2k-1)].
This yields length(levels) - 2*k + 1 contrasts that detect local changes
across the sequence without overlapping masks.
sliding_window_contrasts( levels, facname, window_size = 2, where = NULL, name_prefix = "win" )sliding_window_contrasts( levels, facname, window_size = 2, where = NULL, name_prefix = "win" )
levels |
Character vector of ordered factor levels. |
facname |
Name of the factor (column in the design). |
window_size |
Positive integer window size (default 2). |
where |
Optional formula to subset events used when computing weights. |
name_prefix |
Prefix for generated contrast names (default "win"). |
A contrast_set of pair_contrast specifications.
# For levels 1..5, generate 2 disjoint adjacent-window contrasts (k=2) sliding_window_contrasts(as.character(1:5), facname = "intensity", window_size = 2) # For k=3 with 7 levels (disjoint windows): # A=[1,2,3] vs B=[4,5,6], then A=[2,3,4] vs B=[5,6,7] sliding_window_contrasts(LETTERS[1:7], facname = "difficulty", window_size = 3)# For levels 1..5, generate 2 disjoint adjacent-window contrasts (k=2) sliding_window_contrasts(as.character(1:5), facname = "intensity", window_size = 2) # For k=3 with 7 levels (disjoint windows): # A=[1,2,3] vs B=[4,5,6], then A=[2,3,4] vs B=[5,6,7] sliding_window_contrasts(LETTERS[1:7], facname = "difficulty", window_size = 3)
Split by block
split_by_block(x, ...) ## S3 method for class 'event_model' split_by_block(x, ...)split_by_block(x, ...) ## S3 method for class 'event_model' split_by_block(x, ...)
x |
The object. |
... |
Additional arguments. |
A list split by block/run.
des <- data.frame( onset = c(0, 10, 20, 30, 5, 15, 25, 35), run = c(1, 1, 1, 1, 2, 2, 2, 2), cond = factor(c("A", "B", "A", "B", "A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = c(40, 40), TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) block_list <- split_by_block(emod) length(block_list)des <- data.frame( onset = c(0, 10, 20, 30, 5, 15, 25, 35), run = c(1, 1, 1, 1, 2, 2, 2, 2), cond = factor(c("A", "B", "A", "B", "A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = c(40, 40), TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) block_list <- split_by_block(emod) length(block_list)
Split onsets
split_onsets(x, sframe, global = FALSE, blocksplit = FALSE, ...) ## S3 method for class 'event_term' split_onsets(x, sframe, global = FALSE, blocksplit = FALSE, ...)split_onsets(x, sframe, global = FALSE, blocksplit = FALSE, ...) ## S3 method for class 'event_term' split_onsets(x, sframe, global = FALSE, blocksplit = FALSE, ...)
x |
The object. |
sframe |
The sampling frame object containing timing information. |
global |
Whether onsets are in global time units (across all runs). |
blocksplit |
Whether to split onsets by blocks. |
... |
Additional arguments. |
A list of onset vectors, one per block (unless global=TRUE).
# Create an event term with mixed conditions across blocks conditions <- factor(c("A", "B", "A", "B", "A", "B")) onsets <- c(5, 15, 25, 105, 115, 125) # Events in blocks 1 and 2 blockids <- c(1, 1, 1, 2, 2, 2) term <- event_term( list(condition = conditions), onsets = onsets, blockids = blockids ) # Create sampling frame for two blocks of 50 TRs each sframe <- fmrihrf::sampling_frame(blocklens = c(50, 50), TR = 2) # Split onsets by condition (default behavior) onset_list <- split_onsets(term, sframe) names(onset_list) # Shows condition names onset_list$condition.A # Onsets for condition A # Split with global timing (onsets relative to start of experiment) global_onsets <- split_onsets(term, sframe, global = TRUE) # Split by both condition and block block_split <- split_onsets(term, sframe, blocksplit = TRUE)# Create an event term with mixed conditions across blocks conditions <- factor(c("A", "B", "A", "B", "A", "B")) onsets <- c(5, 15, 25, 105, 115, 125) # Events in blocks 1 and 2 blockids <- c(1, 1, 1, 2, 2, 2) term <- event_term( list(condition = conditions), onsets = onsets, blockids = blockids ) # Create sampling frame for two blocks of 50 TRs each sframe <- fmrihrf::sampling_frame(blocklens = c(50, 50), TR = 2) # Split onsets by condition (default behavior) onset_list <- split_onsets(term, sframe) names(onset_list) # Shows condition names onset_list$condition.A # Onsets for condition A # Split with global timing (onsets relative to start of experiment) global_onsets <- split_onsets(term, sframe, global = TRUE) # Split by both condition and block block_split <- split_onsets(term, sframe, blocksplit = TRUE)
Standardize a numeric vector by centering and scaling, handling NAs appropriately.
If the computed standard deviation is NA or zero, a small constant
(1e-6) is used instead to avoid division by zero.
The returned basis matrix has one column with this standardized name.
Standardized(x)Standardized(x)
x |
a numeric vector to standardize. Missing values are allowed and will be replaced with 0 after standardization. |
an instance of class Standardized extending ParametricBasis
# Standardize a numeric vector x_vals <- c(10, 20, 30, 40, 50) std_basis <- Standardized(x_vals) print(std_basis$y) print(std_basis$mean) print(std_basis$sd)# Standardize a numeric vector x_vals <- c(10, 20, 30, 40, 50) std_basis <- Standardized(x_vals) print(std_basis$y) print(std_basis$mean) print(std_basis$sd)
Subset a parametric basis regressor.
sub_basis(x, subset) ## S3 method for class 'Scale' sub_basis(x, subset) ## S3 method for class 'ScaleWithin' sub_basis(x, subset) ## S3 method for class 'RobustScale' sub_basis(x, subset)sub_basis(x, subset) ## S3 method for class 'Scale' sub_basis(x, subset) ## S3 method for class 'ScaleWithin' sub_basis(x, subset) ## S3 method for class 'RobustScale' sub_basis(x, subset)
x |
the object |
subset |
the subset (logical or integer indices) |
An object of the same class as x with subset applied.
# Create some sample data x_vals <- 1:10 rt_vals <- rnorm(10, 500, 50) # Create different basis objects poly_basis <- Poly(x_vals, degree = 3) scale_basis <- Scale(rt_vals) bspline_basis <- BSpline(x_vals, degree = 2) # Subset with integer indices poly_sub <- sub_basis(poly_basis, 1:5) scale_sub <- sub_basis(scale_basis, c(1, 3, 5, 7, 9)) # Subset with logical indices logical_idx <- x_vals <= 5 bspline_sub <- sub_basis(bspline_basis, logical_idx) # Check dimensions nrow(poly_basis$y) # 10 nrow(poly_sub$y) # 5 nrow(scale_sub$y) # 5 nrow(bspline_sub$y) # 5# Create some sample data x_vals <- 1:10 rt_vals <- rnorm(10, 500, 50) # Create different basis objects poly_basis <- Poly(x_vals, degree = 3) scale_basis <- Scale(rt_vals) bspline_basis <- BSpline(x_vals, degree = 2) # Subset with integer indices poly_sub <- sub_basis(poly_basis, 1:5) scale_sub <- sub_basis(scale_basis, c(1, 3, 5, 7, 9)) # Subset with logical indices logical_idx <- x_vals <= 5 bspline_sub <- sub_basis(bspline_basis, logical_idx) # Check dimensions nrow(poly_basis$y) # 10 nrow(poly_sub$y) # 5 nrow(scale_sub$y) # 5 nrow(bspline_sub$y) # 5
Extract term indices
term_indices(x, ...) ## Default S3 method: term_indices(x, ...)term_indices(x, ...) ## Default S3 method: term_indices(x, ...)
x |
The object. |
... |
Additional arguments. |
Integer vector or list mapping term(s) to column indices.
# Create a sampling frame and event model sf <- fmrihrf::sampling_frame(blocklens = c(100, 100), TR = 2) events <- data.frame( onset = c(10, 30, 50, 70), condition = c("A", "B", "A", "B"), block = c(1, 1, 2, 2) ) model <- event_model(onset ~ hrf(condition), events, ~ block, sf) # Get design matrix and extract term indices dm <- design_matrix(model) indices <- term_indices(dm) print(indices) # Access indices for specific term condition_indices <- indices[["condition"]] print(condition_indices)# Create a sampling frame and event model sf <- fmrihrf::sampling_frame(blocklens = c(100, 100), TR = 2) events <- data.frame( onset = c(10, 30, 50, 70), condition = c("A", "B", "A", "B"), block = c(1, 1, 2, 2) ) model <- event_model(onset ~ hrf(condition), events, ~ block, sf) # Get design matrix and extract term indices dm <- design_matrix(model) indices <- term_indices(dm) print(indices) # Access indices for specific term condition_indices <- indices[["condition"]] print(condition_indices)
Extract term matrices
term_matrices(x, ...)term_matrices(x, ...)
x |
The object. |
... |
Additional arguments. |
A list of matrices/tibbles, one per term.
# Create a simple experimental design with event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Extract term matrices - returns list with one matrix per term term_mats <- term_matrices(emod) names(term_mats) # Shows term names ncol(term_mats[[1]]) # Number of columns for first term # Create baseline model and extract its term matrices bmod <- baseline_model(sframe = sframe) baseline_mats <- term_matrices(bmod) names(baseline_mats) # Shows baseline term names# Create a simple experimental design with event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Extract term matrices - returns list with one matrix per term term_mats <- term_matrices(emod) names(term_mats) # Shows term names ncol(term_mats[[1]]) # Number of columns for first term # Create baseline model and extract its term matrices bmod <- baseline_model(sframe = sframe) baseline_mats <- term_matrices(bmod) names(baseline_mats) # Shows baseline term names
Extract term names
term_names(x, ...)term_names(x, ...)
x |
The object. |
... |
Additional arguments. |
Character vector of term names.
# Create sample event data des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) # Event model example emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) term_names(emod) # Returns "cond" # Baseline model example bmod <- baseline_model(basis = "poly", degree = 3, sframe = sframe) term_names(bmod) # Returns c("constant", "baseline_poly_3")# Create sample event data des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) # Event model example emod <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) term_names(emod) # Returns "cond" # Baseline model example bmod <- baseline_model(basis = "poly", degree = 3, sframe = sframe) term_names(bmod) # Returns c("constant", "baseline_poly_3")
Generate one regressor per trial (plus an optional grand-mean column)
by delegating everything to hrf().
trialwise( basis = "spmg1", lag = 0, nbasis = 1, add_sum = FALSE, label = "trial", durations = NULL, normalize = FALSE )trialwise( basis = "spmg1", lag = 0, nbasis = 1, add_sum = FALSE, label = "trial", durations = NULL, normalize = FALSE )
basis, lag, nbasis
|
Passed straight to |
add_sum |
If TRUE, append a column that is the average of all trialwise columns (useful as a conventional main effect). |
label |
Term label / prefix for the generated columns. |
durations |
Optional durations override (passed to |
normalize |
logical; if TRUE, each trialwise regressor column is peak-normalized
so that |
Use it only on the RHS of an event-model formula:
onset ~ trialwise(basis = "spmg1", add_sum = TRUE)
An hrfspec term to be used on the RHS of an event-model formula.
# Create example trial data for beta-series analysis trial_data <- data.frame( onset = c(2, 8, 14, 20, 26), run = c(1, 1, 1, 1, 1) ) # Create sampling frame (30 TRs, TR=2s) sframe <- fmrihrf::sampling_frame(blocklens = 30, TR = 2) # Basic trialwise model - creates one regressor per trial emod_trials <- event_model(onset ~ trialwise(), data = trial_data, block = ~run, sampling_frame = sframe) print(emod_trials) # Trialwise with different basis and grand mean emod_trials_mean <- event_model(onset ~ trialwise(basis = "spmg2", add_sum = TRUE), data = trial_data, block = ~run, sampling_frame = sframe) print(emod_trials_mean)# Create example trial data for beta-series analysis trial_data <- data.frame( onset = c(2, 8, 14, 20, 26), run = c(1, 1, 1, 1, 1) ) # Create sampling frame (30 TRs, TR=2s) sframe <- fmrihrf::sampling_frame(blocklens = 30, TR = 2) # Basic trialwise model - creates one regressor per trial emod_trials <- event_model(onset ~ trialwise(), data = trial_data, block = ~run, sampling_frame = sframe) print(emod_trials) # Trialwise with different basis and grand mean emod_trials_mean <- event_model(onset ~ trialwise(basis = "spmg2", add_sum = TRUE), data = trial_data, block = ~run, sampling_frame = sframe) print(emod_trials_mean)
Construct a contrast that sums to 1 and is used to define contrasts against the baseline.
unit_contrast(A, name, where = NULL)unit_contrast(A, name, where = NULL)
A |
A formula representing the contrast expression. |
name |
A character string specifying the name of the contrast. |
where |
An optional formula specifying the subset of conditions to apply the contrast to. |
A unit_contrast_spec object containing the contrast that sums to 1.
# Test main effect of Face against baseline con <- unit_contrast(~ Face, name="Main_face") # Test main effect within specific blocks con2 <- unit_contrast(~ Face, name="Face_early", where = ~ block <= 3)# Test main effect of Face against baseline con <- unit_contrast(~ Face, name="Main_face") # Test main effect within specific blocks con2 <- unit_contrast(~ Face, name="Face_early", where = ~ block <= 3)
Provides basic diagnostics for t- and F-contrasts once the design matrix
is available. You can either pass an event_model (to validate all attached
contrasts) or a design matrix plus custom weights.
validate_contrasts(x, weights = NULL, tol = 1e-08)validate_contrasts(x, weights = NULL, tol = 1e-08)
x |
An |
weights |
Optional contrast weights. May be a numeric vector (t-contrast),
a numeric matrix (F-contrast with columns as contrast vectors), or a named
list mapping names to vectors/matrices. If |
tol |
Numeric tolerance for zero checks. Default |
Checks include:
Estimability: whether each contrast column lies in the row space of X.
Sum-to-zero: whether the weights sum to ~0 (t-contrasts only).
Intercept orthogonality: whether weights on intercept-like columns are ~0.
Full-rank (F only): whether an F-contrast matrix has full column rank.
A data.frame with one row per validated contrast column and the
following columns: name, type ("t" or "F"), estimable,
sum_to_zero, orthogonal_to_intercept, full_rank (F only), and
nonzero_weights.
# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emodel <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Validate all attached contrasts on a model res <- validate_contrasts(emodel) # Validate a custom vector against a model v <- rep(0, ncol(design_matrix(emodel))) v[1] <- 1 v[2] <- -1 res2 <- validate_contrasts(emodel, weights = v)# Create a simple event model des <- data.frame( onset = c(0, 10, 20, 30), run = 1, cond = factor(c("A", "B", "A", "B")) ) sframe <- fmrihrf::sampling_frame(blocklens = 40, TR = 1) emodel <- event_model(onset ~ hrf(cond), data = des, block = ~run, sampling_frame = sframe) # Validate all attached contrasts on a model res <- validate_contrasts(emodel) # Validate a custom vector against a model v <- rep(0, ncol(design_matrix(emodel))) v[1] <- 1 v[2] <- -1 res2 <- validate_contrasts(emodel, weights = v)
Creates a generator function for use with the hrf_fun parameter in hrf().
The generator produces weighted impulse HRFs from columns containing lists
of sub-event times and weights.
weighted_hrf_gen( times_col = "sub_times", weights_col = "sub_weights", relative = FALSE, method = "constant", normalize = FALSE )weighted_hrf_gen( times_col = "sub_times", weights_col = "sub_weights", relative = FALSE, method = "constant", normalize = FALSE )
times_col |
Character; name of the column containing sub-event times (relative or absolute). |
weights_col |
Character; name of the column containing sub-event weights. |
relative |
Logical; if TRUE, times are relative to event onset; if FALSE, times are absolute and will be converted to relative by subtracting the onset. Default FALSE (absolute times). |
method |
Character; interpolation method for |
normalize |
Logical; whether to normalize the weighted HRF. Default FALSE. |
A function that takes an event data frame and returns a list of HRF objects.
boxcar_hrf_gen() for duration-based boxcar HRFs
trial_data <- data.frame( onset = c(0, 20), sub_times = I(list(c(0, 1, 2), c(0, 3, 6))), sub_weights = I(list(c(0.2, 0.5, 0.3), c(0.1, 0.6, 0.3))), run = 1 ) sf <- fmrihrf::sampling_frame(blocklens = 50, TR = 2) emod <- event_model( onset ~ hrf(onset, hrf_fun = weighted_hrf_gen("sub_times", "sub_weights", relative = TRUE)), data = trial_data, block = ~run, sampling_frame = sf ) print(emod)trial_data <- data.frame( onset = c(0, 20), sub_times = I(list(c(0, 1, 2), c(0, 3, 6))), sub_weights = I(list(c(0.2, 0.5, 0.3), c(0.1, 0.6, 0.3))), run = 1 ) sf <- fmrihrf::sampling_frame(blocklens = 50, TR = 2) emod <- event_model( onset ~ hrf(onset, hrf_fun = weighted_hrf_gen("sub_times", "sub_weights", relative = TRUE)), data = trial_data, block = ~run, sampling_frame = sf ) print(emod)