访问wilcox_test结果中的一个槽

时间:2015-01-11 15:48:55

标签: r slot

我正在对某些数据进行测试:

wilcox_test(y ~ x, distribution="exact", conf.int=TRUE)
#        Exact Wilcoxon Mann-Whitney Rank Sum Test
#
#    data:  y by x (1, 2)
#    Z = 1.8732, p-value = 0.06106
#    alternative hypothesis: true mu is not equal to 0
#    95 percent confidence interval:
#     0 2
#    sample estimates:
#    difference in location 
#                         1 

difference in location下显示的数字是Hodges-Lehmann估算。我想将它分配给变量以进行进一步计算。但我不知道:

如何访问"位置差异"?

的值

我看了str(wilcox_test(y ~ x, distribution="exact", conf.int=TRUE)),它给了我:

Formal class 'ScalarIndependenceTestConfint' [package "coin"] with 7 slots
  ..@ confint     :function (level)  
  ..@ conf.level  : num 0.95
  ..@ nullvalue   : num 0
  ..@ distribution:Formal class 'ExactNullDistribution' [package "coin"] with 7 slots
  .. .. ..@ q         :function (p)  
  .. .. ..@ d         :function (x)  
  .. .. ..@ support   :function ()  
  .. .. ..@ parameters: list()
  .. .. ..@ pvalue    :function (q)  
  .. .. ..@ p         :function (q)  
  .. .. ..@ name      : chr "exact distribution (via Streitberg-Roehmel algorithm)"
  ..@ statistic   :Formal class 'ScalarIndependenceTestStatistic' [package "coin"] with 14 slots
  .. .. ..@ alternative                : chr "two.sided"
  .. .. ..@ teststatistic              : Named num 1.87
  .. .. .. ..- attr(*, "names")= chr "1"
  .. .. ..@ standardizedlinearstatistic: Named num 1.87
  .. .. .. ..- attr(*, "names")= chr "1"
  .. .. ..@ linearstatistic            : num 4246
  .. .. ..@ expectation                : Named num 3875
  .. .. .. ..- attr(*, "names")= chr "1"
  .. .. ..@ covariance                 :Formal class 'Variance' [package "coin"] with 1 slot
  .. .. .. .. ..@ variance: Named num 39334
  .. .. .. .. .. ..- attr(*, "names")= chr "1"
  .. .. ..@ xtrans                     : num [1:124, 1] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:124] "1" "2" "3" "4" ...
  .. .. .. .. ..$ : chr "1"
  .. .. .. ..- attr(*, "assign")= int 1
  .. .. ..@ ytrans                     : num [1:124, 1] 114.5 93.5 9 50 114.5 ...
  .. .. .. ..- attr(*, "assign")= int 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : NULL
  .. .. .. .. ..$ : chr ""
  .. .. ..@ xtrafo                     :function (data, numeric_trafo = id_trafo, factor_trafo = f_trafo, ordered_trafo = of_trafo, 
    surv_trafo = logrank_trafo, var_trafo = NULL, block = NULL)  
  .. .. ..@ ytrafo                     :function (data)  
  .. .. ..@ x                          :'data.frame':   124 obs. of  1 variable:
  .. .. .. ..$ x: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
  .. .. ..@ y                          :'data.frame':   124 obs. of  1 variable:
  .. .. .. ..$ y: num [1:124] 7 5 0 2 7 1 0 9 1 7 ...
  .. .. ..@ block                      : Factor w/ 1 level "0": 1 1 1 1 1 1 1 1 1 1 ...
  .. .. ..@ weights                    : num [1:124] 1 1 1 1 1 1 1 1 1 1 ...
  ..@ estimates   : list()
  ..@ method      : chr "Wilcoxon Mann-Whitney Rank Sum Test"

wilcox_test(...)@estimates只返回:

list()

显示为空。

那么在结果中1下列出的difference in location存储在哪里???


补充说明:

这有效:

hle <- wilcox.test(x, y, paired = TRUE, correct = FALSE, conf.int = TRUE)$estimate

但这是一个不同的测试,不适合我的数据。

1 个答案:

答案 0 :(得分:1)

由于difference in location值嵌套了几个级别,我不得不做一些挖掘来解决这个问题,但是我会使用帮助中提供的示例对象引导您完成它wilcox_test的文件 - 对象名为wt,我将在下面包含它。对于这样的事情,我通常首先检查感兴趣的类的show方法 -

R> class(wt)
[1] "ScalarIndependenceTestConfint"
attr(,"package")
[1] "coin"
R> getMethod("show","ScalarIndependenceTestConfint")
Method Definition:

function (object) 
{
    x <- object
    stat <- x@statistic@teststatistic
    names(stat) <- "Z"
    dist <- x@distribution
    cld <- class(dist)
    attributes(cld) <- NULL
    distname <- switch(cld, AsymptNullDistribution = "Asymptotic", 
        ApproxNullDistribution = "Approximative", ExactNullDistribution = "Exact")
    dataname <- varnames(x@statistic)
    ci <- confint(object, level = object@conf.level)
    RET <- list(statistic = stat, p.value = x@distribution@pvalue(stat), 
        alternative = x@statistic@alternative, method = paste(distname, 
            x@method), data.name = dataname, conf.int = ci$conf.int, 
        estimate = ci$estimate)
    if (length(x@nullvalue)) 
        RET$null.value = c(mu = x@nullvalue)
    if (length(x@estimates)) 
        RET <- c(RET, x@estimates)
    class(RET) <- "htest"
    print(RET)
    invisible(RET)
}
<environment: namespace:coin>

感兴趣的广告系列是RET列表中的最后一项 - estimate = ci$estimate,上面计算为ci <- confint(object, level = object@conf.level)。因此,使用示例对象wt.ci <- confint(wt, level=.95),您可以执行

R> wt.ci$estimate
difference in location 
                -0.305 

匹配

R> wt

    Exact Wilcoxon Mann-Whitney Rank Sum Test

data:  pd by age (12-26 Weeks, At term)
Z = -1.2247, p-value = 0.2544
alternative hypothesis: true mu is not equal to 0
95 percent confidence interval:
 -0.76  0.15
sample estimates:
difference in location 
                -0.305 

来自帮助文件?wilcox_test()

water_transfer <- data.frame(
  pd = c(0.80, 0.83, 1.89, 1.04, 
         1.45, 1.38, 1.91, 1.64, 0.73, 1.46,
         1.15, 0.88, 0.90, 0.74, 1.21),
  age = factor(c(rep("At term", 10), rep("12-26 Weeks", 5))))
##
wt <- wilcox_test(pd ~ age, data = water_transfer, 
                  distribution = "exact", conf.int = TRUE)