Skip to content

tbl_summary should pass to custom add_stat functiond the variable type as gtsummary treats that variable, not the real one #2393

@raffaelemancuso

Description

@raffaelemancuso

Consider the following example:

N <- 1000
toydf <- data.frame(
  children = rbinom(N, size = 3, prob = 0.5),
  age = extraDistr::rdunif(N, 10, 100),
  gender = sample(c("M", "F"), N, replace = TRUE)
)
toydf$age %<>% as.integer()
toydf$gender %<>% as.factor()

my_mean_diff <- function(data, variable, by, tbl, ...) {
  x <- data[[variable]]
  g <- data[[by]]
  lvls <- levels(g)
  vartype <- class(x)[1]
  
  # <UNCOMMENT THIS TO MAKE IT WORK>
  #if(
  #  (vartype=="character" | vartype=="factor" | vartype=="numeric" | vartype=="integer") &
  #  length(unique(x)) <= 10
  #) 
  #{
  #  vartype <- "categorical"
  #}
  
  print(paste0("Variable: ", variable, ", Type: ", vartype))
  
  switch(
    vartype,
    categorical = {
      prop <- prop.table(table(x, g), margin = 2)
      return((prop[, lvls[2]] - prop[, lvls[1]]) * 100)
    },
    integer = {
      return(diff(tapply(x, g, mean, na.rm = TRUE)))
    },
    {
      stop(glue("ERROR: Unrecognized type {vartype}"))
    }
  )
}

toydf %>% 
  tbl_summary(
    statistic = list(
      all_continuous() ~ "{mean} ({sd})",
      all_categorical() ~ "{n} ({p}%)"
    ),
    missing = "no",
    by = "gender",
    percent = "column"
  ) %>% add_stat(
  fns = everything() ~ my_mean_diff,
  location = list(
    all_continuous() ~ "label",
    all_categorical() ~ "level",
    all_dichotomous() ~ "label"
  )
) 

It will return:

Error in `add_stat()`:
! Dimension of "children" and the added statistic do not match.
ℹ Expecting statistic/data frame to be length/no. rows 4.
Run `rlang::last_trace()` to see where the error occurred.

This is because, having children fewer than 10 levels, gtsummary is treating it as a factor variable.

But at this point we should pass the variable to custom add_stat function as factor, not as numeric.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions