Skip to contents

Fits one regression model per variable and stacks the results in a single table. With y the outcome is fixed and each column of data in include is the predictor in turn; with x the predictor is fixed and each column is the outcome in turn. Each model is summarized with tbl_regression(), so every argument of that function applies.

Usage

tbl_uvregression(
  data,
  y = NULL,
  x = NULL,
  method,
  method.args = list(),
  exponentiate = FALSE,
  label = NULL,
  include = everything(),
  tidy_fun = NULL,
  hide_n = FALSE,
  show_single_row = NULL,
  conf.level = 0.95,
  estimate_fun = ifelse(exponentiate, label_style_ratio(), label_style_sigfig()),
  pvalue_fun = label_style_pvalue(digits = 1),
  formula = "{y} ~ {x}",
  add_estimate_to_reference_rows = FALSE,
  conf.int = TRUE,
  ...
)

Arguments

data

(data.frame)
A data frame.

y, x

(string or bare column name)
The outcome (y) or the predictor (x) used in every model. Exactly one of the two must be given. Either can be an expression that is not a column, e.g. y = survival::Surv(ttdeath, death), passed as a string.

method

(function or string)
The model function, e.g. lm, glm, "glm" or survival::coxph.

method.args

(named list)
Extra arguments for method, e.g. list(family = binomial).

exponentiate

(scalar logical)
Whether to exponentiate the coefficient estimates and their confidence limits. Default is FALSE.

label

(formula-list)
Variable labels, e.g. list(age ~ "Age, years"). The default for each variable is its label attribute in the model data, or the variable name when there is none.

include

(selector)
Columns of data to fit a model for. Columns named in x, y or formula are left out. Default is everything().

tidy_fun

(function)
A tidier with the signature of broom::tidy(), function(x, conf.int, conf.level, exponentiate), returning a data frame with a term column and estimate, std.error, statistic, p.value, conf.low and conf.high columns. Default is NULL, which uses the built-in tidier for lm, glm and coxph models and broom::tidy() for anything else.

hide_n

(scalar logical)
Whether to leave out the N column. Default is FALSE.

show_single_row

(selector)
Dichotomous variables to show on a single row (the row of the non-reference level, labelled with the variable label) instead of a label row, a reference row and a level row. Default is NULL.

conf.level

(scalar numeric)
Confidence level of the interval. Default is 0.95.

estimate_fun

(function)
Function that formats the estimate and the confidence limits. Default is label_style_sigfig() when exponentiate = FALSE and label_style_ratio() when it is TRUE.

pvalue_fun

(function)
Function that formats p-values. Default is label_style_pvalue(digits = 1).

formula

(string)
Template of the model formula. {y} stands for the outcome and {x} for the predictor; both must appear exactly once, on their side of the ~. Default is "{y} ~ {x}"; use e.g. "{y} ~ {x} + age" to adjust every model for age.

add_estimate_to_reference_rows

(scalar logical)
Whether to show the reference estimate (0, or 1 when exponentiated) on reference rows. Default is FALSE.

conf.int

(scalar logical)
Whether to compute and show the confidence interval. Default is TRUE.

...

Passed to tbl_regression().

Value

A tbl_uvregression object, an ltsummary object whose tbls element holds the tbl_regression table of each model. The table is built with tbl_stack()'s machinery, so a hidden tbl_id1 column of table_body records which model each row came from.

Why this is not an S3 generic

gtsummary's tbl_regression() is a generic with a default method. When both packages are attached, a method named tbl_regression.default in ltsummary would be found by gtsummary's generic as well, because R looks along the search path before the generic's own method registry. ltsummary therefore exposes one plain function; model classes without a built-in tidier are supported through tidy_fun.

Examples

# Example 1 ----------------------------------
# a logistic model for each predictor
trial |>
  tbl_uvregression(
    y = response,
    include = c(age, grade, trt),
    method = glm,
    method.args = list(family = binomial),
    exponentiate = TRUE
  )
# Example 2 ---------------------------------- # a linear model for each outcome, with the predictor on a single row trial |> tbl_uvregression( x = trt, include = c(age, marker), method = lm, show_single_row = trt )