Change how checking for missing values works.
[pspp] / src / language / stats / ks-one-sample.c
index d571693cd8cce5da80e6cfcd1044e6e8477024c9..d959ad753e3d700718a3cab223e076dca814fd9a 100644 (file)
 #include "libpspp/hash-functions.h"
 #include "libpspp/message.h"
 #include "libpspp/misc.h"
-#include "output/tab.h"
+#include "output/pivot-table.h"
 
 #include "gl/xalloc.h"
 
 #include "gettext.h"
+#define N_(msgid) msgid
 #define _(msgid) gettext (msgid)
 
 
@@ -93,7 +94,7 @@ theoretical_exponential (const struct ks *ks, double x)
 }
 
 
-static const  theoreticalfp theoreticalf[4] = 
+static const  theoreticalfp theoreticalf[4] =
 {
   theoretical_normal,
   theoretical_uniform,
@@ -101,7 +102,7 @@ static const  theoreticalfp theoreticalf[4] =
   theoretical_exponential
 };
 
-/* 
+/*
    Return the assymptotic approximation to the significance of Z
  */
 static double
@@ -109,7 +110,7 @@ ks_asymp_sig (double z)
 {
   if (z < 0.27)
     return 1;
-  
+
   if (z >= 3.1)
     return 0;
 
@@ -139,13 +140,12 @@ ks_one_sample_execute (const struct dataset *ds,
   const struct ks_one_sample_test *kst = UP_CAST (test, const struct ks_one_sample_test, parent.parent);
   const struct one_sample_test *ost = &kst->parent;
   struct ccase *c;
-  const struct variable *wvar = dict_get_weight (dict);
-  const struct fmt_spec *wfmt = wvar ? var_get_print_format (wvar) : & F_8_0;
+  const struct fmt_spec *wfmt = dict_get_weight_format (dict);
   bool warn = true;
   int v;
   struct casereader *r = casereader_clone (input);
 
-  struct ks *ks = xcalloc (ost->n_vars, sizeof *ks);
+  struct ks *ks = XCALLOC (ost->n_vars,  struct ks);
 
   for (v = 0; v < ost->n_vars; ++v)
     {
@@ -166,8 +166,8 @@ ks_one_sample_execute (const struct dataset *ds,
        {
          const struct variable *var = ost->vars[v];
          const union value *val = case_data (c, var);
-      
-         if (var_is_value_missing (var, val, exclude))
+
+         if (var_is_value_missing (var, val) & exclude)
            continue;
 
          minimize (&ks[v].test_min, val->f);
@@ -215,7 +215,7 @@ ks_one_sample_execute (const struct dataset *ds,
        case KS_EXPONENTIAL:
          if (kst->p[0] != SYSMIS)
            ks[v].mu = ks[v].sigma = kst->p[0];
-         else 
+         else
            ks[v].mu = ks[v].sigma = ks[v].sum / ks[v].obs_cc;
          break;
        default:
@@ -230,25 +230,25 @@ ks_one_sample_execute (const struct dataset *ds,
          const double weight = dict_get_case_weight (dict, c, &warn);
          const union value *val = case_data (c, var);
 
-         if (var_is_value_missing (var, val, exclude))
+         if (var_is_value_missing (var, val) & exclude)
            continue;
 
          cc += weight;
 
          empirical = cc / ks[v].obs_cc;
-      
+
          theoretical = theoreticalf[kst->dist] (&ks[v], val->f);
-      
+
          d = empirical - theoretical;
          dp = prev_empirical - theoretical;
 
          if (d > 0)
-           maximize (&ks[v].diff_pos, d); 
+           maximize (&ks[v].diff_pos, d);
          else
            minimize (&ks[v].diff_neg, d);
 
          if (dp > 0)
-           maximize (&ks[v].diff_pos, dp); 
+           maximize (&ks[v].diff_pos, dp);
          else
            minimize (&ks[v].diff_neg, dp);
 
@@ -270,133 +270,96 @@ show_results (const struct ks *ks,
              const struct ks_one_sample_test *kst,
              const struct fmt_spec *wfmt)
 {
-  int i;
-  const int row_headers = 1;
-  const int column_headers = 2;
-  const int nc = kst->parent.n_vars + column_headers;
-  const int nr = 8 + row_headers;
-  struct tab_table *table = tab_create (nc, nr);
-  tab_set_format (table, RC_WEIGHT, wfmt);
-  tab_headers (table, row_headers, 0, column_headers, 0);
-
-  tab_title (table, _("One-Sample Kolmogorov-Smirnov Test"));
-
-  /* Box around the table */
-  tab_box (table, TAL_2, TAL_2, -1, -1,
-          0,  0, nc - 1, nr - 1 );
-
-  tab_hline (table, TAL_2, 0, nc - 1, row_headers);
+  struct pivot_table *table = pivot_table_create (
+    N_("One-Sample Kolmogorov-Smirnov Test"));
+  pivot_table_set_weight_format (table, wfmt);
 
-  tab_vline (table, TAL_1, column_headers, 0, nr - 1);
-
-  tab_text (table,  0, 1,
-           TAT_TITLE | TAB_LEFT , _("N"));
+  struct pivot_dimension *statistics = pivot_dimension_create (
+    table, PIVOT_AXIS_ROW, N_("Statistics"),
+    N_("N"), PIVOT_RC_COUNT);
 
   switch (kst->dist)
     {
-    case KS_NORMAL:
-      tab_text (table,  0, 2,
-               TAT_TITLE | TAB_LEFT , _("Normal Parameters"));
-      
-      tab_text (table,  1, 2,
-               TAT_TITLE | TAB_LEFT , _("Mean"));
-      tab_text (table,  1, 3,
-               TAT_TITLE | TAB_LEFT , _("Std. Deviation"));
-      break;
     case KS_UNIFORM:
-      tab_text (table,  0, 2,
-               TAT_TITLE | TAB_LEFT , _("Uniform Parameters"));
-      
-      tab_text (table,  1, 2,
-               TAT_TITLE | TAB_LEFT , _("Minimum"));
-      tab_text (table,  1, 3,
-               TAT_TITLE | TAB_LEFT , _("Maximum"));
+      pivot_category_create_group (statistics->root, N_("Uniform Parameters"),
+                                   N_("Minimum"), N_("Maximum"));
       break;
+
+    case KS_NORMAL:
+      pivot_category_create_group (statistics->root, N_("Normal Parameters"),
+                                   N_("Mean"), N_("Std. Deviation"));
+      break;
+
     case KS_POISSON:
-      tab_text (table,  0, 2,
-               TAT_TITLE | TAB_LEFT , _("Poisson Parameters"));
-      
-      tab_text (table,  1, 2,
-               TAT_TITLE | TAB_LEFT , _("Lambda"));
+      pivot_category_create_group (statistics->root, N_("Poisson Parameters"),
+                                   N_("Lambda"));
       break;
+
     case KS_EXPONENTIAL:
-      tab_text (table,  0, 2,
-               TAT_TITLE | TAB_LEFT , _("Exponential Parameters"));
-      
-      tab_text (table,  1, 2,
-               TAT_TITLE | TAB_LEFT , _("Scale"));
+      pivot_category_create_group (statistics->root,
+                                   N_("Exponential Parameters"), N_("Scale"));
       break;
 
     default:
       NOT_REACHED ();
     }
 
-  /* The variable columns */
-  for (i = 0; i < kst->parent.n_vars; ++i)
+  pivot_category_create_group (
+    statistics->root, N_("Most Extreme Differences"),
+    N_("Absolute"), N_("Positive"), N_("Negative"));
+
+  pivot_category_create_leaves (
+    statistics->root, N_("Kolmogorov-Smirnov Z"),
+    _("Asymp. Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
+
+  struct pivot_dimension *variables = pivot_dimension_create (
+    table, PIVOT_AXIS_COLUMN, N_("Variables"));
+
+  for (size_t i = 0; i < kst->parent.n_vars; ++i)
     {
-      double abs = 0;
-      double z = 0;
-      const int col = 2 + i;
-      tab_text (table, col, 0,
-               TAT_TITLE | TAB_CENTER , 
-               var_to_string (kst->parent.vars[i]));
+      int col = pivot_category_create_leaf (
+        variables->root, pivot_value_new_variable (kst->parent.vars[i]));
+
+      double values[10];
+      size_t n = 0;
+
+      values[n++] = ks[i].obs_cc;
 
       switch (kst->dist)
        {
        case KS_UNIFORM:
-         tab_double (table, col, 1, 0, ks[i].obs_cc, NULL, RC_WEIGHT);
-         tab_double (table, col, 2, 0, ks[i].test_min, NULL, RC_OTHER);
-         tab_double (table, col, 3, 0, ks[i].test_max, NULL, RC_OTHER);
+          values[n++] = ks[i].test_min;
+          values[n++] = ks[i].test_max;
          break;
 
        case KS_NORMAL:
-         tab_double (table, col, 1, 0, ks[i].obs_cc, NULL, RC_WEIGHT);
-         tab_double (table, col, 2, 0, ks[i].mu, NULL, RC_OTHER);
-         tab_double (table, col, 3, 0, ks[i].sigma, NULL, RC_OTHER);
+          values[n++] = ks[i].mu;
+          values[n++] = ks[i].sigma;
          break;
 
        case KS_POISSON:
        case KS_EXPONENTIAL:
-         tab_double (table, col, 1, 0, ks[i].obs_cc, NULL, RC_WEIGHT);
-         tab_double (table, col, 2, 0, ks[i].mu, NULL, RC_OTHER);
+          values[n++] = ks[i].mu;
          break;
 
        default:
          NOT_REACHED ();
        }
 
-      abs = ks[i].diff_pos;
+      double abs = ks[i].diff_pos;
       maximize (&abs, -ks[i].diff_neg);
 
-      z = sqrt (ks[i].obs_cc) * abs;
-
-      tab_double (table, col, 5, 0, ks[i].diff_pos, NULL, RC_OTHER);
-      tab_double (table, col, 6, 0, ks[i].diff_neg, NULL, RC_OTHER);
+      double z = sqrt (ks[i].obs_cc) * abs;
 
-      tab_double (table, col, 4, 0, abs, NULL, RC_OTHER);
+      values[n++] = abs;
+      values[n++] = ks[i].diff_pos;
+      values[n++] = ks[i].diff_neg;
+      values[n++] = z;
+      values[n++] = ks_asymp_sig (z);
 
-      tab_double (table, col, 7, 0, z, NULL, RC_OTHER);
-      tab_double (table, col, 8, 0, ks_asymp_sig (z), NULL, RC_PVALUE);
+      for (size_t j = 0; j < n; j++)
+        pivot_table_put2 (table, j, col, pivot_value_new_number (values[j]));
     }
 
-
-  tab_text (table,  0, 4,
-           TAT_TITLE | TAB_LEFT , _("Most Extreme Differences"));
-
-  tab_text (table,  1, 4,
-           TAT_TITLE | TAB_LEFT , _("Absolute"));
-
-  tab_text (table,  1, 5,
-           TAT_TITLE | TAB_LEFT , _("Positive"));
-
-  tab_text (table,  1, 6,
-           TAT_TITLE | TAB_LEFT , _("Negative"));
-
-  tab_text (table,  0, 7,
-           TAT_TITLE | TAB_LEFT , _("Kolmogorov-Smirnov Z"));
-
-  tab_text (table,  0, 8,
-           TAT_TITLE | TAB_LEFT , _("Asymp. Sig. (2-tailed)"));
-
-  tab_submit (table);
+  pivot_table_submit (table);
 }