【问题标题】:How to pass paired argument to summaryM function test in Hmisc [r]如何将配对参数传递给 Hmisc [r] 中的 summaryM 函数测试
【发布时间】:2014-11-22 19:02:03
【问题描述】:

我这里有可重现的例子

library(Hmisc)
set.seed(173)
sex <- factor(sample(c("m","f"), 500, rep=TRUE))
country <- factor(sample(c('US', 'Canada'), 500, rep=TRUE))
age <- rnorm(500, 50, 5)
sbp <- rnorm(500, 120, 12)
label(sbp) <- 'Systolic BP'
units(sbp) <- 'mmHg'
treatment <- factor(rep(c("PreTretment","PostTretment"), 250))

f <- summaryM(age + sex + sbp ~ treatment, test=TRUE)

Hmisc 包中的 SummaryM 函数具有 test 参数,默认情况下将 Wilcoxon 测试应用于 continue 变量,假设它们是独立的。现在我想将paired=TRUE 传递给 Wilcoxon。我该怎么做? 谢谢

【问题讨论】:

  • 但是,但是,但是,……数据没有配对。他们怎么可能?他们有不同的计数。一旦你解决了这个问题,那么为什么不预先计算配对案例感兴趣的变量的差异,并且(也许)然后使用(n.b. untested notion)summaryM 给你配对测试(这实际上只是针对H0 = 0 表示差异。请参阅?wilcox.test中的详细信息部分
  • @BondedDust 我添加了简化版。你能评论一下这个吗?
  • 我尝试制作一个以 conTestkw 为模型的 conTest 对象,但我认为不应该将配对比较传递给 summaryM。我认为summary.formula 是进行配对测试的正确方法,我有一个工作示例。不幸的是,尽管构造了一个提供正确统计信息的方法,但由于 pval 的错误我无法追踪,它无法打印。
  • @BondedDust 感谢您的努力。 SummaryM 是旧的 summary.formula 的新版本,但只有反向方法。在这两个函数中,其余的都是相等的。我认为 Harrell 使用了某种内部 Wilcoxon 方法,它不是 wilcox.test stats 包的通用方法。
  • 嗯,这可能解释了 summaryM 文档中的一些明显错误。

标签: r hmisc


【解决方案1】:

我目前使用 Hmisc 汇总方法进行配对 Wilcoxon 检验的努力:

 conTestWP <- 
function (group, x) 
{
    st <- wilcox.test( x[as.numeric(group)==1], x[as.numeric(group)==2], paired=TRUE)
    list(P = st["p.value"], stat = st["statistic"], df = st[c("df1", "df2")], 
        testname = "Wilcoxon-paired",
        statname = "V", latexstat = "V", plotmathstat = "F[df]")}

summaryM 方法拆分其分组变量,因此不适用于配对测试。 summary.formula 方法集确实允许“反向”方法,其中连续变量位于公式的 RHS 上:

f <- summary.formula(treatment ~ sbp, data=dat, method="reverse", 
                      test=TRUE,conTest=conTestWP)

尝试打印 f 会引发错误(错误地声称 p 值不是数字)但您可以查看内部看到成对的 wilcox.test 结果已传递到对象中,并且与 if 相同你是“手工”完成的:

 str(f) # but did snip some of the output:

 $ testresults:List of 1
  ..$ sbp:List of 7
  .. ..$ P           :List of 1
  .. .. ..$ p.value: num 0.589
  .. ..$ stat        :List of 1
  .. .. ..$ NA: NULL
  .. ..$ df          :List of 2
  .. .. ..$ NA: NULL
  .. .. ..$ NA: NULL
  .. ..$ testname    : chr "Wilcoxon-paired"
  .. ..$ statname    : chr "V"
  .. ..$ latexstat   : chr "V"
  .. ..$ plotmathstat: chr "F[df]"

通过为“df”值输入硬编码数字来修复引发的错误的努力失败了。我没有成功跟踪我仅粘贴在顶部的回溯:

> f
Error in log10(ifelse(pv > 0, pv, 1e-50)) : 
  non-numeric argument to mathematical function
> traceback()
5: format.pval(pval, digits = pdig, eps = eps)
4: formatTestStats(tr, prtest = prtest, latex = latex, testUsed = testUsed, 
       pdig = pdig, eps = eps, footnoteTest = footnoteTest)
3: formatCons(stats[[i]], nam, tr, x$group.freq, prmsd, sep, formatArgs, 
       round, prtest, pdig = pdig, eps = eps)
2: print.summary.formula.reverse(list(stats = list(sbp = c(97.9191028465814, 
   94.9839938500064, 100.014783572809, 97.2881910017715, 107.288034416825, 
   105.746587052709, 111.864782483651, 112.689945667021, 116.229748640414, 
   115.604190135259, 119.743427097173, 119.276780441804, 123.380695706571, 
   122.111672516175, 128.138778071723, 126.592782661592, 133.726823015259, 
   132.141219449201, 140.847941698775, 136.762891898923, 145.175812916341, 
   141.635692905295, 120.013464038065, 118.994407752318, 12.1494617994813, 
   11.9252958974706)), type = 2, group.name = "treatment", group.label = "treatment", 

【讨论】:

    【解决方案2】:

    这对我有用:

    conTestWP <- 
    function (group, x) 
    {
        st <- wilcox.test( x[as.numeric(group)==1], x[as.numeric(group)==2], paired=TRUE)
        list(P=st$p.value, stat=st$statistic, df = "NA", 
        testname = "Wilcoxon-paired",
        statname = "V", namefun = "fstat", latexstat = "V", plotmathstat = "F[df]")}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      相关资源
      最近更新 更多