
Logistic regression fitting and formatting helpers
leo_logistic.RdUsage
leo_logistic(
df,
y_out,
x_exp,
x_cov = NULL,
case_value = 1,
x_exp_type = "auto",
verbose = TRUE
)
leo_logistic_format(x, style = "wide")Arguments
- df
Data frame containing the binary outcome, exposure, and covariates.
- y_out
Character scalar giving the binary outcome column name.
- x_exp
Character scalar giving the exposure column name.
- x_cov
NULL, a character vector of covariate column names, or a list of covariate column-name vectors. All models are fitted on the same complete-case cohort defined byy_out,x_exp, and all covariates that appear inx_cov. Ifx_covis a named list, those model names are preserved in the output columns.- case_value
Value in
y_outthat indicates case status.- x_exp_type
Exposure type handling for
x_exp. Use"auto"to infer from the input type,"continuous"to force a numeric logistic term, or"categorical"to force factor coding.- verbose
Logical; print progress messages.
- x
Result returned by
leo_logistic().- style
One of
"wide","tidy", or"gtsummary".
Value
A leo_logistic object containing the default wide table
in $result, the underlying tidy rows in $result_tidy, model metadata,
and fitted glm objects.
Details
leo_logistic() fits one or more logistic regression models for a
single exposure and binary outcome. leo_logistic_format()
converts the returned object into a wide summary table, tidy result table, or
gtsummary output.
Examples
set.seed(123)
n <- 300
logi_df <- data.frame(
outcome = rbinom(n, 1, 0.35),
prs = rnorm(n),
sex = factor(sample(c("Male", "Female"), n, TRUE)),
bmi_group = factor(sample(c("Low", "Mid", "High"), n, TRUE), levels = c("Low", "Mid", "High"))
)
model_cont <- list(
"Crude" = NULL,
"Model A" = c("sex"),
"Model B" = c("sex", "bmi_group")
)
res_prs <- leo_logistic(
df = logi_df, y_out = "outcome", x_exp = "prs",
x_cov = model_cont, verbose = FALSE
)
res_prs$result
#> Exposure Outcome Case N Control N Class Crude OR Crude 95% CI
#> 1 prs outcome 103 197 Continuous 0.969 0.760, 1.235
#> Crude P value Model A OR Model A 95% CI Model A P value Model B OR
#> 1 0.802 0.97 0.760, 1.235 0.802 0.963
#> Model B 95% CI Model B P value
#> 1 0.754, 1.228 0.761
leo_logistic_format(res_prs, style = "tidy")
#> Model Exposure Outcome Level Case N Control N OR 95% CI P value
#> 1 Crude prs outcome <NA> 103 197 0.969 0.760, 1.235 0.802
#> 2 Model A prs outcome <NA> 103 197 0.970 0.760, 1.235 0.802
#> 3 Model B prs outcome <NA> 103 197 0.963 0.754, 1.228 0.761
#> Class Formula
#> 1 Continuous outcome ~ exposure
#> 2 Continuous outcome ~ exposure + sex
#> 3 Continuous outcome ~ exposure + sex + bmi_group
model_cat <- list(
"Crude" = NULL,
"Model A" = c("sex")
)
res_bmi <- leo_logistic(
df = logi_df, y_out = "outcome", x_exp = "bmi_group",
x_cov = model_cat, x_exp_type = "categorical", verbose = FALSE
)
res_bmi$result
#> Exposure Outcome Case N Control N Class Crude OR
#> 1 Low (Ref) outcome 31 68 Categorical (3 levels) 1.000
#> 2 Mid outcome 34 66 Categorical (3 levels) 1.130
#> 3 High outcome 38 63 Categorical (3 levels) 1.323
#> Crude 95% CI Crude P value Model A OR Model A 95% CI Model A P value
#> 1 1.000, 1.000 NA 1.000 1.000, 1.000 NA
#> 2 0.624, 2.050 0.686 1.130 0.624, 2.050 0.687
#> 3 0.738, 2.385 0.348 1.312 0.731, 2.370 0.364
if (requireNamespace("gtsummary", quietly = TRUE) && requireNamespace("broom.helpers", quietly = TRUE)) {
leo_logistic_format(res_prs, style = "gtsummary")
}