Initial implementation of the Kruskal-Wallis test.
[pspp-builds.git] / src / language / stats / npar.c
index ad1724da151fbdd1313570b1a59d11bead7f4d3a..27d2d22d14947b3b9f39abcabc2dfdd81e0145c5 100644 (file)
@@ -36,6 +36,7 @@
 #include <language/lexer/value-parser.h>
 #include <language/stats/binomial.h>
 #include <language/stats/chisquare.h>
+#include <language/stats/kruskal-wallis.h>
 #include <language/stats/wilcoxon.h>
 #include <language/stats/sign.h>
 #include <libpspp/assertion.h>
@@ -75,6 +76,7 @@ struct cmd_npar_tests
     int binomial;
     int wilcoxon;
     int sign;
+    int kruskal_wallis;
     int missing;
     int method;
     int statistics;
@@ -112,6 +114,7 @@ static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *
 static int npar_binomial (struct lexer *, struct dataset *,  struct npar_specs *);
 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
+static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
 static int npar_method (struct lexer *, struct npar_specs *);
 
 /* Command parsing functions. */
@@ -167,6 +170,24 @@ parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests
               NOT_REACHED ();
             }
         }
+      else if (lex_match_hyphenated_word (lexer, "K-W") ||
+              lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
+        {
+          lex_match (lexer, '=');
+          npt->kruskal_wallis++;
+          switch (npar_kruskal_wallis (lexer, ds, nps))
+            {
+            case 0:
+              goto lossage;
+            case 1:
+              break;
+            case 2:
+              lex_error (lexer, NULL);
+              goto lossage;
+            default:
+              NOT_REACHED ();
+            }
+        }
       else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
         {
           lex_match (lexer, '=');
@@ -733,8 +754,6 @@ parse_n_sample_related_test (struct lexer *lexer,
                             struct pool *pool
                             )
 {
-  union value val1, val2;
-
   if (!parse_variables_const_pool (lexer, pool,
                                   dict,
                                   &nst->vars, &nst->n_vars,
@@ -749,20 +768,20 @@ parse_n_sample_related_test (struct lexer *lexer,
   if ( ! lex_force_match (lexer, '('))
     return false;
 
-  value_init (&val1, var_get_width (nst->indep_var));
-  if ( ! parse_value (lexer, &val1, var_get_width (nst->indep_var)))
+  value_init (&nst->val1, var_get_width (nst->indep_var));
+  if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
     {
-      value_destroy (&val1, var_get_width (nst->indep_var));
+      value_destroy (&nst->val1, var_get_width (nst->indep_var));
       return false;
     }
 
   if ( ! lex_force_match (lexer, ','))
     return false;
 
-  value_init (&val2, var_get_width (nst->indep_var));
-  if ( ! parse_value (lexer, &val2, var_get_width (nst->indep_var)))
+  value_init (&nst->val2, var_get_width (nst->indep_var));
+  if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
     {
-      value_destroy (&val2, var_get_width (nst->indep_var));
+      value_destroy (&nst->val2, var_get_width (nst->indep_var));
       return false;
     }
 
@@ -818,6 +837,30 @@ npar_sign (struct lexer *lexer, struct dataset *ds,
   return 1;
 }
 
+static int
+npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
+                     struct npar_specs *specs)
+{
+  struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
+  struct npar_test *nt = &tp->parent;
+
+  nt->insert_variables = n_sample_insert_variables;
+
+  nt->execute = kruskal_wallis_execute;
+
+  if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
+                                     tp, specs->pool) )
+    return 0;
+
+  specs->n_tests++;
+  specs->test = pool_realloc (specs->pool,
+                             specs->test,
+                             sizeof (*specs->test) * specs->n_tests);
+  specs->test[specs->n_tests - 1] = nt;
+
+  return 1;
+}
+
 /* Insert the variables for TEST into VAR_HASH */
 static void
 one_sample_insert_variables (const struct npar_test *test,
@@ -836,7 +879,6 @@ two_sample_insert_variables (const struct npar_test *test,
                             struct const_hsh_table *var_hash)
 {
   int i;
-
   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
 
   for ( i = 0 ; i < tst->n_pairs ; ++i )