MSKCC-Epi-Bio / bstfun

Miscellaneous collection of functions
http://mskcc-epi-bio.github.io/bstfun
Other
34 stars 23 forks source link

add_n & add_continous_stat for Likert tables #62

Closed larmarange closed 2 years ago

larmarange commented 2 years ago

fix #55

larmarange commented 2 years ago

Hi @ddsjoberg

I hope you are well. I have rewritten add_n.tbl_likert() to use all functionalities available in add_n.tbl_summary.

I have also added a add_continuous_stat() method (name could be changed) to allow the addition of continuous statistics from a Likert table. You may apply add_continuous_stat() several times.

library(tidyverse)
library(bstfun)

data(efc, package = "sjPlot")

cop <- sjmisc::find_var(efc, pattern = "cop", out = "df") %>%
  sjmisc::numeric_to_factor() %>%
  mutate(across(everything(), ~ fct_relabel(.x, str_to_lower))) %>%
  as_tibble() %>%
  mutate(c82cop1 = labelled::recode_if(c82cop1, c82cop1 == "never", NA))
names(cop) <- paste0("q", 1:ncol(cop))

cop %>% 
  tbl_likert(statistic = ~ "{p}%", sort = "desc") %>%
  add_n(statistic = "{n}/{N}", footnote = TRUE) %>%
  add_continuous_stat() %>%
  add_continuous_stat(statistic = "{sd}") %>%
  as_kable()
Characteristic N always never often sometimes Mean SD
do you find caregiving too demanding? 902/908 4.3% 21% 14% 61% 3.31 0.94
do you feel trapped in your role as caregiver? 900/908 8.6% 37% 13% 42% 2.87 1.06
does caregiving have negative effect on your physical health? 898/908 6.5% 46% 9.5% 39% 2.80 1.03
do you feel supported by friends/neighbours? 901/908 12% 35% 27% 26% 2.67 1.00
do you feel you cope well as caregiver? 898/908 23% 0% 66% 11% 2.64 0.96
does caregiving cause difficulties in your relationship with your friends? 902/908 5.8% 57% 9.1% 28% 2.59 0.96
does caregiving cause difficulties in your relationship with your family? 902/908 1.7% 69% 5.5% 23% 2.51 0.87
do you feel caregiving worthwhile? 888/908 34% 8.6% 34% 24% 2.47 1.18
does caregiving cause financial difficulties? 900/908 1.9% 79% 4.3% 15% 2.32 0.74

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

larmarange commented 2 years ago

For a Likert table, I have the feeling that "{p}%" would be a better and more common default value for statistic.

larmarange commented 2 years ago

I have updated the example (levels order was incorrect). In this example, the last variable has voluntarily a missing level.

Should we think about an example dataset to provide for tbl_likert?

library(tidyverse)
library(bstfun)

data(efc, package = "sjPlot")

cop <- sjmisc::find_var(efc, pattern = "cop", out = "df") %>%
  sjmisc::numeric_to_factor() %>%
  mutate(across(everything(), ~ fct_relabel(.x, str_to_lower) %>% fct_relevel("never", "sometimes", "often", "always"))) %>%
  as_tibble() %>%
  mutate(c82cop1 = labelled::recode_if(c82cop1, c82cop1 == "never", NA) %>% fct_drop()) %>%
  relocate(c82cop1, .after = last_col())
names(cop) <- paste0("q", 1:ncol(cop))

tbl <- cop %>% 
  tbl_likert(statistic = ~ "{p}%", sort = "desc") %>%
  add_n(statistic = "{n}/{N}", footnote = TRUE) %>%
  add_continuous_stat() %>%
  add_continuous_stat(statistic = "{sd}") 
tbl %>%
  as_kable()
Characteristic N never sometimes often always Mean SD
do you feel you cope well as caregiver? 898/908 0% 11% 66% 23% 3.13 0.57
do you feel caregiving worthwhile? 888/908 8.6% 24% 34% 34% 2.93 0.96
do you feel supported by friends/neighbours? 901/908 35% 26% 27% 12% 2.16 1.04
do you find caregiving too demanding? 902/908 21% 61% 14% 4.3% 2.02 0.72
do you feel trapped in your role as caregiver? 900/908 37% 42% 13% 8.6% 1.92 0.91
does caregiving have negative effect on your physical health? 898/908 46% 39% 9.5% 6.5% 1.77 0.87
does caregiving cause difficulties in your relationship with your friends? 902/908 57% 28% 9.1% 5.8% 1.63 0.87
does caregiving cause difficulties in your relationship with your family? 902/908 69% 23% 5.5% 1.7% 1.39 0.67
does caregiving cause financial difficulties? 900/908 79% 15% 4.3% 1.9% 1.29 0.64

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

ddsjoberg commented 2 years ago

hey hey @larmarange ! hope you're well! Am I meant to do something with this, or are you working on it?

larmarange commented 2 years ago

Dear @ddsjoberg I'm truly sorry. I'm currently overloaded with my regular work and I forgot to finalise the PR. If you have time, do not hesitate to work on it / finalise it.

The code is working. In the discussion, there are some remaining questions:

Best regards

PS: another open question to be addressed in a separate PR: should we add a group option in tbl_likert() to produce sub-tables according to a factor and to stack them? If yes, that argument will have to be taken into account also in add_n() and add_continuous_stat()

ddsjoberg commented 2 years ago

@larmarange no worries, and on apology needed 😸

PS: another open question to be addressed in a separate PR: should we add a group option in tbl_likert() to produce sub-tables according to a factor and to stack them? If yes, that argument will have to be taken into account also in add_n() and add_continuous_stat()

Would this functionality be covered in tbl_strata()?

larmarange commented 2 years ago

Would this functionality be covered in tbl_strata()?

Yes it can be. Maybe a good idea to simply add an example with it.

larmarange commented 2 years ago

Let me know if you want to finalize the PR. Otherwise, I can try to find some time next week.

Regards

ddsjoberg commented 2 years ago

Let me know if you want to finalize the PR. Otherwise, I can try to find some time next week.

Regards

I won't have time in the next week either. Go ahead when you've got the time. Happy (American) Thanksgiving! 🦃 🦃 🦃

ddsjoberg commented 2 years ago

hey hey @larmarange ! I hope you've been well :)

I may have some time to work on this in the first or second week of April. FYI If you want to work on it, let me know and I won't touch it.

larmarange commented 2 years ago

Hey. :-)

I'm completely overloaded until mid-April. So do not hesitate to move forward. And thanks for all you are doing.

ddsjoberg commented 2 years ago

Hey hey @larmarange ! I think the PR is in a place that can be merged. But before that, I wanted to address some of your comments above:

  • For a Likert table, I have the feeling that "{p}%" would be a better and more common default value for statistic.

That sounds reasonable to me. We can make this update in a future PR?

  • Should we think about an example dataset to provide for tbl_likert?
  • We could/should provide more examples in documentation.

I expanded the example in tbl_likert(), improved the dummy dataset, and added links from the all the tbl_likert() family of functions to all the other functions in the family. If you'd like to add a data set, I think we can do that in the future anytime.

  • Unit tests are missing

Added!

larmarange commented 2 years ago

Thanks