如何在Hmisc [r]中将配对参数传递给summaryM函数测试

时间:2014-11-22 19:02:04

标签: r hmisc

我这里有可复制的例子

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函数具有测试参数,默认情况下将Wilcoxon测试应用于连续变量,假设它们是独立的。现在我想将paired=TRUE传递给威尔科克森。我该怎么做? 谢谢

2 个答案:

答案 0 :(得分: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", 

答案 1 :(得分: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]")}