1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/mrset.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/message.h"
34 #include "libpspp/string-array.h"
35 #include "math/moments.h"
36 #include "output/pivot-table.h"
38 #include "gl/minmax.h"
39 #include "gl/xalloc.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) (msgid)
47 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
48 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
49 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
50 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
54 - unweighted summaries (U*)
55 - lower confidence limits (*.LCL)
56 - upper confidence limits (*.UCL)
57 - standard error (*.SE)
60 /* All variables. */ \
61 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
62 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
63 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
64 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
85 /* Scale variables, totals, and subtotals. */ \
86 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
87 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
97 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
98 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
99 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
100 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
101 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
103 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
105 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
106 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
107 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
108 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
110 /* Multiple response sets. */ \
111 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
112 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
134 enum ctables_summary_function
136 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
143 N_CTSF_FUNCTIONS = SUMMARIES
147 enum ctables_domain_type
149 /* Within a section, where stacked variables divide one section from
151 CTDT_TABLE, /* All layers of a whole section. */
152 CTDT_LAYER, /* One layer within a section. */
153 CTDT_LAYERROW, /* Row in one layer within a section. */
154 CTDT_LAYERCOL, /* Column in one layer within a section. */
156 /* Within a subtable, where a subtable pairs an innermost row variable with
157 an innermost column variable within a single layer. */
158 CTDT_SUBTABLE, /* Whole subtable. */
159 CTDT_ROW, /* Row within a subtable. */
160 CTDT_COL, /* Column within a subtable. */
164 struct ctables_domain
166 struct hmap_node node;
168 const struct ctables_cell *example;
176 /* In struct ctables's 'cells' hmap. Indexed by all the values in all the
177 axes (except the scalar variable, if any). */
178 struct hmap_node node;
180 /* The domains that contain this cell. */
181 struct ctables_domain *domains[N_CTDTS];
189 struct ctables_cell_value
191 const struct ctables_category *category;
199 union ctables_summary *summaries;
204 struct pivot_table_look *look;
206 /* If this is NULL, zeros are displayed using the normal print format.
207 Otherwise, this string is displayed. */
210 /* If this is NULL, missing values are displayed using the normal print
211 format. Otherwise, this string is displayed. */
214 /* Indexed by variable dictionary index. */
215 enum ctables_vlabel *vlabels;
217 bool mrsets_count_duplicates; /* MRSETS. */
218 bool smissing_listwise; /* SMISSING. */
219 struct variable *base_weight; /* WEIGHT. */
220 int hide_threshold; /* HIDESMALLCOUNTS. */
222 struct ctables_table **tables;
226 struct ctables_postcompute
228 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
229 const char *name; /* Name, without leading &. */
231 struct ctables_postcompute_expr *expr;
234 bool hide_source_cats;
237 struct ctables_postcompute_expr
239 enum ctables_postcompute_op
247 /* XXX SUBTOTAL and HSUBTOTAL */
260 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
265 XXX what about string ranges? */
268 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
269 struct ctables_postcompute_expr *subs[2];
273 enum ctables_label_position
280 struct ctables_summary_spec_set
282 struct ctables_summary_spec *summaries;
286 struct variable *var;
289 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
293 struct variable **vars;
296 size_t *domains[N_CTDTS];
297 size_t n_domains[N_CTDTS];
299 struct ctables_summary_spec_set cell_summaries;
300 struct ctables_summary_spec_set total_summaries;
305 struct var_array *vas;
311 struct ctables_axis *axes[PIVOT_N_AXES];
312 struct var_array2 vaas[PIVOT_N_AXES];
313 enum pivot_axis_type summary_axis;
315 struct hmap domains[N_CTDTS];
317 enum pivot_axis_type slabels_position;
318 bool slabels_visible;
320 enum ctables_label_position row_labels;
321 enum ctables_label_position col_labels;
323 /* Indexed by variable dictionary index. */
324 struct ctables_categories **categories;
333 struct ctables_chisq *chisq;
334 struct ctables_pairwise *pairwise;
342 struct variable *var;
343 const struct mrset *mrset;
347 static const struct fmt_spec *
348 ctables_var_get_print_format (const struct ctables_var *var)
350 return (var->is_mrset
351 ? var_get_print_format (var->mrset->vars[0])
352 : var_get_print_format (var->var));
356 ctables_var_name (const struct ctables_var *var)
358 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
361 struct ctables_categories
364 struct ctables_category *cats;
369 struct ctables_category
371 enum ctables_category_type
389 struct ctables_category *subtotal;
393 double number; /* CCT_NUMBER. */
394 char *string; /* CCT_STRING. */
395 double range[2]; /* CCT_RANGE. */
396 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
398 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
401 bool include_missing;
405 enum ctables_summary_function sort_function;
406 struct variable *sort_var;
413 ctables_category_uninit (struct ctables_category *cat)
433 free (cat->total_label);
444 ctables_categories_unref (struct ctables_categories *c)
449 assert (c->n_refs > 0);
453 for (size_t i = 0; i < c->n_cats; i++)
454 ctables_category_uninit (&c->cats[i]);
459 /* Chi-square test (SIGTEST). */
467 /* Pairwise comparison test (COMPARETEST). */
468 struct ctables_pairwise
470 enum { PROP, MEAN } type;
473 bool meansvariance_allcats;
475 enum { BONFERRONI = 1, BH } adjust;
499 struct ctables_var var;
501 struct ctables_summary_spec_set cell_summaries;
502 struct ctables_summary_spec_set total_summaries;
506 struct ctables_axis *subs[2];
509 struct msg_location *loc;
512 static void ctables_axis_destroy (struct ctables_axis *);
521 enum ctables_function_availability
523 CTFA_ALL, /* Any variables. */
524 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
525 CTFA_MRSETS, /* Only multiple-response sets */
528 struct ctables_summary_spec
530 enum ctables_summary_function function;
531 double percentile; /* CTSF_PTILE only. */
533 struct fmt_spec format; /* XXX extra CTABLES formats */
537 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
544 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
546 for (size_t i = 0; i < set->n; i++)
547 ctables_summary_spec_uninit (&set->summaries[i]);
548 free (set->summaries);
552 parse_col_width (struct lexer *lexer, const char *name, double *width)
554 lex_match (lexer, T_EQUALS);
555 if (lex_match_id (lexer, "DEFAULT"))
557 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
559 *width = lex_number (lexer);
569 parse_bool (struct lexer *lexer, bool *b)
571 if (lex_match_id (lexer, "NO"))
573 else if (lex_match_id (lexer, "YES"))
577 lex_error_expecting (lexer, "YES", "NO");
583 static enum ctables_function_availability
584 ctables_function_availability (enum ctables_summary_function f)
586 static enum ctables_function_availability availability[] = {
587 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
592 return availability[f];
596 parse_ctables_summary_function (struct lexer *lexer,
597 enum ctables_summary_function *f)
601 enum ctables_summary_function function;
602 struct substring name;
604 static struct pair names[] = {
605 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
606 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
609 /* The .COUNT suffix may be omitted. */
610 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
611 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
612 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
613 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
614 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
615 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
616 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
620 if (!lex_force_id (lexer))
623 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
624 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
626 *f = names[i].function;
631 lex_error (lexer, _("Expecting summary function name."));
636 ctables_axis_destroy (struct ctables_axis *axis)
644 ctables_summary_spec_set_uninit (&axis->cell_summaries);
645 ctables_summary_spec_set_uninit (&axis->total_summaries);
650 ctables_axis_destroy (axis->subs[0]);
651 ctables_axis_destroy (axis->subs[1]);
654 msg_location_destroy (axis->loc);
658 static struct ctables_axis *
659 ctables_axis_new_nonterminal (enum ctables_axis_op op,
660 struct ctables_axis *sub0,
661 struct ctables_axis *sub1,
662 struct lexer *lexer, int start_ofs)
664 struct ctables_axis *axis = xmalloc (sizeof *axis);
665 *axis = (struct ctables_axis) {
667 .subs = { sub0, sub1 },
668 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
673 struct ctables_axis_parse_ctx
676 struct dictionary *dict;
678 struct ctables_table *t;
681 static struct fmt_spec
682 ctables_summary_default_format (enum ctables_summary_function function,
683 const struct ctables_var *var)
685 static const enum ctables_format default_formats[] = {
686 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
690 switch (default_formats[function])
693 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
696 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
699 return *ctables_var_get_print_format (var);
707 ctables_summary_default_label (enum ctables_summary_function function,
710 static const char *default_labels[] = {
711 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
716 return (function == CTSF_PTILE
717 ? xasprintf (_("Percentile %.2f"), percentile)
718 : xstrdup (gettext (default_labels[function])));
722 ctables_summary_function_name (enum ctables_summary_function function)
724 static const char *names[] = {
725 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
729 return names[function];
733 add_summary_spec (struct ctables_axis *axis,
734 enum ctables_summary_function function, double percentile,
735 const char *label, const struct fmt_spec *format,
736 const struct msg_location *loc, bool totals)
738 if (axis->op == CTAO_VAR)
740 const char *function_name = ctables_summary_function_name (function);
741 const char *var_name = ctables_var_name (&axis->var);
742 switch (ctables_function_availability (function))
745 if (!axis->var.is_mrset)
747 msg_at (SE, loc, _("Summary function %s applies only to multiple "
748 "response sets."), function_name);
749 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
759 _("Summary function %s applies only to scale variables."),
761 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
771 struct ctables_summary_spec_set *set = (totals ? &axis->total_summaries
772 : &axis->cell_summaries);
773 if (set->n >= set->allocated)
774 set->summaries = x2nrealloc (set->summaries, &set->allocated,
775 sizeof *set->summaries);
777 struct ctables_summary_spec *dst = &set->summaries[set->n++];
778 *dst = (struct ctables_summary_spec) {
779 .function = function,
780 .percentile = percentile,
781 .label = xstrdup (label),
782 .format = (format ? *format
783 : ctables_summary_default_format (function, &axis->var)),
789 for (size_t i = 0; i < 2; i++)
790 if (!add_summary_spec (axis->subs[i], function, percentile, label,
791 format, loc, totals))
797 static struct ctables_axis *ctables_axis_parse_stack (
798 struct ctables_axis_parse_ctx *);
801 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
802 struct ctables_var *var)
804 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
806 *var = (struct ctables_var) {
808 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
812 lex_error (lexer, _("'%s' does not name a multiple-response set "
813 "in the active file dictionary."),
814 lex_tokcstr (lexer));
822 *var = (struct ctables_var) {
824 .var = parse_variable (lexer, dict),
826 return var->var != NULL;
830 static struct ctables_axis *
831 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
833 if (lex_match (ctx->lexer, T_LPAREN))
835 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
836 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
838 ctables_axis_destroy (sub);
844 if (!lex_force_id (ctx->lexer))
847 int start_ofs = lex_ofs (ctx->lexer);
848 struct ctables_var var;
849 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
852 struct ctables_axis *axis = xmalloc (sizeof *axis);
853 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
855 /* XXX should figure out default measures by reading data */
856 axis->scale = (var.is_mrset ? false
857 : lex_match_phrase (ctx->lexer, "[S]") ? true
858 : lex_match_phrase (ctx->lexer, "[C]") ? false
859 : var_get_measure (var.var) == MEASURE_SCALE);
860 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
861 lex_ofs (ctx->lexer) - 1);
865 static struct ctables_axis *
866 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
868 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
869 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
875 int start_ofs = lex_ofs (ctx->lexer);
877 /* Parse function. */
878 enum ctables_summary_function function;
879 if (!parse_ctables_summary_function (ctx->lexer, &function))
882 /* Parse percentile. */
883 double percentile = 0;
884 if (function == CTSF_PTILE)
886 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
888 percentile = lex_number (ctx->lexer);
889 lex_get (ctx->lexer);
894 if (lex_is_string (ctx->lexer))
896 label = ss_xstrdup (lex_tokss (ctx->lexer));
897 lex_get (ctx->lexer);
900 label = ctables_summary_default_label (function, percentile);
903 struct fmt_spec format;
904 const struct fmt_spec *formatp;
905 if (lex_token (ctx->lexer) == T_ID)
907 if (!parse_format_specifier (ctx->lexer, &format)
908 || !fmt_check_output (&format)
909 || !fmt_check_type_compat (&format, VAL_NUMERIC))
919 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
920 lex_ofs (ctx->lexer) - 1);
921 add_summary_spec (sub, function, percentile, label, formatp, loc,
924 msg_location_destroy (loc);
926 if (lex_match (ctx->lexer, T_COMMA))
928 if (!totals && lex_match_id (ctx->lexer, "TOTALS"))
930 if (!lex_force_match (ctx->lexer, T_LBRACK))
934 else if (lex_force_match (ctx->lexer, T_RBRACK))
936 if (totals && !lex_force_match (ctx->lexer, T_RBRACK))
945 ctables_axis_destroy (sub);
949 static const struct ctables_axis *
950 find_scale (const struct ctables_axis *axis)
954 else if (axis->op == CTAO_VAR)
958 assert (!axis->var.is_mrset);
966 for (size_t i = 0; i < 2; i++)
968 const struct ctables_axis *scale = find_scale (axis->subs[i]);
976 static const struct ctables_axis *
977 find_categorical_summary_spec (const struct ctables_axis *axis)
981 else if (axis->op == CTAO_VAR)
982 return !axis->scale && axis->cell_summaries.n ? axis : NULL;
985 for (size_t i = 0; i < 2; i++)
987 const struct ctables_axis *sum
988 = find_categorical_summary_spec (axis->subs[i]);
996 static struct ctables_axis *
997 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
999 int start_ofs = lex_ofs (ctx->lexer);
1000 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1004 while (lex_match (ctx->lexer, T_GT))
1006 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1010 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1011 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1013 const struct ctables_axis *outer_scale = find_scale (lhs);
1014 const struct ctables_axis *inner_scale = find_scale (rhs);
1015 if (outer_scale && inner_scale)
1017 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1018 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1019 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1020 ctables_axis_destroy (nest);
1024 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1027 msg_at (SE, nest->loc,
1028 _("Summaries may only be requested for categorical variables "
1029 "at the innermost nesting level."));
1030 msg_at (SN, outer_sum->loc,
1031 _("This outer categorical variable has a summary."));
1032 ctables_axis_destroy (nest);
1042 static struct ctables_axis *
1043 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1045 int start_ofs = lex_ofs (ctx->lexer);
1046 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1050 while (lex_match (ctx->lexer, T_PLUS))
1052 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1056 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1057 ctx->lexer, start_ofs);
1064 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1065 struct ctables *ct, struct ctables_table *t,
1066 enum pivot_axis_type a)
1068 if (lex_token (lexer) == T_BY
1069 || lex_token (lexer) == T_SLASH
1070 || lex_token (lexer) == T_ENDCMD)
1073 struct ctables_axis_parse_ctx ctx = {
1079 t->axes[a] = ctables_axis_parse_stack (&ctx);
1080 return t->axes[a] != NULL;
1084 ctables_chisq_destroy (struct ctables_chisq *chisq)
1090 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1096 ctables_table_destroy (struct ctables_table *t)
1101 for (size_t i = 0; i < t->n_categories; i++)
1102 ctables_categories_unref (t->categories[i]);
1103 free (t->categories);
1105 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1106 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1107 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1111 ctables_chisq_destroy (t->chisq);
1112 ctables_pairwise_destroy (t->pairwise);
1117 ctables_destroy (struct ctables *ct)
1122 pivot_table_look_unref (ct->look);
1126 for (size_t i = 0; i < ct->n_tables; i++)
1127 ctables_table_destroy (ct->tables[i]);
1132 static struct ctables_category
1133 cct_range (double low, double high)
1135 return (struct ctables_category) {
1137 .range = { low, high }
1142 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1143 struct ctables_table *t)
1145 if (!lex_match_id (lexer, "VARIABLES"))
1147 lex_match (lexer, T_EQUALS);
1149 struct variable **vars;
1151 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1154 struct ctables_categories *c = xmalloc (sizeof *c);
1155 *c = (struct ctables_categories) { .n_refs = n_vars };
1156 for (size_t i = 0; i < n_vars; i++)
1158 struct ctables_categories **cp
1159 = &t->categories[var_get_dict_index (vars[i])];
1160 ctables_categories_unref (*cp);
1165 size_t allocated_cats = 0;
1166 if (lex_match (lexer, T_LBRACK))
1170 if (c->n_cats >= allocated_cats)
1171 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1173 struct ctables_category *cat = &c->cats[c->n_cats];
1174 if (lex_match_id (lexer, "OTHERNM"))
1175 cat->type = CCT_OTHERNM;
1176 else if (lex_match_id (lexer, "MISSING"))
1177 cat->type = CCT_MISSING;
1178 else if (lex_match_id (lexer, "SUBTOTAL"))
1179 *cat = (struct ctables_category)
1180 { .type = CCT_SUBTOTAL, .total_label = NULL };
1181 else if (lex_match_id (lexer, "HSUBTOTAL"))
1182 *cat = (struct ctables_category)
1183 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1184 else if (lex_match_id (lexer, "LO"))
1186 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1188 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1191 else if (lex_is_number (lexer))
1193 double number = lex_number (lexer);
1195 if (lex_match_id (lexer, "THRU"))
1197 cat->type = CCT_RANGE;
1198 cat->range[0] = number;
1199 if (lex_match_id (lexer, "HI"))
1200 *cat = cct_range (number, DBL_MAX);
1203 if (!lex_force_num (lexer))
1205 *cat = cct_range (number, lex_number (lexer));
1210 *cat = (struct ctables_category) {
1215 else if (lex_is_string (lexer))
1217 *cat = (struct ctables_category) {
1219 .string = ss_xstrdup (lex_tokss (lexer)),
1225 lex_error (lexer, NULL);
1229 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1231 if (lex_match (lexer, T_EQUALS))
1233 if (!lex_force_string (lexer))
1236 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1240 cat->total_label = xstrdup (_("Subtotal"));
1244 lex_match (lexer, T_COMMA);
1246 while (!lex_match (lexer, T_RBRACK));
1249 struct ctables_category cat = {
1251 .include_missing = false,
1252 .sort_ascending = true,
1254 bool show_totals = false;
1255 char *total_label = NULL;
1256 bool totals_before = false;
1257 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1259 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1261 lex_match (lexer, T_EQUALS);
1262 if (lex_match_id (lexer, "A"))
1263 cat.sort_ascending = true;
1264 else if (lex_match_id (lexer, "D"))
1265 cat.sort_ascending = false;
1268 lex_error_expecting (lexer, "A", "D");
1272 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1274 lex_match (lexer, T_EQUALS);
1275 if (lex_match_id (lexer, "VALUE"))
1276 cat.type = CCT_VALUE;
1277 else if (lex_match_id (lexer, "LABEL"))
1278 cat.type = CCT_LABEL;
1281 cat.type = CCT_FUNCTION;
1282 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1285 if (lex_match (lexer, T_LPAREN))
1287 cat.sort_var = parse_variable (lexer, dict);
1291 if (cat.sort_function == CTSF_PTILE)
1293 lex_match (lexer, T_COMMA);
1294 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1296 cat.percentile = lex_number (lexer);
1300 if (!lex_force_match (lexer, T_RPAREN))
1303 else if (ctables_function_availability (cat.sort_function)
1306 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1311 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1313 lex_match (lexer, T_EQUALS);
1314 if (lex_match_id (lexer, "INCLUDE"))
1315 cat.include_missing = true;
1316 else if (lex_match_id (lexer, "EXCLUDE"))
1317 cat.include_missing = false;
1320 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1324 else if (lex_match_id (lexer, "TOTAL"))
1326 lex_match (lexer, T_EQUALS);
1327 if (!parse_bool (lexer, &show_totals))
1330 else if (lex_match_id (lexer, "LABEL"))
1332 lex_match (lexer, T_EQUALS);
1333 if (!lex_force_string (lexer))
1336 total_label = ss_xstrdup (lex_tokss (lexer));
1339 else if (lex_match_id (lexer, "POSITION"))
1341 lex_match (lexer, T_EQUALS);
1342 if (lex_match_id (lexer, "BEFORE"))
1343 totals_before = true;
1344 else if (lex_match_id (lexer, "AFTER"))
1345 totals_before = false;
1348 lex_error_expecting (lexer, "BEFORE", "AFTER");
1352 else if (lex_match_id (lexer, "EMPTY"))
1354 lex_match (lexer, T_EQUALS);
1355 if (lex_match_id (lexer, "INCLUDE"))
1356 c->show_empty = true;
1357 else if (lex_match_id (lexer, "EXCLUDE"))
1358 c->show_empty = false;
1361 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1368 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1369 "TOTAL", "LABEL", "POSITION", "EMPTY");
1371 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1378 if (c->n_cats >= allocated_cats)
1379 c->cats = x2nrealloc (c->cats, &allocated_cats,
1381 c->cats[c->n_cats++] = cat;
1386 if (c->n_cats >= allocated_cats)
1387 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1389 struct ctables_category *totals;
1392 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1393 totals = &c->cats[0];
1396 totals = &c->cats[c->n_cats];
1399 *totals = (struct ctables_category) {
1401 .total_label = total_label ? total_label : xstrdup (_("Total")),
1405 struct ctables_category *subtotal = NULL;
1406 for (size_t i = totals_before ? 0 : c->n_cats;
1407 totals_before ? i < c->n_cats : i-- > 0;
1408 totals_before ? i++ : 0)
1410 struct ctables_category *cat = &c->cats[i];
1418 cat->subtotal = subtotal;
1438 var_array_uninit (struct var_array *va)
1445 var_array2_uninit (struct var_array2 *vaa)
1449 for (size_t i = 0; i < vaa->n; i++)
1450 var_array_uninit (&vaa->vas[i]);
1455 static struct var_array2
1456 nest_fts (struct var_array2 va0, struct var_array2 va1)
1463 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1464 for (size_t i = 0; i < va0.n; i++)
1465 for (size_t j = 0; j < va1.n; j++)
1467 const struct var_array *a = &va0.vas[i];
1468 const struct var_array *b = &va1.vas[j];
1470 size_t allocate = a->n + b->n;
1471 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1472 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1474 for (size_t k = 0; k < a->n; k++)
1475 vars[n++] = a->vars[k];
1476 for (size_t k = 0; k < b->n; k++)
1477 vars[n++] = b->vars[k];
1478 assert (n == allocate);
1480 const struct var_array *summary_src;
1481 if (!a->cell_summaries.var)
1483 else if (!b->cell_summaries.var)
1487 vaa.vas[vaa.n++] = (struct var_array) {
1489 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1490 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1493 .cell_summaries = summary_src->cell_summaries,
1494 .total_summaries = summary_src->total_summaries,
1497 var_array2_uninit (&va0);
1498 var_array2_uninit (&va1);
1502 static struct var_array2
1503 stack_fts (struct var_array2 va0, struct var_array2 va1)
1505 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1506 for (size_t i = 0; i < va0.n; i++)
1507 vaa.vas[vaa.n++] = va0.vas[i];
1508 for (size_t i = 0; i < va1.n; i++)
1509 vaa.vas[vaa.n++] = va1.vas[i];
1510 assert (vaa.n == va0.n + va1.n);
1516 static struct var_array2
1517 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1520 return (struct var_array2) { .n = 0 };
1525 assert (!a->var.is_mrset);
1527 struct variable **vars = xmalloc (sizeof *vars);
1530 struct var_array *va = xmalloc (sizeof *va);
1531 *va = (struct var_array) {
1534 .scale_idx = a->scale ? 0 : SIZE_MAX,
1536 if (a->cell_summaries.n || a->scale)
1538 va->cell_summaries = a->cell_summaries;
1539 va->total_summaries = a->total_summaries;
1540 va->cell_summaries.var = a->var.var;
1541 va->total_summaries.var = a->var.var;
1543 return (struct var_array2) { .vas = va, .n = 1 };
1546 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1547 enumerate_fts (axis_type, a->subs[1]));
1550 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1551 enumerate_fts (axis_type, a->subs[1]));
1557 union ctables_summary
1559 /* COUNT, VALIDN, TOTALN. */
1566 /* MINIMUM, MAXIMUM, RANGE. */
1573 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1574 struct moments1 *moments;
1576 /* XXX percentiles, median, mode, multiple response */
1580 ctables_summary_init (union ctables_summary *s,
1581 const struct ctables_summary_spec *ss)
1583 switch (ss->function)
1587 case CTSF_ROWPCT_COUNT:
1588 case CTSF_COLPCT_COUNT:
1589 case CTSF_TABLEPCT_COUNT:
1590 case CTSF_SUBTABLEPCT_COUNT:
1591 case CTSF_LAYERPCT_COUNT:
1592 case CTSF_LAYERROWPCT_COUNT:
1593 case CTSF_LAYERCOLPCT_COUNT:
1594 case CTSF_ROWPCT_VALIDN:
1595 case CTSF_COLPCT_VALIDN:
1596 case CTSF_TABLEPCT_VALIDN:
1597 case CTSF_SUBTABLEPCT_VALIDN:
1598 case CTSF_LAYERPCT_VALIDN:
1599 case CTSF_LAYERROWPCT_VALIDN:
1600 case CTSF_LAYERCOLPCT_VALIDN:
1601 case CTSF_ROWPCT_TOTALN:
1602 case CTSF_COLPCT_TOTALN:
1603 case CTSF_TABLEPCT_TOTALN:
1604 case CTSF_SUBTABLEPCT_TOTALN:
1605 case CTSF_LAYERPCT_TOTALN:
1606 case CTSF_LAYERROWPCT_TOTALN:
1607 case CTSF_LAYERCOLPCT_TOTALN:
1612 s->missing = s->valid = 0;
1618 s->min = s->max = SYSMIS;
1626 case CTSF_ROWPCT_SUM:
1627 case CTSF_COLPCT_SUM:
1628 case CTSF_TABLEPCT_SUM:
1629 case CTSF_SUBTABLEPCT_SUM:
1630 case CTSF_LAYERPCT_SUM:
1631 case CTSF_LAYERROWPCT_SUM:
1632 case CTSF_LAYERCOLPCT_SUM:
1633 s->moments = moments1_create (MOMENT_VARIANCE);
1642 case CTSF_RESPONSES:
1643 case CTSF_ROWPCT_RESPONSES:
1644 case CTSF_COLPCT_RESPONSES:
1645 case CTSF_TABLEPCT_RESPONSES:
1646 case CTSF_SUBTABLEPCT_RESPONSES:
1647 case CTSF_LAYERPCT_RESPONSES:
1648 case CTSF_LAYERROWPCT_RESPONSES:
1649 case CTSF_LAYERCOLPCT_RESPONSES:
1650 case CTSF_ROWPCT_RESPONSES_COUNT:
1651 case CTSF_COLPCT_RESPONSES_COUNT:
1652 case CTSF_TABLEPCT_RESPONSES_COUNT:
1653 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1654 case CTSF_LAYERPCT_RESPONSES_COUNT:
1655 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1656 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1657 case CTSF_ROWPCT_COUNT_RESPONSES:
1658 case CTSF_COLPCT_COUNT_RESPONSES:
1659 case CTSF_TABLEPCT_COUNT_RESPONSES:
1660 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1661 case CTSF_LAYERPCT_COUNT_RESPONSES:
1662 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1663 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1669 ctables_summary_uninit (union ctables_summary *s,
1670 const struct ctables_summary_spec *ss)
1672 switch (ss->function)
1676 case CTSF_ROWPCT_COUNT:
1677 case CTSF_COLPCT_COUNT:
1678 case CTSF_TABLEPCT_COUNT:
1679 case CTSF_SUBTABLEPCT_COUNT:
1680 case CTSF_LAYERPCT_COUNT:
1681 case CTSF_LAYERROWPCT_COUNT:
1682 case CTSF_LAYERCOLPCT_COUNT:
1683 case CTSF_ROWPCT_VALIDN:
1684 case CTSF_COLPCT_VALIDN:
1685 case CTSF_TABLEPCT_VALIDN:
1686 case CTSF_SUBTABLEPCT_VALIDN:
1687 case CTSF_LAYERPCT_VALIDN:
1688 case CTSF_LAYERROWPCT_VALIDN:
1689 case CTSF_LAYERCOLPCT_VALIDN:
1690 case CTSF_ROWPCT_TOTALN:
1691 case CTSF_COLPCT_TOTALN:
1692 case CTSF_TABLEPCT_TOTALN:
1693 case CTSF_SUBTABLEPCT_TOTALN:
1694 case CTSF_LAYERPCT_TOTALN:
1695 case CTSF_LAYERROWPCT_TOTALN:
1696 case CTSF_LAYERCOLPCT_TOTALN:
1713 case CTSF_ROWPCT_SUM:
1714 case CTSF_COLPCT_SUM:
1715 case CTSF_TABLEPCT_SUM:
1716 case CTSF_SUBTABLEPCT_SUM:
1717 case CTSF_LAYERPCT_SUM:
1718 case CTSF_LAYERROWPCT_SUM:
1719 case CTSF_LAYERCOLPCT_SUM:
1720 moments1_destroy (s->moments);
1729 case CTSF_RESPONSES:
1730 case CTSF_ROWPCT_RESPONSES:
1731 case CTSF_COLPCT_RESPONSES:
1732 case CTSF_TABLEPCT_RESPONSES:
1733 case CTSF_SUBTABLEPCT_RESPONSES:
1734 case CTSF_LAYERPCT_RESPONSES:
1735 case CTSF_LAYERROWPCT_RESPONSES:
1736 case CTSF_LAYERCOLPCT_RESPONSES:
1737 case CTSF_ROWPCT_RESPONSES_COUNT:
1738 case CTSF_COLPCT_RESPONSES_COUNT:
1739 case CTSF_TABLEPCT_RESPONSES_COUNT:
1740 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1741 case CTSF_LAYERPCT_RESPONSES_COUNT:
1742 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1743 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1744 case CTSF_ROWPCT_COUNT_RESPONSES:
1745 case CTSF_COLPCT_COUNT_RESPONSES:
1746 case CTSF_TABLEPCT_COUNT_RESPONSES:
1747 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1748 case CTSF_LAYERPCT_COUNT_RESPONSES:
1749 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1750 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1756 ctables_summary_add (union ctables_summary *s,
1757 const struct ctables_summary_spec *ss,
1758 const struct variable *var, const union value *value,
1761 switch (ss->function)
1765 case CTSF_ROWPCT_COUNT:
1766 case CTSF_COLPCT_COUNT:
1767 case CTSF_TABLEPCT_COUNT:
1768 case CTSF_SUBTABLEPCT_COUNT:
1769 case CTSF_LAYERPCT_COUNT:
1770 case CTSF_LAYERROWPCT_COUNT:
1771 case CTSF_LAYERCOLPCT_COUNT:
1772 case CTSF_ROWPCT_VALIDN:
1773 case CTSF_COLPCT_VALIDN:
1774 case CTSF_TABLEPCT_VALIDN:
1775 case CTSF_SUBTABLEPCT_VALIDN:
1776 case CTSF_LAYERPCT_VALIDN:
1777 case CTSF_LAYERROWPCT_VALIDN:
1778 case CTSF_LAYERCOLPCT_VALIDN:
1779 case CTSF_ROWPCT_TOTALN:
1780 case CTSF_COLPCT_TOTALN:
1781 case CTSF_TABLEPCT_TOTALN:
1782 case CTSF_SUBTABLEPCT_TOTALN:
1783 case CTSF_LAYERPCT_TOTALN:
1784 case CTSF_LAYERROWPCT_TOTALN:
1785 case CTSF_LAYERCOLPCT_TOTALN:
1790 if (var_is_value_missing (var, value))
1791 s->missing += weight;
1799 if (!var_is_value_missing (var, value))
1801 assert (!var_is_alpha (var)); /* XXX? */
1802 if (s->min == SYSMIS || value->f < s->min)
1804 if (s->max == SYSMIS || value->f > s->max)
1814 case CTSF_ROWPCT_SUM:
1815 case CTSF_COLPCT_SUM:
1816 case CTSF_TABLEPCT_SUM:
1817 case CTSF_SUBTABLEPCT_SUM:
1818 case CTSF_LAYERPCT_SUM:
1819 case CTSF_LAYERROWPCT_SUM:
1820 case CTSF_LAYERCOLPCT_SUM:
1821 moments1_add (s->moments, value->f, weight);
1830 case CTSF_RESPONSES:
1831 case CTSF_ROWPCT_RESPONSES:
1832 case CTSF_COLPCT_RESPONSES:
1833 case CTSF_TABLEPCT_RESPONSES:
1834 case CTSF_SUBTABLEPCT_RESPONSES:
1835 case CTSF_LAYERPCT_RESPONSES:
1836 case CTSF_LAYERROWPCT_RESPONSES:
1837 case CTSF_LAYERCOLPCT_RESPONSES:
1838 case CTSF_ROWPCT_RESPONSES_COUNT:
1839 case CTSF_COLPCT_RESPONSES_COUNT:
1840 case CTSF_TABLEPCT_RESPONSES_COUNT:
1841 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1842 case CTSF_LAYERPCT_RESPONSES_COUNT:
1843 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1844 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1845 case CTSF_ROWPCT_COUNT_RESPONSES:
1846 case CTSF_COLPCT_COUNT_RESPONSES:
1847 case CTSF_TABLEPCT_COUNT_RESPONSES:
1848 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1849 case CTSF_LAYERPCT_COUNT_RESPONSES:
1850 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1851 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1857 ctables_summary_value (const struct ctables_cell *cell,
1858 union ctables_summary *s,
1859 const struct ctables_summary_spec *ss)
1861 switch (ss->function)
1867 case CTSF_SUBTABLEPCT_COUNT:
1868 return cell->domains[CTDT_SUBTABLE]->valid ? s->valid / cell->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1870 case CTSF_ROWPCT_COUNT:
1871 return cell->domains[CTDT_ROW]->valid ? s->valid / cell->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1873 case CTSF_COLPCT_COUNT:
1874 return cell->domains[CTDT_COL]->valid ? s->valid / cell->domains[CTDT_COL]->valid * 100 : SYSMIS;
1876 case CTSF_TABLEPCT_COUNT:
1877 return cell->domains[CTDT_TABLE]->valid ? s->valid / cell->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1879 case CTSF_LAYERPCT_COUNT:
1880 return cell->domains[CTDT_LAYER]->valid ? s->valid / cell->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1882 case CTSF_LAYERROWPCT_COUNT:
1883 return cell->domains[CTDT_LAYERROW]->valid ? s->valid / cell->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1885 case CTSF_LAYERCOLPCT_COUNT:
1886 return cell->domains[CTDT_LAYERCOL]->valid ? s->valid / cell->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1888 case CTSF_ROWPCT_VALIDN:
1889 case CTSF_COLPCT_VALIDN:
1890 case CTSF_TABLEPCT_VALIDN:
1891 case CTSF_SUBTABLEPCT_VALIDN:
1892 case CTSF_LAYERPCT_VALIDN:
1893 case CTSF_LAYERROWPCT_VALIDN:
1894 case CTSF_LAYERCOLPCT_VALIDN:
1895 case CTSF_ROWPCT_TOTALN:
1896 case CTSF_COLPCT_TOTALN:
1897 case CTSF_TABLEPCT_TOTALN:
1898 case CTSF_SUBTABLEPCT_TOTALN:
1899 case CTSF_LAYERPCT_TOTALN:
1900 case CTSF_LAYERROWPCT_TOTALN:
1901 case CTSF_LAYERCOLPCT_TOTALN:
1906 return s->valid + s->missing;
1919 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1924 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1930 double weight, variance;
1931 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1932 return calc_semean (variance, weight);
1938 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1939 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1944 double weight, mean;
1945 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1946 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1952 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1956 case CTSF_ROWPCT_SUM:
1957 case CTSF_COLPCT_SUM:
1958 case CTSF_TABLEPCT_SUM:
1959 case CTSF_SUBTABLEPCT_SUM:
1960 case CTSF_LAYERPCT_SUM:
1961 case CTSF_LAYERROWPCT_SUM:
1962 case CTSF_LAYERCOLPCT_SUM:
1971 case CTSF_RESPONSES:
1972 case CTSF_ROWPCT_RESPONSES:
1973 case CTSF_COLPCT_RESPONSES:
1974 case CTSF_TABLEPCT_RESPONSES:
1975 case CTSF_SUBTABLEPCT_RESPONSES:
1976 case CTSF_LAYERPCT_RESPONSES:
1977 case CTSF_LAYERROWPCT_RESPONSES:
1978 case CTSF_LAYERCOLPCT_RESPONSES:
1979 case CTSF_ROWPCT_RESPONSES_COUNT:
1980 case CTSF_COLPCT_RESPONSES_COUNT:
1981 case CTSF_TABLEPCT_RESPONSES_COUNT:
1982 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1983 case CTSF_LAYERPCT_RESPONSES_COUNT:
1984 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1985 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1986 case CTSF_ROWPCT_COUNT_RESPONSES:
1987 case CTSF_COLPCT_COUNT_RESPONSES:
1988 case CTSF_TABLEPCT_COUNT_RESPONSES:
1989 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1990 case CTSF_LAYERPCT_COUNT_RESPONSES:
1991 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1992 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1999 struct ctables_cell_sort_aux
2001 const struct ctables_table *t;
2002 enum pivot_axis_type a;
2006 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2008 const struct ctables_cell_sort_aux *aux = aux_;
2009 struct ctables_cell *const *ap = a_;
2010 struct ctables_cell *const *bp = b_;
2011 const struct ctables_cell *a = *ap;
2012 const struct ctables_cell *b = *bp;
2014 size_t a_idx = a->axes[aux->a].vaa_idx;
2015 size_t b_idx = b->axes[aux->a].vaa_idx;
2017 return a_idx < b_idx ? -1 : 1;
2019 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
2020 for (size_t i = 0; i < va->n; i++)
2021 if (i != va->scale_idx)
2023 const struct variable *var = va->vars[i];
2024 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2025 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2026 if (a_cv->category != b_cv->category)
2027 return a_cv->category > b_cv->category ? 1 : -1;
2029 const union value *a_val = &a_cv->value;
2030 const union value *b_val = &b_cv->value;
2031 switch (a_cv->category->type)
2038 /* Must be equal. */
2045 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2053 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2055 return a_cv->category->sort_ascending ? cmp : -cmp;
2061 const char *a_label = var_lookup_value_label (var, a_val);
2062 const char *b_label = var_lookup_value_label (var, b_val);
2064 ? (b_label ? strcmp (a_label, b_label) : 1)
2065 : (b_label ? -1 : value_compare_3way (
2066 a_val, b_val, var_get_width (var))));
2068 return a_cv->category->sort_ascending ? cmp : -cmp;
2082 For each ctables_table:
2083 For each combination of row vars:
2084 For each combination of column vars:
2085 For each combination of layer vars:
2087 Make a table of row values:
2088 Sort entries by row values
2089 Assign a 0-based index to each actual value
2090 Construct a dimension
2091 Make a table of column values
2092 Make a table of layer values
2094 Fill the table entry using the indexes from before.
2097 static struct ctables_domain *
2098 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *cell,
2099 enum ctables_domain_type domain)
2102 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2104 size_t idx = cell->axes[a].vaa_idx;
2105 const struct var_array *va = &t->vaas[a].vas[idx];
2106 hash = hash_int (idx, hash);
2107 for (size_t i = 0; i < va->n_domains[domain]; i++)
2109 size_t v_idx = va->domains[domain][i];
2110 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2111 var_get_width (va->vars[v_idx]), hash);
2115 struct ctables_domain *d;
2116 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2118 const struct ctables_cell *df = d->example;
2119 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2121 size_t idx = cell->axes[a].vaa_idx;
2122 if (idx != df->axes[a].vaa_idx)
2125 const struct var_array *va = &t->vaas[a].vas[idx];
2126 for (size_t i = 0; i < va->n_domains[domain]; i++)
2128 size_t v_idx = va->domains[domain][i];
2129 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2130 &cell->axes[a].cvs[v_idx].value,
2131 var_get_width (va->vars[v_idx])))
2140 d = xmalloc (sizeof *d);
2141 *d = (struct ctables_domain) { .example = cell };
2142 hmap_insert (&t->domains[domain], &d->node, hash);
2146 static const struct ctables_category *
2147 ctables_categories_match (const struct ctables_categories *c,
2148 const union value *v, const struct variable *var)
2150 const struct ctables_category *othernm = NULL;
2151 for (size_t i = c->n_cats; i-- > 0; )
2153 const struct ctables_category *cat = &c->cats[i];
2157 if (cat->number == v->f)
2165 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2166 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2171 if (var_is_value_missing (var, v))
2188 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2193 return var_is_value_missing (var, v) ? NULL : othernm;
2196 static const struct ctables_category *
2197 ctables_categories_total (const struct ctables_categories *c)
2199 const struct ctables_category *first = &c->cats[0];
2200 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2201 return (first->type == CCT_TOTAL ? first
2202 : last->type == CCT_TOTAL ? last
2207 ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c,
2208 size_t ix[PIVOT_N_AXES],
2209 const struct ctables_category *cats[PIVOT_N_AXES][10],
2212 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
2216 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2218 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2219 hash = hash_int (ix[a], hash);
2220 for (size_t i = 0; i < va->n; i++)
2221 if (i != va->scale_idx)
2223 hash = hash_pointer (cats[a][i], hash);
2224 if (cats[a][i]->type != CCT_TOTAL
2225 && cats[a][i]->type != CCT_SUBTOTAL
2226 && cats[a][i]->type != CCT_HSUBTOTAL)
2227 hash = value_hash (case_data (c, va->vars[i]),
2228 var_get_width (va->vars[i]), hash);
2234 struct ctables_cell *cell;
2235 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &t->cells)
2237 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2239 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2240 if (cell->axes[a].vaa_idx != ix[a])
2242 for (size_t i = 0; i < va->n; i++)
2243 if (i != va->scale_idx
2244 && (cats[a][i] != cell->axes[a].cvs[i].category
2245 || (cats[a][i]->type != CCT_TOTAL
2246 && cats[a][i]->type != CCT_SUBTOTAL
2247 && cats[a][i]->type != CCT_HSUBTOTAL
2248 && !value_equal (case_data (c, va->vars[i]),
2249 &cell->axes[a].cvs[i].value,
2250 var_get_width (va->vars[i])))))
2259 cell = xmalloc (sizeof *cell);
2261 cell->total = total;
2262 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2264 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2265 cell->axes[a].vaa_idx = ix[a];
2266 cell->axes[a].cvs = (va->n
2267 ? xnmalloc (va->n, sizeof *cell->axes[a].cvs)
2269 for (size_t i = 0; i < va->n; i++)
2271 if (i != va->scale_idx)
2273 const struct ctables_category *subtotal = cats[a][i]->subtotal;
2274 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2278 cell->axes[a].cvs[i].category = cats[a][i];
2279 value_clone (&cell->axes[a].cvs[i].value, case_data (c, va->vars[i]),
2280 var_get_width (va->vars[i]));
2285 const struct ctables_summary_spec_set *sss = &ss->cell_summaries;
2286 cell->summaries = xmalloc (sss->n * sizeof *cell->summaries);
2287 for (size_t i = 0; i < sss->n; i++)
2288 ctables_summary_init (&cell->summaries[i], &sss->summaries[i]);
2290 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2291 cell->domains[dt] = ctables_domain_insert (t, cell, dt);
2292 hmap_insert (&t->cells, &cell->node, hash);
2295 const struct ctables_summary_spec_set *sss = &ss->cell_summaries;
2296 for (size_t i = 0; i < sss->n; i++)
2297 ctables_summary_add (&cell->summaries[i], &sss->summaries[i], sss->var,
2298 case_data (c, sss->var), weight);
2299 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2300 cell->domains[dt]->valid += weight;
2304 recurse_totals (struct ctables_table *t, const struct ccase *c,
2305 size_t ix[PIVOT_N_AXES],
2306 const struct ctables_category *cats[PIVOT_N_AXES][10],
2308 enum pivot_axis_type start_a, size_t start_va)
2310 for (enum pivot_axis_type a = start_a; a < PIVOT_N_AXES; a++)
2312 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2313 for (size_t i = start_va; i < va->n; i++)
2315 if (i == va->scale_idx)
2318 const struct variable *var = va->vars[i];
2320 const struct ctables_category *total = ctables_categories_total (
2321 t->categories[var_get_dict_index (var)]);
2324 const struct ctables_category *save = cats[a][i];
2326 ctables_cell_insert__ (t, c, ix, cats, weight);
2327 recurse_totals (t, c, ix, cats, weight, a, i + 1);
2336 ctables_cell_insert (struct ctables_table *t,
2337 const struct ccase *c,
2338 size_t ir, size_t ic, size_t il,
2341 size_t ix[PIVOT_N_AXES] = {
2342 [PIVOT_AXIS_ROW] = ir,
2343 [PIVOT_AXIS_COLUMN] = ic,
2344 [PIVOT_AXIS_LAYER] = il,
2347 const struct ctables_category *cats[PIVOT_N_AXES][10];
2348 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2350 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2351 for (size_t i = 0; i < va->n; i++)
2353 if (i == va->scale_idx)
2356 const struct variable *var = va->vars[i];
2357 const union value *value = case_data (c, var);
2359 if (var_is_numeric (var) && value->f == SYSMIS)
2362 cats[a][i] = ctables_categories_match (
2363 t->categories[var_get_dict_index (var)], value, var);
2369 ctables_cell_insert__ (t, c, ix, cats, weight);
2371 recurse_totals (t, c, ix, cats, weight, 0, 0);
2373 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2375 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2376 for (size_t i = 0; i < va->n; i++)
2378 if (i == va->scale_idx)
2381 const struct ctables_category *save = cats[a][i];
2384 cats[a][i] = save->subtotal;
2385 ctables_cell_insert__ (t, c, ix, cats, weight);
2393 ctables_execute (struct dataset *ds, struct ctables *ct)
2395 for (size_t i = 0; i < ct->n_tables; i++)
2397 struct ctables_table *t = ct->tables[i];
2398 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2401 t->vaas[a] = enumerate_fts (a, t->axes[a]);
2403 for (size_t j = 0; j < t->vaas[a].n; j++)
2405 struct var_array *va = &t->vaas[a].vas[j];
2406 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2408 va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
2409 va->n_domains[dt] = 0;
2411 for (size_t k = 0; k < va->n; k++)
2413 if (k == va->scale_idx)
2422 if (a != PIVOT_AXIS_LAYER)
2429 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2430 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2431 : a == PIVOT_AXIS_ROW)
2434 || (va->scale_idx == va->n - 1
2441 if (a == PIVOT_AXIS_COLUMN)
2446 if (a == PIVOT_AXIS_ROW)
2451 va->domains[dt][va->n_domains[dt]++] = k;
2458 struct var_array *va = xmalloc (sizeof *va);
2459 *va = (struct var_array) { .n = 0 };
2460 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
2463 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
2465 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
2466 if (!va->cell_summaries.n)
2468 struct ctables_summary_spec_set *css = &va->cell_summaries;
2469 css->summaries = xmalloc (sizeof *css->summaries);
2472 enum ctables_summary_function function
2473 = css->var ? CTSF_MEAN : CTSF_COUNT;
2474 struct ctables_var var = { .is_mrset = false, .var = css->var };
2476 *css->summaries = (struct ctables_summary_spec) {
2477 .function = function,
2478 .format = ctables_summary_default_format (function, &var),
2479 .label = ctables_summary_default_label (function, 0),
2482 css->var = va->vars[0];
2484 va->total_summaries = va->cell_summaries;
2489 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2492 bool warn_on_invalid = true;
2493 double total_weight = 0;
2494 for (struct ccase *c = casereader_read (input); c;
2495 case_unref (c), c = casereader_read (input))
2497 double weight = dict_get_case_weight (dataset_dict (ds), c,
2499 total_weight += weight;
2501 for (size_t i = 0; i < ct->n_tables; i++)
2503 struct ctables_table *t = ct->tables[i];
2505 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2506 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2507 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2508 ctables_cell_insert (t, c, ir, ic, il, weight);
2511 casereader_destroy (input);
2513 for (size_t i = 0; i < ct->n_tables; i++)
2515 struct ctables_table *t = ct->tables[i];
2517 struct pivot_table *pt = pivot_table_create__ (
2519 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2520 : pivot_value_new_text (N_("Custom Tables"))),
2523 pivot_table_set_caption (
2524 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2526 pivot_table_set_caption (
2527 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2529 pivot_table_set_look (pt, ct->look);
2530 struct pivot_dimension *d[PIVOT_N_AXES];
2531 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2533 static const char *names[] = {
2534 [PIVOT_AXIS_ROW] = N_("Rows"),
2535 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2536 [PIVOT_AXIS_LAYER] = N_("Layers"),
2538 d[a] = (t->axes[a] || a == t->summary_axis
2539 ? pivot_dimension_create (pt, a, names[a])
2544 assert (t->axes[a]);
2546 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2548 struct ctables_cell *cell;
2550 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2553 assert (n <= t->cells.count);
2555 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2556 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2558 size_t max_depth = 0;
2559 for (size_t j = 0; j < t->vaas[a].n; j++)
2560 if (t->vaas[a].vas[j].n > max_depth)
2561 max_depth = t->vaas[a].vas[j].n;
2563 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2564 struct pivot_category *top = NULL;
2566 for (size_t j = 0; j < n; j++)
2568 struct ctables_cell *cell = sorted[j];
2569 const struct var_array *va = &t->vaas[a].vas[cell->axes[a].vaa_idx];
2571 size_t n_common = 0;
2572 bool new_subtable = false;
2575 struct ctables_cell *prev = sorted[j - 1];
2576 if (prev->axes[a].vaa_idx == cell->axes[a].vaa_idx)
2578 for (; n_common < va->n; n_common++)
2579 if (n_common != va->scale_idx
2580 && (prev->axes[a].cvs[n_common].category
2581 != cell->axes[a].cvs[n_common].category
2582 || !value_equal (&prev->axes[a].cvs[n_common].value,
2583 &cell->axes[a].cvs[n_common].value,
2584 var_get_type (va->vars[n_common]))))
2588 new_subtable = true;
2591 new_subtable = true;
2595 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2597 if (vlabel != CTVL_NONE)
2598 top = pivot_category_create_group__ (
2599 top, pivot_value_new_variable (va->vars[0]));
2601 if (n_common == va->n)
2603 cell->axes[a].leaf = prev_leaf;
2607 for (size_t k = n_common; k < va->n; k++)
2609 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2611 struct pivot_value *label
2612 = (k == va->scale_idx ? NULL
2613 : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
2614 || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
2615 || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
2616 ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
2618 : pivot_value_new_var_value (va->vars[k],
2619 &cell->axes[a].cvs[k].value));
2622 if (a == t->summary_axis)
2625 parent = pivot_category_create_group__ (parent, label);
2626 for (size_t m = 0; m < va->cell_summaries.n; m++)
2628 int leaf = pivot_category_create_leaf (
2629 parent, pivot_value_new_text (va->cell_summaries.summaries[m].label));
2636 /* This assertion is true as long as the summary axis
2637 is the axis where the summaries are displayed. */
2640 prev_leaf = pivot_category_create_leaf (parent, label);
2646 parent = pivot_category_create_group__ (parent, label);
2648 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2649 if (vlabel != CTVL_NONE)
2650 parent = pivot_category_create_group__ (
2651 parent, pivot_value_new_variable (va->vars[k + 1]));
2655 cell->axes[a].leaf = prev_leaf;
2660 struct ctables_cell *cell;
2661 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2666 const struct ctables_summary_spec_set *sss = &t->vaas[t->summary_axis].vas[cell->axes[t->summary_axis].vaa_idx].cell_summaries;
2667 for (size_t j = 0; j < sss->n; j++)
2670 size_t n_dindexes = 0;
2672 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2675 int leaf = cell->axes[a].leaf;
2676 if (a == t->summary_axis)
2678 dindexes[n_dindexes++] = leaf;
2681 double d = ctables_summary_value (cell, &cell->summaries[j], &sss->summaries[j]);
2682 struct pivot_value *value = pivot_value_new_number (d);
2683 value->numeric.format = sss->summaries[j].format;
2684 pivot_table_put (pt, dindexes, n_dindexes, value);
2688 pivot_table_submit (pt);
2691 return proc_commit (ds);
2695 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2697 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2698 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2699 enum settings_value_show tvars = settings_get_show_variables ();
2700 for (size_t i = 0; i < n_vars; i++)
2701 vlabels[i] = (enum ctables_vlabel) tvars;
2703 struct ctables *ct = xmalloc (sizeof *ct);
2704 *ct = (struct ctables) {
2705 .look = pivot_table_look_unshare (pivot_table_look_ref (
2706 pivot_table_look_get_default ())),
2708 .hide_threshold = 5,
2710 ct->look->omit_empty = false;
2712 if (!lex_force_match (lexer, T_SLASH))
2715 while (!lex_match_id (lexer, "TABLE"))
2717 if (lex_match_id (lexer, "FORMAT"))
2719 double widths[2] = { SYSMIS, SYSMIS };
2720 double units_per_inch = 72.0;
2722 while (lex_token (lexer) != T_SLASH)
2724 if (lex_match_id (lexer, "MINCOLWIDTH"))
2726 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2729 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2731 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2734 else if (lex_match_id (lexer, "UNITS"))
2736 lex_match (lexer, T_EQUALS);
2737 if (lex_match_id (lexer, "POINTS"))
2738 units_per_inch = 72.0;
2739 else if (lex_match_id (lexer, "INCHES"))
2740 units_per_inch = 1.0;
2741 else if (lex_match_id (lexer, "CM"))
2742 units_per_inch = 2.54;
2745 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2749 else if (lex_match_id (lexer, "EMPTY"))
2754 lex_match (lexer, T_EQUALS);
2755 if (lex_match_id (lexer, "ZERO"))
2757 /* Nothing to do. */
2759 else if (lex_match_id (lexer, "BLANK"))
2760 ct->zero = xstrdup ("");
2761 else if (lex_force_string (lexer))
2763 ct->zero = ss_xstrdup (lex_tokss (lexer));
2769 else if (lex_match_id (lexer, "MISSING"))
2771 lex_match (lexer, T_EQUALS);
2772 if (!lex_force_string (lexer))
2776 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2777 ? ss_xstrdup (lex_tokss (lexer))
2783 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2784 "UNITS", "EMPTY", "MISSING");
2789 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2790 && widths[0] > widths[1])
2792 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2796 for (size_t i = 0; i < 2; i++)
2797 if (widths[i] != SYSMIS)
2799 int *wr = ct->look->width_ranges[TABLE_HORZ];
2800 wr[i] = widths[i] / units_per_inch * 96.0;
2805 else if (lex_match_id (lexer, "VLABELS"))
2807 if (!lex_force_match_id (lexer, "VARIABLES"))
2809 lex_match (lexer, T_EQUALS);
2811 struct variable **vars;
2813 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2817 if (!lex_force_match_id (lexer, "DISPLAY"))
2822 lex_match (lexer, T_EQUALS);
2824 enum ctables_vlabel vlabel;
2825 if (lex_match_id (lexer, "DEFAULT"))
2826 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2827 else if (lex_match_id (lexer, "NAME"))
2829 else if (lex_match_id (lexer, "LABEL"))
2830 vlabel = CTVL_LABEL;
2831 else if (lex_match_id (lexer, "BOTH"))
2833 else if (lex_match_id (lexer, "NONE"))
2837 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2843 for (size_t i = 0; i < n_vars; i++)
2844 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2847 else if (lex_match_id (lexer, "MRSETS"))
2849 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2851 lex_match (lexer, T_EQUALS);
2852 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2855 else if (lex_match_id (lexer, "SMISSING"))
2857 if (lex_match_id (lexer, "VARIABLE"))
2858 ct->smissing_listwise = false;
2859 else if (lex_match_id (lexer, "LISTWISE"))
2860 ct->smissing_listwise = true;
2863 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2868 else if (lex_match_id (lexer, "WEIGHT"))
2870 if (!lex_force_match_id (lexer, "VARIABLE"))
2872 lex_match (lexer, T_EQUALS);
2873 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2874 if (!ct->base_weight)
2877 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2879 if (!lex_force_match_id (lexer, "COUNT"))
2881 lex_match (lexer, T_EQUALS);
2882 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2884 ct->hide_threshold = lex_integer (lexer);
2889 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2890 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2891 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2895 if (!lex_force_match (lexer, T_SLASH))
2899 size_t allocated_tables = 0;
2902 if (ct->n_tables >= allocated_tables)
2903 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2904 sizeof *ct->tables);
2906 struct ctables_category *cat = xmalloc (sizeof *cat);
2907 *cat = (struct ctables_category) {
2909 .include_missing = false,
2910 .sort_ascending = true,
2913 struct ctables_categories *c = xmalloc (sizeof *c);
2914 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2915 *c = (struct ctables_categories) {
2921 struct ctables_categories **categories = xnmalloc (n_vars,
2922 sizeof *categories);
2923 for (size_t i = 0; i < n_vars; i++)
2926 struct ctables_table *t = xmalloc (sizeof *t);
2927 *t = (struct ctables_table) {
2928 .cells = HMAP_INITIALIZER (t->cells),
2929 .slabels_position = PIVOT_AXIS_COLUMN,
2930 .slabels_visible = true,
2931 .row_labels = CTLP_NORMAL,
2932 .col_labels = CTLP_NORMAL,
2933 .categories = categories,
2934 .n_categories = n_vars,
2937 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2938 hmap_init (&t->domains[dt]);
2939 ct->tables[ct->n_tables++] = t;
2941 lex_match (lexer, T_EQUALS);
2942 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2944 if (lex_match (lexer, T_BY))
2946 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2947 ct, t, PIVOT_AXIS_COLUMN))
2950 if (lex_match (lexer, T_BY))
2952 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2953 ct, t, PIVOT_AXIS_LAYER))
2958 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2959 && !t->axes[PIVOT_AXIS_LAYER])
2961 lex_error (lexer, _("At least one variable must be specified."));
2965 const struct ctables_axis *scales[PIVOT_N_AXES];
2966 size_t n_scales = 0;
2967 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2969 scales[a] = find_scale (t->axes[a]);
2975 msg (SE, _("Scale variables may appear only on one axis."));
2976 if (scales[PIVOT_AXIS_ROW])
2977 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2978 _("This scale variable appears on the rows axis."));
2979 if (scales[PIVOT_AXIS_COLUMN])
2980 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2981 _("This scale variable appears on the columns axis."));
2982 if (scales[PIVOT_AXIS_LAYER])
2983 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2984 _("This scale variable appears on the layer axis."));
2988 const struct ctables_axis *summaries[PIVOT_N_AXES];
2989 size_t n_summaries = 0;
2990 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2992 summaries[a] = (scales[a]
2994 : find_categorical_summary_spec (t->axes[a]));
2998 if (n_summaries > 1)
3000 msg (SE, _("Summaries may appear only on one axis."));
3001 if (summaries[PIVOT_AXIS_ROW])
3002 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3003 _("This variable on the rows axis has a summary."));
3004 if (summaries[PIVOT_AXIS_COLUMN])
3005 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3006 _("This variable on the columns axis has a summary."));
3007 if (summaries[PIVOT_AXIS_LAYER])
3008 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3009 _("This variable on the layers axis has a summary."));
3012 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3013 if (n_summaries ? summaries[a] : t->axes[a])
3015 t->summary_axis = a;
3019 if (lex_token (lexer) == T_ENDCMD)
3021 if (!lex_force_match (lexer, T_SLASH))
3024 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3026 if (lex_match_id (lexer, "SLABELS"))
3028 while (lex_token (lexer) != T_SLASH)
3030 if (lex_match_id (lexer, "POSITION"))
3032 lex_match (lexer, T_EQUALS);
3033 if (lex_match_id (lexer, "COLUMN"))
3034 t->slabels_position = PIVOT_AXIS_COLUMN;
3035 else if (lex_match_id (lexer, "ROW"))
3036 t->slabels_position = PIVOT_AXIS_ROW;
3037 else if (lex_match_id (lexer, "LAYER"))
3038 t->slabels_position = PIVOT_AXIS_LAYER;
3041 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3045 else if (lex_match_id (lexer, "VISIBLE"))
3047 lex_match (lexer, T_EQUALS);
3048 if (!parse_bool (lexer, &t->slabels_visible))
3053 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3058 else if (lex_match_id (lexer, "CLABELS"))
3060 while (lex_token (lexer) != T_SLASH)
3062 if (lex_match_id (lexer, "AUTO"))
3063 t->row_labels = t->col_labels = CTLP_NORMAL;
3064 else if (lex_match_id (lexer, "ROWLABELS"))
3066 lex_match (lexer, T_EQUALS);
3067 if (lex_match_id (lexer, "OPPOSITE"))
3068 t->row_labels = CTLP_OPPOSITE;
3069 else if (lex_match_id (lexer, "LAYER"))
3070 t->row_labels = CTLP_LAYER;
3073 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3077 else if (lex_match_id (lexer, "COLLABELS"))
3079 lex_match (lexer, T_EQUALS);
3080 if (lex_match_id (lexer, "OPPOSITE"))
3081 t->col_labels = CTLP_OPPOSITE;
3082 else if (lex_match_id (lexer, "LAYER"))
3083 t->col_labels = CTLP_LAYER;
3086 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3092 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3098 else if (lex_match_id (lexer, "CRITERIA"))
3100 if (!lex_force_match_id (lexer, "CILEVEL"))
3102 lex_match (lexer, T_EQUALS);
3104 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3106 t->cilevel = lex_number (lexer);
3109 else if (lex_match_id (lexer, "CATEGORIES"))
3111 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3114 else if (lex_match_id (lexer, "TITLES"))
3119 if (lex_match_id (lexer, "CAPTION"))
3120 textp = &t->caption;
3121 else if (lex_match_id (lexer, "CORNER"))
3123 else if (lex_match_id (lexer, "TITLE"))
3127 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3130 lex_match (lexer, T_EQUALS);
3132 struct string s = DS_EMPTY_INITIALIZER;
3133 while (lex_is_string (lexer))
3135 if (!ds_is_empty (&s))
3136 ds_put_byte (&s, ' ');
3137 ds_put_substring (&s, lex_tokss (lexer));
3141 *textp = ds_steal_cstr (&s);
3143 while (lex_token (lexer) != T_SLASH
3144 && lex_token (lexer) != T_ENDCMD);
3146 else if (lex_match_id (lexer, "SIGTEST"))
3150 t->chisq = xmalloc (sizeof *t->chisq);
3151 *t->chisq = (struct ctables_chisq) {
3153 .include_mrsets = true,
3154 .all_visible = true,
3160 if (lex_match_id (lexer, "TYPE"))
3162 lex_match (lexer, T_EQUALS);
3163 if (!lex_force_match_id (lexer, "CHISQUARE"))
3166 else if (lex_match_id (lexer, "ALPHA"))
3168 lex_match (lexer, T_EQUALS);
3169 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3171 t->chisq->alpha = lex_number (lexer);
3174 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3176 lex_match (lexer, T_EQUALS);
3177 if (parse_bool (lexer, &t->chisq->include_mrsets))
3180 else if (lex_match_id (lexer, "CATEGORIES"))
3182 lex_match (lexer, T_EQUALS);
3183 if (lex_match_id (lexer, "ALLVISIBLE"))
3184 t->chisq->all_visible = true;
3185 else if (lex_match_id (lexer, "SUBTOTALS"))
3186 t->chisq->all_visible = false;
3189 lex_error_expecting (lexer,
3190 "ALLVISIBLE", "SUBTOTALS");
3196 lex_error_expecting (lexer, "TYPE", "ALPHA",
3197 "INCLUDEMRSETS", "CATEGORIES");
3201 while (lex_token (lexer) != T_SLASH
3202 && lex_token (lexer) != T_ENDCMD);
3204 else if (lex_match_id (lexer, "COMPARETEST"))
3208 t->pairwise = xmalloc (sizeof *t->pairwise);
3209 *t->pairwise = (struct ctables_pairwise) {
3211 .alpha = { .05, .05 },
3212 .adjust = BONFERRONI,
3213 .include_mrsets = true,
3214 .meansvariance_allcats = true,
3215 .all_visible = true,
3224 if (lex_match_id (lexer, "TYPE"))
3226 lex_match (lexer, T_EQUALS);
3227 if (lex_match_id (lexer, "PROP"))
3228 t->pairwise->type = PROP;
3229 else if (lex_match_id (lexer, "MEAN"))
3230 t->pairwise->type = MEAN;
3233 lex_error_expecting (lexer, "PROP", "MEAN");
3237 else if (lex_match_id (lexer, "ALPHA"))
3239 lex_match (lexer, T_EQUALS);
3241 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3243 double a0 = lex_number (lexer);
3246 lex_match (lexer, T_COMMA);
3247 if (lex_is_number (lexer))
3249 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3251 double a1 = lex_number (lexer);
3254 t->pairwise->alpha[0] = MIN (a0, a1);
3255 t->pairwise->alpha[1] = MAX (a0, a1);
3258 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3260 else if (lex_match_id (lexer, "ADJUST"))
3262 lex_match (lexer, T_EQUALS);
3263 if (lex_match_id (lexer, "BONFERRONI"))
3264 t->pairwise->adjust = BONFERRONI;
3265 else if (lex_match_id (lexer, "BH"))
3266 t->pairwise->adjust = BH;
3267 else if (lex_match_id (lexer, "NONE"))
3268 t->pairwise->adjust = 0;
3271 lex_error_expecting (lexer, "BONFERRONI", "BH",
3276 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3278 lex_match (lexer, T_EQUALS);
3279 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3282 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3284 lex_match (lexer, T_EQUALS);
3285 if (lex_match_id (lexer, "ALLCATS"))
3286 t->pairwise->meansvariance_allcats = true;
3287 else if (lex_match_id (lexer, "TESTEDCATS"))
3288 t->pairwise->meansvariance_allcats = false;
3291 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3295 else if (lex_match_id (lexer, "CATEGORIES"))
3297 lex_match (lexer, T_EQUALS);
3298 if (lex_match_id (lexer, "ALLVISIBLE"))
3299 t->pairwise->all_visible = true;
3300 else if (lex_match_id (lexer, "SUBTOTALS"))
3301 t->pairwise->all_visible = false;
3304 lex_error_expecting (lexer, "ALLVISIBLE",
3309 else if (lex_match_id (lexer, "MERGE"))
3311 lex_match (lexer, T_EQUALS);
3312 if (!parse_bool (lexer, &t->pairwise->merge))
3315 else if (lex_match_id (lexer, "STYLE"))
3317 lex_match (lexer, T_EQUALS);
3318 if (lex_match_id (lexer, "APA"))
3319 t->pairwise->apa_style = true;
3320 else if (lex_match_id (lexer, "SIMPLE"))
3321 t->pairwise->apa_style = false;
3324 lex_error_expecting (lexer, "APA", "SIMPLE");
3328 else if (lex_match_id (lexer, "SHOWSIG"))
3330 lex_match (lexer, T_EQUALS);
3331 if (!parse_bool (lexer, &t->pairwise->show_sig))
3336 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3337 "INCLUDEMRSETS", "MEANSVARIANCE",
3338 "CATEGORIES", "MERGE", "STYLE",
3343 while (lex_token (lexer) != T_SLASH
3344 && lex_token (lexer) != T_ENDCMD);
3348 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3349 "CRITERIA", "CATEGORIES", "TITLES",
3350 "SIGTEST", "COMPARETEST");
3354 if (!lex_match (lexer, T_SLASH))
3358 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3360 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3365 while (lex_token (lexer) != T_ENDCMD);
3367 bool ok = ctables_execute (ds, ct);
3368 ctables_destroy (ct);
3369 return ok ? CMD_SUCCESS : CMD_FAILURE;
3372 ctables_destroy (ct);