如何从gtsummary中的tbl_custom_summary访问当前的按类别分类?

huangapple go评论99阅读模式
英文:

How to access the current by-category in a tbl_custom_summary from gtsummary?

问题

我想创建一个定制的gtsummary表格,在这个表格中,我需要访问由“by”分组的组中的病例数量。

例如,我想计算按治疗分组的每个阶段的患者响应比例。
目前,我使用以下方法来访问治疗组:

  1. temp_drugname = pull(data %>% select(trt) %>% distinct())

只有在治疗组中至少有一个患者具有当前阶段时,这才有效。
如果有一个阶段没有分配给任何患者,我找不到计算正确总数的方法。

在下面的示例中,我删除了组A中患有1期癌症的病例。总数应为21,而不是54,以便稍后正确计算置信区间。

如何从gtsummary中的tbl_custom_summary访问当前的按类别分类?

  1. library(gtsummary)
  2. library(tidyverse)
  3. my_ratio_summary = function (numerator, na.rm = TRUE, conf.level = 0.95)
  4. {
  5. function(data, full_data, ...) {
  6. temp_drugname = pull(data %>% select(trt) %>% distinct())
  7. if(length(temp_drugname) > 0){
  8. druggroup_data = full_data %>% filter(trt == temp_drugname)
  9. }
  10. else {
  11. druggroup_data = full_data
  12. }
  13. num <- sum(data[[numerator]], na.rm = na.rm)
  14. denom <- sum(druggroup_data[[numerator]], na.rm = na.rm)
  15. ratio <- num/denom
  16. dplyr::tibble(num = num, denom = denom, ratio = ratio)
  17. }
  18. }
  19. trial %>%
  20. filter(!(trt == 'Drug A' & stage == 'T1')) %>%
  21. tbl_custom_summary(
  22. include = c("stage"),
  23. by = "trt",
  24. stat_fns = ~ my_ratio_summary("response"),
  25. statistic = ~ "{ratio}%, {num}/{denom}",
  26. digits = ~ c(style_percent, 0, 0)
  27. )
  1. <details>
  2. <summary>英文:</summary>
  3. I would like to create a customized gtsummary table, where I have to access the number of cases in that group resulting by `by`.
  4. For example, I would like to calculate the proportion of patients with a response per stage grouped by treatment.
  5. Currently I use this workaround, to access the treatment group:
  6. temp_drugname = pull(data%&gt;%select(trt)%&gt;%distinct())
  7. This works only, if there is at least one patient with the current stage in the treatment group.
  8. If there is a stage without patients assigned to it, I can&#39;t find a way to calculate the correct total.
  9. In the example below, I deleted the cases in group A with stage 1 cancer. The total should be 21, not 54, in order to later-on calculate confidence intervals correctly.
  10. [![example with wrong total][1]][1]

library(gtsummary)
library(tidyverse)

my_ratio_summary = function (numerator, na.rm = TRUE, conf.level = 0.95)
{
function(data, full_data, ...) {
temp_drugname = pull(data%>%select(trt)%>%distinct())
if(length(temp_drugname) > 0){
druggroup_data = full_data%>%filter(trt == temp_drugname)
}
else {
druggroup_data = full_data
}
num <- sum(data[[numerator]], na.rm = na.rm)
denom <- sum(druggroup_data[[numerator]], na.rm = na.rm)
ratio <- num/denom

  1. dplyr::tibble(num = num, denom = denom, ratio = ratio)

}
}

trial %>%
filter(!(trt == 'Drug A' & stage == 'T1'))%>%
tbl_custom_summary(
include = c("stage"),
by = "trt",
stat_fns = ~ my_ratio_summary("response"),
statistic = ~"{ratio}%, {num}/{denom}",
digits = ~ c(style_percent, 0, 0)
)

  1. [1]: https://i.stack.imgur.com/EUcHX.jpg
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 一个选择是使用通过`...`传递给自定义函数的附加参数,例如第一个元素是一个包含有关当前分组信息的`tibble`,可以用于筛选`full_data`,我使用了`semi_join`。即使`data`不包含任何观察值,这也可以正常工作。
  6. 在下面的代码中,我添加了一个`print`语句来显示`...`的内容:
  7. ```r
  8. library(gtsummary)
  9. library(tidyverse)
  10. my_ratio_summary <- function(numerator, na.rm = TRUE, conf.level = 0.95) {
  11. function(data, full_data, ...) {
  12. dots <- list(...)
  13. # 仅添加以打印`dots`的内容
  14. print(dots)
  15. druggroup_data <- full_data %>% semi_join(dots[[1]], by = dots$by)
  16. num <- sum(data[[numerator]], na.rm = na.rm)
  17. denom <- sum(druggroup_data[[numerator]], na.rm = na.rm)
  18. ratio <- num / denom
  19. dplyr::tibble(num = num, denom = denom, ratio = ratio)
  20. }
  21. }
  22. trial %>%
  23. filter(!(trt == "Drug A" & stage == "T1")) %>%
  24. tbl_custom_summary(
  25. include = c("stage"),
  26. by = "trt",
  27. stat_fns = ~ my_ratio_summary("response"),
  28. statistic = ~"{ratio}%, {num}/{denom}",
  29. digits = ~ c(style_percent, 0, 0)
  30. )

如何从gtsummary中的tbl_custom_summary访问当前的按类别分类?

  1. [1]: https://i.stack.imgur.com/yzP0t.png
  2. <details>
  3. <summary>英文:</summary>
  4. One option would be to use the additional arguments passed to the custom function via `...`, e.g. the first element is a `tibble` which contains the info about the current groups which can be used to filter `full_data` for which I use a `semi_join`. This works even if `data` does not contain any observations.
  5. In the code below I added a `print` statement to show the content of `...`:
  6. ``` r
  7. library(gtsummary)
  8. library(tidyverse)
  9. my_ratio_summary &lt;- function(numerator, na.rm = TRUE, conf.level = 0.95) {
  10. function(data, full_data, ...) {
  11. dots &lt;- list(...)
  12. # Only added to print the content of `dots`
  13. print(dots)
  14. druggroup_data &lt;- full_data %&gt;% semi_join(dots[[1]], by = dots$by)
  15. num &lt;- sum(data[[numerator]], na.rm = na.rm)
  16. denom &lt;- sum(druggroup_data[[numerator]], na.rm = na.rm)
  17. ratio &lt;- num / denom
  18. dplyr::tibble(num = num, denom = denom, ratio = ratio)
  19. }
  20. }
  21. trial %&gt;%
  22. filter(!(trt == &quot;Drug A&quot; &amp; stage == &quot;T1&quot;)) %&gt;%
  23. tbl_custom_summary(
  24. include = c(&quot;stage&quot;),
  25. by = &quot;trt&quot;,
  26. stat_fns = ~ my_ratio_summary(&quot;response&quot;),
  27. statistic = ~&quot;{ratio}%, {num}/{denom}&quot;,
  28. digits = ~ c(style_percent, 0, 0)
  29. )
  30. #&gt; [[1]]
  31. #&gt; # A tibble: 1 &#215; 2
  32. #&gt; trt stage
  33. #&gt; &lt;chr&gt; &lt;fct&gt;
  34. #&gt; 1 Drug A T1
  35. #&gt;
  36. #&gt; $variable
  37. #&gt; [1] &quot;stage&quot;
  38. #&gt;
  39. #&gt; $by
  40. #&gt; [1] &quot;trt&quot;
  41. #&gt;
  42. #&gt; $type
  43. #&gt; [1] &quot;categorical&quot;
  44. #&gt;
  45. #&gt; $stat_display
  46. #&gt; [1] &quot;{ratio}%, {num}/{denom}&quot;
  47. ...

如何从gtsummary中的tbl_custom_summary访问当前的按类别分类?

huangapple
  • 本文由 发表于 2023年7月11日 00:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定