Allow users to set the precision of output statistics.
[pspp-builds.git] / src / language / stats / crosstabs.q
index 86a561e82ff7f57c2049cbcbc8a7333f5b073f37..186ee12b995551b51b66851b502445ec008fe0b1 100644 (file)
 #include <language/dictionary/split-file.h>
 #include <language/lexer/lexer.h>
 #include <language/lexer/variable-parser.h>
-#include <libpspp/alloc.h>
 #include <libpspp/array.h>
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
 #include <libpspp/hash.h>
 #include <libpspp/message.h>
-#include <libpspp/message.h>
 #include <libpspp/misc.h>
 #include <libpspp/pool.h>
 #include <libpspp/str.h>
@@ -60,6 +58,8 @@
 #include <output/table.h>
 
 #include "minmax.h"
+#include "xalloc.h"
+#include "xmalloca.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -179,7 +179,7 @@ static int internal_cmd_crosstabs (struct lexer *lexer, struct dataset *ds);
 static void precalc (struct casereader *, const struct dataset *);
 static void calc_general (struct ccase *, const struct dataset *);
 static void calc_integer (struct ccase *, const struct dataset *);
-static void postcalc (void);
+static void postcalc (const struct dataset *);
 static void submit (struct tab_table *);
 
 static void format_short (char *s, const struct fmt_spec *fp,
@@ -318,7 +318,7 @@ internal_cmd_crosstabs (struct lexer *lexer, struct dataset *ds)
         }
       casereader_destroy (group);
 
-      postcalc ();
+      postcalc (ds);
     }
   ok = casegrouper_destroy (grouper);
   ok = proc_commit (ds) && ok;
@@ -520,10 +520,11 @@ precalc (struct casereader *input, const struct dataset *ds)
 {
   struct ccase c;
 
-  if (!casereader_peek (input, 0, &c))
-    return;
-  output_split_file_values (ds, &c);
-  case_destroy (&c);
+  if (casereader_peek (input, 0, &c))
+    {
+      output_split_file_values (ds, &c);
+      case_destroy (&c);
+    }
 
   if (mode == GENERAL)
     {
@@ -551,7 +552,7 @@ precalc (struct casereader *input, const struct dataset *ds)
 
          sorted_tab = xnrealloc (sorted_tab,
                                   n_sorted_tab + count, sizeof *sorted_tab);
-         v = local_alloc (sizeof *v * x->nvar);
+         v = xmalloca (sizeof *v * x->nvar);
          for (j = 2; j < x->nvar; j++)
             v[j] = get_var_range (x->vars[j])->min;
          for (j = 0; j < count; j++)
@@ -585,7 +586,7 @@ precalc (struct casereader *input, const struct dataset *ds)
                     break;
                 }
            }
-         local_free (v);
+         freea (v);
        }
 
       sorted_tab = xnrealloc (sorted_tab,
@@ -615,7 +616,7 @@ calc_general (struct ccase *c, const struct dataset *ds)
       struct crosstab *x = xtab[t];
       const size_t entry_size = (sizeof (struct table_entry)
                                 + sizeof (union value) * (x->nvar - 1));
-      struct table_entry *te = local_alloc (entry_size);
+      struct table_entry *te = xmalloca (entry_size);
 
       /* Construct table entry for the current record and table. */
       te->table = t;
@@ -636,12 +637,14 @@ calc_general (struct ccase *c, const struct dataset *ds)
              te->values[j].f = case_num (c, x->vars[j]);
            else
              {
-               memcpy (te->values[j].s, case_str (c, x->vars[j]),
-                        var_get_width (x->vars[j]));
+                size_t n = var_get_width (x->vars[j]);
+                if (n > MAX_SHORT_STRING)
+                  n = MAX_SHORT_STRING;
+               memcpy (te->values[j].s, case_str (c, x->vars[j]), n);
 
                /* Necessary in order to simplify comparisons. */
                memset (&te->values[j].s[var_get_width (x->vars[j])], 0,
-                       sizeof (union value) - var_get_width (x->vars[j]));
+                       sizeof (union value) - n);
              }
          }
       }
@@ -664,7 +667,7 @@ calc_general (struct ccase *c, const struct dataset *ds)
       }
 
     next_crosstab:
-      local_free (te);
+      freea (te);
     }
 }
 
@@ -785,12 +788,13 @@ static void enum_var_values (struct table_entry **entries, int entry_cnt,
                              int var_idx,
                              union value **values, int *value_cnt);
 static void output_pivot_table (struct table_entry **, struct table_entry **,
+                               const struct dictionary *,
                                double **, double **, double **,
                                int *, int *, int *);
-static void make_summary_table (void);
+static void make_summary_table (const struct dictionary *);
 
 static void
-postcalc (void)
+postcalc (const struct dataset *ds)
 {
   if (mode == GENERAL)
     {
@@ -798,7 +802,7 @@ postcalc (void)
       sorted_tab = (struct table_entry **) hsh_sort (gen_tab);
     }
 
-  make_summary_table ();
+  make_summary_table (dataset_dict (ds));
 
   /* Identify all the individual crosstabulation tables, and deal with
      them. */
@@ -815,7 +819,8 @@ postcalc (void)
        if (pe == NULL)
          break;
 
-       output_pivot_table (pb, pe, &mat, &row_tot, &col_tot,
+       output_pivot_table (pb, pe, dataset_dict (ds),
+                           &mat, &row_tot, &col_tot,
                            &maxrows, &maxcols, &maxcells);
 
        pb = pe;
@@ -838,11 +843,13 @@ postcalc (void)
     }
 }
 
-static void insert_summary (struct tab_table *, int tab_index, double valid);
+static void insert_summary (struct tab_table *, int tab_index,
+                           const struct dictionary *,
+                           double valid);
 
 /* Output a table summarizing the cases processed. */
 static void
-make_summary_table (void)
+make_summary_table (const struct dictionary *dict)
 {
   struct tab_table *summary;
 
@@ -881,7 +888,7 @@ make_summary_table (void)
        break;
 
       while (cur_tab < (*pb)->table)
-       insert_summary (summary, cur_tab++, 0.);
+       insert_summary (summary, cur_tab++, dict, 0.);
 
       if (mode == GENERAL)
        for (valid = 0.; pb < pe; pb++)
@@ -902,13 +909,13 @@ make_summary_table (void)
                valid += *data++;
            }
        }
-      insert_summary (summary, cur_tab++, valid);
+      insert_summary (summary, cur_tab++, dict, valid);
 
       pb = pe;
     }
 
   while (cur_tab < nxtab)
-    insert_summary (summary, cur_tab++, 0.);
+    insert_summary (summary, cur_tab++, dict, 0.);
 
   submit (summary);
 }
@@ -916,15 +923,20 @@ make_summary_table (void)
 /* Inserts a line into T describing the crosstabulation at index
    TAB_INDEX, which has VALID valid observations. */
 static void
-insert_summary (struct tab_table *t, int tab_index, double valid)
+insert_summary (struct tab_table *t, int tab_index,
+               const struct dictionary *dict,
+               double valid)
 {
   struct crosstab *x = xtab[tab_index];
 
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+
   tab_hline (t, TAL_1, 0, 6, 0);
 
   /* Crosstabulation name. */
   {
-    char *buf = local_alloc (128 * x->nvar);
+    char *buf = xmalloca (128 * x->nvar);
     char *cp = buf;
     int i;
 
@@ -937,7 +949,7 @@ insert_summary (struct tab_table *t, int tab_index, double valid)
       }
     tab_text (t, 0, 0, TAB_LEFT, buf);
 
-    local_free (buf);
+    freea (buf);
   }
 
   /* Counts and percentages. */
@@ -952,7 +964,7 @@ insert_summary (struct tab_table *t, int tab_index, double valid)
 
     for (i = 0; i < 3; i++)
       {
-       tab_float (t, i * 2 + 1, 0, TAB_RIGHT, n[i], 8, 0);
+       tab_double (t, i * 2 + 1, 0, TAB_RIGHT, n[i], wfmt);
        tab_text (t, i * 2 + 2, 0, TAB_RIGHT | TAT_PRINTF, "%.1f%%",
                  n[i] / n[2] * 100.);
       }
@@ -1001,9 +1013,9 @@ static double W;          /* Grand total. */
 static void display_dimensions (struct tab_table *, int first_difference,
                                struct table_entry *);
 static void display_crosstabulation (void);
-static void display_chisq (void);
-static void display_symmetric (void);
-static void display_risk (void);
+static void display_chisq (const struct dictionary *);
+static void display_symmetric (const struct dictionary *);
+static void display_risk (const struct dictionary *);
 static void display_directional (void);
 static void crosstabs_dim (struct tab_table *, struct outp_driver *);
 static void table_value_missing (struct tab_table *table, int c, int r,
@@ -1016,6 +1028,7 @@ static void delete_missing (void);
    hold *MAXROWS entries. */
 static void
 output_pivot_table (struct table_entry **pb, struct table_entry **pe,
+                   const struct dictionary *dict,
                    double **matp, double **row_totp, double **col_totp,
                    int *maxrows, int *maxcols, int *maxcells)
 {
@@ -1064,7 +1077,7 @@ output_pivot_table (struct table_entry **pb, struct table_entry **pe,
 
       /* Title. */
       {
-       char *title = local_alloc (x->nvar * 64 + 128);
+       char *title = xmalloca (x->nvar * 64 + 128);
        char *cp = title;
        int i;
 
@@ -1130,7 +1143,7 @@ output_pivot_table (struct table_entry **pb, struct table_entry **pe,
        strcpy (cp, "].");
 
        tab_title (table, "%s", title);
-       local_free (title);
+       freea (title);
       }
 
       tab_offset (table, 0, 2);
@@ -1422,11 +1435,11 @@ output_pivot_table (struct table_entry **pb, struct table_entry **pe,
       if (cmd.miss == CRS_REPORT)
        delete_missing ();
       if (chisq)
-       display_chisq ();
+       display_chisq (dict);
       if (sym)
-       display_symmetric ();
+       display_symmetric (dict);
       if (risk)
-       display_risk ();
+       display_risk (dict);
       if (direct)
        display_directional ();
 
@@ -1671,7 +1684,7 @@ enum_var_values (struct table_entry **entries, int entry_cnt, int var_idx,
 
   if (mode == GENERAL)
     {
-      int width = var_get_width (v);
+      int width = MIN (var_get_width (v), MAX_SHORT_STRING);
       int i;
 
       *values = xnmalloc (entry_cnt, sizeof **values);
@@ -1858,7 +1871,6 @@ display_crosstabulation (void)
     tab_offset (table, -1, tab_row (table) - num_cells * n_rows);
     for (r = 0; r < n_rows; r++)
       {
-        char suffix = 0;
         bool mark_missing = false;
 
         if (cmd.miss == CRS_REPORT
@@ -1867,6 +1879,7 @@ display_crosstabulation (void)
 
         for (i = 0; i < num_cells; i++)
           {
+            char suffix = 0;
             double v;
 
             switch (cells[i])
@@ -1875,7 +1888,7 @@ display_crosstabulation (void)
                 v = row_tot[r];
                 break;
               case CRS_CL_ROW:
-                v = 100.;
+                v = 100.0;
                 suffix = '%';
                 break;
               case CRS_CL_COLUMN:
@@ -1913,7 +1926,6 @@ display_crosstabulation (void)
       {
        double ct = c < n_cols ? col_tot[c] : W;
         bool mark_missing = false;
-        char suffix = 0;
         int i;
 
         if (cmd.miss == CRS_REPORT && c < n_cols
@@ -1922,13 +1934,13 @@ display_crosstabulation (void)
 
         for (i = 0; i < num_cells; i++)
          {
+            char suffix = 0;
            double v;
 
            switch (cells[i])
              {
              case CRS_CL_COUNT:
                v = ct;
-                suffix = '%';
                break;
              case CRS_CL_ROW:
                v = ct / W * 100.;
@@ -1951,7 +1963,7 @@ display_crosstabulation (void)
                 NOT_REACHED ();
              }
 
-            format_cell_entry (table, c, i, v, suffix, mark_missing);
+           format_cell_entry (table, c, i, v, suffix, mark_missing);
          }
         last_row = i;
       }
@@ -1967,8 +1979,11 @@ static void calc_chisq (double[N_CHISQ], int[N_CHISQ], double *, double *);
 
 /* Display chi-square statistics. */
 static void
-display_chisq (void)
+display_chisq (const struct dictionary *dict)
 {
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+
   static const char *chisq_stats[N_CHISQ] =
     {
       N_("Pearson Chi-Square"),
@@ -1998,22 +2013,22 @@ display_chisq (void)
       tab_text (chisq, 0, 0, TAB_LEFT, gettext (chisq_stats[i]));
       if (i != 2)
        {
-         tab_float (chisq, 1, 0, TAB_RIGHT, chisq_v[i], 8, 3);
-         tab_float (chisq, 2, 0, TAB_RIGHT, df[i], 8, 0);
-         tab_float (chisq, 3, 0, TAB_RIGHT,
-                    gsl_cdf_chisq_Q (chisq_v[i], df[i]), 8, 3);
+         tab_double (chisq, 1, 0, TAB_RIGHT, chisq_v[i], NULL);
+         tab_double (chisq, 2, 0, TAB_RIGHT, df[i], wfmt);
+         tab_double (chisq, 3, 0, TAB_RIGHT,
+                    gsl_cdf_chisq_Q (chisq_v[i], df[i]), NULL);
        }
       else
        {
          chisq_fisher = 1;
-         tab_float (chisq, 4, 0, TAB_RIGHT, fisher2, 8, 3);
-         tab_float (chisq, 5, 0, TAB_RIGHT, fisher1, 8, 3);
+         tab_double (chisq, 4, 0, TAB_RIGHT, fisher2, NULL);
+         tab_double (chisq, 5, 0, TAB_RIGHT, fisher1, NULL);
        }
       tab_next_row (chisq);
     }
 
   tab_text (chisq, 0, 0, TAB_LEFT, _("N of Valid Cases"));
-  tab_float (chisq, 1, 0, TAB_RIGHT, W, 8, 0);
+  tab_double (chisq, 1, 0, TAB_RIGHT, W, wfmt);
   tab_next_row (chisq);
 
   tab_offset (chisq, 0, -1);
@@ -2024,8 +2039,11 @@ static int calc_symmetric (double[N_SYMMETRIC], double[N_SYMMETRIC],
 
 /* Display symmetric measures. */
 static void
-display_symmetric (void)
+display_symmetric (const struct dictionary *dict)
 {
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+
   static const char *categories[] =
     {
       N_("Nominal by Nominal"),
@@ -2073,17 +2091,17 @@ display_symmetric (void)
        }
 
       tab_text (sym, 1, 0, TAB_LEFT, gettext (stats[i]));
-      tab_float (sym, 2, 0, TAB_RIGHT, sym_v[i], 8, 3);
+      tab_double (sym, 2, 0, TAB_RIGHT, sym_v[i], NULL);
       if (sym_ase[i] != SYSMIS)
-       tab_float (sym, 3, 0, TAB_RIGHT, sym_ase[i], 8, 3);
+       tab_double (sym, 3, 0, TAB_RIGHT, sym_ase[i], NULL);
       if (sym_t[i] != SYSMIS)
-       tab_float (sym, 4, 0, TAB_RIGHT, sym_t[i], 8, 3);
-      /*tab_float (sym, 5, 0, TAB_RIGHT, normal_sig (sym_v[i]), 8, 3);*/
+       tab_double (sym, 4, 0, TAB_RIGHT, sym_t[i], NULL);
+      /*tab_double (sym, 5, 0, TAB_RIGHT, normal_sig (sym_v[i]), NULL);*/
       tab_next_row (sym);
     }
 
   tab_text (sym, 0, 0, TAB_LEFT, _("N of Valid Cases"));
-  tab_float (sym, 2, 0, TAB_RIGHT, W, 8, 0);
+  tab_double (sym, 2, 0, TAB_RIGHT, W, wfmt);
   tab_next_row (sym);
 
   tab_offset (sym, 0, -1);
@@ -2093,8 +2111,11 @@ static int calc_risk (double[], double[], double[], union value *);
 
 /* Display risk estimate. */
 static void
-display_risk (void)
+display_risk (const struct dictionary *dict)
 {
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+
   char buf[256];
   double risk_v[3], lower[3], upper[3];
   union value c[2];
@@ -2135,14 +2156,14 @@ display_risk (void)
        }
 
       tab_text (risk, 0, 0, TAB_LEFT, buf);
-      tab_float (risk, 1, 0, TAB_RIGHT, risk_v[i], 8, 3);
-      tab_float (risk, 2, 0, TAB_RIGHT, lower[i], 8, 3);
-      tab_float (risk, 3, 0, TAB_RIGHT, upper[i], 8, 3);
+      tab_double (risk, 1, 0, TAB_RIGHT, risk_v[i], NULL);
+      tab_double (risk, 2, 0, TAB_RIGHT, lower[i],  NULL);
+      tab_double (risk, 3, 0, TAB_RIGHT, upper[i],  NULL);
       tab_next_row (risk);
     }
 
   tab_text (risk, 0, 0, TAB_LEFT, _("N of Valid Cases"));
-  tab_float (risk, 1, 0, TAB_RIGHT, W, 8, 0);
+  tab_double (risk, 1, 0, TAB_RIGHT, W, wfmt);
   tab_next_row (risk);
 
   tab_offset (risk, 0, -1);
@@ -2255,12 +2276,12 @@ display_directional (void)
            }
       }
 
-      tab_float (direct, 3, 0, TAB_RIGHT, direct_v[i], 8, 3);
+      tab_double (direct, 3, 0, TAB_RIGHT, direct_v[i], NULL);
       if (direct_ase[i] != SYSMIS)
-       tab_float (direct, 4, 0, TAB_RIGHT, direct_ase[i], 8, 3);
+       tab_double (direct, 4, 0, TAB_RIGHT, direct_ase[i], NULL);
       if (direct_t[i] != SYSMIS)
-       tab_float (direct, 5, 0, TAB_RIGHT, direct_t[i], 8, 3);
-      /*tab_float (direct, 6, 0, TAB_RIGHT, normal_sig (direct_v[i]), 8, 3);*/
+       tab_double (direct, 5, 0, TAB_RIGHT, direct_t[i], NULL);
+      /*tab_double (direct, 6, 0, TAB_RIGHT, normal_sig (direct_v[i]), NULL);*/
       tab_next_row (direct);
     }
 
@@ -2734,8 +2755,8 @@ calc_symmetric (double v[N_SYMMETRIC], double ase[N_SYMMETRIC],
   /* Spearman correlation, Pearson's r. */
   if (cmd.a_statistics[CRS_ST_CORR])
     {
-      double *R = local_alloc (sizeof *R * n_rows);
-      double *C = local_alloc (sizeof *C * n_cols);
+      double *R = xmalloca (sizeof *R * n_rows);
+      double *C = xmalloca (sizeof *C * n_cols);
 
       {
        double y, t, c = 0., s = 0.;
@@ -2774,8 +2795,8 @@ calc_symmetric (double v[N_SYMMETRIC], double ase[N_SYMMETRIC],
       calc_r (R, C, &v[6], &t[6], &ase[6]);
       t[6] = v[6] / t[6];
 
-      local_free (R);
-      local_free (C);
+      freea (R);
+      freea (C);
 
       calc_r ((double *) rows, (double *) cols, &v[7], &t[7], &ase[7]);
       t[7] = v[7] / t[7];