Question
我正在使用一个 gsummary 表,其中包括平均值和频率的统计测试。我想将效果大小包含在表格中并遇到这篇文章展示了如何进行 Wilcoxon 测试效果大小 https://stackoverflow.com/questions/67262846/how-to-add-the-effect-size-in-the-summary-table-using-r-package-gtsummary。该代码也适用于 Cohen's d,但我正在努力修改它以适用于 Cramer's V。 看来cramer_v() 函数需要调用现成的列联表,而不是使用原始数据。有什么方法可以计算并将 Cramer's V 包含在 gtsummary 表中的卡方中吗?
Example
library(tidyverse)
library(rstatix)
library(gtsummary)
theme_gtsummary_mean_sd()
my_ES_test <- function(data, variable, by, ...) {
rstatix::cohens_d(data, as.formula(glue::glue("{variable} ~ {by}")))$effsize
}
gtTable <- mtcars %>%
select(hp, vs, am) %>%
tbl_summary(by = vs) %>%
add_p() %>%
add_stat(fns = all_continuous() ~ my_ES_test) %>%
modify_header(add_stat_1 ~ "Effect size")
print(gtTable)
contTable <- table(mtcars$vs, mtcars$am)
chisq.test(contTable, correct = FALSE)
#>
#> Pearson's Chi-squared test
#>
#> data: contTable
#> X-squared = 0.90688, df = 1, p-value = 0.3409
cramer_v(contTable)
#> [1] 0.1042136
我在输出表中添加了一个包含 Cramer's V 的示例。快乐编程!
library(gtsummary)
my_ES_test <- function(data, variable, by, ...) {
rstatix::cohens_d(data, as.formula(glue::glue("{variable} ~ {by}")))$effsize
}
my_cramer_v <- function(data, variable, by, ...) {
table(data[[variable]], data[[by]]) %>%
rstatix::cramer_v()
}
gtTable <-
mtcars %>%
select(hp, vs, am) %>%
tbl_summary(by = vs) %>%
add_p() %>%
add_stat(
fns = list(all_continuous() ~ my_ES_test,
all_categorical() ~ my_cramer_v)) %>%
modify_header(add_stat_1 ~ "**Effect size**")
Created on 2021-09-07 by the reprex package https://reprex.tidyverse.org (v2.0.1)
仅供参考,有对 Cohen's D 的内置支持,因此您无需创建自定义方法。您可以在此处查看完整列表https://www.danieldsjoberg.com/gtsummary/reference/tests.html https://www.danieldsjoberg.com/gtsummary/reference/tests.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)