T-TEST: Avoid NaN in paired-sample correlation significance calcuation.
[pspp-builds.git] / src / language / stats / t-test.q
index 40bf97788b35e157082ebaf37a460f5216946c7c..dd40de991bf3e78df07523af2e6564b06ee9f162 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -1279,7 +1279,7 @@ trbox_paired_populate (struct trbox *trb,
       tab_double (trb->t, 8, i+3, TAB_RIGHT, df, wfmt);
 
       p = gsl_cdf_tdist_P (t,df);
-      q = gsl_cdf_tdist_P (t,df);
+      q = gsl_cdf_tdist_Q (t,df);
 
       tab_double (trb->t, 9, i+3, TAB_RIGHT, 2.0* (t>0?q:p), NULL);
 
@@ -1432,9 +1432,16 @@ pscbox (const struct dictionary *dict)
 
       double df = pairs[i].n -2;
 
+      /* pairs[i].correlation is a correlation, so mathematically it will
+         always be in the range [-1.0, 1.0].  Inaccurate calculations sometimes
+         cause it to be slightly greater than 1.0, however, which makes the
+         sqrt() below to come out as NaN instead of 0.  So force it to be 1.0
+         or less. */
+      double corr = MIN (1.0, pairs[i].correlation);
+
       double correlation_t =
        pairs[i].correlation * sqrt (df) /
-       sqrt (1 - pow2 (pairs[i].correlation));
+       sqrt (1 - pow2 (corr));
 
 
       /* row headings */