output: Introduce pivot tables.
[pspp] / src / language / stats / friedman.c
index 9950543f706c3c3ef16fedc179881b0767d87670..9ea3cbc516aec69bfd91b4cba019e0859afa13f6 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis. -*-c-*-
-   Copyright (C) 2010 Free Software Foundation, Inc.
+   Copyright (C) 2010, 2011 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
 
 #include <config.h>
 
-#include "friedman.h"
+#include "language/stats/friedman.h"
 
 #include <gsl/gsl_cdf.h>
 #include <math.h>
 
-#include <data/format.h>
-
-#include <libpspp/misc.h>
-#include <libpspp/message.h>
-#include <data/procedure.h>
-#include <data/casereader.h>
-#include <data/dictionary.h>
-#include <data/variable.h>
-
+#include "data/casereader.h"
+#include "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/format.h"
+#include "data/variable.h"
+#include "libpspp/message.h"
+#include "libpspp/misc.h"
+#include "output/pivot-table.h"
 
 #include "gettext.h"
+#define N_(msgid) msgid
 #define _(msgid) gettext (msgid)
 
 
@@ -45,7 +45,7 @@ struct friedman
   const struct dictionary *dict;
 };
 
-static void show_ranks_box (const struct one_sample_test *ost, 
+static void show_ranks_box (const struct one_sample_test *ost,
                            const struct friedman *fr);
 
 static void show_sig_box (const struct one_sample_test *ost,
@@ -65,7 +65,7 @@ cmp_x (const void *a_, const void *b_)
 
   if (a->x < b->x)
     return -1;
-  
+
   return (a->x > b->x);
 }
 
@@ -77,7 +77,7 @@ cmp_posn (const void *a_, const void *b_)
 
   if (a->posn < b->posn)
     return -1;
-  
+
   return (a->posn > b->posn);
 }
 
@@ -100,7 +100,7 @@ friedman_execute (const struct dataset *ds,
   struct friedman_test *ft = UP_CAST (ost, struct friedman_test, parent);
   bool warn = true;
 
-  double sigma_t = 0.0;        
+  double sigma_t = 0.0;
   struct datum *row = xcalloc (ost->n_vars, sizeof *row);
   double rsq;
   struct friedman fr;
@@ -213,45 +213,28 @@ friedman_execute (const struct dataset *ds,
 
 \f
 
-#include <output/tab.h>
 
 static void
 show_ranks_box (const struct one_sample_test *ost, const struct friedman *fr)
 {
-  int i;
-  const int row_headers = 1;
-  const int column_headers = 1;
-  struct tab_table *table =
-    tab_create (row_headers + 1, column_headers + ost->n_vars);
-
-  tab_headers (table, row_headers, 0, column_headers, 0);
-
-  tab_title (table, _("Ranks"));
+  struct pivot_table *table = pivot_table_create (N_("Ranks"));
 
-  /* Vertical lines inside the box */
-  tab_box (table, 1, 0, -1, TAL_1,
-          row_headers, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
+  pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Mean Rank"),
+                          N_("Mean Rank"), PIVOT_RC_OTHER);
 
-  /* Box around the table */
-  tab_box (table, TAL_2, TAL_2, -1, -1,
-          0,  0, tab_nc (table) - 1, tab_nr (table) - 1 );
+  struct pivot_dimension *variables = pivot_dimension_create (
+    table, PIVOT_AXIS_ROW, N_("Variable"));
 
-
-  tab_text (table, 1, 0, 0, _("Mean Rank"));
-
-  tab_hline (table, TAL_2, 0, tab_nc (table) - 1, column_headers);
-  tab_vline (table, TAL_2, row_headers, 0, tab_nr (table) - 1);
-
-  for (i = 0 ; i < ost->n_vars ; ++i)
+  for (size_t i = 0 ; i < ost->n_vars ; ++i)
     {
-      tab_text (table, 0, row_headers + i,
-               TAB_LEFT, var_to_string (ost->vars[i]));
+      int row = pivot_category_create_leaf (
+        variables->root, pivot_value_new_variable (ost->vars[i]));
 
-      tab_double (table, 1, row_headers + i,
-                 0, fr->rank_sum[i] / fr->cc, 0);
+      pivot_table_put2 (table, 0, row,
+                        pivot_value_new_number (fr->rank_sum[i] / fr->cc));
     }
 
-  tab_submit (table);
+  pivot_table_submit (table);
 }
 
 
@@ -259,61 +242,34 @@ static void
 show_sig_box (const struct one_sample_test *ost, const struct friedman *fr)
 {
   const struct friedman_test *ft = UP_CAST (ost, const struct friedman_test, parent);
-  
-  int row = 0;
-  const struct variable *weight = dict_get_weight (fr->dict);
-  const struct fmt_spec *wfmt = weight ? var_get_print_format (weight) : &F_8_0;
-
-  const int row_headers = 1;
-  const int column_headers = 0;
-  struct tab_table *table =
-    tab_create (row_headers + 1, column_headers + (ft->kendalls_w ? 5 : 4));
-
-  tab_headers (table, row_headers, 0, column_headers, 0);
 
-  tab_title (table, _("Test Statistics"));
-
-  tab_text (table,  0, column_headers + row++,
-           TAT_TITLE | TAB_LEFT , _("N"));
-
-  if ( ft->kendalls_w)
-    tab_text (table,  0, column_headers + row++,
-             TAT_TITLE | TAB_LEFT , _("Kendall's W"));
-
-  tab_text (table,  0, column_headers + row++,
-           TAT_TITLE | TAB_LEFT , _("Chi-Square"));
-
-  tab_text (table,  0, column_headers + row++,
-           TAT_TITLE | TAB_LEFT, _("df"));
-
-  tab_text (table,  0, column_headers + row++,
-           TAT_TITLE | TAB_LEFT, _("Asymp. Sig."));
-
-  /* Box around the table */
-  tab_box (table, TAL_2, TAL_2, -1, -1,
-          0,  0, tab_nc (table) - 1, tab_nr (table) - 1 );
-
-
-  tab_hline (table, TAL_2, 0, tab_nc (table) -1, column_headers);
-  tab_vline (table, TAL_2, row_headers, 0, tab_nr (table) - 1);
-
-  row = 0;
-  tab_double (table, 1, column_headers + row++, 
-             0, fr->cc, wfmt);
+  struct pivot_table *table = pivot_table_create (N_("Test Statistics"));
+  pivot_table_set_weight_var (table, dict_get_weight (fr->dict));
 
+  struct pivot_dimension *statistics = pivot_dimension_create (
+    table, PIVOT_AXIS_ROW, N_("Statistics"),
+    N_("N"), PIVOT_RC_COUNT);
   if (ft->kendalls_w)
-    tab_double (table, 1, column_headers + row++, 
-               0, fr->w, 0);
+    pivot_category_create_leaves (statistics->root, N_("Kendall's W"),
+                                  PIVOT_RC_OTHER);
+  pivot_category_create_leaves (statistics->root,
+                                N_("Chi-Square"), PIVOT_RC_OTHER,
+                                N_("df"), PIVOT_RC_INTEGER,
+                                N_("Asymp. Sig."), PIVOT_RC_SIGNIFICANCE);
 
-  tab_double (table, 1, column_headers + row++, 
-             0, fr->chi_sq, 0);
+  double entries[5];
+  int n = 0;
 
-  tab_double (table, 1, column_headers + row++, 
-             0, ost->n_vars - 1, &F_8_0);
+  entries[n++] = fr->cc;
+  if (ft->kendalls_w)
+    entries[n++] = fr->w;
+  entries[n++] = fr->chi_sq;
+  entries[n++] = ost->n_vars - 1;
+  entries[n++] = gsl_cdf_chisq_Q (fr->chi_sq, ost->n_vars - 1);
+  assert (n <= sizeof entries / sizeof *entries);
 
-  tab_double (table, 1, column_headers + row++, 
-             0, gsl_cdf_chisq_Q (fr->chi_sq, ost->n_vars - 1), 
-             0);
+  for (size_t i = 0; i < n; i++)
+    pivot_table_put1 (table, i, pivot_value_new_number (entries[i]));
 
-  tab_submit (table);
+  pivot_table_submit (table);
 }