| Title: | Tools for Creating Publication-Ready Regression Tables |
|---|---|
| Description: | Simplifies regression modeling in R by integrating multiple modeling and summarization tools into a cohesive, user-friendly interface. Designed to be accessible for researchers, particularly those in Low- and Middle-Income Countries (LMIC). Built upon widely accepted statistical methods, including logistic regression (Hosmer et al. 2013, ISBN:9781118548429), log-binomial regression (Spiegelman and Hertzmark 2005 <doi:10.1093/aje/kwi188>), Poisson and robust Poisson regression (Zou 2004 <doi:10.1093/aje/kwh090>), negative binomial regression (Hilbe 2011, ISBN:9780521179515), and linear regression (Kutner et al. 2005, ISBN:9780071122214). Leverages multiple dependencies to ensure high-quality output and generate reproducible, publication-ready tables in alignment with best practices in epidemiology and applied statistics. |
| Authors: | Rubeshkumar Polani [aut, cre] (ORCID: <https://orcid.org/0000-0002-0418-7592>), Salin K Eliyas [aut] (ORCID: <https://orcid.org/0000-0002-8020-5860>), Manikandanesan Sakthivel [aut] (ORCID: <https://orcid.org/0000-0002-5438-3970>), Yuvaraj Krishnamoorthy [aut] (ORCID: <https://orcid.org/0000-0003-4688-510X>), Marie Gilbert Majella [aut] (ORCID: <https://orcid.org/0000-0003-4036-5162>) |
| Maintainer: | Rubeshkumar Polani <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.1.0 |
| Built: | 2026-07-22 17:58:56 UTC |
| Source: | https://github.com/thinkdenominator/gtregression |
Works for any object from this package, since they all carry class '"gtregression"'. Returns NULL (quietly) if the field is not present.
## S3 method for class 'gtregression' x$name## S3 method for class 'gtregression' x$name
x |
A |
name |
Field name to access. |
Common fields: - table, table_display, table_body - models, model_summaries, reg_check - approach, format (or engine), source - parts, spanners (for merged tables) - by, levels (for descriptive tables)
Computes Variance Inflation Factors (VIF) for fitted multivariable models
returned by multi_reg() or related functions.
check_collinearity(model, format = c("flextable", "gt", "tibble"))check_collinearity(model, format = c("flextable", "gt", "tibble"))
model |
A fitted model object returned by |
format |
Output format. One of |
If the car package is installed, check_collinearity()
uses car::vif(). Otherwise, it computes VIF/GVIF from the fitted model
matrix so that diagnostics remain available without an additional dependency.
For multivariable models, a tibble if a single fitted model is
present, or a named list of tibbles if multiple fitted models are present.
With format = "gt" or format = "flextable", leaf tibbles are
converted to formatted tables while preserving any list nesting. The tibble
contains:
VariableModel term.
VIFVariance inflation factor. For multi-degree-of-freedom
terms, this is the adjusted GVIF: GVIF^(1 / (2 * Df)).
InterpretationSimple interpretation based on common cut points: no collinearity, moderate, or high.
For univariate models, an error is raised indicating that VIF is not applicable.
Assesses model convergence and provides diagnostics for each exposure (in univariate mode) or for the full model (in multivariable mode), depending on the regression approach used.
check_convergence( data, exposures, outcome, approach = "logit", multivariate = FALSE, format = c("flextable", "gt", "tibble") )check_convergence( data, exposures, outcome, approach = "logit", multivariate = FALSE, format = c("flextable", "gt", "tibble") )
data |
A data frame containing the dataset. |
exposures |
A character vector of predictor variable names. Quoted names
are recommended in scripts, and bare names are also accepted.
If |
outcome |
A character string specifying the outcome variable. Quoted and bare names are accepted. |
approach |
A character string specifying the regression approach.
One of:
|
multivariate |
Logical. If |
format |
Output format. One of |
For robpoisson, predicted probabilities (fitted values) may exceed 1,
which is acceptable when estimating risk ratios but should not be interpreted
as actual probabilities.
This function is useful for identifying convergence issues, especially for
"logbinomial" models, which often fail to converge.
A data frame, gt_tbl, or flextable summarizing
convergence diagnostics, including:
ExposureName of the exposure variable.
ModelThe regression approach used.
ConvergedTRUE if the model converged successfully;
FALSE otherwise.
Max.prob.Maximum predicted probability or fitted value in the dataset.
[identify_confounder()], [interaction_models()]
if (requireNamespace("gtregression", quietly = TRUE)) { data(data_PimaIndiansDiabetes, package = "gtregression") check_convergence( data = data_PimaIndiansDiabetes, exposures = c("age", "mass"), outcome = "diabetes", approach = "logit" ) check_convergence( data = data_PimaIndiansDiabetes, exposures = c("age", "mass"), outcome = "diabetes", approach = "logit", multivariate = TRUE ) }if (requireNamespace("gtregression", quietly = TRUE)) { data(data_PimaIndiansDiabetes, package = "gtregression") check_convergence( data = data_PimaIndiansDiabetes, exposures = c("age", "mass"), outcome = "diabetes", approach = "logit" ) check_convergence( data = data_PimaIndiansDiabetes, exposures = c("age", "mass"), outcome = "diabetes", approach = "logit", multivariate = TRUE ) }
A dataset from the MASS package containing risk factors associated with low birth weight (LBW) in newborns. Originally collected at Baystate Medical Center, Springfield, Massachusetts, USA.
data_birthwtdata_birthwt
A data frame with 189 observations and 10 variables:
Indicator for birth weight < 2500g (binary):
0 = normal, 1 = low birth weight
Mother's age in years (numeric)
Mother's weight in pounds at last menstrual period (numeric)
Mother's race (factor): 1 = White, 2 = Black,
3 = Other
Smoking status during pregnancy (binary): 0 = No,
1 = Yes
Number of previous premature labors (integer)
History of hypertension (binary): 0 = No, 1 = Yes
Presence of uterine irritability (binary): 0 = No,
1 = Yes
no of physician visits during the 1st trimester (integer, 0–6)
Birth weight in grams (numeric)
The outcome variable is binary ('low'): birth weight < 2500g (yes = 1) or not (no = 0).
Hosmer, D.W., Lemeshow, S. (1989). *Applied Logistic Regression.* New York: Wiley. Also available in MASS and described in detail in its documentation.
RCT on the effect of a drug on the seizures in patients with epilepsy. Contains repeated measures data with treatment groups, baseline seizure counts, and follow-up counts.
data_epilepsydata_epilepsy
A data frame with 236 observations and 9 variables:
Number of seizures in a 2-week period (count)
Treatment group (factor): placebo or progabide
Seizure count during baseline period (numeric)
Age of patient (numeric)
Indicator for 4th visit (binary)
Patient ID (factor)
Follow-up period number (integer)
Log of baseline seizures (numeric)
Log of age (numeric)
MASS package. Original data from Thall and Vail (1990)
This dataset contains observations on the number of days absent from school for children in rural Australia, along with student characteristics. It's commonly used to demonstrate count models such as Poisson and Negative Binomial regression.
data_gt_quindata_gt_quin
A data frame with 146 observations and 5 variables:
Ethnicity ("A" = Aboriginal,
"N" = Non-Aboriginal)
Sex ("F" or "M")
Age group ("F0", "F1", "F2", "F3")
Learner status ("AL" = average learner,
"SL" = slow learner)
Number of days absent from school (count outcome)
MASS package. See also Venables and Ripley (2002), *Modern Applied Statistics with S*.
investigating the relationship between infertility and abortions.
data_infertilitydata_infertility
A data frame with 248 observations and 8 variables:
Education level (0 = 0–5 years, 1 = 6–11 years, 2 = 12+ years)
Age in years
Number of prior pregnancies
Number of induced abortions
Infertility case status (1 = case, 0 = control)
Number of spontaneous abortions
Matched set ID
Pooled stratum ID used for conditional regression
https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/infert.html
Survival data from a clinical trial of lung cancer patients conducted by the Veteran's Administration.
data_lungcancerdata_lungcancer
A data frame with 137 observations and 8 variables:
Treatment group (1 = standard, 2 = test)
Cell type (squamous, smallcell, adeno, large)
Survival time (in days)
Censoring status (1 = died, 0 = censored)
Karnofsky performance score (higher = better)
Months from diagnosis to randomization
Age in years
Prior therapy (0 = no, 10 = yes)
https://CRAN.R-project.org/package=survival
Kalbfleisch JD and Prentice RL (1980). The Statistical Analysis of Failure Time Data.
A cleaned version of the original Pima Indians Diabetes dataset from the 'mlbench' package. Useful for demonstrating regression approaches for binary outcomes.
data_PimaIndiansDiabetesdata_PimaIndiansDiabetes
A data frame with 768 observations and 9 variables:
Number of times pregnant
Plasma glucose concentration (glucose tolerance test)
Diastolic blood pressure (mm Hg)
Triceps skin fold thickness (mm)
2-Hour serum insulin (mu U/ml)
Body mass index (BMI)
Diabetes pedigree function
Age in years
Factor indicating diabetes status (pos/neg)
Publication-ready summary of categorical and continuous variables (optionally stratified). Mimics the OG gtsummary style: * column headers include N, e.g. "Overall, N=200" * categorical rows shown as n (%) * continuous rows default to Median (IQR) (footnote reflects summary)
descriptive_table( data, exposures, by = NULL, percent = c("column", "row"), digits = 1, show_missing = c("ifany", "no"), show_dichotomous = c("all_levels", "single_row"), show_overall = c("no", "first", "last"), statistic = NULL, value = NULL, format = c("flextable", "gt"), theme = c("minimal") )descriptive_table( data, exposures, by = NULL, percent = c("column", "row"), digits = 1, show_missing = c("ifany", "no"), show_dichotomous = c("all_levels", "single_row"), show_overall = c("no", "first", "last"), statistic = NULL, value = NULL, format = c("flextable", "gt"), theme = c("minimal") )
data |
data.frame |
exposures |
Character vector of variables to summarise. Quoted names are recommended in scripts, and bare names are also accepted. |
by |
Optional single grouping variable. Quoted and bare names are accepted. |
percent |
"column" (default) or "row"; aliases like "col"/"rows" accepted |
digits |
integer; decimals for % and continuous stats (default 1) |
show_missing |
"ifany" (default) or "no" |
show_dichotomous |
"all_levels" (default) or "single_row" |
show_overall |
"no" (default), "first", or "last" |
statistic |
optional named vector per continuous var: values in "mean","median","mode","count" (default is "median" = Median (IQR)) |
value |
optional named list for single-row binaries (e.g., list(sex="Female")); formula entries like list(sex ~ "Female") are also accepted |
format |
"flextable" (default) or "gt" |
theme |
preset or primitives |
A list with class c("gtregression", "descriptive_table", ...)
containing:
tableA gt_tbl or flextable.
table_displayDisplay-ready data.
table_bodyLong audit data with variable, level, and type.
Additional metadata fields.
Returns a tidy summary of each variable's structure, missingness, uniqueness, and suitability for use in regression models.
dissect(data, verbose = FALSE, format = c("flextable", "gt", "tibble"))dissect(data, verbose = FALSE, format = c("flextable", "gt", "tibble"))
data |
A data frame. |
verbose |
Logical; if |
format |
Output format. One of |
A tibble, gt_tbl, or flextable, depending on
format. The tibble has columns: Variable, Type, Missing (
Levels, Compatibility, and Hint.
dissect(data_birthwt) dissect(data_birthwt, format = "gt") # Print interpretation notes for beginners dissect(data_birthwt, verbose = TRUE)dissect(data_birthwt) dissect(data_birthwt, format = "gt") # Print interpretation notes for beginners dissect(data_birthwt, verbose = TRUE)
Creates the tabular input used by forest_reg() from uni_reg(),
multi_reg(), and optionally descriptive_table() objects. This
function is useful when users want to inspect, edit, or reuse the exact data
that will be passed to the forest plot.
forest_df(uni, multi = NULL, desc = NULL, digits = 2)forest_df(uni, multi = NULL, desc = NULL, digits = 2)
uni |
A |
multi |
Optional |
desc |
Optional descriptive table object from |
digits |
Number of digits used when formatting confidence intervals. |
A data frame with display columns, formatted effect-size columns,
standard-error columns, and plotting attributes used by forest_reg()
to draw confidence intervals.
birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) uni_or <- uni_reg( birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), approach = "logit" ) multi_or <- multi_reg( birthwt_data, outcome = "low", exposures = c("smoke", "ht"), adjust_for = c("age", "lwt"), approach = "logit" ) forest_data <- forest_df(uni_or, multi_or) head(forest_data)birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) uni_or <- uni_reg( birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), approach = "logit" ) multi_or <- multi_reg( birthwt_data, outcome = "low", exposures = c("smoke", "ht"), adjust_for = c("age", "lwt"), approach = "logit" ) forest_data <- forest_df(uni_or, multi_or) head(forest_data)
Wrapper around forestploter::forest() that works directly with
forest_df() output or with gtregression regression objects.
It can show descriptive columns and one or two model effect columns in a
table-style forest plot.
forest_reg( df = NULL, uni = NULL, multi = NULL, desc = NULL, theme = NULL, ci_col_width = 0.25, side = c("right", "left"), quiet = TRUE, effects = NULL, ticks_at = NULL, ticks_digits = NULL, ... )forest_reg( df = NULL, uni = NULL, multi = NULL, desc = NULL, theme = NULL, ci_col_width = 0.25, side = c("right", "left"), quiet = TRUE, effects = NULL, ticks_at = NULL, ticks_digits = NULL, ... )
df |
Output of |
uni, multi, desc
|
Optional |
theme |
Optional |
ci_col_width |
Numeric or length-2 numeric. Relative width of the CI column(s).
A vector like |
side |
Character. For each effect, position of the plot relative to the effect-size text:
|
quiet |
Logical. Suppress forestploter warnings. Default = 'TRUE'. |
effects |
Optional effect labels passed to |
ticks_at |
Optional numeric vector, or length-2 list for two effect
columns, specifying x-axis tick positions. If |
ticks_digits |
Optional number of digits for x-axis tick labels. |
... |
Passed to |
A gtregression_forest object with elements:
plotThe forest plot object.
dataThe plotting data sent to forestploter::forest().
input_dataThe original forest_df() data, including
standard-error helper columns.
metaModel metadata, including reference line and x-axis transformation.
birthwt_data <- data_birthwt |> transform( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) uni_or <- uni_reg( birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), approach = "logit" ) multi_or <- multi_reg( birthwt_data, outcome = "low", exposures = c("smoke", "ht"), adjust_for = c("age", "lwt"), approach = "logit" ) forest_reg(uni = uni_or, multi = multi_or)birthwt_data <- data_birthwt |> transform( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) uni_or <- uni_reg( birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), approach = "logit" ) multi_or <- multi_reg( birthwt_data, outcome = "low", exposures = c("smoke", "ht"), adjust_for = c("age", "lwt"), approach = "logit" ) forest_reg(uni = uni_or, multi = multi_or)
Assesses whether one or more candidate variables act as confounders or effect modifiers for one or more exposures.
identify_confounder( data, outcome, exposure, potential_confounder, approach = "logit", method = "change", threshold = 10, emm_threshold = 10, emm_test = c("interaction", "both", "estimate"), interaction_alpha = 0.05, format = c("flextable", "gt"), theme = c("minimal") )identify_confounder( data, outcome, exposure, potential_confounder, approach = "logit", method = "change", threshold = 10, emm_threshold = 10, emm_test = c("interaction", "both", "estimate"), interaction_alpha = 0.05, format = c("flextable", "gt"), theme = c("minimal") )
data |
A data frame. |
outcome |
Outcome variable name. Quoted and bare names are accepted. |
exposure |
Exposure variable name(s). Can be a character scalar or vector. Quoted names are recommended in scripts, and bare names are also accepted. |
potential_confounder |
Candidate confounder/effect-modifier variable name(s). Can be a character scalar or vector. Quoted names are recommended in scripts, and bare names are also accepted. |
approach |
Regression approach. One of |
method |
Confounding assessment method. One of |
threshold |
Percent change threshold for confounding assessment. |
emm_threshold |
Threshold for relative spread in stratum-specific estimates when using estimate-based effect-modification screening. |
emm_test |
One of |
interaction_alpha |
Alpha threshold for interaction p-values. |
format |
Output table format. One of |
theme |
Table theme preset or primitives. |
The function first assesses possible effect modification using stratum-specific estimates and/or an interaction test. If no important effect modification is detected, it then assesses confounding using the selected method.
This is a screening aid for viewing and organising results. Confounding and effect modification should be interpreted using subject-matter knowledge, study design, and causal diagrams such as DAGs. Automated change-in-estimate and interaction checks should not be used as the sole basis for model adjustment.
Use this function when you want to screen one or more candidate variables and
organise crude, adjusted, Mantel-Haenszel, and effect-modification signals in
one place. For a focused comparison of models with and without a planned
exposure-by-modifier interaction term, use interaction_models().
If a single exposure-candidate pair is supplied, returns a detailed list.
If multiple combinations are supplied, returns a list with:
A tibble with one row per exposure-candidate combination.
A named list of detailed results for each combination.
interaction_models() for focused model comparison of
a planned interaction term.
Fits two models, one with and one without an interaction term between an exposure and a potential effect modifier. The models are compared using a likelihood ratio test or Wald test to assess statistical evidence of interaction.
interaction_models( data, outcome, exposure, covariates = NULL, effect_modifier, approach = "logit", test = c("LRT", "Wald"), alpha = 0.05, verbose = FALSE, format = c("flextable", "gt", "tibble") )interaction_models( data, outcome, exposure, covariates = NULL, effect_modifier, approach = "logit", test = c("LRT", "Wald"), alpha = 0.05, verbose = FALSE, format = c("flextable", "gt", "tibble") )
data |
A data frame containing all required variables. |
outcome |
Outcome variable name. Quoted and bare names are accepted. |
exposure |
Main exposure variable name. Quoted and bare names are accepted. |
covariates |
Optional character vector of additional covariates. Quoted names are recommended in scripts, and bare names are also accepted. |
effect_modifier |
Variable name for the potential effect modifier. Quoted and bare names are accepted. |
approach |
Regression approach. One of |
test |
Statistical test for model comparison. One of |
alpha |
Significance threshold used to classify the interaction result. |
verbose |
Logical; if |
format |
Output format for the viewing table. One of
|
Use this function when the interaction is planned or clinically/causally
motivated and you want a focused model comparison. Mantel-Haenszel estimation
is not used here because this function tests an explicit interaction term in
a regression model. For broader screening of candidate confounders or effect
modifiers, including Mantel-Haenszel-supported checks when appropriate, use
identify_confounder().
A list with model objects, formulas, p-value, decision, and a
one-row summary tibble. When format is "gt" or
"flextable", the list also includes table.
identify_confounder() for broader confounding and
effect-modification screening.
Merge gtregression tables and preserve structure and notes
merge_tables(..., spanners = NULL, theme = "minimal")merge_tables(..., spanners = NULL, theme = "minimal")
... |
Two or more |
spanners |
Character vector of spanner labels, one per table.
If |
theme |
Merge theme preset or vector of primitives. |
A merged table object of class
c("gtregression", "merged_table", ...).
birthwt_data <- data_birthwt |> dplyr::mutate( race = factor(race, levels = c(1, 2, 3), labels = c("White", "Black", "Other")), smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) uni_tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), approach = "logit" ) multi_tbl <- multi_reg( data = birthwt_data, outcome = "low", exposures = c("smoke", "ht"), adjust_for = c("age", "lwt"), approach = "logit" ) merge_tables( uni_tbl, multi_tbl, spanners = c("Univariable", "Adjusted") )birthwt_data <- data_birthwt |> dplyr::mutate( race = factor(race, levels = c(1, 2, 3), labels = c("White", "Black", "Other")), smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) uni_tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), approach = "logit" ) multi_tbl <- multi_reg( data = birthwt_data, outcome = "low", exposures = c("smoke", "ht"), adjust_for = c("age", "lwt"), approach = "logit" ) merge_tables( uni_tbl, multi_tbl, spanners = c("Univariable", "Adjusted") )
Works with objects created by this package (class "gtregression"):
uni_reg(), multi_reg(), descriptive_table(),
and merge_tables().
No gtsummary dependency or fallback.
modify_table( gt_table, variable_labels = NULL, level_labels = NULL, header_labels = NULL, caption = NULL, bold_labels = FALSE, bold_levels = FALSE, remove_N = FALSE, remove_N_obs = FALSE, remove_abbreviations = FALSE, caveat = NULL )modify_table( gt_table, variable_labels = NULL, level_labels = NULL, header_labels = NULL, caption = NULL, bold_labels = FALSE, bold_levels = FALSE, remove_N = FALSE, remove_N_obs = FALSE, remove_abbreviations = FALSE, caveat = NULL )
gt_table |
Table object produced by this package (must contain
|
variable_labels |
Named character vector, for example
|
level_labels |
Named list for factor levels:
|
header_labels |
Named character vector to rename visible headers, e.g.
|
caption |
Optional caption/title. |
bold_labels |
Logical; bold variable (header) rows in the body. |
bold_levels |
Logical; bold factor level rows in the body. |
remove_N |
Logical; if |
remove_N_obs |
Logical; if |
remove_abbreviations |
Logical; if |
caveat |
Optional extra footnote. |
The modified table object (same class as input).
birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "smoke", "ht"), approach = "logit" ) modify_table( tbl, variable_labels = c(age = "Maternal age", smoke = "Smoking"), level_labels = list(smoke = c(Yes = "Smoker")), header_labels = c(estimate = "Crude OR", p.value = "P"), caption = "Univariable regression for low birth weight" )$tablebirthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "smoke", "ht"), approach = "logit" ) modify_table( tbl, variable_labels = c(age = "Maternal age", smoke = "Smoking"), level_labels = list(smoke = c(Yes = "Smoker")), header_labels = c(estimate = "Crude OR", p.value = "P"), caption = "Univariable regression for low birth weight" )$table
Create a publication-ready multivariable regression table using either gt or flextable, without a gtsummary dependency.
multi_reg( data, outcome, exposures, adjust_for = NULL, interaction = NULL, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )multi_reg( data, outcome, exposures, adjust_for = NULL, interaction = NULL, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )
data |
A |
outcome |
Character scalar; name of the outcome variable. Quoted and bare names are accepted. |
exposures |
Character vector; exposure variable(s) to report.
Quoted names are recommended in scripts, and bare names are also accepted.
If |
adjust_for |
Optional character vector of adjustment variables. Quoted
and bare names are accepted. Must not overlap with |
interaction |
Optional character scalar specifying one interaction term
using standard formula syntax, e.g. |
approach |
Character scalar specifying the regression approach.
One of |
format |
Output table format; one of |
theme |
Table styling preset (e.g. |
In default mode (adjust_for = NULL), all exposures are included in a
single multivariable model. In adjusted mode, one model is fitted per exposure,
adjusting for the variables specified in adjust_for.
Interaction terms specified via interaction are included in the model
using standard formula expansion (e.g. bmi*sex). Interaction effects are
displayed as additional rows beneath the corresponding exposure.
A list of class c("gtregression","multi_reg", ...) with elements:
A flextable (if format="flextable") or
gt_tbl (if format="gt").
A data frame of adjusted estimates and confidence intervals for each exposure and level.
A data frame used for rendering the final table, including header and level rows.
A list of fitted model(s).
summary() output for the fitted model(s).
Regression diagnostics for linear models; otherwise a message.
The regression approach used.
The output format used.
Function identifier ("multi_reg").
Whether one adjusted model per exposure was fitted.
Adjustment variables supplied by the user, if any.
Exposure variables supplied by the user.
Interaction term supplied by the user, if any.
Creates a forest plot from a fitted gtregression object produced by
functions such as uni_reg() or multi_reg().
plot_reg( tbl, title = NULL, caption = NULL, ref_line = NULL, order_y = NULL, log_x = FALSE, xlim = NULL, breaks = NULL, point_color = "#1F77B4", errorbar_color = "#4C4C4C", base_size = 14, show_ref = TRUE, sig_color = NULL, sig_errorbar_color = NULL, alpha = 0.05, show_adjustment_note = TRUE )plot_reg( tbl, title = NULL, caption = NULL, ref_line = NULL, order_y = NULL, log_x = FALSE, xlim = NULL, breaks = NULL, point_color = "#1F77B4", errorbar_color = "#4C4C4C", base_size = 14, show_ref = TRUE, sig_color = NULL, sig_errorbar_color = NULL, alpha = 0.05, show_adjustment_note = TRUE )
tbl |
A fitted |
title |
Optional plot title. |
caption |
Optional plot caption. If |
ref_line |
Optional numeric value for the reference line. Defaults to 0 for linear models and 1 otherwise. |
order_y |
Optional character vector specifying exposure order. |
log_x |
Logical; if |
xlim |
Optional numeric vector of length 2 specifying x-axis limits. |
breaks |
Optional numeric vector of x-axis tick breaks. |
point_color |
Fill color for points. |
errorbar_color |
Color for error bars. |
base_size |
Base font size. |
show_ref |
Logical; if |
sig_color |
Optional fill color for significant points. |
sig_errorbar_color |
Optional color for significant error bars. |
alpha |
Significance level for linear models when |
show_adjustment_note |
Logical; if |
A ggplot2 object.
Creates two aligned forest plots from gtregression objects returned by
uni_reg() and multi_reg().
plot_reg_combine( tbl_uni, tbl_multi, title_uni = NULL, title_multi = NULL, caption = NULL, ref_line = NULL, order_y = NULL, log_x = FALSE, point_color = "#1F77B4", errorbar_color = "#4C4C4C", base_size = 14, show_ref = TRUE, sig_color = NULL, sig_errorbar_color = NULL, xlim_uni = NULL, breaks_uni = NULL, xlim_multi = NULL, breaks_multi = NULL, alpha = 0.05, show_adjustment_note = TRUE )plot_reg_combine( tbl_uni, tbl_multi, title_uni = NULL, title_multi = NULL, caption = NULL, ref_line = NULL, order_y = NULL, log_x = FALSE, point_color = "#1F77B4", errorbar_color = "#4C4C4C", base_size = 14, show_ref = TRUE, sig_color = NULL, sig_errorbar_color = NULL, xlim_uni = NULL, breaks_uni = NULL, xlim_multi = NULL, breaks_multi = NULL, alpha = 0.05, show_adjustment_note = TRUE )
tbl_uni |
A univariate |
tbl_multi |
A multivariable |
title_uni, title_multi
|
Optional panel titles. |
caption |
Optional combined plot caption. If |
ref_line |
Optional numeric reference line. If |
order_y |
Optional character vector to customize exposure ordering. |
log_x |
Logical; if |
point_color, errorbar_color
|
Base colors for non-significant rows. |
base_size |
Base font size for |
show_ref |
Logical; if |
sig_color, sig_errorbar_color
|
Optional colors for significant rows.
If |
xlim_uni, breaks_uni
|
Optional x-axis limits and breaks for the univariate panel. |
xlim_multi, breaks_multi
|
Optional x-axis limits and breaks for the multivariable panel. |
alpha |
Significance level for linear models when |
show_adjustment_note |
Logical; if |
A patchwork object with two ggplot2 panels.
Prints the rendered table for any object produced by this package
(objects that include class "gtregression"), regardless of subtype
(uni_reg, multi_reg, stratified_*, merged_table,
descriptive_table, ...). If no rendered table is found, a compact
structure of the object (or its display data) is shown.
## S3 method for class 'gtregression' print(x, ...)## S3 method for class 'gtregression' print(x, ...)
x |
An object with class |
... |
Ignored. Present for compatibility with the generic. |
Saves a collection of gtregression tables, merged tables,
gt_tbl/flextable objects, and ggplot2 plots into a
single Word document.
save_docx( tables = NULL, plots = NULL, filename = "report.docx", titles = NULL, plot_width = 6, plot_height = 5 )save_docx( tables = NULL, plots = NULL, filename = "report.docx", titles = NULL, plot_width = 6, plot_height = 5 )
tables |
A list of tables. Each element may be a |
plots |
A list of |
filename |
File name for the output, with or without |
titles |
Optional character vector of titles for tables and plots in the order they are added. |
plot_width |
Width of inserted plots in inches. |
plot_height |
Height of inserted plots in inches. |
Saves the Word document to disk. Invisibly returns the normalized file path.
birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "smoke"), approach = "logit", format = "flextable" ) save_docx(tables = tbl, filename = tempfile("report"))birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "smoke"), approach = "logit", format = "flextable" ) save_docx(tables = tbl, filename = tempfile("report"))
Saves a ggplot2 plot to a file in PNG, PDF, or JPG format.
save_plot( plot, filename = "plot", format = c("png", "pdf", "jpg"), width = 8, height = 6, dpi = 300 )save_plot( plot, filename = "plot", format = c("png", "pdf", "jpg"), width = 8, height = 6, dpi = 300 )
plot |
A |
filename |
Name of the file to save, with or without extension. If no
directory is supplied, the file is saved in |
format |
Output format. One of |
width |
Width of the saved plot in inches. |
height |
Height of the saved plot in inches. |
dpi |
Resolution of the plot in dots per inch. |
Saves the file to disk. Invisibly returns the normalized file path.
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point() save_plot(p, filename = tempfile("plot"), format = "png")p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point() save_plot(p, filename = tempfile("plot"), format = "png")
Saves a gtregression table, merged table, gt_tbl, or
flextable as a Word, PDF, or HTML file.
save_table(tbl, filename = "table", format = c("docx", "pdf", "html"))save_table(tbl, filename = "table", format = c("docx", "pdf", "html"))
tbl |
A |
filename |
File name for the output. Extension is optional. If no
directory is supplied, the file is saved in |
format |
Output format. One of |
Saves the file to disk. Invisibly returns the normalized file path.
birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "smoke"), approach = "logit" ) save_table(tbl, filename = tempfile("table"), format = "html")birthwt_data <- data_birthwt |> dplyr::mutate( smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) tbl <- uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "smoke"), approach = "logit" ) save_table(tbl, filename = tempfile("table"), format = "html")
Performs stepwise model selection using forward, backward, or both directions across different regression approaches. The function returns a summary table with evaluation metrics and the best model based on the lowest AIC.
select_models( data, outcome, exposures, approach = "logit", direction = "forward", format = c("flextable", "gt", "tibble") )select_models( data, outcome, exposures, approach = "logit", direction = "forward", format = c("flextable", "gt", "tibble") )
data |
A data frame containing the outcome and predictor variables. |
outcome |
A single character string indicating the outcome variable. Quoted and bare names are accepted. |
exposures |
Character vector of predictor variables to consider. Quoted names are recommended in scripts, and bare names are also accepted. |
approach |
Regression method. One of:
|
direction |
Stepwise selection direction. One of:
|
format |
Output format for the viewing table. One of
|
A list with the following components:
results_table: A tibble summarising each accepted step's
model metrics (AIC, BIC, deviance, log-likelihood, and adjusted R-squared
for linear models).
best_model: The best-fitting model object based on lowest AIC.
all_models: A named list of the accepted stepwise models.
direction: Stepwise selection direction used.
table: A formatted gt_tbl or flextable when
format is "gt" or "flextable".
data <- data_birthwt stepwise <- select_models( data = data, outcome = "bwt", exposures = c("age", "lwt", "smoke"), approach = "linear", direction = "forward" ) stepwise$results_table stepwise$best_modeldata <- data_birthwt stepwise <- select_models( data = data, outcome = "bwt", exposures = c("age", "lwt", "smoke"), approach = "linear", direction = "forward" ) stepwise$results_table stepwise$best_model
Fits multivariable regression models within each stratum and returns a unified wide table with one "Characteristic" column and, under bold spanners for each stratum, two columns: "Adjusted <effect>" and "p-value".
stratified_multi_reg( data, outcome, exposures, stratifier, adjust_for = NULL, interaction = NULL, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )stratified_multi_reg( data, outcome, exposures, stratifier, adjust_for = NULL, interaction = NULL, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )
data |
A data frame containing the variables. |
outcome |
Character scalar; name of the outcome variable. Quoted and bare names are accepted. |
exposures |
Character vector of exposure variables to report. Quoted names are recommended in scripts, and bare names are also accepted. |
stratifier |
Character scalar; name of the stratifying variable. Quoted and bare names are accepted. |
adjust_for |
Optional character vector of adjustment variables.
Quoted and bare names are accepted. This argument works the same way as in
|
interaction |
Optional character scalar specifying one interaction term
using standard formula syntax, e.g. |
approach |
One of |
format |
One of |
theme |
Preset name (e.g. |
If adjust_for = NULL, all exposures are included in one
multivariable model within each stratum. If adjust_for is supplied,
one adjusted model is fitted per exposure within each stratum.
A list of class c("gtregression","stratified_multi_reg", ...) with:
tableA flextable (format = "flextable") or
gt_tbl (format = "gt").
table_displayWide data frame used to build the table.
per_stratumNamed list of per-stratum regression results.
modelsNamed list of fitted models by stratum.
model_summariesNamed list of model summaries by stratum.
reg_checkNamed list of diagnostics by stratum.
by, levels, approach, format, source
Metadata fields.
birthwt_data <- data_birthwt |> transform( race = factor(race, levels = c(1, 2, 3), labels = c("White", "Black", "Other")), smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), ui = factor(ui, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) stratified_multi <- stratified_multi_reg( data = birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), stratifier = "race", approach = "logit" ) stratified_adjusted <- stratified_multi_reg( data = birthwt_data, outcome = "low", exposures = c("smoke", "ht", "ui"), stratifier = "race", adjust_for = c("age", "lwt"), approach = "logit" )birthwt_data <- data_birthwt |> transform( race = factor(race, levels = c(1, 2, 3), labels = c("White", "Black", "Other")), smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")), ui = factor(ui, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) stratified_multi <- stratified_multi_reg( data = birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke", "ht"), stratifier = "race", approach = "logit" ) stratified_adjusted <- stratified_multi_reg( data = birthwt_data, outcome = "low", exposures = c("smoke", "ht", "ui"), stratifier = "race", adjust_for = c("age", "lwt"), approach = "logit" )
Fit univariable regression models within each level of a stratifier and combine the results into a publication-ready stratified table rendered with gt or flextable.
stratified_uni_reg( data, outcome, exposures, stratifier, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )stratified_uni_reg( data, outcome, exposures, stratifier, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )
data |
A data frame containing the variables. |
outcome |
Character scalar; name of the outcome variable. Quoted and bare names are accepted. |
exposures |
Character vector of exposure variables to model. Quoted names are recommended in scripts, and bare names are also accepted. |
stratifier |
Character scalar; name of the stratifying variable. Quoted and bare names are accepted. |
approach |
Modeling approach. One of |
format |
Output table format; one of |
theme |
Table styling preset or theme primitives. |
A list of class
c("gtregression", "stratified_uni_reg", ...) with elements:
tableA rendered gt_tbl or flextable.
table_displayDisplay-ready wide stratified results.
per_stratumList of complete uni_reg() results by
stratum.
modelsList of fitted model objects by stratum.
model_summariesList of model summaries by stratum.
reg_checkRegression diagnostics by stratum.
byThe stratifier variable.
levelsStrata included in the analysis.
approachThe regression approach used.
formatThe output format used.
sourceFunction identifier
("stratified_uni_reg").
$tableRendered stratified regression table.
$table_displayWide display data used to build the table.
$per_stratumFull uni_reg() result objects by
stratum.
$modelsList of fitted model objects for each stratum.
$model_summariesList of model summaries.
$reg_checkDiagnostic check results (when applicable).
[multi_reg()], [plot_reg()], [identify_confounder()]
birthwt_data <- data_birthwt |> transform( race = factor(race, levels = c(1, 2, 3), labels = c("White", "Black", "Other")), smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) stratified_uni <- stratified_uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke"), stratifier = "race", approach = "logit" ) stratified_uni$tablebirthwt_data <- data_birthwt |> transform( race = factor(race, levels = c(1, 2, 3), labels = c("White", "Black", "Other")), smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")), low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")) ) stratified_uni <- stratified_uni_reg( data = birthwt_data, outcome = "low", exposures = c("age", "lwt", "smoke"), stratifier = "race", approach = "logit" ) stratified_uni$table
Create a publication-ready univariate regression table using either gt or flextable
uni_reg( data, outcome, exposures, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )uni_reg( data, outcome, exposures, approach = "logit", format = c("flextable", "gt"), theme = c("minimal") )
data |
data.frame |
outcome |
Character scalar; outcome column name. Quoted and bare names are accepted. |
exposures |
Character vector; exposure column names. Quoted names are recommended in scripts, and bare names are also accepted. |
approach |
one of |
format |
One of |
theme |
preset name (e.g. |
A list of class c("gtregression","uni_reg", ...) with elements:
A flextable (when format="flextable") or
gt_tbl (when format="gt").
Data frame of numeric estimates and CIs.
Data frame for display (headers + levels).
List of fitted univariate models.
Per-model summary() results.
Diagnostics for linear models; message otherwise.
Metadata fields.
d <- mtcars if (requireNamespace("gt", quietly = TRUE)) { uni_reg(d, "am", c("mpg","cyl"), approach = "logit", format = "gt")$table } if (requireNamespace("flextable", quietly = TRUE)) { uni_reg(d, "am", c("mpg","cyl"), approach = "logit", format = "flextable")$table }d <- mtcars if (requireNamespace("gt", quietly = TRUE)) { uni_reg(d, "am", c("mpg","cyl"), approach = "logit", format = "gt")$table } if (requireNamespace("flextable", quietly = TRUE)) { uni_reg(d, "am", c("mpg","cyl"), approach = "logit", format = "flextable")$table }