CROSSTABS: Make 3-way crosstabulation work reasonably. 20100309040527/pspp
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 9 Mar 2010 05:25:46 +0000 (21:25 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 9 Mar 2010 05:29:29 +0000 (21:29 -0800)
As reported in bug #27452, until now 3-way crosstabulations have completely
failed to work.  This commit makes them work, apparently correctly at
first glance.  Only the actual crosstabulation tables have been tested; it
is likely that statistics tables still do not display correctly or cause
runtime failures.

src/language/stats/crosstabs.q
tests/language/stats/crosstabs.at

index fcc8dd12c67a2374735625fc421fa6ef07a0451f..5dc7069af569d597e65fac0eb9daf1ee6d58ba49 100644 (file)
@@ -1141,42 +1141,46 @@ create_crosstab_table (struct crosstabs_proc *proc, struct pivot_table *pt)
 
   struct tab_table *table;
   struct string title;
+  struct pivot_table x;
+
   int i;
 
-  table = tab_create (pt->n_consts + 1 + pt->n_cols + 1,
-                      (pt->n_entries / pt->n_cols) * 3 / 2 * proc->n_cells + 10);
-  tab_headers (table, pt->n_consts + 1, 0, 2, 0);
+  make_pivot_table_subset (pt, 0, 0, &x);
+
+  table = tab_create (x.n_consts + 1 + x.n_cols + 1,
+                      (x.n_entries / x.n_cols) * 3 / 2 * proc->n_cells + 10);
+  tab_headers (table, x.n_consts + 1, 0, 2, 0);
 
   /* First header line. */
-  tab_joint_text (table, pt->n_consts + 1, 0,
-                  (pt->n_consts + 1) + (pt->n_cols - 1), 0,
-                  TAB_CENTER | TAT_TITLE, var_get_name (pt->vars[COL_VAR]));
+  tab_joint_text (table, x.n_consts + 1, 0,
+                  (x.n_consts + 1) + (x.n_cols - 1), 0,
+                  TAB_CENTER | TAT_TITLE, var_get_name (x.vars[COL_VAR]));
 
-  tab_hline (table, TAL_1, pt->n_consts + 1,
-             pt->n_consts + 2 + pt->n_cols - 2, 1);
+  tab_hline (table, TAL_1, x.n_consts + 1,
+             x.n_consts + 2 + x.n_cols - 2, 1);
 
   /* Second header line. */
-  for (i = 2; i < pt->n_consts + 2; i++)
-    tab_joint_text (table, pt->n_consts + 2 - i - 1, 0,
-                    pt->n_consts + 2 - i - 1, 1,
-                    TAB_RIGHT | TAT_TITLE, var_to_string (pt->vars[i]));
-  tab_text (table, pt->n_consts + 2 - 2, 1, TAB_RIGHT | TAT_TITLE,
-            var_get_name (pt->vars[ROW_VAR]));
-  for (i = 0; i < pt->n_cols; i++)
-    table_value_missing (proc, table, pt->n_consts + 2 + i - 1, 1, TAB_RIGHT,
-                         &pt->cols[i], pt->vars[COL_VAR]);
-  tab_text (table, pt->n_consts + 2 + pt->n_cols - 1, 1, TAB_CENTER, _("Total"));
-
-  tab_hline (table, TAL_1, 0, pt->n_consts + 2 + pt->n_cols - 1, 2);
-  tab_vline (table, TAL_1, pt->n_consts + 2 + pt->n_cols - 1, 0, 1);
+  for (i = 2; i < x.n_consts + 2; i++)
+    tab_joint_text (table, x.n_consts + 2 - i - 1, 0,
+                    x.n_consts + 2 - i - 1, 1,
+                    TAB_RIGHT | TAT_TITLE, var_to_string (x.vars[i]));
+  tab_text (table, x.n_consts + 2 - 2, 1, TAB_RIGHT | TAT_TITLE,
+            var_get_name (x.vars[ROW_VAR]));
+  for (i = 0; i < x.n_cols; i++)
+    table_value_missing (proc, table, x.n_consts + 2 + i - 1, 1, TAB_RIGHT,
+                         &x.cols[i], x.vars[COL_VAR]);
+  tab_text (table, x.n_consts + 2 + x.n_cols - 1, 1, TAB_CENTER, _("Total"));
+
+  tab_hline (table, TAL_1, 0, x.n_consts + 2 + x.n_cols - 1, 2);
+  tab_vline (table, TAL_1, x.n_consts + 2 + x.n_cols - 1, 0, 1);
 
   /* Title. */
   ds_init_empty (&title);
-  for (i = 0; i < pt->n_consts + 2; i++)
+  for (i = 0; i < x.n_consts + 2; i++)
     {
       if (i)
         ds_put_cstr (&title, " * ");
-      ds_put_cstr (&title, var_get_name (pt->vars[i]));
+      ds_put_cstr (&title, var_get_name (x.vars[i]));
     }
   for (i = 0; i < pt->n_consts; i++)
     {
@@ -1491,10 +1495,10 @@ static void
 display_dimensions (struct crosstabs_proc *proc, struct pivot_table *pt,
                     struct tab_table *table, int first_difference)
 {
-  tab_hline (table, TAL_1, pt->n_vars - first_difference - 1, tab_nc (table) - 1, 0);
+  tab_hline (table, TAL_1, pt->n_consts + pt->n_vars - first_difference - 1, tab_nc (table) - 1, 0);
 
   for (; first_difference >= 2; first_difference--)
-    table_value_missing (proc, table, pt->n_vars - first_difference - 1, 0,
+    table_value_missing (proc, table, pt->n_consts + pt->n_vars - first_difference - 1, 0,
                         TAB_RIGHT, &pt->entries[0]->values[first_difference],
                         pt->vars[first_difference]);
 }
@@ -1536,15 +1540,16 @@ display_crosstabulation (struct crosstabs_proc *proc, struct pivot_table *pt,
   double *mp;
 
   for (r = 0; r < pt->n_rows; r++)
-    table_value_missing (proc, table, pt->n_vars - 2, r * proc->n_cells,
-                         TAB_RIGHT, &pt->rows[r], pt->vars[ROW_VAR]);
+    table_value_missing (proc, table, pt->n_consts + pt->n_vars - 2,
+                         r * proc->n_cells, TAB_RIGHT, &pt->rows[r],
+                         pt->vars[ROW_VAR]);
 
   tab_text (table, pt->n_vars - 2, pt->n_rows * proc->n_cells,
            TAB_LEFT, _("Total"));
 
   /* Put in the actual cells. */
   mp = pt->mat;
-  tab_offset (table, pt->n_vars - 1, -1);
+  tab_offset (table, pt->n_consts + pt->n_vars - 1, -1);
   for (r = 0; r < pt->n_rows; r++)
     {
       if (proc->n_cells > 1)
index 0e2b3136ceb5f2f05b14678a9c862625010b111e..633679f3fe7a409f718e2d142e3ec795febb0c7d 100644 (file)
@@ -275,3 +275,70 @@ Continuity Correction,.00,1,1.00,,
 N of Valid Cases,4,,,,
 ]])
 AT_CLEANUP
+
+# Bug #24752.
+AT_SETUP([3-way CROSSTABS])
+AT_DATA([crosstabs.sps],
+  [[DATA LIST FIXED
+     / x   1-2
+       y   3
+       z   4.
+
+BEGIN DATA.
+0111 
+0222 
+0311 
+0412 
+0521 
+0612 
+0711 
+0811 
+0912 
+END DATA.
+
+LIST.
+
+
+CROSSTABS TABLES  x by y by z.
+]])
+AT_CHECK([pspp -O format=csv crosstabs.sps], [0],
+  [[Table: Reading 1 record from INLINE.
+Variable,Record,Columns,Format
+x,1,1-  2,F2.0
+y,1,3-  3,F1.0
+z,1,4-  4,F1.0
+
+Table: Data List
+x,y,z
+1,1,1
+2,2,2
+3,1,1
+4,1,2
+5,2,1
+6,1,2
+7,1,1
+8,1,1
+9,1,2
+
+Table: Summary.
+,Cases,,,,,
+,Valid,,Missing,,Total,
+,N,Percent,N,Percent,N,Percent
+x * y * z,9,100.0%,0,0.0%,9,100.0%
+
+Table: x * y * z [count].
+z,,y,,
+,x,1,2,Total
+1,1,1.0,.0,1.0
+,3,1.0,.0,1.0
+,5,.0,1.0,1.0
+,7,1.0,.0,1.0
+,8,1.0,.0,1.0
+Total,,4.0,1.0,5.0
+2,2,.0,1.0,1.0
+,4,1.0,.0,1.0
+,6,1.0,.0,1.0
+,9,1.0,.0,1.0
+Total,,3.0,1.0,4.0
+]])
+AT_CLEANUP