【问题标题】:error in add_p()' for variable X and test 'fisher.test', p-value omitted变量 X 和测试“fisher.test”的 add_p()' 中的错误,省略了 p 值
【发布时间】:2020-04-22 08:49:21
【问题描述】:

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

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 值吗?

    变量“gender”和测试“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”和测试“fisher.test”的“add_p()”中存在错误,省略了 p 值: stats::fisher.test(data[[variable]], as.factor(data[[by]])) 中的错误:FEXACT 错误 7(位置)。 LDSTP=18570 对于这个问题来说太小了, (pastp=36.4614, ipn_0:=ipoin[itp=58]=1, stp[ipn_0]=31.8106)。 增加工作空间或考虑使用 'simulate.p.value=TRUE'

【问题讨论】:

  • 嗨,Nelly,当您执行fisher.test(as.factor(miro_def$gender), as.factor(miro_def$mheim)) 时,您会得到什么?另外,你能告诉我们table(as.factor(miro_def$gender), as.factor(miro_def$mheim)) 的样子吗?我怀疑 fisher.test 可能无法运行,它可能不是 gtsummary 问题!
  • > fisher.test(miro_def$gender, miro_def$mheim) Error in fisher.test(miro_def$gender, miro_def$mheim) : FEXACT error 7(location). LDSTP=18540 is too small for this problem, (pastp=51.2364, ipn_0:=ipoin[itp=150]=215, stp[ipn_0]=40.6787). Increase workspace or consider using 'simulate.p.value=TRUE'
  • > table(as.factor(miro_def$gender), as.factor(miro_def$mheim)) Alc AlcNic Eff Her HerEff ImmNicAlc Msc Nic Nut Une female 9 72 37 23 2 25 17 51 0 24 male 49 193 39 24 3 43 10 52 6 32
  • 嗨,玛格丽特,如果您的团队无法解决此问题:(1) 在哪里或哪个论坛以及哪些标签 - 如果是这个论坛 - 您会建议我使用哪些标签来增加回复的机会? (2)假设我得到了一个解决方案,然后我如何将它移植到 gtsummary 以便我仍然可以获得漂亮的表格?谢谢
  • 嗨@Nelly!看起来@Margaret 诊断出了这个问题。您的数据与fisher.test() 有误,这就是为什么您没有在add_p() 中获得输出的原因。首先,寻找适合您数据的测试,然后在add_p() 中尝试该测试。编码快乐!

标签: r gtsummary


【解决方案1】:

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

## 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 参数更改为上面的fisher.test() 来修改计算模拟p 值的迭代次数。

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

【讨论】:

    【解决方案2】:

    既然它为我解决了这个问题,我想指出,由于gtsummary 的版本1.3.6add_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)。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题。您必须在add_p() 中使用test.args 来增加您的工作空间。

      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))
      

      【讨论】:

        猜你喜欢
        • 2019-02-03
        • 2019-04-02
        • 1970-01-01
        • 1970-01-01
        • 2018-09-19
        • 2023-02-02
        • 2013-10-10
        • 1970-01-01
        • 2021-12-15
        相关资源
        最近更新 更多