Correct error calculating significance of fisher exact test.
authorJohn Darrington <john@darrington.wattle.id.au>
Wed, 18 Sep 2013 19:00:34 +0000 (21:00 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Fri, 20 Sep 2013 15:34:40 +0000 (17:34 +0200)
The two tailed significance is the sum of the one tailed signficance and those point significances which
are greater than P(N = n1)

src/language/stats/crosstabs.q

index 2246d3af1702a6940baaec20423fd31b1667161f..6d4ab64ea0fd987a49109a2fa3dc18d389083e70 100644 (file)
@@ -2131,6 +2131,7 @@ static void
 calc_fisher (int a, int b, int c, int d, double *fisher1, double *fisher2)
 {
   int pt;
+  double pn1;
 
   if (MIN (c, d) < MIN (a, b))
     swap (&a, &c), swap (&b, &d);
@@ -2144,13 +2145,21 @@ calc_fisher (int a, int b, int c, int d, double *fisher1, double *fisher2)
        swap (&a, &c), swap (&b, &d);
     }
 
-  *fisher1 = 0.;
-  for (pt = 0; pt <= a; pt++)
-    *fisher1 += Pr (a - pt, b + pt, c + pt, d - pt);
+  pn1 = Pr (a, b, c, d);
+  *fisher1 = pn1;
+  for (pt = 1; pt <= a; pt++)
+    {
+      *fisher1 += Pr (a - pt, b + pt, c + pt, d - pt);
+    }
 
   *fisher2 = *fisher1;
+
   for (pt = 1; pt <= b; pt++)
-    *fisher2 += Pr (a + pt, b - pt, c - pt, d + pt);
+    {
+      double p = Pr (a + pt, b - pt, c - pt, d + pt);
+      if (p < pn1)
+       *fisher2 += p;
+    }
 }
 
 /* Calculates chi-squares into CHISQ.  MAT is a matrix with N_COLS