Search code examples
rgtsummary

Wald confidence interval in gtsummary


Is there a way to specify the method of calculating confidence interval in gtsummary. Am asking this because of some inconsistencies with profile confidence intervals by confint fuction. Have a look at the pvalues and CIs of var1 and var2 below:

library(tidyverse)
library(gtsummary)

set.seed(2021)

testdata <- tibble(
  var1 = rbinom(1114, 1, 0.12),
  var2 = rbinom(1114, 1, 0.82),
  var3 = rbinom(1114, 1, 0.60),
  var4 = rbinom(1114, 1, 0.18),
  var5 = rbinom(1114, 1, 0.12),
  var6 = rbinom(1114, 1, 0.05),
  var7 = rbinom(1114, 1, 0.63),
  var8 = rbinom(1114, 1, 0.20),
  var9 = rbinom(1114, 1, 0.06),
  var10 = rbinom(1114, 1, 0.40),
  var11 = rbinom(1114, 1, 0.35),
  var12 = rbinom(1114, 1, 0.32),
  outcome = rbinom(1114, 1, 0.04)
) %>%
  mutate(across(.cols = everything(),
                ~factor(., levels = c(0, 1),
                        labels = c("No", "Yes"))))



mvariate.regress <- function(outcome, covariates, mydata) {
  form <- paste(outcome, "~",
                paste(covariates, collapse = " + "))

  model1 <- glm(as.formula(form),
                data = mydata, family = binomial)

  model1

}


ipvars <- paste0("var", 1:12)

mlogitfit <- mvariate.regress("outcome", ipvars, testdata)


mlogitfit %>%
  tbl_regression(
    exponentiate = TRUE
  ) %>%
  bold_p() %>%
  bold_p(t = 0.05) %>%
  bold_labels() %>%
  modify_header(label = "**Variable**",
                estimate = "**adjusted OR**") %>%
  modify_table_styling(
    columns = c("ci", "estimate"),
    rows = reference_row %in% TRUE,
    missing_symbol = "Ref"
  )

I would get the desired results if I were to use confint.default function


Solution

  • The gtstummary package website has a solution to this. Look for "Wald confidence interval" here or see below for the my_tidy function from there. You can calculate the Wald confidence interval separately and bind it to the table.

    my_tidy <- function(x, exponentiate =  TRUE, conf.level = 0.95, ...) {
      dplyr::bind_cols(
        broom::tidy(x, exponentiate = exponentiate, conf.int = FALSE),
        # calculate the confidence intervals, and save them in a tibble
        stats::confint.default(x) %>%
          tibble::as_tibble() %>%
          rlang::set_names(c("conf.low", "conf.high"))  )
    }
    
    mlogitfit %>%
      tbl_regression(tidy_fun = my_tidy) %>%
      bold_p() %>%
      bold_p(t = 0.05) %>%
      bold_labels() %>%
      modify_header(label = "**Variable**",
                    estimate = "**adjusted OR**") %>%
      modify_table_styling(
        columns = c("ci", "estimate"),
        rows = reference_row %in% TRUE,
        missing_symbol = "Ref"
      )