[R] Goodman / Kruskal gamma

Jonathan Baron baron at cattell.psych.upenn.edu
Tue Mar 11 13:06:08 CET 2003

The Goodman/Kruskal gamma is a nice descriptive rank-order
correlation statistic, often used in psychology.  It is nice
because it is easy to understand.  It takes all pairs of values
of each variable and asks whether they are congruent (S+ is the
number in the same order for both variables) or discordant (S-,
opposite ranking).  The statistic is (S+ - S-)/(S+ + S-).  It is
like tau except for the denominator.  (And the significance test
is the same as the test for tau, in cor.test.)

In trying to find the gamma statistic in R, I found one version
as part of lrm in the Design library and another as part of
rcorr.cens in the Hmisc library.  The former won't compute if the
model does not converge.  The latter is not really gamma, because
it discards ties in only one of the two variables.

So here is a little function to compute gamma.  I tried other
ways of doing it, but this one seems as fast as any.  I post it
here to make sure I haven't made a major error.  x and y are
vectors of the same length.

goodman <- function(x,y){
  Rx <- outer(x,x,function(u,v) sign(u-v))
  Ry <- outer(y,y,function(u,v) sign(u-v))
  S1 <- Rx*Ry

Jonathan Baron, Professor of Psychology, University of Pennsylvania
R page:               http://finzi.psych.upenn.edu/

More information about the R-help mailing list