Wrapper for pretty_model_output(). This function takes a dataset, along with variables names for x (could be multiple), y, and possibly event status, for model fit.
run_pretty_model_output(x_in, model_data, y_in, event_in = NULL, event_level = NULL, title_name = NULL, fail_if_warning = TRUE, conf_level = 0.95, overall_p_test_stat = c("Wald", "LR"), est_digits = 3, p_digits = 4, output_type = NULL, sig_alpha = 0.05, background = "yellow", verbose = FALSE, ...)
x_in | name of x variables in model (can be vector of x names) |
---|---|
model_data | data.frame or tibble that contains |
y_in | name of outcome measure for logistic and linear model, or name of time component in cox model |
event_in | name of event status variable. Shouled be left NULL for logistic and linear models. If |
event_level | outcome variable event level for logistic model, and event status level for cox model. |
title_name | title to use (will be repeated in first column) |
fail_if_warning | Should program stop and give useful message if there is a warning message when running model (Default is TRUE) |
conf_level | the confidence level required (default is 0.95). |
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. |
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 not NULL) |
background | background color of significant values, or no highlighting if NULL. Default is "yellow" (Only used if output_type not NULL) |
verbose | a logical variable indicating if warnings and messages should be displayed. Default FALSE. |
... | 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), n/n (event)
.
x_in
can be single variable name, or vector of variables to include in the model. All variables must be present in the model_data
dataset.
fail_if_warning
variable default to TRUE because most warnings should be addressed, such as the "Loglik converged before variable XX; beta may be infinite" warning.
# Basic linear model example set.seed(542542522) ybin <- sample(0:1, 100, replace = TRUE) ybin2 <- sample(c('Male','Female'), 100, replace = TRUE) ybin3 <- sample(c('Dead','Alive'), 100, replace = TRUE) y <- rexp(100,.1) x1 <- factor(sample(LETTERS[1:2],100,replace = TRUE)) x2 <- factor(sample(letters[1:4],100,replace = TRUE)) my_data <- data.frame(y, ybin, ybin2, ybin3, x1, x2) Hmisc::label(my_data$x1) <- "X1 Variable" # Single runs run_pretty_model_output(x_in = 'x1', model_data = my_data, y_in = 'y', event_in = 'ybin')#> # A tibble: 2 x 6 #> Variable Level `HR (95% CI)` `P Value` `Overall P Valu… `n (events)` #> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 X1 Variable A 1.0 (Reference) - "" n=100 (48) #> 2 X1 Variable B 0.913 (0.516, 1.615) 0.7537 "" ""run_pretty_model_output(x_in = 'x1', model_data = my_data, y_in = 'y', event_in = 'ybin3', event_level = 'Dead')#> # A tibble: 2 x 6 #> Variable Level `HR (95% CI)` `P Value` `Overall P Valu… `n (events)` #> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 X1 Variable A 1.0 (Reference) - "" n=100 (47) #> 2 X1 Variable B 0.965 (0.542, 1.718) 0.9024 "" ""run_pretty_model_output(x_in = c('x1','x2'), model_data = my_data, y_in = 'y', event_in = 'ybin')#> # A tibble: 6 x 6 #> Variable Level `HR (95% CI)` `P Value` `Overall P Valu… `n (events)` #> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 X1 Variable A 1.0 (Reference) - "" n=100 (48) #> 2 X1 Variable B 0.935 (0.527, 1.660) 0.8191 "" "" #> 3 x2 a 1.0 (Reference) - 0.8239 "" #> 4 x2 b 0.801 (0.389, 1.649) 0.5475 "" "" #> 5 x2 c 0.713 (0.298, 1.704) 0.4462 "" "" #> 6 x2 d 0.716 (0.311, 1.647) 0.4322 "" ""run_pretty_model_output(x_in = 'x2', model_data = my_data, y_in = 'ybin', event_in = NULL, verbose = T)#>#> # A tibble: 4 x 6 #> Variable Level `OR (95% CI)` `P Value` `Overall P Value` n #> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 x2 a 1.0 (Reference) - 0.4173 n=100 #> 2 x2 b 0.500 (0.166, 1.444) 0.2057 "" "" #> 3 x2 c 0.491 (0.142, 1.625) 0.2483 "" "" #> 4 x2 d 0.386 (0.115, 1.228) 0.1125 "" ""run_pretty_model_output(x_in = 'x2', model_data = my_data, y_in = 'y', event_in = NULL)#> # A tibble: 4 x 6 #> Variable Level `Est (95% CI)` `P Value` `Overall P Value` n #> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 x2 a 1.0 (Reference) - 0.9503 n=100 #> 2 x2 b -1.681 (-7.506, 4.144) 0.5682 "" "" #> 3 x2 c -0.766 (-7.340, 5.808) 0.8176 "" "" #> 4 x2 d -1.248 (-7.584, 5.088) 0.6968 "" ""# Multiple runs for different variables library(dplyr) vars_to_run = c('x1', 'x2') cox_models <- purrr::map_dfr(vars_to_run, run_pretty_model_output, model_data = my_data, y_in = 'y', event_in = 'ybin') kableExtra::kable(cox_models, 'html', caption = 'My Table') %>% kableExtra::collapse_rows(c(1:2), row_group_label_position = 'stack', headers_to_remove = 1:2)#> <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> #> <th style="text-align:left;"> n (events) </th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="2"> X1 Variable </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;"> </td> #> <td style="text-align:left;"> n=100 (48) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> B </td> #> <td style="text-align:left;"> 0.913 (0.516, 1.615) </td> #> <td style="text-align:left;"> 0.7537 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="4"> x2 </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.8123 </td> #> <td style="text-align:left;"> n=100 (48) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> b </td> #> <td style="text-align:left;"> 0.797 (0.388, 1.639) </td> #> <td style="text-align:left;"> 0.5380 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> c </td> #> <td style="text-align:left;"> 0.707 (0.297, 1.688) </td> #> <td style="text-align:left;"> 0.4354 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> d </td> #> <td style="text-align:left;"> 0.711 (0.310, 1.631) </td> #> <td style="text-align:left;"> 0.4207 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> </tbody> #> </table># Real World Example data(Bladder_Cancer) vars_to_run = c('Gender', 'Clinical_Stage_Grouped', 'PT0N0', 'Any_Downstaging') univariate_output <- purrr::map_dfr(vars_to_run, run_pretty_model_output, model_data = Bladder_Cancer, y_in = 'Survival_Months', event_in = 'Vital_Status', event_level = 'Dead') kableExtra::kable(univariate_output, 'html') %>% kableExtra::collapse_rows(c(1:2), row_group_label_position = 'stack', headers_to_remove = 1:2)#> <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> #> <th style="text-align:left;"> n (events) </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> #> <td style="text-align:left;"> n=166 (59) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Male </td> #> <td style="text-align:left;"> 1.412 (0.748, 2.663) </td> #> <td style="text-align:left;"> 0.2872 </td> #> <td style="text-align:left;"> </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.0503 </td> #> <td style="text-align:left;"> n=166 (59) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Stage III (T3NxMx) </td> #> <td style="text-align:left;"> 1.985 (1.128, 3.494) </td> #> <td style="text-align:left;"> 0.0174 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Stage IV (T4NxMx) </td> #> <td style="text-align:left;"> 1.632 (0.684, 3.891) </td> #> <td style="text-align:left;"> 0.2693 </td> #> <td style="text-align:left;"> </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> #> <td style="text-align:left;"> n=166 (59) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Complete Response </td> #> <td style="text-align:left;"> 0.108 (0.026, 0.443) </td> #> <td style="text-align:left;"> 0.0020 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="2"> Any Downstaging </td> #> <td style="text-align:left;"> No Downstaging </td> #> <td style="text-align:left;"> 1.0 (Reference) </td> #> <td style="text-align:left;"> - </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> n=166 (59) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Downstaging </td> #> <td style="text-align:left;"> 0.228 (0.115, 0.451) </td> #> <td style="text-align:left;"> <0.0001 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> </tbody> #> </table>multivariable_output <- run_pretty_model_output(vars_to_run, model_data = Bladder_Cancer, y_in = 'Survival_Months', event_in = 'Vital_Status', event_level = 'Dead') kableExtra::kable(multivariable_output, 'html') %>% kableExtra::collapse_rows(c(1:2), row_group_label_position = 'stack', headers_to_remove = 1:2)#> <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> #> <th style="text-align:left;"> n (events) </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> #> <td style="text-align:left;"> n=166 (59) </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Male </td> #> <td style="text-align:left;"> 1.484 (0.767, 2.874) </td> #> <td style="text-align:left;"> 0.2411 </td> #> <td style="text-align:left;"> </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.2182 </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Stage III (T3NxMx) </td> #> <td style="text-align:left;"> 1.683 (0.937, 3.022) </td> #> <td style="text-align:left;"> 0.0814 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Stage IV (T4NxMx) </td> #> <td style="text-align:left;"> 1.239 (0.518, 2.964) </td> #> <td style="text-align:left;"> 0.6303 </td> #> <td style="text-align:left;"> </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> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Complete Response </td> #> <td style="text-align:left;"> 0.225 (0.048, 1.067) </td> #> <td style="text-align:left;"> 0.0604 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> <td style="text-align:left;vertical-align: middle !important;" rowspan="2"> Any Downstaging </td> #> <td style="text-align:left;"> No Downstaging </td> #> <td style="text-align:left;"> 1.0 (Reference) </td> #> <td style="text-align:left;"> - </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> <tr> #> #> <td style="text-align:left;"> Downstaging </td> #> <td style="text-align:left;"> 0.430 (0.200, 0.922) </td> #> <td style="text-align:left;"> 0.0302 </td> #> <td style="text-align:left;"> </td> #> <td style="text-align:left;"> </td> #> </tr> #> </tbody> #> </table>