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 *specs;
286 struct variable *var;
289 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
291 /* A nested sequence of variables, e.g. a > b > c. */
294 struct variable **vars;
297 size_t *domains[N_CTDTS];
298 size_t n_domains[N_CTDTS];
300 struct ctables_summary_spec_set cell_sss;
301 struct ctables_summary_spec_set total_sss;
304 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
307 struct ctables_nest *nests;
313 struct ctables_axis *axes[PIVOT_N_AXES];
314 struct ctables_stack stacks[PIVOT_N_AXES];
315 enum pivot_axis_type summary_axis;
317 struct hmap domains[N_CTDTS];
319 enum pivot_axis_type slabels_position;
320 bool slabels_visible;
322 enum ctables_label_position row_labels;
323 enum ctables_label_position col_labels;
325 /* Indexed by variable dictionary index. */
326 struct ctables_categories **categories;
335 struct ctables_chisq *chisq;
336 struct ctables_pairwise *pairwise;
344 struct variable *var;
345 const struct mrset *mrset;
349 static const struct fmt_spec *
350 ctables_var_get_print_format (const struct ctables_var *var)
352 return (var->is_mrset
353 ? var_get_print_format (var->mrset->vars[0])
354 : var_get_print_format (var->var));
358 ctables_var_name (const struct ctables_var *var)
360 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
363 struct ctables_categories
366 struct ctables_category *cats;
371 struct ctables_category
373 enum ctables_category_type
391 struct ctables_category *subtotal;
395 double number; /* CCT_NUMBER. */
396 char *string; /* CCT_STRING. */
397 double range[2]; /* CCT_RANGE. */
398 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
400 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
403 bool include_missing;
407 enum ctables_summary_function sort_function;
408 struct variable *sort_var;
415 ctables_category_uninit (struct ctables_category *cat)
435 free (cat->total_label);
446 ctables_categories_unref (struct ctables_categories *c)
451 assert (c->n_refs > 0);
455 for (size_t i = 0; i < c->n_cats; i++)
456 ctables_category_uninit (&c->cats[i]);
461 /* Chi-square test (SIGTEST). */
469 /* Pairwise comparison test (COMPARETEST). */
470 struct ctables_pairwise
472 enum { PROP, MEAN } type;
475 bool meansvariance_allcats;
477 enum { BONFERRONI = 1, BH } adjust;
501 struct ctables_var var;
503 struct ctables_summary_spec_set cell_sss;
504 struct ctables_summary_spec_set total_sss;
508 struct ctables_axis *subs[2];
511 struct msg_location *loc;
514 static void ctables_axis_destroy (struct ctables_axis *);
523 enum ctables_function_availability
525 CTFA_ALL, /* Any variables. */
526 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
527 CTFA_MRSETS, /* Only multiple-response sets */
530 struct ctables_summary_spec
532 enum ctables_summary_function function;
533 double percentile; /* CTSF_PTILE only. */
535 struct fmt_spec format; /* XXX extra CTABLES formats */
539 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
546 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
548 for (size_t i = 0; i < set->n; i++)
549 ctables_summary_spec_uninit (&set->specs[i]);
554 parse_col_width (struct lexer *lexer, const char *name, double *width)
556 lex_match (lexer, T_EQUALS);
557 if (lex_match_id (lexer, "DEFAULT"))
559 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
561 *width = lex_number (lexer);
571 parse_bool (struct lexer *lexer, bool *b)
573 if (lex_match_id (lexer, "NO"))
575 else if (lex_match_id (lexer, "YES"))
579 lex_error_expecting (lexer, "YES", "NO");
585 static enum ctables_function_availability
586 ctables_function_availability (enum ctables_summary_function f)
588 static enum ctables_function_availability availability[] = {
589 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
594 return availability[f];
598 parse_ctables_summary_function (struct lexer *lexer,
599 enum ctables_summary_function *f)
603 enum ctables_summary_function function;
604 struct substring name;
606 static struct pair names[] = {
607 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
608 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
611 /* The .COUNT suffix may be omitted. */
612 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
613 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
614 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
615 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
616 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
617 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
618 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
622 if (!lex_force_id (lexer))
625 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
626 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
628 *f = names[i].function;
633 lex_error (lexer, _("Expecting summary function name."));
638 ctables_axis_destroy (struct ctables_axis *axis)
646 ctables_summary_spec_set_uninit (&axis->cell_sss);
647 ctables_summary_spec_set_uninit (&axis->total_sss);
652 ctables_axis_destroy (axis->subs[0]);
653 ctables_axis_destroy (axis->subs[1]);
656 msg_location_destroy (axis->loc);
660 static struct ctables_axis *
661 ctables_axis_new_nonterminal (enum ctables_axis_op op,
662 struct ctables_axis *sub0,
663 struct ctables_axis *sub1,
664 struct lexer *lexer, int start_ofs)
666 struct ctables_axis *axis = xmalloc (sizeof *axis);
667 *axis = (struct ctables_axis) {
669 .subs = { sub0, sub1 },
670 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
675 struct ctables_axis_parse_ctx
678 struct dictionary *dict;
680 struct ctables_table *t;
683 static struct fmt_spec
684 ctables_summary_default_format (enum ctables_summary_function function,
685 const struct ctables_var *var)
687 static const enum ctables_format default_formats[] = {
688 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
692 switch (default_formats[function])
695 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
698 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
701 return *ctables_var_get_print_format (var);
709 ctables_summary_default_label (enum ctables_summary_function function,
712 static const char *default_labels[] = {
713 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
718 return (function == CTSF_PTILE
719 ? xasprintf (_("Percentile %.2f"), percentile)
720 : xstrdup (gettext (default_labels[function])));
724 ctables_summary_function_name (enum ctables_summary_function function)
726 static const char *names[] = {
727 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
731 return names[function];
735 add_summary_spec (struct ctables_axis *axis,
736 enum ctables_summary_function function, double percentile,
737 const char *label, const struct fmt_spec *format,
738 const struct msg_location *loc, bool totals)
740 if (axis->op == CTAO_VAR)
742 const char *function_name = ctables_summary_function_name (function);
743 const char *var_name = ctables_var_name (&axis->var);
744 switch (ctables_function_availability (function))
747 if (!axis->var.is_mrset)
749 msg_at (SE, loc, _("Summary function %s applies only to multiple "
750 "response sets."), function_name);
751 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
761 _("Summary function %s applies only to scale variables."),
763 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
773 struct ctables_summary_spec_set *set = (totals ? &axis->total_sss
775 if (set->n >= set->allocated)
776 set->specs = x2nrealloc (set->specs, &set->allocated,
779 struct ctables_summary_spec *dst = &set->specs[set->n++];
780 *dst = (struct ctables_summary_spec) {
781 .function = function,
782 .percentile = percentile,
783 .label = xstrdup (label),
784 .format = (format ? *format
785 : ctables_summary_default_format (function, &axis->var)),
791 for (size_t i = 0; i < 2; i++)
792 if (!add_summary_spec (axis->subs[i], function, percentile, label,
793 format, loc, totals))
799 static struct ctables_axis *ctables_axis_parse_stack (
800 struct ctables_axis_parse_ctx *);
803 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
804 struct ctables_var *var)
806 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
808 *var = (struct ctables_var) {
810 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
814 lex_error (lexer, _("'%s' does not name a multiple-response set "
815 "in the active file dictionary."),
816 lex_tokcstr (lexer));
824 *var = (struct ctables_var) {
826 .var = parse_variable (lexer, dict),
828 return var->var != NULL;
832 static struct ctables_axis *
833 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
835 if (lex_match (ctx->lexer, T_LPAREN))
837 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
838 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
840 ctables_axis_destroy (sub);
846 if (!lex_force_id (ctx->lexer))
849 int start_ofs = lex_ofs (ctx->lexer);
850 struct ctables_var var;
851 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
854 struct ctables_axis *axis = xmalloc (sizeof *axis);
855 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
857 /* XXX should figure out default measures by reading data */
858 axis->scale = (var.is_mrset ? false
859 : lex_match_phrase (ctx->lexer, "[S]") ? true
860 : lex_match_phrase (ctx->lexer, "[C]") ? false
861 : var_get_measure (var.var) == MEASURE_SCALE);
862 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
863 lex_ofs (ctx->lexer) - 1);
867 static struct ctables_axis *
868 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
870 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
871 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
877 int start_ofs = lex_ofs (ctx->lexer);
879 /* Parse function. */
880 enum ctables_summary_function function;
881 if (!parse_ctables_summary_function (ctx->lexer, &function))
884 /* Parse percentile. */
885 double percentile = 0;
886 if (function == CTSF_PTILE)
888 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
890 percentile = lex_number (ctx->lexer);
891 lex_get (ctx->lexer);
896 if (lex_is_string (ctx->lexer))
898 label = ss_xstrdup (lex_tokss (ctx->lexer));
899 lex_get (ctx->lexer);
902 label = ctables_summary_default_label (function, percentile);
905 struct fmt_spec format;
906 const struct fmt_spec *formatp;
907 if (lex_token (ctx->lexer) == T_ID)
909 if (!parse_format_specifier (ctx->lexer, &format)
910 || !fmt_check_output (&format)
911 || !fmt_check_type_compat (&format, VAL_NUMERIC))
921 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
922 lex_ofs (ctx->lexer) - 1);
923 add_summary_spec (sub, function, percentile, label, formatp, loc,
926 msg_location_destroy (loc);
928 if (lex_match (ctx->lexer, T_COMMA))
930 if (!totals && lex_match_id (ctx->lexer, "TOTALS"))
932 if (!lex_force_match (ctx->lexer, T_LBRACK))
937 else if (lex_force_match (ctx->lexer, T_RBRACK))
939 if (totals && !lex_force_match (ctx->lexer, T_RBRACK))
948 ctables_axis_destroy (sub);
952 static const struct ctables_axis *
953 find_scale (const struct ctables_axis *axis)
957 else if (axis->op == CTAO_VAR)
961 assert (!axis->var.is_mrset);
969 for (size_t i = 0; i < 2; i++)
971 const struct ctables_axis *scale = find_scale (axis->subs[i]);
979 static const struct ctables_axis *
980 find_categorical_summary_spec (const struct ctables_axis *axis)
984 else if (axis->op == CTAO_VAR)
985 return !axis->scale && axis->cell_sss.n ? axis : NULL;
988 for (size_t i = 0; i < 2; i++)
990 const struct ctables_axis *sum
991 = find_categorical_summary_spec (axis->subs[i]);
999 static struct ctables_axis *
1000 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1002 int start_ofs = lex_ofs (ctx->lexer);
1003 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1007 while (lex_match (ctx->lexer, T_GT))
1009 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1013 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1014 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1016 const struct ctables_axis *outer_scale = find_scale (lhs);
1017 const struct ctables_axis *inner_scale = find_scale (rhs);
1018 if (outer_scale && inner_scale)
1020 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1021 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1022 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1023 ctables_axis_destroy (nest);
1027 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1030 msg_at (SE, nest->loc,
1031 _("Summaries may only be requested for categorical variables "
1032 "at the innermost nesting level."));
1033 msg_at (SN, outer_sum->loc,
1034 _("This outer categorical variable has a summary."));
1035 ctables_axis_destroy (nest);
1045 static struct ctables_axis *
1046 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1048 int start_ofs = lex_ofs (ctx->lexer);
1049 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1053 while (lex_match (ctx->lexer, T_PLUS))
1055 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1059 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1060 ctx->lexer, start_ofs);
1067 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1068 struct ctables *ct, struct ctables_table *t,
1069 enum pivot_axis_type a)
1071 if (lex_token (lexer) == T_BY
1072 || lex_token (lexer) == T_SLASH
1073 || lex_token (lexer) == T_ENDCMD)
1076 struct ctables_axis_parse_ctx ctx = {
1082 t->axes[a] = ctables_axis_parse_stack (&ctx);
1083 return t->axes[a] != NULL;
1087 ctables_chisq_destroy (struct ctables_chisq *chisq)
1093 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1099 ctables_table_destroy (struct ctables_table *t)
1104 for (size_t i = 0; i < t->n_categories; i++)
1105 ctables_categories_unref (t->categories[i]);
1106 free (t->categories);
1108 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1109 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1110 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1114 ctables_chisq_destroy (t->chisq);
1115 ctables_pairwise_destroy (t->pairwise);
1120 ctables_destroy (struct ctables *ct)
1125 pivot_table_look_unref (ct->look);
1129 for (size_t i = 0; i < ct->n_tables; i++)
1130 ctables_table_destroy (ct->tables[i]);
1135 static struct ctables_category
1136 cct_range (double low, double high)
1138 return (struct ctables_category) {
1140 .range = { low, high }
1145 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1146 struct ctables_table *t)
1148 if (!lex_match_id (lexer, "VARIABLES"))
1150 lex_match (lexer, T_EQUALS);
1152 struct variable **vars;
1154 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1157 struct ctables_categories *c = xmalloc (sizeof *c);
1158 *c = (struct ctables_categories) { .n_refs = n_vars };
1159 for (size_t i = 0; i < n_vars; i++)
1161 struct ctables_categories **cp
1162 = &t->categories[var_get_dict_index (vars[i])];
1163 ctables_categories_unref (*cp);
1168 size_t allocated_cats = 0;
1169 if (lex_match (lexer, T_LBRACK))
1173 if (c->n_cats >= allocated_cats)
1174 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1176 struct ctables_category *cat = &c->cats[c->n_cats];
1177 if (lex_match_id (lexer, "OTHERNM"))
1178 cat->type = CCT_OTHERNM;
1179 else if (lex_match_id (lexer, "MISSING"))
1180 cat->type = CCT_MISSING;
1181 else if (lex_match_id (lexer, "SUBTOTAL"))
1182 *cat = (struct ctables_category)
1183 { .type = CCT_SUBTOTAL, .total_label = NULL };
1184 else if (lex_match_id (lexer, "HSUBTOTAL"))
1185 *cat = (struct ctables_category)
1186 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1187 else if (lex_match_id (lexer, "LO"))
1189 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1191 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1194 else if (lex_is_number (lexer))
1196 double number = lex_number (lexer);
1198 if (lex_match_id (lexer, "THRU"))
1200 cat->type = CCT_RANGE;
1201 cat->range[0] = number;
1202 if (lex_match_id (lexer, "HI"))
1203 *cat = cct_range (number, DBL_MAX);
1206 if (!lex_force_num (lexer))
1208 *cat = cct_range (number, lex_number (lexer));
1213 *cat = (struct ctables_category) {
1218 else if (lex_is_string (lexer))
1220 *cat = (struct ctables_category) {
1222 .string = ss_xstrdup (lex_tokss (lexer)),
1228 lex_error (lexer, NULL);
1232 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1234 if (lex_match (lexer, T_EQUALS))
1236 if (!lex_force_string (lexer))
1239 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1243 cat->total_label = xstrdup (_("Subtotal"));
1247 lex_match (lexer, T_COMMA);
1249 while (!lex_match (lexer, T_RBRACK));
1252 struct ctables_category cat = {
1254 .include_missing = false,
1255 .sort_ascending = true,
1257 bool show_totals = false;
1258 char *total_label = NULL;
1259 bool totals_before = false;
1260 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1262 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1264 lex_match (lexer, T_EQUALS);
1265 if (lex_match_id (lexer, "A"))
1266 cat.sort_ascending = true;
1267 else if (lex_match_id (lexer, "D"))
1268 cat.sort_ascending = false;
1271 lex_error_expecting (lexer, "A", "D");
1275 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1277 lex_match (lexer, T_EQUALS);
1278 if (lex_match_id (lexer, "VALUE"))
1279 cat.type = CCT_VALUE;
1280 else if (lex_match_id (lexer, "LABEL"))
1281 cat.type = CCT_LABEL;
1284 cat.type = CCT_FUNCTION;
1285 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1288 if (lex_match (lexer, T_LPAREN))
1290 cat.sort_var = parse_variable (lexer, dict);
1294 if (cat.sort_function == CTSF_PTILE)
1296 lex_match (lexer, T_COMMA);
1297 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1299 cat.percentile = lex_number (lexer);
1303 if (!lex_force_match (lexer, T_RPAREN))
1306 else if (ctables_function_availability (cat.sort_function)
1309 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1314 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1316 lex_match (lexer, T_EQUALS);
1317 if (lex_match_id (lexer, "INCLUDE"))
1318 cat.include_missing = true;
1319 else if (lex_match_id (lexer, "EXCLUDE"))
1320 cat.include_missing = false;
1323 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1327 else if (lex_match_id (lexer, "TOTAL"))
1329 lex_match (lexer, T_EQUALS);
1330 if (!parse_bool (lexer, &show_totals))
1333 else if (lex_match_id (lexer, "LABEL"))
1335 lex_match (lexer, T_EQUALS);
1336 if (!lex_force_string (lexer))
1339 total_label = ss_xstrdup (lex_tokss (lexer));
1342 else if (lex_match_id (lexer, "POSITION"))
1344 lex_match (lexer, T_EQUALS);
1345 if (lex_match_id (lexer, "BEFORE"))
1346 totals_before = true;
1347 else if (lex_match_id (lexer, "AFTER"))
1348 totals_before = false;
1351 lex_error_expecting (lexer, "BEFORE", "AFTER");
1355 else if (lex_match_id (lexer, "EMPTY"))
1357 lex_match (lexer, T_EQUALS);
1358 if (lex_match_id (lexer, "INCLUDE"))
1359 c->show_empty = true;
1360 else if (lex_match_id (lexer, "EXCLUDE"))
1361 c->show_empty = false;
1364 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1371 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1372 "TOTAL", "LABEL", "POSITION", "EMPTY");
1374 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1381 if (c->n_cats >= allocated_cats)
1382 c->cats = x2nrealloc (c->cats, &allocated_cats,
1384 c->cats[c->n_cats++] = cat;
1389 if (c->n_cats >= allocated_cats)
1390 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1392 struct ctables_category *totals;
1395 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1396 totals = &c->cats[0];
1399 totals = &c->cats[c->n_cats];
1402 *totals = (struct ctables_category) {
1404 .total_label = total_label ? total_label : xstrdup (_("Total")),
1408 struct ctables_category *subtotal = NULL;
1409 for (size_t i = totals_before ? 0 : c->n_cats;
1410 totals_before ? i < c->n_cats : i-- > 0;
1411 totals_before ? i++ : 0)
1413 struct ctables_category *cat = &c->cats[i];
1421 cat->subtotal = subtotal;
1441 ctables_nest_uninit (struct ctables_nest *nest)
1448 ctables_stack_uninit (struct ctables_stack *stack)
1452 for (size_t i = 0; i < stack->n; i++)
1453 ctables_nest_uninit (&stack->nests[i]);
1454 free (stack->nests);
1458 static struct ctables_stack
1459 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1466 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1467 for (size_t i = 0; i < s0.n; i++)
1468 for (size_t j = 0; j < s1.n; j++)
1470 const struct ctables_nest *a = &s0.nests[i];
1471 const struct ctables_nest *b = &s1.nests[j];
1473 size_t allocate = a->n + b->n;
1474 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1475 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1477 for (size_t k = 0; k < a->n; k++)
1478 vars[n++] = a->vars[k];
1479 for (size_t k = 0; k < b->n; k++)
1480 vars[n++] = b->vars[k];
1481 assert (n == allocate);
1483 const struct ctables_nest *summary_src;
1484 if (!a->cell_sss.var)
1486 else if (!b->cell_sss.var)
1490 stack.nests[stack.n++] = (struct ctables_nest) {
1492 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1493 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1496 .cell_sss = summary_src->cell_sss,
1497 .total_sss = summary_src->total_sss,
1500 ctables_stack_uninit (&s0);
1501 ctables_stack_uninit (&s1);
1505 static struct ctables_stack
1506 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1508 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1509 for (size_t i = 0; i < s0.n; i++)
1510 stack.nests[stack.n++] = s0.nests[i];
1511 for (size_t i = 0; i < s1.n; i++)
1512 stack.nests[stack.n++] = s1.nests[i];
1513 assert (stack.n == s0.n + s1.n);
1519 static struct ctables_stack
1520 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1523 return (struct ctables_stack) { .n = 0 };
1528 assert (!a->var.is_mrset);
1530 struct variable **vars = xmalloc (sizeof *vars);
1533 struct ctables_nest *nest = xmalloc (sizeof *nest);
1534 *nest = (struct ctables_nest) {
1537 .scale_idx = a->scale ? 0 : SIZE_MAX,
1539 if (a->cell_sss.n || a->scale)
1541 nest->cell_sss = a->cell_sss;
1542 nest->total_sss = a->total_sss;
1543 nest->cell_sss.var = a->var.var;
1544 nest->total_sss.var = a->var.var;
1546 return (struct ctables_stack) { .nests = nest, .n = 1 };
1549 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1550 enumerate_fts (axis_type, a->subs[1]));
1553 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1554 enumerate_fts (axis_type, a->subs[1]));
1560 union ctables_summary
1562 /* COUNT, VALIDN, TOTALN. */
1569 /* MINIMUM, MAXIMUM, RANGE. */
1576 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1577 struct moments1 *moments;
1579 /* XXX percentiles, median, mode, multiple response */
1583 ctables_summary_init (union ctables_summary *s,
1584 const struct ctables_summary_spec *ss)
1586 switch (ss->function)
1590 case CTSF_ROWPCT_COUNT:
1591 case CTSF_COLPCT_COUNT:
1592 case CTSF_TABLEPCT_COUNT:
1593 case CTSF_SUBTABLEPCT_COUNT:
1594 case CTSF_LAYERPCT_COUNT:
1595 case CTSF_LAYERROWPCT_COUNT:
1596 case CTSF_LAYERCOLPCT_COUNT:
1597 case CTSF_ROWPCT_VALIDN:
1598 case CTSF_COLPCT_VALIDN:
1599 case CTSF_TABLEPCT_VALIDN:
1600 case CTSF_SUBTABLEPCT_VALIDN:
1601 case CTSF_LAYERPCT_VALIDN:
1602 case CTSF_LAYERROWPCT_VALIDN:
1603 case CTSF_LAYERCOLPCT_VALIDN:
1604 case CTSF_ROWPCT_TOTALN:
1605 case CTSF_COLPCT_TOTALN:
1606 case CTSF_TABLEPCT_TOTALN:
1607 case CTSF_SUBTABLEPCT_TOTALN:
1608 case CTSF_LAYERPCT_TOTALN:
1609 case CTSF_LAYERROWPCT_TOTALN:
1610 case CTSF_LAYERCOLPCT_TOTALN:
1615 s->missing = s->valid = 0;
1621 s->min = s->max = SYSMIS;
1629 case CTSF_ROWPCT_SUM:
1630 case CTSF_COLPCT_SUM:
1631 case CTSF_TABLEPCT_SUM:
1632 case CTSF_SUBTABLEPCT_SUM:
1633 case CTSF_LAYERPCT_SUM:
1634 case CTSF_LAYERROWPCT_SUM:
1635 case CTSF_LAYERCOLPCT_SUM:
1636 s->moments = moments1_create (MOMENT_VARIANCE);
1645 case CTSF_RESPONSES:
1646 case CTSF_ROWPCT_RESPONSES:
1647 case CTSF_COLPCT_RESPONSES:
1648 case CTSF_TABLEPCT_RESPONSES:
1649 case CTSF_SUBTABLEPCT_RESPONSES:
1650 case CTSF_LAYERPCT_RESPONSES:
1651 case CTSF_LAYERROWPCT_RESPONSES:
1652 case CTSF_LAYERCOLPCT_RESPONSES:
1653 case CTSF_ROWPCT_RESPONSES_COUNT:
1654 case CTSF_COLPCT_RESPONSES_COUNT:
1655 case CTSF_TABLEPCT_RESPONSES_COUNT:
1656 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1657 case CTSF_LAYERPCT_RESPONSES_COUNT:
1658 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1659 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1660 case CTSF_ROWPCT_COUNT_RESPONSES:
1661 case CTSF_COLPCT_COUNT_RESPONSES:
1662 case CTSF_TABLEPCT_COUNT_RESPONSES:
1663 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1664 case CTSF_LAYERPCT_COUNT_RESPONSES:
1665 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1666 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1672 ctables_summary_uninit (union ctables_summary *s,
1673 const struct ctables_summary_spec *ss)
1675 switch (ss->function)
1679 case CTSF_ROWPCT_COUNT:
1680 case CTSF_COLPCT_COUNT:
1681 case CTSF_TABLEPCT_COUNT:
1682 case CTSF_SUBTABLEPCT_COUNT:
1683 case CTSF_LAYERPCT_COUNT:
1684 case CTSF_LAYERROWPCT_COUNT:
1685 case CTSF_LAYERCOLPCT_COUNT:
1686 case CTSF_ROWPCT_VALIDN:
1687 case CTSF_COLPCT_VALIDN:
1688 case CTSF_TABLEPCT_VALIDN:
1689 case CTSF_SUBTABLEPCT_VALIDN:
1690 case CTSF_LAYERPCT_VALIDN:
1691 case CTSF_LAYERROWPCT_VALIDN:
1692 case CTSF_LAYERCOLPCT_VALIDN:
1693 case CTSF_ROWPCT_TOTALN:
1694 case CTSF_COLPCT_TOTALN:
1695 case CTSF_TABLEPCT_TOTALN:
1696 case CTSF_SUBTABLEPCT_TOTALN:
1697 case CTSF_LAYERPCT_TOTALN:
1698 case CTSF_LAYERROWPCT_TOTALN:
1699 case CTSF_LAYERCOLPCT_TOTALN:
1716 case CTSF_ROWPCT_SUM:
1717 case CTSF_COLPCT_SUM:
1718 case CTSF_TABLEPCT_SUM:
1719 case CTSF_SUBTABLEPCT_SUM:
1720 case CTSF_LAYERPCT_SUM:
1721 case CTSF_LAYERROWPCT_SUM:
1722 case CTSF_LAYERCOLPCT_SUM:
1723 moments1_destroy (s->moments);
1732 case CTSF_RESPONSES:
1733 case CTSF_ROWPCT_RESPONSES:
1734 case CTSF_COLPCT_RESPONSES:
1735 case CTSF_TABLEPCT_RESPONSES:
1736 case CTSF_SUBTABLEPCT_RESPONSES:
1737 case CTSF_LAYERPCT_RESPONSES:
1738 case CTSF_LAYERROWPCT_RESPONSES:
1739 case CTSF_LAYERCOLPCT_RESPONSES:
1740 case CTSF_ROWPCT_RESPONSES_COUNT:
1741 case CTSF_COLPCT_RESPONSES_COUNT:
1742 case CTSF_TABLEPCT_RESPONSES_COUNT:
1743 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1744 case CTSF_LAYERPCT_RESPONSES_COUNT:
1745 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1746 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1747 case CTSF_ROWPCT_COUNT_RESPONSES:
1748 case CTSF_COLPCT_COUNT_RESPONSES:
1749 case CTSF_TABLEPCT_COUNT_RESPONSES:
1750 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1751 case CTSF_LAYERPCT_COUNT_RESPONSES:
1752 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1753 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1759 ctables_summary_add (union ctables_summary *s,
1760 const struct ctables_summary_spec *ss,
1761 const struct variable *var, const union value *value,
1764 switch (ss->function)
1768 case CTSF_ROWPCT_COUNT:
1769 case CTSF_COLPCT_COUNT:
1770 case CTSF_TABLEPCT_COUNT:
1771 case CTSF_SUBTABLEPCT_COUNT:
1772 case CTSF_LAYERPCT_COUNT:
1773 case CTSF_LAYERROWPCT_COUNT:
1774 case CTSF_LAYERCOLPCT_COUNT:
1775 case CTSF_ROWPCT_VALIDN:
1776 case CTSF_COLPCT_VALIDN:
1777 case CTSF_TABLEPCT_VALIDN:
1778 case CTSF_SUBTABLEPCT_VALIDN:
1779 case CTSF_LAYERPCT_VALIDN:
1780 case CTSF_LAYERROWPCT_VALIDN:
1781 case CTSF_LAYERCOLPCT_VALIDN:
1782 case CTSF_ROWPCT_TOTALN:
1783 case CTSF_COLPCT_TOTALN:
1784 case CTSF_TABLEPCT_TOTALN:
1785 case CTSF_SUBTABLEPCT_TOTALN:
1786 case CTSF_LAYERPCT_TOTALN:
1787 case CTSF_LAYERROWPCT_TOTALN:
1788 case CTSF_LAYERCOLPCT_TOTALN:
1793 if (var_is_value_missing (var, value))
1794 s->missing += weight;
1802 if (!var_is_value_missing (var, value))
1804 assert (!var_is_alpha (var)); /* XXX? */
1805 if (s->min == SYSMIS || value->f < s->min)
1807 if (s->max == SYSMIS || value->f > s->max)
1817 case CTSF_ROWPCT_SUM:
1818 case CTSF_COLPCT_SUM:
1819 case CTSF_TABLEPCT_SUM:
1820 case CTSF_SUBTABLEPCT_SUM:
1821 case CTSF_LAYERPCT_SUM:
1822 case CTSF_LAYERROWPCT_SUM:
1823 case CTSF_LAYERCOLPCT_SUM:
1824 moments1_add (s->moments, value->f, weight);
1833 case CTSF_RESPONSES:
1834 case CTSF_ROWPCT_RESPONSES:
1835 case CTSF_COLPCT_RESPONSES:
1836 case CTSF_TABLEPCT_RESPONSES:
1837 case CTSF_SUBTABLEPCT_RESPONSES:
1838 case CTSF_LAYERPCT_RESPONSES:
1839 case CTSF_LAYERROWPCT_RESPONSES:
1840 case CTSF_LAYERCOLPCT_RESPONSES:
1841 case CTSF_ROWPCT_RESPONSES_COUNT:
1842 case CTSF_COLPCT_RESPONSES_COUNT:
1843 case CTSF_TABLEPCT_RESPONSES_COUNT:
1844 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1845 case CTSF_LAYERPCT_RESPONSES_COUNT:
1846 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1847 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1848 case CTSF_ROWPCT_COUNT_RESPONSES:
1849 case CTSF_COLPCT_COUNT_RESPONSES:
1850 case CTSF_TABLEPCT_COUNT_RESPONSES:
1851 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1852 case CTSF_LAYERPCT_COUNT_RESPONSES:
1853 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1854 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1860 ctables_summary_value (const struct ctables_cell *cell,
1861 union ctables_summary *s,
1862 const struct ctables_summary_spec *ss)
1864 switch (ss->function)
1870 case CTSF_SUBTABLEPCT_COUNT:
1871 return cell->domains[CTDT_SUBTABLE]->valid ? s->valid / cell->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1873 case CTSF_ROWPCT_COUNT:
1874 return cell->domains[CTDT_ROW]->valid ? s->valid / cell->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1876 case CTSF_COLPCT_COUNT:
1877 return cell->domains[CTDT_COL]->valid ? s->valid / cell->domains[CTDT_COL]->valid * 100 : SYSMIS;
1879 case CTSF_TABLEPCT_COUNT:
1880 return cell->domains[CTDT_TABLE]->valid ? s->valid / cell->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1882 case CTSF_LAYERPCT_COUNT:
1883 return cell->domains[CTDT_LAYER]->valid ? s->valid / cell->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1885 case CTSF_LAYERROWPCT_COUNT:
1886 return cell->domains[CTDT_LAYERROW]->valid ? s->valid / cell->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1888 case CTSF_LAYERCOLPCT_COUNT:
1889 return cell->domains[CTDT_LAYERCOL]->valid ? s->valid / cell->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1891 case CTSF_ROWPCT_VALIDN:
1892 case CTSF_COLPCT_VALIDN:
1893 case CTSF_TABLEPCT_VALIDN:
1894 case CTSF_SUBTABLEPCT_VALIDN:
1895 case CTSF_LAYERPCT_VALIDN:
1896 case CTSF_LAYERROWPCT_VALIDN:
1897 case CTSF_LAYERCOLPCT_VALIDN:
1898 case CTSF_ROWPCT_TOTALN:
1899 case CTSF_COLPCT_TOTALN:
1900 case CTSF_TABLEPCT_TOTALN:
1901 case CTSF_SUBTABLEPCT_TOTALN:
1902 case CTSF_LAYERPCT_TOTALN:
1903 case CTSF_LAYERROWPCT_TOTALN:
1904 case CTSF_LAYERCOLPCT_TOTALN:
1909 return s->valid + s->missing;
1922 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1927 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1933 double weight, variance;
1934 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1935 return calc_semean (variance, weight);
1941 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1942 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1947 double weight, mean;
1948 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1949 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1955 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1959 case CTSF_ROWPCT_SUM:
1960 case CTSF_COLPCT_SUM:
1961 case CTSF_TABLEPCT_SUM:
1962 case CTSF_SUBTABLEPCT_SUM:
1963 case CTSF_LAYERPCT_SUM:
1964 case CTSF_LAYERROWPCT_SUM:
1965 case CTSF_LAYERCOLPCT_SUM:
1974 case CTSF_RESPONSES:
1975 case CTSF_ROWPCT_RESPONSES:
1976 case CTSF_COLPCT_RESPONSES:
1977 case CTSF_TABLEPCT_RESPONSES:
1978 case CTSF_SUBTABLEPCT_RESPONSES:
1979 case CTSF_LAYERPCT_RESPONSES:
1980 case CTSF_LAYERROWPCT_RESPONSES:
1981 case CTSF_LAYERCOLPCT_RESPONSES:
1982 case CTSF_ROWPCT_RESPONSES_COUNT:
1983 case CTSF_COLPCT_RESPONSES_COUNT:
1984 case CTSF_TABLEPCT_RESPONSES_COUNT:
1985 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1986 case CTSF_LAYERPCT_RESPONSES_COUNT:
1987 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1988 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1989 case CTSF_ROWPCT_COUNT_RESPONSES:
1990 case CTSF_COLPCT_COUNT_RESPONSES:
1991 case CTSF_TABLEPCT_COUNT_RESPONSES:
1992 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1993 case CTSF_LAYERPCT_COUNT_RESPONSES:
1994 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1995 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2002 struct ctables_cell_sort_aux
2004 const struct ctables_table *t;
2005 enum pivot_axis_type a;
2009 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2011 const struct ctables_cell_sort_aux *aux = aux_;
2012 struct ctables_cell *const *ap = a_;
2013 struct ctables_cell *const *bp = b_;
2014 const struct ctables_cell *a = *ap;
2015 const struct ctables_cell *b = *bp;
2017 size_t a_idx = a->axes[aux->a].stack_idx;
2018 size_t b_idx = b->axes[aux->a].stack_idx;
2020 return a_idx < b_idx ? -1 : 1;
2022 const struct ctables_nest *nest = &aux->t->stacks[aux->a].nests[a_idx];
2023 for (size_t i = 0; i < nest->n; i++)
2024 if (i != nest->scale_idx)
2026 const struct variable *var = nest->vars[i];
2027 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2028 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2029 if (a_cv->category != b_cv->category)
2030 return a_cv->category > b_cv->category ? 1 : -1;
2032 const union value *a_val = &a_cv->value;
2033 const union value *b_val = &b_cv->value;
2034 switch (a_cv->category->type)
2041 /* Must be equal. */
2048 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2056 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2058 return a_cv->category->sort_ascending ? cmp : -cmp;
2064 const char *a_label = var_lookup_value_label (var, a_val);
2065 const char *b_label = var_lookup_value_label (var, b_val);
2067 ? (b_label ? strcmp (a_label, b_label) : 1)
2068 : (b_label ? -1 : value_compare_3way (
2069 a_val, b_val, var_get_width (var))));
2071 return a_cv->category->sort_ascending ? cmp : -cmp;
2085 For each ctables_table:
2086 For each combination of row vars:
2087 For each combination of column vars:
2088 For each combination of layer vars:
2090 Make a table of row values:
2091 Sort entries by row values
2092 Assign a 0-based index to each actual value
2093 Construct a dimension
2094 Make a table of column values
2095 Make a table of layer values
2097 Fill the table entry using the indexes from before.
2100 static struct ctables_domain *
2101 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *cell,
2102 enum ctables_domain_type domain)
2105 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2107 size_t idx = cell->axes[a].stack_idx;
2108 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2109 hash = hash_int (idx, hash);
2110 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2112 size_t v_idx = nest->domains[domain][i];
2113 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2114 var_get_width (nest->vars[v_idx]), hash);
2118 struct ctables_domain *d;
2119 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2121 const struct ctables_cell *df = d->example;
2122 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2124 size_t idx = cell->axes[a].stack_idx;
2125 if (idx != df->axes[a].stack_idx)
2128 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2129 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2131 size_t v_idx = nest->domains[domain][i];
2132 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2133 &cell->axes[a].cvs[v_idx].value,
2134 var_get_width (nest->vars[v_idx])))
2143 d = xmalloc (sizeof *d);
2144 *d = (struct ctables_domain) { .example = cell };
2145 hmap_insert (&t->domains[domain], &d->node, hash);
2149 static const struct ctables_category *
2150 ctables_categories_match (const struct ctables_categories *c,
2151 const union value *v, const struct variable *var)
2153 const struct ctables_category *othernm = NULL;
2154 for (size_t i = c->n_cats; i-- > 0; )
2156 const struct ctables_category *cat = &c->cats[i];
2160 if (cat->number == v->f)
2168 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2169 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2174 if (var_is_value_missing (var, v))
2191 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2196 return var_is_value_missing (var, v) ? NULL : othernm;
2199 static const struct ctables_category *
2200 ctables_categories_total (const struct ctables_categories *c)
2202 const struct ctables_category *first = &c->cats[0];
2203 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2204 return (first->type == CCT_TOTAL ? first
2205 : last->type == CCT_TOTAL ? last
2209 static struct ctables_cell *
2210 ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c,
2211 size_t ix[PIVOT_N_AXES],
2212 const struct ctables_category *cats[PIVOT_N_AXES][10])
2214 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2218 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2220 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2221 hash = hash_int (ix[a], hash);
2222 for (size_t i = 0; i < nest->n; i++)
2223 if (i != nest->scale_idx)
2225 hash = hash_pointer (cats[a][i], hash);
2226 if (cats[a][i]->type != CCT_TOTAL
2227 && cats[a][i]->type != CCT_SUBTOTAL
2228 && cats[a][i]->type != CCT_HSUBTOTAL)
2229 hash = value_hash (case_data (c, nest->vars[i]),
2230 var_get_width (nest->vars[i]), hash);
2236 struct ctables_cell *cell;
2237 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &t->cells)
2239 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2241 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2242 if (cell->axes[a].stack_idx != ix[a])
2244 for (size_t i = 0; i < nest->n; i++)
2245 if (i != nest->scale_idx
2246 && (cats[a][i] != cell->axes[a].cvs[i].category
2247 || (cats[a][i]->type != CCT_TOTAL
2248 && cats[a][i]->type != CCT_SUBTOTAL
2249 && cats[a][i]->type != CCT_HSUBTOTAL
2250 && !value_equal (case_data (c, nest->vars[i]),
2251 &cell->axes[a].cvs[i].value,
2252 var_get_width (nest->vars[i])))))
2261 cell = xmalloc (sizeof *cell);
2263 cell->total = total;
2264 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2266 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2267 cell->axes[a].stack_idx = ix[a];
2268 cell->axes[a].cvs = (nest->n
2269 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2271 for (size_t i = 0; i < nest->n; i++)
2273 if (i != nest->scale_idx)
2275 const struct ctables_category *subtotal = cats[a][i]->subtotal;
2276 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2280 cell->axes[a].cvs[i].category = cats[a][i];
2281 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2282 var_get_width (nest->vars[i]));
2287 const struct ctables_summary_spec_set *sss
2288 = (cell->total ? &ss->total_sss : &ss->cell_sss);
2289 cell->summaries = xmalloc (sss->n * sizeof *cell->summaries);
2290 for (size_t i = 0; i < sss->n; i++)
2291 ctables_summary_init (&cell->summaries[i], &sss->specs[i]);
2293 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2294 cell->domains[dt] = ctables_domain_insert (t, cell, dt);
2295 hmap_insert (&t->cells, &cell->node, hash);
2300 ctables_cell_add__ (struct ctables_table *t, const struct ccase *c,
2301 size_t ix[PIVOT_N_AXES],
2302 const struct ctables_category *cats[PIVOT_N_AXES][10],
2305 struct ctables_cell *cell = ctables_cell_insert__ (t, c, ix, cats);
2306 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2308 const struct ctables_summary_spec_set *sss
2309 = (cell->total ? &ss->total_sss : &ss->cell_sss);
2310 for (size_t i = 0; i < sss->n; i++)
2311 ctables_summary_add (&cell->summaries[i], &sss->specs[i], sss->var,
2312 case_data (c, sss->var), weight);
2313 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2314 cell->domains[dt]->valid += weight;
2318 recurse_totals (struct ctables_table *t, const struct ccase *c,
2319 size_t ix[PIVOT_N_AXES],
2320 const struct ctables_category *cats[PIVOT_N_AXES][10],
2322 enum pivot_axis_type start_axis, size_t start_nest)
2324 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2326 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2327 for (size_t i = start_nest; i < nest->n; i++)
2329 if (i == nest->scale_idx)
2332 const struct variable *var = nest->vars[i];
2334 const struct ctables_category *total = ctables_categories_total (
2335 t->categories[var_get_dict_index (var)]);
2338 const struct ctables_category *save = cats[a][i];
2340 ctables_cell_add__ (t, c, ix, cats, weight);
2341 recurse_totals (t, c, ix, cats, weight, a, i + 1);
2350 ctables_cell_insert (struct ctables_table *t,
2351 const struct ccase *c,
2352 size_t ir, size_t ic, size_t il,
2355 size_t ix[PIVOT_N_AXES] = {
2356 [PIVOT_AXIS_ROW] = ir,
2357 [PIVOT_AXIS_COLUMN] = ic,
2358 [PIVOT_AXIS_LAYER] = il,
2361 const struct ctables_category *cats[PIVOT_N_AXES][10];
2362 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2364 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2365 for (size_t i = 0; i < nest->n; i++)
2367 if (i == nest->scale_idx)
2370 const struct variable *var = nest->vars[i];
2371 const union value *value = case_data (c, var);
2373 if (var_is_numeric (var) && value->f == SYSMIS)
2376 cats[a][i] = ctables_categories_match (
2377 t->categories[var_get_dict_index (var)], value, var);
2383 ctables_cell_add__ (t, c, ix, cats, weight);
2385 recurse_totals (t, c, ix, cats, weight, 0, 0);
2387 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2389 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2390 for (size_t i = 0; i < nest->n; i++)
2392 if (i == nest->scale_idx)
2395 const struct ctables_category *save = cats[a][i];
2398 cats[a][i] = save->subtotal;
2399 ctables_cell_add__ (t, c, ix, cats, weight);
2407 ctables_execute (struct dataset *ds, struct ctables *ct)
2409 for (size_t i = 0; i < ct->n_tables; i++)
2411 struct ctables_table *t = ct->tables[i];
2412 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2415 t->stacks[a] = enumerate_fts (a, t->axes[a]);
2417 for (size_t j = 0; j < t->stacks[a].n; j++)
2419 struct ctables_nest *nest = &t->stacks[a].nests[j];
2420 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2422 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
2423 nest->n_domains[dt] = 0;
2425 for (size_t k = 0; k < nest->n; k++)
2427 if (k == nest->scale_idx)
2436 if (a != PIVOT_AXIS_LAYER)
2443 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2444 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2445 : a == PIVOT_AXIS_ROW)
2447 if (k == nest->n - 1
2448 || (nest->scale_idx == nest->n - 1
2449 && k == nest->n - 2))
2455 if (a == PIVOT_AXIS_COLUMN)
2460 if (a == PIVOT_AXIS_ROW)
2465 nest->domains[dt][nest->n_domains[dt]++] = k;
2472 struct ctables_nest *nest = xmalloc (sizeof *nest);
2473 *nest = (struct ctables_nest) { .n = 0 };
2474 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
2477 for (size_t i = 0; i < t->stacks[t->summary_axis].n; i++)
2479 struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[i];
2480 if (!nest->cell_sss.n)
2482 struct ctables_summary_spec_set *sss = &nest->cell_sss;
2483 sss->specs = xmalloc (sizeof *sss->specs);
2486 enum ctables_summary_function function
2487 = sss->var ? CTSF_MEAN : CTSF_COUNT;
2488 struct ctables_var var = { .is_mrset = false, .var = sss->var };
2490 *sss->specs = (struct ctables_summary_spec) {
2491 .function = function,
2492 .format = ctables_summary_default_format (function, &var),
2493 .label = ctables_summary_default_label (function, 0),
2496 sss->var = nest->vars[0];
2498 nest->total_sss = nest->cell_sss;
2500 else if (!nest->total_sss.n)
2501 nest->total_sss = nest->cell_sss;
2505 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2508 bool warn_on_invalid = true;
2509 double total_weight = 0;
2510 for (struct ccase *c = casereader_read (input); c;
2511 case_unref (c), c = casereader_read (input))
2513 double weight = dict_get_case_weight (dataset_dict (ds), c,
2515 total_weight += weight;
2517 for (size_t i = 0; i < ct->n_tables; i++)
2519 struct ctables_table *t = ct->tables[i];
2521 for (size_t ir = 0; ir < t->stacks[PIVOT_AXIS_ROW].n; ir++)
2522 for (size_t ic = 0; ic < t->stacks[PIVOT_AXIS_COLUMN].n; ic++)
2523 for (size_t il = 0; il < t->stacks[PIVOT_AXIS_LAYER].n; il++)
2524 ctables_cell_insert (t, c, ir, ic, il, weight);
2527 casereader_destroy (input);
2529 for (size_t i = 0; i < ct->n_tables; i++)
2531 struct ctables_table *t = ct->tables[i];
2533 struct pivot_table *pt = pivot_table_create__ (
2535 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2536 : pivot_value_new_text (N_("Custom Tables"))),
2539 pivot_table_set_caption (
2540 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2542 pivot_table_set_caption (
2543 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2545 pivot_table_set_look (pt, ct->look);
2546 struct pivot_dimension *d[PIVOT_N_AXES];
2547 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2549 static const char *names[] = {
2550 [PIVOT_AXIS_ROW] = N_("Rows"),
2551 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2552 [PIVOT_AXIS_LAYER] = N_("Layers"),
2554 d[a] = (t->axes[a] || a == t->summary_axis
2555 ? pivot_dimension_create (pt, a, names[a])
2560 assert (t->axes[a]);
2562 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2564 struct ctables_cell *cell;
2566 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2569 assert (n <= t->cells.count);
2571 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2572 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2574 size_t max_depth = 0;
2575 for (size_t j = 0; j < t->stacks[a].n; j++)
2576 if (t->stacks[a].nests[j].n > max_depth)
2577 max_depth = t->stacks[a].nests[j].n;
2579 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2580 struct pivot_category *top = NULL;
2582 for (size_t j = 0; j < n; j++)
2584 struct ctables_cell *cell = sorted[j];
2585 const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
2587 size_t n_common = 0;
2588 bool new_subtable = false;
2591 struct ctables_cell *prev = sorted[j - 1];
2592 if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
2594 for (; n_common < nest->n; n_common++)
2595 if (n_common != nest->scale_idx
2596 && (prev->axes[a].cvs[n_common].category
2597 != cell->axes[a].cvs[n_common].category
2598 || !value_equal (&prev->axes[a].cvs[n_common].value,
2599 &cell->axes[a].cvs[n_common].value,
2600 var_get_type (nest->vars[n_common]))))
2604 new_subtable = true;
2607 new_subtable = true;
2611 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
2613 if (vlabel != CTVL_NONE)
2614 top = pivot_category_create_group__ (
2615 top, pivot_value_new_variable (nest->vars[0]));
2617 if (n_common == nest->n)
2619 cell->axes[a].leaf = prev_leaf;
2623 for (size_t k = n_common; k < nest->n; k++)
2625 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2627 struct pivot_value *label
2628 = (k == nest->scale_idx ? NULL
2629 : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
2630 || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
2631 || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
2632 ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
2634 : pivot_value_new_var_value (nest->vars[k],
2635 &cell->axes[a].cvs[k].value));
2636 if (k == nest->n - 1)
2638 if (a == t->summary_axis)
2641 parent = pivot_category_create_group__ (parent, label);
2642 const struct ctables_summary_spec_set *sss
2643 = cell->total ? &nest->total_sss : &nest->cell_sss;
2644 for (size_t m = 0; m < sss->n; m++)
2646 int leaf = pivot_category_create_leaf (
2647 parent, pivot_value_new_text (sss->specs[m].label));
2654 /* This assertion is true as long as the summary axis
2655 is the axis where the summaries are displayed. */
2658 prev_leaf = pivot_category_create_leaf (parent, label);
2664 parent = pivot_category_create_group__ (parent, label);
2666 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
2667 if (vlabel != CTVL_NONE)
2668 parent = pivot_category_create_group__ (
2669 parent, pivot_value_new_variable (nest->vars[k + 1]));
2673 cell->axes[a].leaf = prev_leaf;
2678 struct ctables_cell *cell;
2679 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2684 const struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].stack_idx];
2685 const struct ctables_summary_spec_set *sss = cell->total ? &nest->total_sss : &nest->cell_sss;
2686 for (size_t j = 0; j < sss->n; j++)
2689 size_t n_dindexes = 0;
2691 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2694 int leaf = cell->axes[a].leaf;
2695 if (a == t->summary_axis)
2697 dindexes[n_dindexes++] = leaf;
2700 double d = ctables_summary_value (cell, &cell->summaries[j], &sss->specs[j]);
2701 struct pivot_value *value = pivot_value_new_number (d);
2702 value->numeric.format = sss->specs[j].format;
2703 pivot_table_put (pt, dindexes, n_dindexes, value);
2707 pivot_table_submit (pt);
2710 return proc_commit (ds);
2714 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2716 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2717 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2718 enum settings_value_show tvars = settings_get_show_variables ();
2719 for (size_t i = 0; i < n_vars; i++)
2720 vlabels[i] = (enum ctables_vlabel) tvars;
2722 struct ctables *ct = xmalloc (sizeof *ct);
2723 *ct = (struct ctables) {
2724 .look = pivot_table_look_unshare (pivot_table_look_ref (
2725 pivot_table_look_get_default ())),
2727 .hide_threshold = 5,
2729 ct->look->omit_empty = false;
2731 if (!lex_force_match (lexer, T_SLASH))
2734 while (!lex_match_id (lexer, "TABLE"))
2736 if (lex_match_id (lexer, "FORMAT"))
2738 double widths[2] = { SYSMIS, SYSMIS };
2739 double units_per_inch = 72.0;
2741 while (lex_token (lexer) != T_SLASH)
2743 if (lex_match_id (lexer, "MINCOLWIDTH"))
2745 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2748 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2750 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2753 else if (lex_match_id (lexer, "UNITS"))
2755 lex_match (lexer, T_EQUALS);
2756 if (lex_match_id (lexer, "POINTS"))
2757 units_per_inch = 72.0;
2758 else if (lex_match_id (lexer, "INCHES"))
2759 units_per_inch = 1.0;
2760 else if (lex_match_id (lexer, "CM"))
2761 units_per_inch = 2.54;
2764 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2768 else if (lex_match_id (lexer, "EMPTY"))
2773 lex_match (lexer, T_EQUALS);
2774 if (lex_match_id (lexer, "ZERO"))
2776 /* Nothing to do. */
2778 else if (lex_match_id (lexer, "BLANK"))
2779 ct->zero = xstrdup ("");
2780 else if (lex_force_string (lexer))
2782 ct->zero = ss_xstrdup (lex_tokss (lexer));
2788 else if (lex_match_id (lexer, "MISSING"))
2790 lex_match (lexer, T_EQUALS);
2791 if (!lex_force_string (lexer))
2795 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2796 ? ss_xstrdup (lex_tokss (lexer))
2802 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2803 "UNITS", "EMPTY", "MISSING");
2808 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2809 && widths[0] > widths[1])
2811 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2815 for (size_t i = 0; i < 2; i++)
2816 if (widths[i] != SYSMIS)
2818 int *wr = ct->look->width_ranges[TABLE_HORZ];
2819 wr[i] = widths[i] / units_per_inch * 96.0;
2824 else if (lex_match_id (lexer, "VLABELS"))
2826 if (!lex_force_match_id (lexer, "VARIABLES"))
2828 lex_match (lexer, T_EQUALS);
2830 struct variable **vars;
2832 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2836 if (!lex_force_match_id (lexer, "DISPLAY"))
2841 lex_match (lexer, T_EQUALS);
2843 enum ctables_vlabel vlabel;
2844 if (lex_match_id (lexer, "DEFAULT"))
2845 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2846 else if (lex_match_id (lexer, "NAME"))
2848 else if (lex_match_id (lexer, "LABEL"))
2849 vlabel = CTVL_LABEL;
2850 else if (lex_match_id (lexer, "BOTH"))
2852 else if (lex_match_id (lexer, "NONE"))
2856 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2862 for (size_t i = 0; i < n_vars; i++)
2863 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2866 else if (lex_match_id (lexer, "MRSETS"))
2868 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2870 lex_match (lexer, T_EQUALS);
2871 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2874 else if (lex_match_id (lexer, "SMISSING"))
2876 if (lex_match_id (lexer, "VARIABLE"))
2877 ct->smissing_listwise = false;
2878 else if (lex_match_id (lexer, "LISTWISE"))
2879 ct->smissing_listwise = true;
2882 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2887 else if (lex_match_id (lexer, "WEIGHT"))
2889 if (!lex_force_match_id (lexer, "VARIABLE"))
2891 lex_match (lexer, T_EQUALS);
2892 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2893 if (!ct->base_weight)
2896 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2898 if (!lex_force_match_id (lexer, "COUNT"))
2900 lex_match (lexer, T_EQUALS);
2901 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2903 ct->hide_threshold = lex_integer (lexer);
2908 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2909 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2910 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2914 if (!lex_force_match (lexer, T_SLASH))
2918 size_t allocated_tables = 0;
2921 if (ct->n_tables >= allocated_tables)
2922 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2923 sizeof *ct->tables);
2925 struct ctables_category *cat = xmalloc (sizeof *cat);
2926 *cat = (struct ctables_category) {
2928 .include_missing = false,
2929 .sort_ascending = true,
2932 struct ctables_categories *c = xmalloc (sizeof *c);
2933 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2934 *c = (struct ctables_categories) {
2940 struct ctables_categories **categories = xnmalloc (n_vars,
2941 sizeof *categories);
2942 for (size_t i = 0; i < n_vars; i++)
2945 struct ctables_table *t = xmalloc (sizeof *t);
2946 *t = (struct ctables_table) {
2947 .cells = HMAP_INITIALIZER (t->cells),
2948 .slabels_position = PIVOT_AXIS_COLUMN,
2949 .slabels_visible = true,
2950 .row_labels = CTLP_NORMAL,
2951 .col_labels = CTLP_NORMAL,
2952 .categories = categories,
2953 .n_categories = n_vars,
2956 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2957 hmap_init (&t->domains[dt]);
2958 ct->tables[ct->n_tables++] = t;
2960 lex_match (lexer, T_EQUALS);
2961 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2963 if (lex_match (lexer, T_BY))
2965 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2966 ct, t, PIVOT_AXIS_COLUMN))
2969 if (lex_match (lexer, T_BY))
2971 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2972 ct, t, PIVOT_AXIS_LAYER))
2977 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2978 && !t->axes[PIVOT_AXIS_LAYER])
2980 lex_error (lexer, _("At least one variable must be specified."));
2984 const struct ctables_axis *scales[PIVOT_N_AXES];
2985 size_t n_scales = 0;
2986 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2988 scales[a] = find_scale (t->axes[a]);
2994 msg (SE, _("Scale variables may appear only on one axis."));
2995 if (scales[PIVOT_AXIS_ROW])
2996 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2997 _("This scale variable appears on the rows axis."));
2998 if (scales[PIVOT_AXIS_COLUMN])
2999 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3000 _("This scale variable appears on the columns axis."));
3001 if (scales[PIVOT_AXIS_LAYER])
3002 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3003 _("This scale variable appears on the layer axis."));
3007 const struct ctables_axis *summaries[PIVOT_N_AXES];
3008 size_t n_summaries = 0;
3009 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3011 summaries[a] = (scales[a]
3013 : find_categorical_summary_spec (t->axes[a]));
3017 if (n_summaries > 1)
3019 msg (SE, _("Summaries may appear only on one axis."));
3020 if (summaries[PIVOT_AXIS_ROW])
3021 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3022 _("This variable on the rows axis has a summary."));
3023 if (summaries[PIVOT_AXIS_COLUMN])
3024 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3025 _("This variable on the columns axis has a summary."));
3026 if (summaries[PIVOT_AXIS_LAYER])
3027 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3028 _("This variable on the layers axis has a summary."));
3031 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3032 if (n_summaries ? summaries[a] : t->axes[a])
3034 t->summary_axis = a;
3038 if (lex_token (lexer) == T_ENDCMD)
3040 if (!lex_force_match (lexer, T_SLASH))
3043 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3045 if (lex_match_id (lexer, "SLABELS"))
3047 while (lex_token (lexer) != T_SLASH)
3049 if (lex_match_id (lexer, "POSITION"))
3051 lex_match (lexer, T_EQUALS);
3052 if (lex_match_id (lexer, "COLUMN"))
3053 t->slabels_position = PIVOT_AXIS_COLUMN;
3054 else if (lex_match_id (lexer, "ROW"))
3055 t->slabels_position = PIVOT_AXIS_ROW;
3056 else if (lex_match_id (lexer, "LAYER"))
3057 t->slabels_position = PIVOT_AXIS_LAYER;
3060 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3064 else if (lex_match_id (lexer, "VISIBLE"))
3066 lex_match (lexer, T_EQUALS);
3067 if (!parse_bool (lexer, &t->slabels_visible))
3072 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3077 else if (lex_match_id (lexer, "CLABELS"))
3079 while (lex_token (lexer) != T_SLASH)
3081 if (lex_match_id (lexer, "AUTO"))
3082 t->row_labels = t->col_labels = CTLP_NORMAL;
3083 else if (lex_match_id (lexer, "ROWLABELS"))
3085 lex_match (lexer, T_EQUALS);
3086 if (lex_match_id (lexer, "OPPOSITE"))
3087 t->row_labels = CTLP_OPPOSITE;
3088 else if (lex_match_id (lexer, "LAYER"))
3089 t->row_labels = CTLP_LAYER;
3092 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3096 else if (lex_match_id (lexer, "COLLABELS"))
3098 lex_match (lexer, T_EQUALS);
3099 if (lex_match_id (lexer, "OPPOSITE"))
3100 t->col_labels = CTLP_OPPOSITE;
3101 else if (lex_match_id (lexer, "LAYER"))
3102 t->col_labels = CTLP_LAYER;
3105 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3111 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3117 else if (lex_match_id (lexer, "CRITERIA"))
3119 if (!lex_force_match_id (lexer, "CILEVEL"))
3121 lex_match (lexer, T_EQUALS);
3123 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3125 t->cilevel = lex_number (lexer);
3128 else if (lex_match_id (lexer, "CATEGORIES"))
3130 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3133 else if (lex_match_id (lexer, "TITLES"))
3138 if (lex_match_id (lexer, "CAPTION"))
3139 textp = &t->caption;
3140 else if (lex_match_id (lexer, "CORNER"))
3142 else if (lex_match_id (lexer, "TITLE"))
3146 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3149 lex_match (lexer, T_EQUALS);
3151 struct string s = DS_EMPTY_INITIALIZER;
3152 while (lex_is_string (lexer))
3154 if (!ds_is_empty (&s))
3155 ds_put_byte (&s, ' ');
3156 ds_put_substring (&s, lex_tokss (lexer));
3160 *textp = ds_steal_cstr (&s);
3162 while (lex_token (lexer) != T_SLASH
3163 && lex_token (lexer) != T_ENDCMD);
3165 else if (lex_match_id (lexer, "SIGTEST"))
3169 t->chisq = xmalloc (sizeof *t->chisq);
3170 *t->chisq = (struct ctables_chisq) {
3172 .include_mrsets = true,
3173 .all_visible = true,
3179 if (lex_match_id (lexer, "TYPE"))
3181 lex_match (lexer, T_EQUALS);
3182 if (!lex_force_match_id (lexer, "CHISQUARE"))
3185 else if (lex_match_id (lexer, "ALPHA"))
3187 lex_match (lexer, T_EQUALS);
3188 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3190 t->chisq->alpha = lex_number (lexer);
3193 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3195 lex_match (lexer, T_EQUALS);
3196 if (parse_bool (lexer, &t->chisq->include_mrsets))
3199 else if (lex_match_id (lexer, "CATEGORIES"))
3201 lex_match (lexer, T_EQUALS);
3202 if (lex_match_id (lexer, "ALLVISIBLE"))
3203 t->chisq->all_visible = true;
3204 else if (lex_match_id (lexer, "SUBTOTALS"))
3205 t->chisq->all_visible = false;
3208 lex_error_expecting (lexer,
3209 "ALLVISIBLE", "SUBTOTALS");
3215 lex_error_expecting (lexer, "TYPE", "ALPHA",
3216 "INCLUDEMRSETS", "CATEGORIES");
3220 while (lex_token (lexer) != T_SLASH
3221 && lex_token (lexer) != T_ENDCMD);
3223 else if (lex_match_id (lexer, "COMPARETEST"))
3227 t->pairwise = xmalloc (sizeof *t->pairwise);
3228 *t->pairwise = (struct ctables_pairwise) {
3230 .alpha = { .05, .05 },
3231 .adjust = BONFERRONI,
3232 .include_mrsets = true,
3233 .meansvariance_allcats = true,
3234 .all_visible = true,
3243 if (lex_match_id (lexer, "TYPE"))
3245 lex_match (lexer, T_EQUALS);
3246 if (lex_match_id (lexer, "PROP"))
3247 t->pairwise->type = PROP;
3248 else if (lex_match_id (lexer, "MEAN"))
3249 t->pairwise->type = MEAN;
3252 lex_error_expecting (lexer, "PROP", "MEAN");
3256 else if (lex_match_id (lexer, "ALPHA"))
3258 lex_match (lexer, T_EQUALS);
3260 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3262 double a0 = lex_number (lexer);
3265 lex_match (lexer, T_COMMA);
3266 if (lex_is_number (lexer))
3268 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3270 double a1 = lex_number (lexer);
3273 t->pairwise->alpha[0] = MIN (a0, a1);
3274 t->pairwise->alpha[1] = MAX (a0, a1);
3277 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3279 else if (lex_match_id (lexer, "ADJUST"))
3281 lex_match (lexer, T_EQUALS);
3282 if (lex_match_id (lexer, "BONFERRONI"))
3283 t->pairwise->adjust = BONFERRONI;
3284 else if (lex_match_id (lexer, "BH"))
3285 t->pairwise->adjust = BH;
3286 else if (lex_match_id (lexer, "NONE"))
3287 t->pairwise->adjust = 0;
3290 lex_error_expecting (lexer, "BONFERRONI", "BH",
3295 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3297 lex_match (lexer, T_EQUALS);
3298 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3301 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3303 lex_match (lexer, T_EQUALS);
3304 if (lex_match_id (lexer, "ALLCATS"))
3305 t->pairwise->meansvariance_allcats = true;
3306 else if (lex_match_id (lexer, "TESTEDCATS"))
3307 t->pairwise->meansvariance_allcats = false;
3310 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3314 else if (lex_match_id (lexer, "CATEGORIES"))
3316 lex_match (lexer, T_EQUALS);
3317 if (lex_match_id (lexer, "ALLVISIBLE"))
3318 t->pairwise->all_visible = true;
3319 else if (lex_match_id (lexer, "SUBTOTALS"))
3320 t->pairwise->all_visible = false;
3323 lex_error_expecting (lexer, "ALLVISIBLE",
3328 else if (lex_match_id (lexer, "MERGE"))
3330 lex_match (lexer, T_EQUALS);
3331 if (!parse_bool (lexer, &t->pairwise->merge))
3334 else if (lex_match_id (lexer, "STYLE"))
3336 lex_match (lexer, T_EQUALS);
3337 if (lex_match_id (lexer, "APA"))
3338 t->pairwise->apa_style = true;
3339 else if (lex_match_id (lexer, "SIMPLE"))
3340 t->pairwise->apa_style = false;
3343 lex_error_expecting (lexer, "APA", "SIMPLE");
3347 else if (lex_match_id (lexer, "SHOWSIG"))
3349 lex_match (lexer, T_EQUALS);
3350 if (!parse_bool (lexer, &t->pairwise->show_sig))
3355 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3356 "INCLUDEMRSETS", "MEANSVARIANCE",
3357 "CATEGORIES", "MERGE", "STYLE",
3362 while (lex_token (lexer) != T_SLASH
3363 && lex_token (lexer) != T_ENDCMD);
3367 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3368 "CRITERIA", "CATEGORIES", "TITLES",
3369 "SIGTEST", "COMPARETEST");
3373 if (!lex_match (lexer, T_SLASH))
3377 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3379 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3384 while (lex_token (lexer) != T_ENDCMD);
3386 bool ok = ctables_execute (ds, ct);
3387 ctables_destroy (ct);
3388 return ok ? CMD_SUCCESS : CMD_FAILURE;
3391 ctables_destroy (ct);