generate_interaction_contrast: Fast factorial contrast generators

View source: R/gen_contrast.R

generate_interaction_contrastR Documentation

Fast factorial contrast generators

Description

Returns a matrix N_cells × N_contrastseach row is a design cell, columns are independent contrasts (difference‑coded for the factors you ask for, grand‑mean for the rest). Suitable for tcrossprod(dm, C) or lm.fit(design, y) followed by ⁠%*% coef⁠ in the usual way.

Usage

generate_interaction_contrast(des, factors)

generate_main_effect_contrast(des, factor)

Arguments

des

data.frame with one column per factor (must be factor)

factors

character vector: which factor(s) get difference coding. • generate_main_effect_contrast() takes a single factor name.
generate_interaction_contrast() takes ≥ 2 for an interaction (or 1 to reproduce a main‑effect matrix).

factor

Single factor name for the main effect.

Value

numeric matrix nrow = ∏ levels(f) , ncol = ∏ (Lᵢ − 1) for the chosen factors.

Examples

des <- expand.grid(Time = factor(1:4),
                   Cond = factor(c("face","scene")))

# Main effect of Time (4‑1 = 3 contrasts)
M <- generate_main_effect_contrast(des, "Time")

# Full Time×Cond interaction ( (4‑1)*(2‑1) = 3 contrasts )
I <- generate_interaction_contrast(des, c("Time","Cond"))
dim(I)   # 8 rows (cells) × 3 columns (contrasts)

bbuchsbaum/fmrireg documentation built on June 10, 2025, 8:18 p.m.