design_matrix: design_matrix

View source: R/all_generic.R

design_matrixR Documentation

design_matrix

Description

Extract or construct the design matrix from a model term or object. The design matrix contains the predictor variables used in the model, with one row per time point and one column per predictor. For event-related designs, the design matrix typically contains the convolved HRF responses. For baseline terms, it contains drift and nuisance regressors.

Usage

design_matrix(x, ...)

## S3 method for class 'event_model'
design_matrix(x, blockid = NULL, ...)

## S3 method for class 'convolved_term'
design_matrix(x, blockid = NULL, ...)

## S3 method for class 'matrix_term'
design_matrix(x, ...)

## S3 method for class 'fmri_model'
design_matrix(x, blockid = NULL, ...)

Arguments

x

The term or model object (typically an event_term, event_model, baseline_model, or fmri_model)

...

Additional arguments passed to methods. Common arguments include:

  • blockidNumeric vector specifying which blocks/runs to include

blockid

the block id to extract

Details

Construct a design matrix from a term or model.

Value

A tibble containing the design matrix, where:

  • Rows represent time points (scans)

  • Columns represent predictor variables

  • Column names indicate the condition or regressor

See Also

event_model(), baseline_model(), fmri_model()

Examples

# Create event data
event_data <- data.frame(
  condition = factor(c("face", "house", "face", "house")),
  rt = c(0.8, 1.2, 0.9, 1.1),
  onsets = c(1, 10, 20, 30),
  run = c(1, 1, 1, 1)
)

# Create sampling frame
sframe <- sampling_frame(blocklens = 50, TR = 2)

# Create event model with multiple terms
evmodel <- event_model(
  onsets ~ hrf(condition) + hrf(rt),
  data = event_data,
  block = ~run,
  sampling_frame = sframe
)

# Get full design matrix
dmat <- design_matrix(evmodel)

# Get design matrix for specific block
block1_dmat <- design_matrix(evmodel, blockid = 1)

# Create and get baseline design matrix
bmodel <- baseline_model(basis = "bs", degree = 3, sframe = sframe)
bdmat <- design_matrix(bmodel)

# Get combined design matrix from full model
fmodel <- fmri_model(evmodel, bmodel)
full_dmat <- design_matrix(fmodel)

bbuchsbaum/fmrireg documentation built on March 1, 2025, 11:20 a.m.