This function estimates univariable regression models and returns them in a publication-ready table. It can create regression models holding either a covariate or an outcome constant.
Usage
tbl_uvregression(data, ...)
# S3 method for class 'data.frame'
tbl_uvregression(
data,
y = NULL,
x = NULL,
method,
method.args = list(),
exponentiate = FALSE,
label = NULL,
include = everything(),
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
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,
...
)
# S3 method for class 'survey.design'
tbl_uvregression(
data,
y = NULL,
x = NULL,
method,
method.args = list(),
exponentiate = FALSE,
label = NULL,
include = everything(),
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
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
,survey.design
)
A data frame or a survey design object.- ...
Additional arguments passed to
broom.helpers::tidy_plus_plus()
.- y, x
(
expression
,string
)
Model outcome (e.g.y=recurrence
ory=Surv(time, recur)
) or covariate (e.g.x=trt
. All other column specified ininclude
will be regressed against the constanty
orx
. Specify one and only one ofy
orx
.- method
(
string
/function
)
Regression method or function, e.g. lm, glm, survival::coxph,survey::svyglm
, etc. Methods may be passed as functions (method=lm
) or as strings (method='lm'
).- method.args
(named
list
)
Named list of arguments passed tomethod
.- exponentiate
(scalar
logical
)
Logical indicating whether to exponentiate the coefficient estimates. Default isFALSE
.- label
(
formula-list-selector
)
Used to change variables labels, e.g.list(age = "Age", stage = "Path T Stage")
- include
(
tidy-select
)
Variables to include in output. Default iseverything()
.- tidy_fun
(
function
)
Tidier function for the model. Default is to usebroom::tidy()
. If an error occurs, the tidying of the model is attempted withparameters::model_parameters()
, if installed.- hide_n
(scalar
logical
)
Hide N column. Default isFALSE
- show_single_row
(
tidy-select
)
By default categorical variables are printed on multiple rows. If a variable is dichotomous (e.g. Yes/No) and you wish to print the regression coefficient on a single row, include the variable name(s) here.- conf.level
(scalar
real
)
Confidence level for confidence interval/credible interval. Defaults to0.95
.- estimate_fun
(
function
)
Function to round and format coefficient estimates. Default islabel_style_sigfig()
when the coefficients are not transformed, andlabel_style_ratio()
when the coefficients have been exponentiated.- pvalue_fun
(
function
)
Function to round and format p-values. Default islabel_style_pvalue()
.- formula
(
string
)
String of the model formula. Usesglue::glue()
syntax. Default is"{y} ~ {x}"
, where{y}
is the dependent variable, and{x}
represents a single covariate. For a random intercept model, the formula may beformula = "{y} ~ {x} + (1 | gear)"
.- add_estimate_to_reference_rows
(scalar
logical
)
Add a reference value. Default isFALSE
.- conf.int
(scalar
logical
)
Logical indicating whether or not to include a confidence interval in the output. Default isTRUE
.
x
and y
arguments
For models holding outcome constant, the function takes as arguments a data frame,
the type of regression model, and the outcome variable y=
. Each column in the
data frame is regressed on the specified outcome. The tbl_uvregression()
function arguments are similar to the tbl_regression()
arguments. Review the
tbl_uvregression vignette
for detailed examples.
You may alternatively hold a single covariate constant. For this, pass a data
frame, the type of regression model, and a single
covariate in the x=
argument. Each column of the data frame will serve as
the outcome in a univariate regression model. Take care using the x
argument
that each of the columns in the data frame are appropriate for the same type
of model, e.g. they are all continuous variables appropriate for lm, or
dichotomous variables appropriate for logistic regression with glm.
Methods
The default method for tbl_regression()
model summary uses broom::tidy(x)
to perform the initial tidying of the model object. There are, however,
a few models that use modifications.
"parsnip/workflows"
: If the model was prepared using parsnip/workflows, the original model fit is extracted and the originalx=
argument is replaced with the model fit. This will typically go unnoticed; however,if you've provided a custom tidier intidy_fun=
the tidier will be applied to the model fit object and not the parsnip/workflows object."survreg"
: The scale parameter is removed,broom::tidy(x) %>% dplyr::filter(term != "Log(scale)")
"multinom"
: This multinomial outcome is complex, with one line per covariate per outcome (less the reference group)"gam"
: Uses the internal tidiertidy_gam()
to print both parametric and smooth terms."lmerMod"
,"glmerMod"
,"glmmTMB"
,"glmmadmb"
,"stanreg"
,"brmsfit"
: These mixed effects models usebroom.mixed::tidy(x, effects = "fixed")
. Specifytidy_fun = broom.mixed::tidy
to print the random components.
See also
See tbl_regression vignette for detailed examples
Examples
# Example 1 ----------------------------------
tbl_uvregression(
trial,
method = glm,
y = response,
method.args = list(family = binomial),
exponentiate = TRUE,
include = c("age", "grade")
)
Characteristic
N
OR
95% CI
p-value
Abbreviations: CI = Confidence Interval, OR = Odds Ratio
# Example 2 ----------------------------------
# rounding pvalues to 2 decimal places
library(survival)
tbl_uvregression(
trial,
method = coxph,
y = Surv(ttdeath, death),
exponentiate = TRUE,
include = c("age", "grade", "response"),
pvalue_fun = label_style_pvalue(digits = 2)
)
Characteristic
N
HR
95% CI
p-value
Abbreviations: CI = Confidence Interval, HR = Hazard Ratio