Correct error in Mann-Whitney test
authorJohn Darrington <john@darrington.wattle.id.au>
Fri, 5 Feb 2016 12:00:35 +0000 (13:00 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Fri, 5 Feb 2016 15:02:59 +0000 (16:02 +0100)
Fixes bug #47041

NEWS
src/language/stats/mann-whitney.c
tests/language/stats/npar.at

diff --git a/NEWS b/NEWS
index 0318b285c77f6d0b5b121ef99fa8aeccceb8160b..dde55318f761d04b19f25ef3878c02f0b4268050 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,10 @@ Changes from 0.8.5 to 0.9.0:
    - Rendering of the variable and data sheets in right-to-left locales
      now works properly.
 
+   - The Mann-Whitney test, when run on a dataset containing values
+     of the group variable, other than those defining the test groups,
+     would incorrectly calculate the ranks.  This has been corrected.
+
  * The IMPORTCASES subcommand on GET DATA is now ignored, for
    compatibility.
 
index 81fe033af52ac9bb4cb8b995db7d6ddc03efae27..f752b463a2d434467eb300c6c74fefc3086a2b2d 100644 (file)
@@ -54,6 +54,26 @@ static void show_ranks_box (const struct n_sample_test *nst, const struct mw *mw
 static void show_statistics_box (const struct n_sample_test *nst, const struct mw *mw, bool exact);
 
 
+
+static bool
+belongs_to_test (const struct ccase *c, void *aux)
+{
+  const struct n_sample_test *nst = aux;
+
+  const union value *group = case_data (c, nst->indep_var);
+  const size_t group_var_width = var_get_width (nst->indep_var);
+
+  if ( value_equal (group, &nst->val1, group_var_width))
+    return true;
+
+  if ( value_equal (group, &nst->val2, group_var_width))
+    return true;
+
+  return false;
+}
+
+                                        
+
 void
 mann_whitney_execute (const struct dataset *ds,
                      struct casereader *input,
@@ -79,9 +99,16 @@ mann_whitney_execute (const struct dataset *ds,
       struct casereader *rr;
       struct ccase *c;
       const struct variable *var = nst->vars[i];
+
+      struct casereader *reader = 
+       casereader_create_filter_func (casereader_clone (input),
+                                      belongs_to_test,
+                                      NULL,
+                                      CONST_CAST (struct n_sample_test *, nst),
+                                      NULL);
+
       
-      struct casereader *reader =
-       sort_execute_1var (casereader_clone (input), var);
+      reader = sort_execute_1var (reader, var);
 
       rr = casereader_create_append_rank (reader, var,
                                          dict_get_weight (dict),
index 3f620147626acf17d2bfb1f4ba06c99dcf9fdef8..eec7433c34997c5541e3dbf7da71105dacb69150 100644 (file)
@@ -1008,7 +1008,11 @@ begin data.
 85 0            
 76 1            
 145 1            
-24 1            
+24 1
+1 4
+-4 5
+34 5
+21 4
 end data.
 
 NPAR TESTS