
Linear regression fitting and formatting helpers
leo_linear.RdUsage
leo_linear(df, y_out, x_exp, x_cov = NULL, x_exp_type = "auto", verbose = TRUE)
leo_linear_format(x, style = "wide")Arguments
- df
Data frame containing the continuous outcome, exposure, and covariates.
- y_out
Character scalar giving the continuous 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.- x_exp_type
Exposure type handling for
x_exp. Use"auto"to infer from the input type,"continuous"to force a numeric linear term, or"categorical"to force factor coding.- verbose
Logical; print progress messages.
- x
Result returned by
leo_linear().- style
One of
"wide","tidy", or"gtsummary".
Value
A leo_linear object containing the default wide table in
$result, the underlying tidy rows in $result_tidy, model metadata, and
fitted lm objects.
Details
leo_linear() fits one or more linear regression models for a
single exposure and continuous outcome. leo_linear_format()
converts the returned object into a wide summary table, tidy result table, or
gtsummary output.
Examples
linear_df <- dplyr::transmute(
mtcars,
outcome = mpg,
wt = wt,
am = factor(am, levels = c(0, 1), labels = c("Auto", "Manual")),
cyl_group = factor(cyl, levels = c(4, 6, 8), labels = c("Cyl4", "Cyl6", "Cyl8"))
)
model_cont <- list(
"Crude" = NULL,
"Model A" = c("am"),
"Model B" = c("am", "cyl_group")
)
res_wt <- leo_linear(
df = linear_df, y_out = "outcome", x_exp = "wt",
x_cov = model_cont, verbose = FALSE
)
res_wt$result
#> Exposure Outcome N Class Crude Beta Crude 95% CI Crude P value
#> 1 wt outcome 32 Continuous -5.344 -6.486, -4.203 <0.001
#> Model A Beta Model A 95% CI Model A P value Model B Beta Model B 95% CI
#> 1 -5.353 -6.965, -3.741 <0.001 -3.15 -5.013, -1.286
#> Model B P value
#> 1 0.002
leo_linear_format(res_wt, style = "tidy")
#> Model Exposure Outcome Level N Beta 95% CI P value Class
#> 1 Crude wt outcome <NA> 32 -5.344 -6.486, -4.203 <0.001 Continuous
#> 2 Model A wt outcome <NA> 32 -5.353 -6.965, -3.741 <0.001 Continuous
#> 3 Model B wt outcome <NA> 32 -3.150 -5.013, -1.286 0.002 Continuous
#> Formula
#> 1 outcome ~ exposure
#> 2 outcome ~ exposure + am
#> 3 outcome ~ exposure + am + cyl_group
model_cat <- list(
"Crude" = NULL,
"Model A" = c("am")
)
res_cyl <- leo_linear(
df = linear_df, y_out = "outcome", x_exp = "cyl_group",
x_cov = model_cat, x_exp_type = "categorical", verbose = FALSE
)
res_cyl$result
#> Exposure Outcome N Class Crude Beta Crude 95% CI
#> 1 Cyl4 (Ref) outcome 11 Categorical (3 levels) 0.000 0.000, 0.000
#> 2 Cyl6 outcome 7 Categorical (3 levels) -6.921 -10.108, -3.734
#> 3 Cyl8 outcome 14 Categorical (3 levels) -11.564 -14.220, -8.908
#> Crude P value Model A Beta Model A 95% CI Model A P value
#> 1 NA 0.000 0.000, 0.000 NA
#> 2 <0.001 -6.156 -9.302, -3.010 <0.001
#> 3 <0.001 -10.068 -13.042, -7.093 <0.001
if (requireNamespace("gtsummary", quietly = TRUE) && requireNamespace("broom.helpers", quietly = TRUE)) {
leo_linear_format(res_wt, style = "gtsummary")
}