Search code examples
rr-s4

R: get value of a slot from S4 object(ScalarIndependenceTest)


I'm not an advanced user of R.Anyway I just want to do a Wilcox test on two datasets:

library(coin)
GroupA = c(2,4,3,1,2,3,3,2,3,1)
GroupB = c(3,5,4,2,4,3,5,5,3,2)
g = factor(c(rep("GroupA", length(GroupA)), rep("GroupB", length(GroupB))))
v = c(GroupA, GroupB)
ss = wilcox_test(v ~ g, distribution="exact")

What I want is to get the Zvalue and compute the effect size. ss is a S4 object, when I print it I see it reports a Z value, but I can not find it in ss@statistic. The only slot that has the value which I expect is 'test statistic', but even if this is the z-value of the test when I do :

slot(ss, 'teststatistic')

I get the error :

no slot of name "teststatistic" for this object of class "ScalarIndependenceTest"

Can anyone please give me a hint?Thanks


Solution

  • The print equivalent for S4 classes is the show method, which can be inspected using getMethod. In this case,

    ss
    #
    #   Exact Wilcoxon-Mann-Whitney Test
    #
    #data:  v by g (GroupA, GroupB)
    #Z = -2.1095, p-value = 0.0385
    #alternative hypothesis: true mu is not equal to 0
    
    getMethod("show","ScalarIndependenceTest")
    #Method Definition:
    #
    # function (object) 
    # {
    #     distname <- switch(class(object@distribution), AsymptNullDistribution = "Asymptotic", 
    #         ApproxNullDistribution = "Approximative", ExactNullDistribution = "Exact")
    #     RET <- list(statistic = setNames(object@statistic@teststatistic, 
    #         nm = "Z"), p.value = object@distribution@pvalue(object@statistic@teststatistic), 
    #         alternative = object@statistic@alternative, data.name = varnames(object@statistic), 
    #         method = paste(distname, object@method))
    #...
    #...
    # }
    
    ss@statistic@teststatistic
    #   GroupA 
    #-2.109531 
    
    ss@distribution@pvalue(ss@statistic@teststatistic)
    #[1] 0.03850484