0

我在这里有可重复的例子

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=TRUEWilcoxon。我该怎么做?谢谢

4

2 回答 2

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结果已传递到对象中,并且与您“手动”完成它们的结果相同:

 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", 
于 2014-11-22T22:58:46.293 回答
0

这对我行得通:

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]")}
于 2017-03-20T16:01:43.577 回答