Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should logical covariates be treated? #125

Closed
ddsjoberg opened this issue Oct 25, 2021 · 4 comments · Fixed by #127
Closed

How should logical covariates be treated? #125

ddsjoberg opened this issue Oct 25, 2021 · 4 comments · Fixed by #127

Comments

@ddsjoberg
Copy link
Collaborator

ddsjoberg commented Oct 25, 2021

Hello!

It seems that model covariates of class 'logical' are treated as a hybrid between a continuous variable and a categorical. When a header row is added, they are only added to categorical variables (including logicals). But for logical covariates, we don't add the reference row, like we do for the rest of categorical covariates. To be consistent, do you think the reference group should be added?

lm(age ~ response + marker, 
  data = gtsummary::trial |> dplyr::mutate(response = as.logical(response))) |>
  broom.helpers::tidy_plus_plus(add_header_rows = TRUE) |>
  dplyr::select(term, variable, var_type, var_label, header_row, label, estimate, p.value)
#> # A tibble: 3 x 8
#>   term         variable var_type    var_label  header_row label estimate p.value
#>   <chr>        <chr>    <chr>       <chr>      <lgl>      <chr>    <dbl>   <dbl>
#> 1 <NA>         response categorical response   TRUE       resp~  NA       NA    
#> 2 responseTRUE response categorical response   FALSE      resp~   3.85     0.111
#> 3 marker       marker   continuous  Marker Le~ NA         Mark~   0.0339   0.979

Created on 2021-10-24 by the reprex package (v2.0.1)

@larmarange
Copy link
Owner

Dear @ddsjoberg Thank you for your feedback.

I see here two different issues:

  • Logical variables are not properly detected as dichotomous variables and should be treated in the same way that a factor with two levels. Therefore, if no_reference_row = NULL, a reference row should be added.

I will explore it

  • For categorical variables, even if we display only one row (because no reference row is added), so far we systematically add a header row (see below). Maybe we could think of an option in tidy_add_header_rows() for not adding a header row if a categorical variable is displayed only on one row. However, should we update the label in such case? Because the label contains only the value of the level and not the name of the variable
library(broom.helpers)
lm(age ~ response + marker, 
   data = gtsummary::trial %>% dplyr::mutate(response = factor(response))) %>%
  tidy_plus_plus(add_header_rows = TRUE) %>%
  dplyr::select(term, variable, var_type, var_label, header_row, label, estimate, p.value)
#> # A tibble: 4 x 8
#>   term      variable var_type    var_label   header_row label   estimate p.value
#>   <chr>     <chr>    <chr>       <chr>       <lgl>      <chr>      <dbl>   <dbl>
#> 1 <NA>      response dichotomous response    TRUE       respon~  NA       NA    
#> 2 response0 response dichotomous response    FALSE      0         0       NA    
#> 3 response1 response dichotomous response    FALSE      1         3.85     0.111
#> 4 marker    marker   continuous  Marker Lev~ NA         Marker~   0.0339   0.979
lm(age ~ response + marker, 
   data = gtsummary::trial %>% dplyr::mutate(response = factor(response))) %>%
  tidy_plus_plus(add_reference_rows = FALSE, add_header_rows = TRUE) %>%
  dplyr::select(term, variable, var_type, var_label, header_row, label, estimate, p.value)
#> # A tibble: 3 x 8
#>   term      variable var_type    var_label   header_row label   estimate p.value
#>   <chr>     <chr>    <chr>       <chr>       <lgl>      <chr>      <dbl>   <dbl>
#> 1 <NA>      response dichotomous response    TRUE       respon~  NA       NA    
#> 2 response1 response dichotomous response    FALSE      1         3.85     0.111
#> 3 marker    marker   continuous  Marker Lev~ NA         Marker~   0.0339   0.979

Created on 2021-10-25 by the reprex package (v2.0.1)

@larmarange
Copy link
Owner

#127 should do the first fix. A logical variable will be handled as a factor with two levels.

lm(age ~ response + marker, 
   data = gtsummary::trial |> dplyr::mutate(response = as.logical(response))) |>
  broom.helpers::tidy_plus_plus(add_header_rows = TRUE) |>
  dplyr::select(term, variable, var_type, var_label, header_row, label, estimate, p.value)
#> # A tibble: 4 x 8
#>   term          variable var_type  var_label  header_row label  estimate p.value
#>   <chr>         <chr>    <chr>     <chr>      <lgl>      <chr>     <dbl>   <dbl>
#> 1 <NA>          response dichotom~ response   TRUE       respo~  NA       NA    
#> 2 responseFALSE response dichotom~ response   FALSE      FALSE    0       NA    
#> 3 responseTRUE  response dichotom~ response   FALSE      TRUE     3.85     0.111
#> 4 marker        marker   continuo~ Marker Le~ NA         Marke~   0.0339   0.979

Created on 2021-10-25 by the reprex package (v2.0.1)

@larmarange
Copy link
Owner

Regarding the second point, we already have an option show_single_row for not adding an header row for dichotomous variables (and updating the label in such case).

library(broom.helpers)
lm(age ~ response + marker, 
   data = gtsummary::trial |> dplyr::mutate(response = as.logical(response))) |>
  tidy_plus_plus(add_header_rows = TRUE, show_single_row = all_dichotomous()) |>
  dplyr::select(term, variable, var_type, var_label, header_row, label, estimate, p.value)
#> # A tibble: 2 x 8
#>   term         variable var_type    var_label  header_row label estimate p.value
#>   <chr>        <chr>    <chr>       <chr>      <lgl>      <chr>    <dbl>   <dbl>
#> 1 responseTRUE response dichotomous response   NA         resp~   3.85     0.111
#> 2 marker       marker   continuous  Marker Le~ NA         Mark~   0.0339   0.979

Created on 2021-10-25 by the reprex package (v2.0.1)

@ddsjoberg
Copy link
Collaborator Author

Beautiful ! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants