Home > Mobile >  Is it possible to table AUC output using gtsummary package in R?
Is it possible to table AUC output using gtsummary package in R?

Time:05-16

I am using auc function from pROC package in R. Is it possible to use gtsummary package to put the results in table?

CodePudding user response:

You can turn any data frame into a table with gt::gt on which gtsummary is based, so, using the aSAH data set from pROC, you could do, for example:

library(pROC)
library(gt)
library(dplyr)

aSAH %>% 
    filter(gender == "Female") %>%
    roc(outcome, s100b) %>%
  {data.frame(
    predictor = as.character(.$call$predictor),
    "Area under curve" = auc(.), 
    "Confidence interval" =
        paste(as.character(round(ci.auc(b), 2))[c(1, 3)], collapse = " - "),
    check.names = FALSE)} %>%
  gt()

enter image description here

CodePudding user response:

There are a few ways to add the AUC. Below is an example adding it with the glance statistics from broom::glance()

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.0'

mod <- glm(response ~ age   trt, trial, family = binomial)

glance_with_auc <- function(x, ...) {
  df_glance <- broom::glance(x)
  
  # add AUC
  df_glance %>%
    dplyr::mutate(
      AUC = with(broom::augment(x), pROC::roc(response, .fitted))$auc %>% as.numeric()
    )
}
glance_with_auc(mod)
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases
#> # A tibble: 1 × 9
#>   null.deviance df.null logLik   AIC   BIC deviance df.residual  nobs   AUC
#>           <dbl>   <int>  <dbl> <dbl> <dbl>    <dbl>       <int> <int> <dbl>
#> 1          229.     182  -113.  232.  241.     226.         180   183 0.576

tbl_regression(mod, exponentiate = TRUE) %>%
  add_glance_table(include = c(nobs, AIC, AUC), glance_fun = glance_with_auc) %>%
  as_kable() # convert to kable to display on SO
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases
Characteristic OR 95% CI p-value
Age 1.02 1.00, 1.04 0.095
Chemotherapy Treatment
Drug A
Drug B 1.13 0.60, 2.13 0.7
No. Obs. 183
AIC 232
AUC 0.576

Created on 2022-05-15 by the reprex package (v2.0.1)

  • Related