R/pretty_output_functions.R
pretty_model_output.Rd
pretty_model_output() takes a Linear, Logistic, and Cox model fit object and calculate estimates, odds ratios, or hazard ratios, respectively, with confidence intervals. P values are also produced. For categorical variables with 3+ levels overall Type 3 p values are calculated, in addition to p values comparing to the first level (reference).
pretty_model_output(fit, model_data, overall_p_test_stat = c("Wald", "LR"), title_name = NULL, conf_level = 0.95, est_digits = 3, p_digits = 4, output_type = NULL, sig_alpha = 0.05, background = "yellow", ...)
fit | lm, glm, or coxph fit (currently only tested on logistic glm fit) |
---|---|
model_data | data.frame or tibble used to create model fits. Used for capturing variable labels, if they exist |
overall_p_test_stat | "Wald" (default) or "LR"; the test.statistic to pass through to the test.statistic param in car::Anova. Ignored for lm fits. |
title_name | title to use (will be repeated in first column) |
conf_level | the confidence level required (default is 0.95). |
est_digits | number of digits to round OR or HR to (default is 3) |
p_digits | number of digits to round p values (default is 4) |
output_type | output type, either NULL (default), "latex", or "html" (making special charaters latex friendly) |
sig_alpha | the defined significance level for highlighting. Default = 0.05 (Only used if output_type is not NULL) |
background | background color of significant values, or no highlighting if NULL. Default is "yellow" (Only used if output_type is not NULL) |
... | other params to pass to |
A tibble with: Name
(if provided), Variable
, Level
, Est/OR/HR (95% CI)
, P Value
(for categorical variables comparing to reference), Overall P Value
(for categorical variables with 3+ levels).
Model type is determined by fit
class, and also family if glm class. If the class is glm and binomial or quasibinomial family, then the output is designed for a Logistic model (i.e. Odd Ratios), if the class is coxph the output is designed for a Cox model (i.e. Harzard Ratios), otherwise the output is designed for a linear model or other model where normal coefficient estimates are displayed.
# Basic linear model example set.seed(542542522) ybin <- sample(0:1, 100, replace = TRUE) y <- rexp(100,.1) x1 <- rnorm(100) x2 <- y + rnorm(100) x3 <- factor(sample(letters[1:4],100,replace = TRUE)) my_data <- data.frame(y, ybin, x1, x2, x3) # Linear Regression my_fit <- lm(y ~ x1 + x2 + x3, data = my_data) pretty_model_output(fit = my_fit, model_data = my_data)#> # A tibble: 6 x 5 #> Variable Level `Est (95% CI)` `P Value` `Overall P Value` #> <chr> <chr> <chr> <chr> <chr> #> 1 x1 "" -0.012 (-0.223, 0.199) 0.9131 "" #> 2 x2 "" 0.990 (0.962, 1.017) <0.0001 "" #> 3 x3 a 1.0 (Reference) - 0.1879 #> 4 x3 b -0.151 (-0.821, 0.519) 0.6554 "" #> 5 x3 c -0.602 (-1.223, 0.018) 0.0571 "" #> 6 x3 d -0.031 (-0.641, 0.579) 0.9207 ""# Logistic Regression my_fit <- glm(ybin ~ x1 + x2 + x3, data = my_data, family = binomial(link = "logit")) pretty_model_output(fit = my_fit, model_data = my_data)#> # A tibble: 6 x 5 #> Variable Level `OR (95% CI)` `P Value` `Overall P Value` #> <chr> <chr> <chr> <chr> <chr> #> 1 x1 "" 0.895 (0.601, 1.324) 0.5786 "" #> 2 x2 "" 0.987 (0.936, 1.038) 0.6175 "" #> 3 x3 a 1.0 (Reference) - 0.3408 #> 4 x3 b 1.114 (0.323, 3.840) 0.8632 "" #> 5 x3 c 0.953 (0.299, 3.027) 0.9352 "" #> 6 x3 d 2.333 (0.764, 7.420) 0.1413 ""# Coxph Regression my_fit <- survival::coxph(survival::Surv(y, ybin) ~ x1 + x2 + x3, data = my_data) my_pretty_model_output <- pretty_model_output(fit = my_fit, model_data = my_data) # Printing of Fancy table in HTML library(dplyr) kableExtra::kable(my_pretty_model_output, 'html', caption = 'My Table') %>% kableExtra::collapse_rows(c(1:2), row_group_label_position = 'stack')#> <table> #> <caption>My Table</caption> #> <thead> #> <tr> #> <th style="text-align:left;"> Variable </th> #> <th style="text-align:left;"> Level </th> #> <th style="text-align:left;"> HR (95% CI) </th> #> <th style="text-align:left;"> P Value </th> #> <th style="text-align:left;"> Overall P Value </th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style="text-align:left;"> x1 </td> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="2"> </td> #> <td style="text-align:left;"> 1.083 (0.760, 1.543) </td> #> <td style="text-align:left;"> 0.6604 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;"> x2 </td> #> #> <td style="text-align:left;"> 0.423 (0.336, 0.533) </td> #> <td style="text-align:left;"> <0.0001 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="4"> x3 </td> #> <td style="text-align:left;"> a </td> #> <td style="text-align:left;"> 1.0 (Reference) </td> #> <td style="text-align:left;"> - </td> #> <td style="text-align:left;"> 0.1897 </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> b </td> #> <td style="text-align:left;"> 1.063 (0.356, 3.175) </td> #> <td style="text-align:left;"> 0.9133 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> c </td> #> <td style="text-align:left;"> 1.953 (0.681, 5.604) </td> #> <td style="text-align:left;"> 0.2130 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> d </td> #> <td style="text-align:left;"> 2.214 (0.888, 5.518) </td> #> <td style="text-align:left;"> 0.0881 </td> #> <td style="text-align:left;"> </td> #> </tr> #> </tbody> #> </table># Real World Examples data(Bladder_Cancer) surv_obj <- survival::Surv(Bladder_Cancer$Survival_Months, Bladder_Cancer$Vital_Status == 'Dead') my_fit <- survival::coxph(surv_obj ~ Gender + Clinical_Stage_Grouped + PT0N0, data = Bladder_Cancer) my_output <- pretty_model_output(fit = my_fit, model_data = Bladder_Cancer) kableExtra::kable(my_output, 'html') %>% kableExtra::collapse_rows(c(1:2), row_group_label_position = 'stack')#> <table> #> <thead> #> <tr> #> <th style="text-align:left;"> Variable </th> #> <th style="text-align:left;"> Level </th> #> <th style="text-align:left;"> HR (95% CI) </th> #> <th style="text-align:left;"> P Value </th> #> <th style="text-align:left;"> Overall P Value </th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="2"> Gender </td> #> <td style="text-align:left;"> Female </td> #> <td style="text-align:left;"> 1.0 (Reference) </td> #> <td style="text-align:left;"> - </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Male </td> #> <td style="text-align:left;"> 1.701 (0.888, 3.258) </td> #> <td style="text-align:left;"> 0.1090 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="3"> Clinical AJCC Stage </td> #> <td style="text-align:left;"> Stage I/II (<=T2NxMx) </td> #> <td style="text-align:left;"> 1.0 (Reference) </td> #> <td style="text-align:left;"> - </td> #> <td style="text-align:left;"> 0.1085 </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Stage III (T3NxMx) </td> #> <td style="text-align:left;"> 1.861 (1.042, 3.322) </td> #> <td style="text-align:left;"> 0.0358 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Stage IV (T4NxMx) </td> #> <td style="text-align:left;"> 1.351 (0.565, 3.227) </td> #> <td style="text-align:left;"> 0.4985 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="2"> Downstaged to pT0N0 </td> #> <td style="text-align:left;"> No Completed Response </td> #> <td style="text-align:left;"> 1.0 (Reference) </td> #> <td style="text-align:left;"> - </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Complete Response </td> #> <td style="text-align:left;"> 0.117 (0.028, 0.483) </td> #> <td style="text-align:left;"> 0.0030 </td> #> <td style="text-align:left;"> </td> #> </tr> #> </tbody> #> </table>