Measures of association in R — Kendall's tau-b and tau-c

前端 未结 9 850

Are there any R packages for the calculation of Kendall\'s tau-b and tau-c, and their associated standard errors? My searches on Google and Rseek have turned up nothing, but su

相关标签:
9条回答
  • 2021-01-30 10:03

    Just to expand of Stedy's answer... cor(x,y,method="kendall") will give you the correlation, cor.test(x,y,method="kendall") will give you a p-value and CI.

    Also, take a look at the Kendall package, which provides a function which claims a better approximation.

    > library(Kendall)
    > Kendall(x,y)
    

    There is also the cor.matrix function in the Deducer package for nice printing:

    > library(Deducer)
    > cor.matrix(variables=d(mpg,hp,wt),,
    + data=mtcars,
    + test=cor.test,
    + method='kendall',
    + alternative="two.sided",exact=F)
    
                              Kendall's rank correlation tau                          
    
               mpg     hp      wt     
    mpg    cor 1       -0.7428 -0.7278
             N 32      32      32     
        stat**         -5.871  -5.798 
       p-value         0.0000  0.0000 
    ----------                        
     hp    cor -0.7428 1       0.6113 
             N 32      32      32     
        stat** -5.871          4.845  
       p-value 0.0000          0.0000 
    ----------                        
     wt    cor -0.7278 0.6113  1      
             N 32      32      32     
        stat** -5.798  4.845          
       p-value 0.0000  0.0000         
    ----------                        
        ** z
        HA: two.sided 
    
    0 讨论(0)
  • 2021-01-30 10:11

    According to this r-tutor page http://www.r-tutor.com/gpu-computing/correlation/kendall-tau-b tau-b is in fact computed by the base r function.

    0 讨论(0)
  • 2021-01-30 10:12

    Doug's answer here is incorrect. Package Kendall can be used to calculate Tau b.

    The Kendall package function Kendall (and it would also seem cor(x,y,method="kendall")) calculate ties using the formula for Tau-b. However, for vectors with ties, the Kendall package has the more correct p-value. See page 4 of the documentation for Kendall, from https://cran.r-project.org/web/packages/Kendall/Kendall.pdf page 4, with D referencing the denominator of the Kendall calculation:

    and D = n(n − 1)/2. S is called the score and D, the denominator, is the maximum possible value of S. When there are ties, the formula for D is more complicated (Kendall, 1974, Ch. 3) and this general forumla for ties in both reankings is implemented in our function.The p-value of tau under the null hypothesis of no association is computed by in the case of no ties using an exact algorithm given by Best and Gipps (1974). When ties are present, a normal approximation with continuity correction is used by taking S as normally distributed with mean zero and variance var(S), where var(S) is given byKendall (1976, eqn 4.4, p.55). Unless ties are very extensive and/or the data is very short, this approximation is adequate. If extensive ties are present then the bootstrap provides an expedient solution (Davis and Hinkley, 1997). Alternatively an exact method based on exhaustive enumeration is also available (Valz and Thompson, 1994) but this is not implemented in this package.

    I originally made an edit to Doug's answer regarding this, but it was rejected for 'being directed at the author and more appropriate as an answer or a comment'. I would have left this as a comment on the answer, but my reputation is not yet high enough to comment.

    0 讨论(0)
  • 2021-01-30 10:13

    Stumbled across this page today, as I was looking for an implementation of kendall tau-b in R
    For anyone else looking for the same thing:
    tau-b is in fact part of the stats package.

    See this link for more details: https://stat.ethz.ch/pipermail/r-help//2012-August/333656.html

    I tried it and it works: library(stats)

    x <- c(1,1,2)
    y<-c(1,2,3)
    cor.test(x, y, method = "kendall", alternative = "greater")
    

    this is the output:

    data:  x and y
    z = 1.2247, p-value = 0.1103
    alternative hypothesis: true tau is greater than 0
    sample estimates:
          tau 
    0.8164966 
    
    Warning message:
    In cor.test.default(x, y, method = "kendall", alternative = "greater") :
      Cannot compute exact p-value with ties
    

    Just ignore the warning messege. The tau is in fact tau b !!!

    0 讨论(0)
  • 2021-01-30 10:13

    There's a routine for Kendall's coefficient in psych package with corr.test(x, method = "kendall"). This function can be applied on data.frame, and also displays p-values for each pair of variables. I guess it displays tau-a coefficient. Only downside is that it's actually a wrapper for cor() function.

    Wikipedia has good reference on Kendall's coefficient, and check this link out. Try sos package and findFn() function. I got bunch of stuff when querying "tau a" and tau b, but both ended with no luck. And search results seem to merge to Kendall package, as @Ian suggested.

    0 讨论(0)
  • 2021-01-30 10:14

    Quite a while, but the 3 functions are implemented in DescTools.

    library(DescTools)
    # example in: 
    # http://support.sas.com/documentation/cdl/en/statugfreq/63124/PDF/default/statugfreq.pdf
    # pp. S. 1821
    tab <- as.table(rbind(c(26,26,23,18,9),c(6,7,9,14,23)))
    
    # tau-a
    KendallTauA(tab, conf.level=0.95)
    tau_a    lwr.ci    ups.ci 
    0.2068323 0.1771300 0.2365346 
    
    # tau-b
    KendallTauB(tab, conf.level=0.95)
        tau_b    lwr.ci    ups.ci 
    0.3372567 0.2114009 0.4631126 
    
    # tau-c
    > StuartTauC(tab, conf.level=0.95)
         tauc    lwr.ci    ups.ci 
    0.4110953 0.2546754 0.5675151 
    
    # alternative for tau-b:
    d.frm <- Untable(tab, dimnames = list(1:2, 1:5))
    cor(as.numeric(d.frm$Var1), as.numeric(d.frm$Var2),method="kendall")
    [1] 0.3372567
    
    # but no confidence intervalls for tau-b! Check:
    unclass(cor.test(as.numeric(d.frm$Var1), as.numeric(d.frm$Var2), method="kendall"))
    
    0 讨论(0)
提交回复
热议问题