Skip to contents

Fits linear models for each row (or column) of a tidymatrix. Can return statistics for a specific coefficient or overall model fit statistics.

Usage

compute_lm(
  .data,
  formula_rhs,
  coef = NULL,
  adjust = "fdr",
  add_to_data = FALSE,
  prefix = NULL,
  return_tibble = TRUE,
  ...
)

Arguments

.data

A tidymatrix object with rows or columns active

formula_rhs

Right-hand side of formula (e.g., ~ condition + batch + age)

coef

Character. Name of coefficient to extract. If NULL, returns overall model statistics

adjust

Character. Method for p-value adjustment. Default "fdr". Use "none" for no adjustment

add_to_data

Logical. If TRUE, adds results to row_data/col_data and returns modified tidymatrix. If FALSE (default), returns data.frame

prefix

Character. Optional prefix for result column names when add_to_data = TRUE

return_tibble

Logical. If TRUE (default), returns tibble. If FALSE, returns data.frame. Only applies when add_to_data = FALSE

...

Additional arguments passed to lm()

Value

A data.frame/tibble with model statistics. If coef specified: estimate, p.value, se, and p.adj. If coef = NULL: r.squared, f.statistic, p.value, and p.adj

Examples

mat <- matrix(rnorm(100), nrow = 10, ncol = 10)
col_data <- data.frame(
  sample = paste0("S", 1:10),
  condition = factor(rep(c("Control", "Treatment"), each = 5)),
  batch = factor(rep(1:2, 5)),
  age = rnorm(10, 50, 10)
)
row_data <- data.frame(gene = paste0("Gene", 1:10))
tm <- tidymatrix(mat, row_data, col_data)

# Extract specific coefficient
results <- tm |>
  activate(rows) |>
  compute_lm(
    formula_rhs = ~ condition + batch + age,
    coef = "conditionTreatment"
  )

# Overall model statistics
results <- tm |>
  activate(rows) |>
  compute_lm(formula_rhs = ~ condition + batch + age)