Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to calculate concordant and discordant pairs on nx2 tables.


|    Value1   |  Value2  |
|_____________|__________|
|    0.3434   | 1        |
|    0.2132   | 2        |
|    0.3      | 3.3245   |
|    0.6      | 0.12321  |
|    0.745234 | 523      |
|    4        | 0.2134   |
|    3        | 111      |
|    .        | .        |
|    .        | .        |
|    .        | .        |
|    .        | .        |
|    .        | .        |
|_____________|__________|

This is a n x 2 table. How can I calculate concordant and discordant pairs? I am sorry I can't find suitable question tag.

share|improve this question
2  
Are you sure your question is about Mathematica? –  Öskå Jun 23 at 16:08
    
I have a document about this problem, you can see [link]uregina.ca/~gingrich/gamma.pdf But this is not suitable for me... I can't understand. I found a lot of document about this topic, some of them calculate different the others. –  user3646666 Jun 24 at 5:43

2 Answers 2

SeedRandom[123];
data = 5 + RandomVariate[BinormalDistribution[.5], 20];

TableForm[data,TableHeadings->{Range@Length@data,{"value1", "value2"}}]//Style[#, 16] &

enter image description here

Concordant and discordant pairs:

From the documentation on KendallTau

enter image description here

Generalizing @eldo's post we can get the concordant and discordant pairs of observations as follows:

cdpairsF = With[{pairs = Tuples[#, 2]}, 
          Pick[pairs, Sign /@ (Times @@ Subtract @@ # & /@ pairs), #] & /@ {1, -1}] &;
{concordantpairs, discordantpairs} = cdpairsF[data];

Example:

MatrixPlot[
  Partition[Sign[Times @@ Subtract @@ #] & /@ Tuples[data, 2], Length[data]], 
  ColorRules -> {-1 -> Red, 1 -> Green, 0 -> Blue}, 
  Mesh -> True, MeshStyle -> Directive[White, Thick],
  PlotLegends -> SwatchLegend[{Red, Green}, {"concordant", "discordant"}]]

enter image description here

Example: Observations concordant/discordant with a reference observation:

SeedRandom[123];
data = 5 + RandomVariate[BinormalDistribution[.5], 100];
{concordantpairs, discordantpairs} = cdpairsF[data];
options = {PlotStyle -> {Blue, Green, Red}, BaseStyle -> PointSize[Large],
           Frame -> True, 
           FrameLabel -> {Style["value1", 14], Style["value2", 14], None, None},
           PlotRange -> {{0, 8}, {0, 8}}, AspectRatio -> 1, ImageSize -> 400, 
           ImagePadding -> 40};

Manipulate[With[{cp = Select[concordantpairs, #[[1]] == data[[k]] &],
                  dp = Select[discordantpairs, #[[1]] == data[[k]] &]},
  ListPlot[{data,
            If[# == {}, ## &[], #[[All, 2]]] &@cp,
            If[# == {}, ## &[], #[[All, 2]]] &@dp},
   GridLines -> List /@ data[[k]], options,
   PlotLegends ->
     PointLegend[{Blue, Green, Red},
            {Style[StringForm["data[[``]] = ``", k, Round[data[[k]], 0.01]], 
                  Blue, 16],
             Style[StringForm["`` points \n concordant with data[[``]]",Length[cp], k], 
                  Green, 16], 
             Style[StringForm["`` points \n discordant with data[[``]]",Length[dp], k],
                  Red, 16]}, 
            LegendMarkers -> Graphics[Disk[]]]]],
   {{k, 1}, 1, Length[data], 1}, ContentSize -> {750, 450}, Alignment -> Center]

enter image description here

Measures of Association:

With Version 9.0.1.0 we have the following measures of association:

KendallTau

SpearmanRho

BlomqvistBeta

GoodmanKruskalGamma

HoeffdingD

{#, # @@ Transpose@data} & /@ 
     {KendallTau, SpearmanRho, BlomqvistBeta, GoodmanKruskalGamma, HoeffdingD} 
             //  TableForm // Style[#, 20] &

enter image description here

share|improve this answer
    
I searched so much. But if the table 2xn I didn't find anything :( –  user3646666 Jun 23 at 16:57
    
@user3646666, hope the post and the links are useful. Welcome to mma.se. –  kguler Jun 23 at 17:00
    
I don't think so. Thanks. –  user3646666 Jun 23 at 17:03
    
@user3646666, please note that the data you posted is nx2 (n rows 2 columns); so is the data in my example. –  kguler Jun 23 at 17:13
    
ohh you are right , sorry. It has to be n rows and 2 columns.. nx2 table. I will edit the problem. –  user3646666 Jun 24 at 5:40
m = {{{1, 1}, {2, 4}}, {{1, 5}, {2, 4}}, {{1, 1}, {1, 1}}};

Times @@@ (Sign /@ Subtract @@@ m) /. (0 | 1) -> "Concordant" /. -1 -> "Discordant"

{"Concordant", "Discordant", "Concordant"}

share|improve this answer
    
I couldn't understand anything :( sorry. Can you explain this with mathematics ? –  user3646666 Jun 23 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.