Add Control and Cases numbers instead of add_nevent()

141 views Asked by At

The following code produces an N column of "N" and "Event N" as part of the univariate regression table. I have a case control dataset and I would like to have the columns "Cases" and "Controls" giving the numbers of cases and controls instead.

The "cases" and "control" are in determined by the variable "response" in the code below. e.g. response(1) = "cases", while response(0) = "controls".

How can I do this?

thanks, nelly

tbl_uv_nevent_ex <-
trial[c("response", "trt", "age", "grade")] %>%
tbl_uvregression(
method = glm,
y = response,
method.args = list(family = binomial)
) %>%
add_nevent()
1

There are 1 answers

0
Daniel D. Sjoberg On BEST ANSWER

You can do this by adding the number of controls to the .$table_body data frame. I've included an example below. There is a sticking point at the moment....after you add the number of controls to the data frame that will be printed, we need to add the new column to the internal set of instructions to print in gtsummary. This step is a headache now, but we're working on a solution to make it accessible to users. Here is the first draft of that function: http://www.danieldsjoberg.com/gtsummary/dev/reference/modify_table_header.html

In the meantime, here's how you can get that done:

library(gtsummary)

tbl <-
  trial[c("response", "trt", "age")] %>%
  tbl_uvregression(
    method = glm,
    y = response,
    method.args = list(family = binomial),
    exponentiate = TRUE
  ) %>%
  add_nevent()

# add the number of controls to table
tbl$table_body <-
  tbl$table_body %>%
  dplyr::mutate(
    n_nonevent = N - nevent
  ) %>%
  dplyr::relocate(n_nonevent, .after = nevent)

# updating internal info with new column (this part will not be required in the future)
tbl$table_header <- 
  gtsummary:::table_header_fill_missing(tbl$table_header, 
                                        tbl$table_body)

# print tbl with Case and Control Ns
tbl %>%
  modify_header(
    list(nevent ~ "**Case N**",
         n_nonevent ~ "**Control N**")
  )

enter image description here