Produces a formatted table showing the outputs from the Odds Ratio analysis, including details on covariate characteristics and model results.
Usage
table_or(
glm_model_results,
conf_level = 0.95,
output = "tibble",
confint_fast_estimate = FALSE
)
Arguments
- glm_model_results
Results from a binomial Generalised Linear Model (GLM), as produced by
stats::glm()
.- conf_level
Numeric value between 0.001 and 0.999 (default = 0.95) specifying the confidence level for the confidence interval.
- output
String describing of the output type (default = 'tibble'). Options include 'tibble' and 'gt'.
- confint_fast_estimate
Boolean (default =
FALSE
) indicating whether to use a faster estimate of the confidence interval. Note: this assumes normally distributed data, which may not be suitable for your data.
Value
The returned object depends on the output
parameter:
If
output = 'tibble'
, the function returns an object of class "tbl_df", "tbl" and "data.frame".If
output = 'gt'
, the function returns an object of class "gt_tbl" and "list"
Details
The table includes the following information:
Covariate characteristics:
Number of observations for each characteristic
Number of observiations resulting in the outcome of interest
Conversion rate of outcome by the number of observations
Model results:
Estimated Odds Ratio, standard error and p-value
Calculated confidence interval for the specified confidence level
A visualisation of the OR plot is also provided for an at-a-glance view of the model results
Includes details on the characteristics of the covariates, such as:
the number of observations for each characteristic,
the number of observations resulting in the outcome of interest,
the conversion rate of outcome by the number of observations,
Details are calculated showing the:
estimated Odds Ratio, standard error and p-value,
calculated confidence interval for the confidence level,
Also included is a visualisation of the OR plot to provide an at-a-glance view of the model results.
Examples
# Load the Titanic dataset
df <- datasets::Titanic |>
dplyr::as_tibble() |>
# convert aggregated counts to individual observations
dplyr::filter(n > 0) |>
tidyr::uncount(weights = n) |>
# convert character variables to factors
dplyr::mutate(dplyr::across(dplyr::where(is.character), as.factor))
# Perform logistic regression using `glm`
lr <- stats::glm(
data = df,
family = 'binomial',
formula = Survived ~ Class + Sex + Age
)
# Produce the Odds Ratio table as a tibble
table_or(lr)
#> # A tibble: 8 × 14
#> label level rows outcome outcome_rate class estimate std.error statistic
#> <fct> <fct> <int> <int> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 Class 1st 325 203 0.625 factor NA NA NA
#> 2 Class 2nd 285 118 0.414 factor 0.361 0.196 -5.19
#> 3 Class 3rd 706 178 0.252 factor 0.169 0.172 -10.4
#> 4 Class Crew 885 212 0.240 factor 0.424 0.157 -5.45
#> 5 Sex Female 470 344 0.732 factor NA NA NA
#> 6 Sex Male 1731 367 0.212 factor 0.0889 0.140 -17.2
#> 7 Age Adult 2092 654 0.313 factor NA NA NA
#> 8 Age Child 109 57 0.523 factor 2.89 0.244 4.35
#> # ℹ 5 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>,
#> # significance <chr>, comparator <dbl>
# Produce the Odds Ratio table as a gt object
table_or(lr, output = 'gt')
#> Warning: Ignoring unknown parameters: `linewidth`
#> Warning: Ignoring unknown parameters: `linewidth`
#> Warning: Ignoring unknown parameters: `linewidth`
#> Warning: Ignoring unknown parameters: `linewidth`
#> Warning: Ignoring unknown parameters: `linewidth`
Survived
Odds Ratio summary table with 95% Confidence Interval
Level
N
n
Rate
Class
OR
SE
p
Lower
Upper
Significance
1 Characteristics are the explanatory variables in the logistic regression analysis. For categorical variables the first characteristic is designated as a reference against which the others are compared. For numeric variables the results indicate a change per single unit increase.
Level - the name or the description of the explanatory variable.
N - the number of observations examined.
n - the number of observations resulting in the outcome of interest.
Rate - the proportion of observations resulting in the outcome of interest (n / N).
Class - description of the data type.
2 Odds Ratios estimate the relative odds of an outcome with reference to the Characteristic. For categorical data the first level is the reference against which the odds of other levels are compared. Numerical characteristics indicate the change in OR for each additional increase of one unit in the variable.
OR - The Odds Ratio point estimate - values below 1 indicate an inverse relationship whereas values above 1 indicate a positive relationship. Values shown to 4 significant figures.
SE - Standard Error of the point estimate. Values shown to 4 significant figures.
p - The p-value estimate based on the residual Chi-squared statistic.
3 Confidence Interval - the range of values likely to contain the OR in 95% of cases if this study were to be repeated multiple times. If the CI touches or crosses the value 1 then it is unlikely the Characteristic is significantly associated with the outcome.
Lower & Upper - The range of values comprising the CI, shown to 4 significant figures.
Significance - The statistical significance indicated by the CI, Significant where the CI does not touch or cross the value 1.