0

当我尝试使用 add_p() 函数获取我的 by 变量(具有 10 个级别)和具有两个级别(是/否)的分类变量之间差异的 p 值时,出现以下错误。我不确定如何提供可重现的示例。根据试验数据,我想我的 by变量将是具有 10 个级别的“T 阶段”变量,分类变量将是:(1)具有 2 个级别的“化疗治疗”,以及(2)具有 4 个级别的“化疗治疗 2”水平。但这是我运行的代码。

library(gtsummary)
library(tidyverse)
miro_def %>% 
  select(mheim, age_dx, time_t1d_yrs, gender, collard, fhist_pandz) %>% 
  tbl_summary(by = mheim, missing = "no",
              type = list(c(gender, collard, fhist_pandz, mheim) ~ "categorical"),
              label = list(gender ~ "Gender", 
                           fhist_pandz ~ "Family history of PD", 
                           age_dx ~ "Age at diagnosis", 
                           time_t1d_yrs ~ "Follow-up(years)")) %>% 
  add_p() %>% 
  # style the output with custom header 
  #modify_header(stat_by = "{level}") %>% 
  # convert to kableExtra as_kable_extra(booktabs = TRUE) %>% 
  # reduce font size to make table fit. # you may also use the `latex_options = "scale_down"` argument here. 
  kable_styling(font_size = 7, latex_options = "scale_down")

但是,我确实通过变量(10 个级别)和其他变量(连续/数字)得到了一个 p 值

  1. 我该如何解决这个错误?
  2. 在我有提到的多级变量和多级(> 2级)分类变量的情况下,我应该做一些特别的事情来获得p值吗?

    变量“性别”和测试“fisher.test”的“add_p()”中存在错误,省略了 p 值:stats::fisher.test(data[[variable]], as.factor(data[ [by]])):FEXACT 错误 7(位置)。LDSTP=18540 对于这个问题来说太小了,(pastp=51.2364, ipn_0:=ipoin[itp=150]=215, stp[ipn_0]=40.6787)。增加工作空间或考虑使用“simulate.p.value=TRUE”变量“collard”和测试“fisher.test”的“add_p()”中有错误,省略了 p 值:stats::fisher.test 中的错误(data[[variable]], as.factor(data[[by]])):FEXACT 错误 7(位置)。LDSTP=18570 对于这个问题来说太小了,(pastp=37.0199, ipn_0:=ipoin[itp=211]=823, stp[ipn_0]=23.0304)。增加工作空间或考虑使用“simulate.p.value=TRUE”变量“fhist_pandz”的“add_p()”有错误 并测试“fisher.test”,省略 p 值:stats::fisher.test(data[[variable]], as.factor(data[[by]])) 中的错误:FEXACT 错误 7(location)。LDSTP=18570 对于这个问题来说太小了,(pastp=36.4614, ipn_0:=ipoin[itp=58]=1, stp[ipn_0]=31.8106)。增加工作空间或考虑使用 'simulate.p.value=TRUE'

4

3 回答 3

2

由于没有人发布答案,这就是我遇到这个时使用的。按照帮助文件中给出的示例,我编写了一个使用该选项?gtsummary::add_p.tbl_summary运行的自定义函数:fisher.testsimulate.p.values = TRUE

## define custom test
fisher.test.simulate.p.values <- function(data, variable, by, ...) {
  result <- list()
  test_results <- stats::fisher.test(data[[variable]], data[[by]], simulate.p.value = TRUE)
  result$p <- test_results$p.value
  result$test <- test_results$method
  result
}

## add p-values to your gtsummary table, using custom test defined above
summary_table %>%
add_p(
  test = list(all_categorical() ~ "fisher.test.simulate.p.values")  # this applies the custom test to all categorical variables
) 

B = 2000您还可以通过将默认参数更改为上述来修改计算模拟 p 值的迭代次数fisher.test()

当然,所有这些都假设首先使用 Fisher 检验是合适的。

于 2020-10-22T14:47:40.557 回答
2

因为它为我解决了这个问题,所以我想指出,因为版本1.3.6gtsummary一个选项add_p(),您可以在其中指定测试函数的参数(即test.args)。感谢开发人员为此!

来自NEWS:现在
每种add_p()方法都有test.args = argument. 使用此参数将附加参数传递给统计方法,例如

add_p(test = c(age, marker) ~ "t.test",
      test.args = c(age, marker) ~ list(var.equal = TRUE))

帮助中也有说明add_p()(即?add_p)。

于 2021-02-02T14:21:15.553 回答
1

我有一个类似的问题。您必须使用test.argswithin增加您的工作空间add_p()

miro_def %>% 
  select(mheim, age_dx, time_t1d_yrs, gender, collard, fhist_pandz) %>% 
  tbl_summary(by = mheim, missing = "no",
              type = list(c(gender, collard, fhist_pandz, mheim) ~ "categorical"),
              label = list(gender ~ "Gender", 
                           fhist_pandz ~ "Family history of PD", 
                           age_dx ~ "Age at diagnosis", 
                           time_t1d_yrs ~ "Follow-up(years)")) %>% 
  add_p(test.args = variable_with_no_pval ~ list(workspace=2e9))

或者

add_p(test.args = all_test("fisher.test") ~ list(workspace=2e9))
于 2021-08-06T21:32:29.977 回答