如何用自己的函数在gtsummary中制作卡方检验的比值比和95%CI?

nhaq1z21  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(153)

我想用gtsummary创建这里显示的表格,你能告诉我吗?
table

i7uaboj4

i7uaboj41#

您可以通过传递给add_difference()的自定义函数添加比值比。有关如何构造自定义函数的说明请参见https://www.danieldsjoberg.com/gtsummary/reference/tests.html#custom-functions-1。
我在下面写了一个例子。但是请仔细注意fisher.test()是如何从2x2表中构造比值比的。您可能需要切换table()中变量的顺序,甚至颠倒变量水平的顺序,以获得所需的比值比。

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.0'

# function to calculate OR from 2x2 table
or_fun <- function(data, variable, by, ...) {
  table(data[[by]], data[[variable]]) %>%
    fisher.test() %>%
    broom::tidy()
}
# testing function
or_fun(trial, "trt", "response")
#> # A tibble: 1 × 6
#>   estimate p.value conf.low conf.high method                         alternative
#>      <dbl>   <dbl>    <dbl>     <dbl> <chr>                          <chr>      
#> 1     1.21   0.540    0.633      2.34 Fisher's Exact Test for Count… two.sided

# add OR to a `tbl_summary()`
tbl <-
  trial %>%
  select(response, trt) %>%
  tbl_summary(by = trt, missing = "no", statistic = all_categorical() ~ "{n} / {N} ({p}%)") %>%
  add_difference(test = response ~ or_fun, estimate_fun = response ~ style_ratio) %>%
  modify_header(estimate ~ "**Odds Ratio**")

x1c 0d1x由reprex package(v2.0.1)于2022-05-08创建

相关问题