| m2dt | R Documentation |
Extracts coefficients, confidence intervals, and comprehensive model statistics from fitted regression models and converts them to a standardized data.table format suitable for further analysis or publication. This is a core utility function frequently used internally by other summata regression functions, although it can be used as a standalone function as well.
m2dt(
data,
model,
conf_level = 0.95,
keep_qc_stats = TRUE,
include_intercept = TRUE,
terms_to_exclude = NULL,
reference_rows = TRUE,
reference_label = "reference",
skip_counts = FALSE,
conf_method = NULL
)
data |
Data frame or data.table containing the dataset used to fit the model. Required for computing group-level sample sizes and event counts. |
model |
Fitted model object. Supported classes include:
|
conf_level |
Numeric confidence level for confidence intervals. Must be between 0 and 1. Default is 0.95 (95% CI). |
keep_qc_stats |
Logical. If |
include_intercept |
Logical. If |
terms_to_exclude |
Character vector of term names to exclude from output.
Useful for removing specific unwanted parameters (e.g., nuisance variables,
spline terms). Default is |
reference_rows |
Logical. If |
reference_label |
Character string used to label reference category rows
in the output. Appears in the |
skip_counts |
Logical. If |
conf_method |
Character string controlling the confidence interval method.
If
Cox and mixed-effects models use Wald intervals regardless of this setting.
Set globally with |
This function is the core extraction utility used by fit() and other
regression functions. It handles the complexities of different model classes
and provides a consistent output format suitable for tables and forest plots.
Model Type Detection: The function automatically detects model type and applies appropriate:
Effect measure naming (OR, HR, RR, Coefficient)
Confidence interval calculation (see below)
Event counting for binary/survival outcomes
Confidence Interval Methods:
The CI method is selected per model class using stats::confint()
dispatch:
GLM/negative binomial: Profile likelihood via
MASS::confint.glm(), except quasi-families which use Wald
Linear models: Exact t-distribution via
confint.lm()
Cox PH: Wald intervals (coefficient \pm
z \times SE)
Mixed-effects models: Wald intervals
Falls back to Wald on profiling failure.
Mixed Effects Models: For lme4 models (glmer, lmer), the function extracts fixed effects only. Random effects variance components are not included in the output table, as they represent clustering structure rather than predictor effects.
A data.table containing extracted model information with the
following standard columns:
Character. Either "Univariable" (unadjusted model with single predictor) or "Multivariable" (adjusted model with multiple predictors)
Character. Type of regression (e.g., "Logistic", "Linear", "Cox PH", "Poisson", etc.)
Character. Variable name (for factor variables, the base variable name without the level)
Character. Group/level name for factor variables; empty string for continuous variables
Integer. Total sample size used in the model
Integer. Sample size for this specific variable level (factor variables only)
Integer. Total number of events in the model (for survival and logistic models)
Integer. Number of events for this specific variable level (for survival and logistic models with factor variables)
Numeric. Raw regression coefficient (log odds, log hazard, etc.)
Numeric. Standard error of the coefficient
Numeric. Effect estimate - column name depends on model type:
OR for logistic regression (odds ratio)
HR for Cox models (hazard ratio)
RR for Poisson regression (rate/risk ratio)
Coefficient for linear models or other GLMs
Numeric. Lower bound of confidence interval for effect estimate
Numeric. Upper bound of confidence interval for effect estimate
Numeric. Test statistic (z-value for GLM/Cox, t-value for LM)
Numeric. p-value for coefficient test
Character. Significance markers: *** (p < 0.001), **
(p < 0.01), * (p < 0.05), . (p < 0.10).
Logical. Binary indicator: TRUE if p < 0.05,
FALSE otherwise
Character. Contains reference_label for reference
category rows when reference_rows = TRUE, empty string otherwise
fit for the main regression interface,
glmforest, coxforest, lmforest for
forest plot visualization
# Load example data
data(clintrial)
# Example 1: Extract from logistic regression
glm_model <- glm(os_status ~ age + sex + treatment,
data = clintrial, family = binomial)
glm_result <- m2dt(clintrial, glm_model)
glm_result
# Example 2: Extract from linear model
lm_model <- lm(los_days ~ age + sex + surgery, data = clintrial)
lm_result <- m2dt(clintrial, lm_model)
lm_result
# Example 3: Cox proportional hazards model
library(survival)
cox_model <- coxph(Surv(os_months, os_status) ~ age + sex + stage,
data = clintrial)
cox_result <- m2dt(clintrial, cox_model)
cox_result
# Example 4: Exclude intercept for cleaner tables
clean_result <- m2dt(clintrial, glm_model, include_intercept = FALSE)
clean_result
# Example 5: Change confidence level
result_90ci <- m2dt(clintrial, glm_model, conf_level = 0.90)
result_90ci
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.