Separate table functions that format their arguments from those that don't.
[pspp-builds.git] / src / language / stats / reliability.q
index edacdf12a1a5aea5fb615b91d07cfc0f80fb39cd..681669d18999fdac3c5516c9815b9079aae99dfb 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008, 2009 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 "xalloc.h"
-#include "xmalloca.h"
-
-#include "gettext.h"
-#define _(msgid) gettext (msgid)
-#define N_(msgid) msgid
+#include <math.h>
 
-#include <data/variable.h>
+#include <data/case.h>
+#include <data/casegrouper.h>
+#include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/procedure.h>
-#include <data/casereader.h>
-#include <data/casegrouper.h>
-#include <math/moments.h>
-#include <data/case.h>
-
+#include <data/variable.h>
 #include <language/command.h>
-
+#include <libpspp/misc.h>
+#include <math/moments.h>
 #include <output/manager.h>
 #include <output/table.h>
 
+#include "xalloc.h"
+#include "xmalloca.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
+
 /* (headers) */
 
 /* (specification)
@@ -100,6 +101,7 @@ enum model
 
 struct reliability
 {
+  const struct dictionary *dict;
   const struct variable **variables;
   int n_variables;
   enum mv_class exclude;
@@ -142,7 +144,7 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
   struct casereader *group;
   struct cmd_reliability cmd;
 
-  struct reliability rel = {
+  struct reliability rel = {NULL,
     NULL, 0, MV_ANY, NULL, 0, -1,
     DS_EMPTY_INITIALIZER,
     MODEL_ALPHA, 0};
@@ -154,6 +156,7 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
       goto done;
     }
 
+  rel.dict = dataset_dict (ds);
   rel.variables = cmd.v_variables;
   rel.n_variables = cmd.n_variables;
   rel.exclude = MV_ANY;
@@ -262,8 +265,11 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
       free (c->items);
 
       moments1_destroy (c->total);
-      for (x = 0 ; x < c->n_items; ++x)
-       moments1_destroy (c->m[x]);
+
+      if ( c->m)
+       for (x = 0 ; x < c->n_items; ++x)
+         moments1_destroy (c->m[x]);
+
       free (c->m);
     }
 
@@ -293,15 +299,16 @@ append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
 };
 
 
-static void case_processing_summary (casenumber n_valid, casenumber n_missing);
+static void case_processing_summary (casenumber n_valid, casenumber n_missing, 
+                                    const struct dictionary *dict);
 
 static void
-run_reliability (struct casereader *input, struct dataset *ds UNUSED,
+run_reliability (struct casereader *input, struct dataset *ds,
                 struct reliability *rel)
 {
   int i;
   int si;
-  struct ccase c;
+  struct ccase *c;
   casenumber n_missing ;
   casenumber n_valid = 0;
 
@@ -329,13 +336,13 @@ run_reliability (struct casereader *input, struct dataset *ds UNUSED,
       struct cronbach *s = &rel->sc[si];
 
 
-      s->totals_idx = casereader_get_value_cnt (input);
+      s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
       input =
        casereader_create_append_numeric (input, append_sum,
                                          s, NULL);
     }
 
-  for (; casereader_read (input, &c); case_destroy (&c))
+  for (; (c = casereader_read (input)) != NULL; case_unref (c))
     {
       double weight = 1.0;
       n_valid ++;
@@ -345,9 +352,9 @@ run_reliability (struct casereader *input, struct dataset *ds UNUSED,
          struct cronbach *s = &rel->sc[si];
 
          for (i = 0 ; i < s->n_items ; ++i )
-           moments1_add (s->m[i], case_data (&c, s->items[i])->f, weight);
+           moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
 
-         moments1_add (s->total, case_data_idx (&c, s->totals_idx)->f, weight);
+         moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
        }
     }
   casereader_destroy (input);
@@ -376,16 +383,16 @@ run_reliability (struct casereader *input, struct dataset *ds UNUSED,
   {
     struct tab_table *tab = tab_create(1, 1, 0);
 
-    tab_dim (tab, tab_natural_dimensions);
+    tab_dim (tab, tab_natural_dimensions, NULL);
     tab_flags (tab, SOMF_NO_TITLE );
 
-    tab_text(tab, 0, 0, TAT_PRINTF, "Scale: %s", ds_cstr (&rel->scale_name));
+    tab_text_format (tab, 0, 0, 0, "Scale: %s", ds_cstr (&rel->scale_name));
 
     tab_submit(tab);
   }
 
 
-  case_processing_summary (n_valid, n_missing);
+  case_processing_summary (n_valid, n_missing, dataset_dict (ds));
 }
 
 
@@ -401,7 +408,7 @@ struct reliability_output_table
   int n_rows;
   int heading_cols;
   int heading_rows;
-  void (*populate)(struct tab_table *, const struct reliability *);
+  void (*populate) (struct tab_table *, const struct reliability *);
 };
 
 static struct reliability_output_table rol[2] =
@@ -421,7 +428,7 @@ reliability_statistics (const struct reliability *rel)
   struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
 
-  tab_dim (tbl, tab_natural_dimensions);
+  tab_dim (tbl, tab_natural_dimensions, NULL);
 
   tab_title (tbl, _("Reliability Statistics"));
 
@@ -464,7 +471,7 @@ reliability_summary_total (const struct reliability *rel)
   struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
 
-  tab_dim (tbl, tab_natural_dimensions);
+  tab_dim (tbl, tab_natural_dimensions, NULL);
 
   tab_title (tbl, _("Item-Total Statistics"));
 
@@ -511,14 +518,14 @@ reliability_summary_total (const struct reliability *rel)
 
       moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
 
-      tab_float (tbl, 1, heading_rows + i, TAB_RIGHT,
-                mean, 8, 3);
+      tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
+                mean, NULL);
 
-      tab_float (tbl, 2, heading_rows + i, TAB_RIGHT,
-                s->variance_of_sums, 8, 3);
+      tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
+                s->variance_of_sums, NULL);
 
-      tab_float (tbl, 4, heading_rows + i, TAB_RIGHT,
-                s->alpha, 8, 3);
+      tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
+                s->alpha, NULL);
 
 
       moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
@@ -528,8 +535,8 @@ reliability_summary_total (const struct reliability *rel)
       item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
 
 
-      tab_float (tbl, 3, heading_rows + i, TAB_RIGHT,
-                item_to_total_r, 8, 3);
+      tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
+                item_to_total_r, NULL);
     }
 
 
@@ -541,6 +548,9 @@ static void
 reliability_statistics_model_alpha (struct tab_table *tbl,
                                    const struct reliability *rel)
 {
+  const struct variable *wv = dict_get_weight (rel->dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+
   const struct cronbach *s = &rel->sc[0];
 
   tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
@@ -549,9 +559,9 @@ reliability_statistics_model_alpha (struct tab_table *tbl,
   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
                _("N of items"));
 
-  tab_float (tbl, 0, 1, TAB_RIGHT, s->alpha, 8, 3);
+  tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
 
-  tab_float (tbl, 1, 1, TAB_RIGHT, s->n_items, 8, 0);
+  tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
 }
 
 
@@ -559,6 +569,9 @@ static void
 reliability_statistics_model_split (struct tab_table *tbl,
                                    const struct reliability *rel)
 {
+  const struct variable *wv = dict_get_weight (rel->dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+
   tab_text (tbl, 0, 0, TAB_LEFT,
            _("Cronbach's Alpha"));
 
@@ -606,14 +619,14 @@ reliability_statistics_model_split (struct tab_table *tbl,
 
 
 
-  tab_float (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, 8, 3);
-  tab_float (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, 8, 3);
+  tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
+  tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
 
-  tab_float (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, 8, 0);
-  tab_float (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, 8, 0);
+  tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
+  tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
 
-  tab_float (tbl, 3, 4, TAB_RIGHT,
-            rel->sc[1].n_items + rel->sc[2].n_items, 8, 0);
+  tab_double (tbl, 3, 4, TAB_RIGHT,
+            rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
 
   {
     /* R is the correlation between the two parts */
@@ -632,30 +645,33 @@ reliability_statistics_model_split (struct tab_table *tbl,
     r /= sqrt (rel->sc[2].variance_of_sums);
     r /= 2.0;
 
-    tab_float (tbl, 3, 5, TAB_RIGHT, r, 8, 3);
+    tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
 
     /* Equal length Spearman-Brown Coefficient */
-    tab_float (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), 8, 3);
+    tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
 
-    tab_float (tbl, 3, 8, TAB_RIGHT, g, 8, 3);
+    tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
 
     tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
-      SQR (rel->sc[0].n_items);
+      pow2 (rel->sc[0].n_items);
 
-    uly = sqrt( SQR (SQR (r)) + 4 * SQR (r) * tmp);
-    uly -= SQR (r);
+    uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
+    uly -= pow2 (r);
     uly /= 2 * tmp;
 
-    tab_float (tbl, 3, 7, TAB_RIGHT, uly, 8, 3);
-
+    tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);
   }
 }
 
 
 
 static void
-case_processing_summary (casenumber n_valid, casenumber n_missing)
+case_processing_summary (casenumber n_valid, casenumber n_missing,
+                        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;
+
   casenumber total;
   int n_cols = 4;
   int n_rows = 4;
@@ -665,7 +681,7 @@ case_processing_summary (casenumber n_valid, casenumber n_missing)
   tbl = tab_create (n_cols, n_rows, 0);
   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
 
-  tab_dim (tbl, tab_natural_dimensions);
+  tab_dim (tbl, tab_natural_dimensions, NULL);
 
   tab_title (tbl, _("Case Processing Summary"));
 
@@ -704,33 +720,32 @@ case_processing_summary (casenumber n_valid, casenumber n_missing)
   tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
                _("N"));
 
-  tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF,
-               _("%%"));
+  tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
 
   total = n_missing + n_valid;
 
-  tab_float (tbl, 2, heading_rows, TAB_RIGHT,
-            n_valid, 8, 0);
+  tab_double (tbl, 2, heading_rows, TAB_RIGHT,
+            n_valid, wfmt);
 
 
-  tab_float (tbl, 2, heading_rows + 1, TAB_RIGHT,
-            n_missing, 8, 0);
+  tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
+            n_missing, wfmt);
 
 
-  tab_float (tbl, 2, heading_rows + 2, TAB_RIGHT,
-            total, 8, 0);
+  tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
+            total, wfmt);
 
 
-  tab_float (tbl, 3, heading_rows, TAB_RIGHT,
-            100 * n_valid / (double) total, 8, 1);
+  tab_double (tbl, 3, heading_rows, TAB_RIGHT,
+            100 * n_valid / (double) total, NULL);
 
 
-  tab_float (tbl, 3, heading_rows + 1, TAB_RIGHT,
-            100 * n_missing / (double) total, 8, 1);
+  tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
+            100 * n_missing / (double) total, NULL);
 
 
-  tab_float (tbl, 3, heading_rows + 2, TAB_RIGHT,
-            100 * total / (double) total, 8, 1);
+  tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
+            100 * total / (double) total, NULL);
 
 
   tab_submit (tbl);