MSKCC-Epi-Bio/bstfun

Feature request: Support for checkbox questions

svenhalvorson opened this issue · 8 comments

I'm frequently using data sets that have 'checkbox' style questions in which the respondent selects any number of some set of discrete options. The resulting data is then spread across multiple columns, one for each option, and will just have a 1 or NA for possible values.

The function gtsummary::tbl_summary treats these each as their own variable instead of listing a common heading for them. Here is my question from stackoverflow along with @ddsjoberg 's solution. This solution is okay but it's a bit tedious, especially if you have a lot of other variables in the data set.

Ideally, gtsummary::tbl_summary could be given a list of columns to treat as one question or a pattern to find them, and then apply this similar formatting.

Thank you! Love the library

`
tibble(

a = sample(c(1, NA), size = 10, replace = TRUE),

b = sample(c(1, NA), size = 10, replace = TRUE),

c = sample(c(1, NA), size = 10, replace = TRUE)

) %>%

mutate(across(everything(), ~replace_na(., 0L))) %>%

pivot_longer(everything()) %>%

filter(value == 1) %>%

select(name) %>%

gtsummary::tbl_summary()

`

Does this get you the desired results?

If you need it to be part of the longer table, you can always use the tbl_stack to append it to another table.

image

@svenhalvorson thank you for the post! I've transferred this issue to our bstfun package, which (among other purposes) serves as a sandbox for gtsummary features.

I was thinking the interface to the function will be similar to tbl_summary(), but the include argument controls the grouping of the variables.

df <-
  tibble::tibble(
    race_white = sample(c(T, F), 10, replace = TRUE),
    race_black = sample(c(T, F), 10, replace = TRUE),
    race_asian = sample(c(T, F), 10, replace = TRUE),
    
    
  )

tbl_check_all_that_apply(
  data = df, 
  include = list("Race" = c(race_white, race_black, race_asian))
)

We would require that the variables included be logical (to ensure they can be summarized on a single row).

Do you have any suggestions for the function name?

@kwakuduahc1 thanks for the suggestion! I think in this case, the percentages will not be correct after the pivoting unfortunately .

Do you have any suggestions for the function name?

tbl_checks

@ddsjoberg thanks for continuing with this.

As far as name goes, I think these kinds of variables are widely referred to as 'checkbox' questions and that is shorter than check_all_that_apply

A couple other thoughts:

  1. I have only minor experience with package development but would like to learn from ya'll
  2. I formatted the example with 1 and NA as the values because if respondents fill out none of the boxes, you can't infer whether none of the choices apply or that they just skipped it. I generally replace missing values with 0 in the case that at least one other box was checked. I think it would be reasonable to have the user pre-process it to binary or logical. If possible, the missing summary should report the proportion of observations for which no box was checked.
  3. The task that prompted this question is I'm trying to make a cohort description of a data set with many data types. Ideally, we would be able to pipe the output of gtsummary::tbl_summary into this function instead of having to stack the checkbox questions with the rest of the variables.
  1. I formatted the example with 1 and NA as the values because if respondents fill out none of the boxes, you can't infer whether none of the choices apply or that they just skipped it. I generally replace missing values with 0 in the case that at least one other box was checked. I think it would be reasonable to have the user pre-process it to binary or logical. If possible, the missing summary should report the proportion of observations for which no box was checked.

Great, we'll leave it up to the user to differentiate between uncheck and missing, by requiring logical class variables.

  1. The task that prompted this question is I'm trying to make a cohort description of a data set with many data types. Ideally, we would be able to pipe the output of gtsummary::tbl_summary into this function instead of having to stack the checkbox questions with the rest of the variables.

Unfortunately, I think you're going to be left with stacking the tbl_summary() results with the tbl_checkbox() results. Incorporating this special case into tbl_summary() would be tricky!

Hey hey @svenhalvorson

I added a draft of the function in this pull request #81

How familiar are you with GitHub and Pull Requests? Are you able to install the branch with the tbl_checkbox() function and test?

Pinging @svenhalvorson @kwakuduahc1 , can either of you review this pull request with the added function? #81