NPAR TESTS: Implement parser for kruskal wallis test
[pspp-builds.git] / src / language / stats / npar.c
index 97cb7c7bf1c750a171edfcb4fa7f51d0c5a549d4..6e2fae71940f8e4e639c00a8f693801c2db19a17 100644 (file)
@@ -33,6 +33,7 @@
 #include <language/command.h>
 #include <language/lexer/lexer.h>
 #include <language/lexer/variable-parser.h>
+#include <language/lexer/value-parser.h>
 #include <language/stats/binomial.h>
 #include <language/stats/chisquare.h>
 #include <language/stats/wilcoxon.h>
@@ -56,12 +57,6 @@ enum missing_type
     MISS_LISTWISE,
   };
 
-enum
-  {
-    NPAR_INCLUDE,
-    NPAR_EXCLUDE
-  };
-
 /* Array indices for STATISTICS subcommand. */
 enum
   {
@@ -74,28 +69,21 @@ enum
 /* NPAR TESTS structure. */
 struct cmd_npar_tests
   {
-    /* CHISQUARE subcommand. */
+    /* Count variables indicating how many
+       of the subcommands have been given. */
     int chisquare;
-
-    /* BINOMIAL subcommand. */
     int binomial;
-
-    /* WILCOXON subcommand. */
     int wilcoxon;
-
-    /* SIGN subcommand. */
     int sign;
-
-    /* MISSING subcommand. */
+    int kruskal_wallis;
     int missing;
-    long miss;
-    long incl;
-
-    /* METHOD subcommand. */
     int method;
-
-    /* STATISTICS subcommand. */
     int statistics;
+
+    /* How missing values should be treated */
+    long miss;
+
+    /* Which statistics have been requested */
     int a_statistics[NPAR_ST_count];
   };
 
@@ -125,6 +113,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. */
@@ -141,7 +130,6 @@ parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests
   npt->sign = 0;
   npt->missing = 0;
   npt->miss = MISS_ANALYSIS;
-  npt->incl = NPAR_EXCLUDE;
   npt->method = 0;
   npt->statistics = 0;
   memset (npt->a_statistics, 0, sizeof npt->a_statistics);
@@ -181,6 +169,24 @@ parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests
               NOT_REACHED ();
             }
         }
+      else if (lex_match_hyphenated_word (lexer, "K-S") ||
+              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, '=');
@@ -231,9 +237,9 @@ parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests
               else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
                 npt->miss = MISS_LISTWISE;
               else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
-                npt->incl = NPAR_INCLUDE;
+                nps->filter = MV_SYSTEM;
               else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
-                npt->incl = NPAR_EXCLUDE;
+                nps->filter = MV_ANY;
               else
                 {
                   lex_error (lexer, NULL);
@@ -318,6 +324,11 @@ static void two_sample_insert_variables (const struct npar_test *test,
                                         struct const_hsh_table *variables);
 
 
+static void n_sample_insert_variables (const struct npar_test *test,
+                                        struct const_hsh_table *variables);
+
+
+
 static void
 npar_execute (struct casereader *input,
              const struct npar_specs *specs,
@@ -370,6 +381,7 @@ cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
   struct casereader *input, *group;
 
   npar_specs.pool = pool_create ();
+  npar_specs.filter = MV_ANY;
 
   var_hash = const_hsh_create_pool (npar_specs.pool, 0,
                                    compare_vars_by_name, hash_var_by_name,
@@ -417,8 +429,6 @@ cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
        }
     }
 
-  npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
-
   input = proc_open (ds);
   if ( cmd.miss == MISS_LISTWISE )
     {
@@ -735,6 +745,53 @@ parse_two_sample_related_test (struct lexer *lexer,
   return true;
 }
 
+
+static bool
+parse_n_sample_related_test (struct lexer *lexer,
+                            const struct dictionary *dict,
+                            struct n_sample_test *nst,
+                            struct pool *pool
+                            )
+{
+  union value val1, val2;
+
+  if (!parse_variables_const_pool (lexer, pool,
+                                  dict,
+                                  &nst->vars, &nst->n_vars,
+                                  PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
+    return false;
+
+  if ( ! lex_force_match (lexer, T_BY))
+    return false;
+
+  nst->indep_var = parse_variable_const (lexer, dict);
+
+  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_destroy (&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_destroy (&val2, var_get_width (nst->indep_var));
+      return false;
+    }
+
+  if ( ! lex_force_match (lexer, ')'))
+    return false;
+
+  return true;
+}
+
 static int
 npar_wilcoxon (struct lexer *lexer,
               struct dataset *ds,
@@ -781,25 +838,50 @@ 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 = kruskall_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,
                             struct const_hsh_table *var_hash)
 {
   int i;
-  struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
+  const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
 
   for ( i = 0 ; i < ost->n_vars ; ++i )
     const_hsh_insert (var_hash, ost->vars[i]);
 }
 
+
 static void
 two_sample_insert_variables (const struct npar_test *test,
                             struct const_hsh_table *var_hash)
 {
   int i;
 
-  const struct two_sample_test *tst = (const struct two_sample_test *) test;
+  const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
 
   for ( i = 0 ; i < tst->n_pairs ; ++i )
     {
@@ -810,6 +892,19 @@ two_sample_insert_variables (const struct npar_test *test,
     }
 }
 
+static void 
+n_sample_insert_variables (const struct npar_test *test,
+                          struct const_hsh_table *var_hash)
+{
+  int i;
+  const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
+
+  for ( i = 0 ; i < tst->n_vars ; ++i )
+    const_hsh_insert (var_hash, tst->vars[i]);
+
+  const_hsh_insert (var_hash, tst->indep_var);
+}
+
 static int
 npar_method (struct lexer *lexer,  struct npar_specs *specs)
 {