From 67d1e36f29bc59dc38ebe7b197a4c40751ad92fa Mon Sep 17 00:00:00 2001
From: John Darrington <john@darrington.wattle.id.au>
Date: Sun, 8 Nov 2009 18:17:04 +0100
Subject: [PATCH] Use significance_of_correlation function in t-test.

Use the new function in src/math/correlation.c instead
of doing it ourselves.
---
 src/language/stats/t-test.q | 15 +++------------
 src/math/correlation.c      |  5 +++++
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/src/language/stats/t-test.q b/src/language/stats/t-test.q
index c448d52ea0..8f23cefd4a 100644
--- a/src/language/stats/t-test.q
+++ b/src/language/stats/t-test.q
@@ -43,6 +43,7 @@
 #include <libpspp/taint.h>
 #include <math/group-proc.h>
 #include <math/levene.h>
+#include <math/correlation.h>
 #include <output/manager.h>
 #include <output/table.h>
 #include <data/format.h>
@@ -1110,14 +1111,6 @@ pscbox (struct t_test_proc *proc)
   for (i = 0; i < proc->n_pairs; i++)
     {
       struct pair *pair = &proc->pairs[i];
-      double df = pair->n - 2;
-      double p, q;
-
-      /* corr2 will mathematically always be in the range [0, 1.0].  Inaccurate
-         calculations sometimes cause it to be slightly greater than 1.0, so
-         force it into the correct range to avoid NaN from sqrt(). */
-      double corr2 = MIN (1.0, pow2 (pair->correlation));
-      double correlation_t = pair->correlation * sqrt (df) / sqrt (1 - corr2);
 
       /* row headings */
       tab_text_format (table, 0, i + 1, TAB_LEFT | TAT_TITLE,
@@ -1131,10 +1124,8 @@ pscbox (struct t_test_proc *proc)
       tab_double (table, 2, i + 1, TAB_RIGHT, pair->n, &proc->weight_format);
       tab_double (table, 3, i + 1, TAB_RIGHT, pair->correlation, NULL);
 
-      p = gsl_cdf_tdist_P (correlation_t, df);
-      q = gsl_cdf_tdist_Q (correlation_t, df);
-      tab_double (table, 4, i + 1, TAB_RIGHT,
-                 2.0 * (correlation_t > 0 ? q : p), NULL);
+      tab_double (table, 4, i + 1, TAB_RIGHT, 
+		  2.0 * significance_of_correlation (pair->correlation, pair->n), NULL);
     }
 
   tab_submit (table);
diff --git a/src/math/correlation.c b/src/math/correlation.c
index 303b48bcfa..4776274734 100644
--- a/src/math/correlation.c
+++ b/src/math/correlation.c
@@ -29,7 +29,12 @@ double
 significance_of_correlation (double rho, double w)
 {
   double t = w - 2;
+
+  /* |rho| will mathematically always be in the range [0, 1.0].  Inaccurate
+     calculations sometimes cause it to be slightly greater than 1.0, so
+     force it into the correct range to avoid NaN from sqrt(). */
   t /= 1 - MIN (1, pow2 (rho));
+
   t = sqrt (t);
   t *= rho;
   
-- 
2.30.2