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/>. */
19 #include "data/casereader.h"
20 #include "data/dataset.h"
21 #include "data/dictionary.h"
22 #include "data/mrset.h"
23 #include "language/command.h"
24 #include "language/lexer/format-parser.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/variable-parser.h"
27 #include "language/stats/freq.h"
28 #include "libpspp/array.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/hmap.h"
31 #include "libpspp/message.h"
32 #include "libpspp/string-array.h"
33 #include "output/pivot-table.h"
35 #include "gl/minmax.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
40 #define N_(msgid) (msgid)
44 CTVL_DEFAULT = SETTINGS_VALUE_SHOW_DEFAULT,
45 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
46 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
47 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
51 ctables_vlabel_unique (enum ctables_vlabel vlabel)
53 /* This ensures that all of the values are unique. */
66 - unweighted summaries (U*)
67 - lower confidence limits (*.LCL)
68 - upper confidence limits (*.UCL)
69 - standard error (*.SE)
72 /* All variables. */ \
73 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
74 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
75 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
90 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
91 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
92 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
93 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
94 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
95 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
97 /* Scale variables, totals, and subtotals. */ \
98 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
103 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
104 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
105 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
106 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
107 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
108 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
109 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
110 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
111 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
112 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
113 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
114 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
115 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
117 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
118 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
119 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
120 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
122 /* Multiple response sets. */ \
123 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
124 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
139 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
140 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
141 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
142 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
143 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
144 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
146 enum ctables_summary_function
148 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
154 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
155 N_CTSF_FUNCTIONS = SUMMARIES
161 struct pivot_table_look *look;
163 /* If this is NULL, zeros are displayed using the normal print format.
164 Otherwise, this string is displayed. */
167 /* If this is NULL, missing values are displayed using the normal print
168 format. Otherwise, this string is displayed. */
171 /* Indexed by variable dictionary index. */
172 enum ctables_vlabel *vlabels;
174 bool mrsets_count_duplicates; /* MRSETS. */
175 bool smissing_listwise; /* SMISSING. */
176 struct variable *base_weight; /* WEIGHT. */
177 int hide_threshold; /* HIDESMALLCOUNTS. */
179 struct ctables_table *tables;
183 struct ctables_postcompute
185 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
186 const char *name; /* Name, without leading &. */
188 struct ctables_postcompute_expr *expr;
191 bool hide_source_cats;
194 struct ctables_postcompute_expr
196 enum ctables_postcompute_op
204 /* XXX SUBTOTAL and HSUBTOTAL */
217 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
222 XXX what about string ranges? */
225 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
226 struct ctables_postcompute_expr *subs[2];
230 enum ctables_label_position
239 struct ctables_axis *axes[PIVOT_N_AXES];
241 enum pivot_axis_type slabels_position;
242 bool slabels_visible;
244 enum ctables_label_position row_labels;
245 enum ctables_label_position col_labels;
247 /* Indexed by variable dictionary index. */
248 struct ctables_categories **categories;
257 struct ctables_chisq *chisq;
258 struct ctables_pairwise *pairwise;
260 struct ctables_freqtab **fts;
269 struct variable *var;
270 const struct mrset *mrset;
274 static const struct fmt_spec *
275 ctables_var_get_print_format (const struct ctables_var *var)
277 return (var->is_mrset
278 ? var_get_print_format (var->mrset->vars[0])
279 : var_get_print_format (var->var));
283 ctables_var_name (const struct ctables_var *var)
285 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
288 struct ctables_categories
292 /* Explicit categories. */
293 struct ctables_cat_value *values;
296 /* Implicit categories. */
298 bool include_missing;
299 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
300 enum ctables_summary_function sort_func;
301 struct variable *sort_func_var;
309 /* Empty categories. */
313 struct ctables_cat_value
315 enum ctables_cat_value_type
329 double number; /* CCVT_NUMBER. */
330 char *string; /* CCVT_STRING. */
331 double range[2]; /* CCVT_RANGE. */
332 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
337 ctables_cat_value_uninit (struct ctables_cat_value *cv)
356 free (cv->subtotal_label);
361 ctables_categories_unref (struct ctables_categories *c)
366 assert (c->n_refs > 0);
370 for (size_t i = 0; i < c->n_values; i++)
371 ctables_cat_value_uninit (&c->values[i]);
373 free (c->total_label);
377 /* Chi-square test (SIGTEST). */
385 /* Pairwise comparison test (COMPARETEST). */
386 struct ctables_pairwise
388 enum { PROP, MEAN } type;
391 bool meansvariance_allcats;
393 enum { BONFERRONI = 1, BH } adjust;
417 struct ctables_var var;
419 struct ctables_summary *summaries;
421 size_t allocated_summaries;
425 struct ctables_axis *subs[2];
428 struct msg_location *loc;
431 static void ctables_axis_destroy (struct ctables_axis *);
440 enum ctables_function_availability
442 CTFA_ALL, /* Any variables. */
443 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
444 CTFA_MRSETS, /* Only multiple-response sets */
447 struct ctables_summary
449 enum ctables_summary_function function;
450 double percentile; /* CTSF_PTILE only. */
452 struct fmt_spec format; /* XXX extra CTABLES formats */
456 ctables_summary_uninit (struct ctables_summary *s)
463 parse_col_width (struct lexer *lexer, const char *name, double *width)
465 lex_match (lexer, T_EQUALS);
466 if (lex_match_id (lexer, "DEFAULT"))
468 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
470 *width = lex_number (lexer);
480 parse_bool (struct lexer *lexer, bool *b)
482 if (lex_match_id (lexer, "NO"))
484 else if (lex_match_id (lexer, "YES"))
488 lex_error_expecting (lexer, "YES", "NO");
494 static enum ctables_function_availability
495 ctables_function_availability (enum ctables_summary_function f)
497 static enum ctables_function_availability availability[] = {
498 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
503 return availability[f];
507 parse_ctables_summary_function (struct lexer *lexer,
508 enum ctables_summary_function *f)
512 enum ctables_summary_function function;
513 struct substring name;
515 static struct pair names[] = {
516 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
517 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
520 /* The .COUNT suffix may be omitted. */
521 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
522 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
523 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
524 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
525 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
526 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
527 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
531 if (!lex_force_id (lexer))
534 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
535 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
537 *f = names[i].function;
542 lex_error (lexer, _("Expecting summary function name."));
547 ctables_axis_destroy (struct ctables_axis *axis)
555 for (size_t i = 0; i < axis->n_summaries; i++)
556 ctables_summary_uninit (&axis->summaries[i]);
557 free (axis->summaries);
562 ctables_axis_destroy (axis->subs[0]);
563 ctables_axis_destroy (axis->subs[1]);
566 msg_location_destroy (axis->loc);
570 static struct ctables_axis *
571 ctables_axis_new_nonterminal (enum ctables_axis_op op,
572 struct ctables_axis *sub0,
573 struct ctables_axis *sub1,
574 struct lexer *lexer, int start_ofs)
576 struct ctables_axis *axis = xmalloc (sizeof *axis);
577 *axis = (struct ctables_axis) {
579 .subs = { sub0, sub1 },
580 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
585 struct ctables_axis_parse_ctx
588 struct dictionary *dict;
590 struct ctables_table *t;
593 static struct fmt_spec
594 ctables_summary_default_format (enum ctables_summary_function function,
595 const struct ctables_var *var)
597 static const enum ctables_format default_formats[] = {
598 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
602 switch (default_formats[function])
605 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
608 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
611 return *ctables_var_get_print_format (var);
619 ctables_summary_function_name (enum ctables_summary_function function)
621 static const char *names[] = {
622 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
626 return names[function];
630 add_summary (struct ctables_axis *axis,
631 enum ctables_summary_function function, double percentile,
632 const char *label, const struct fmt_spec *format,
633 const struct msg_location *loc)
635 if (axis->op == CTAO_VAR)
637 if (axis->n_summaries >= axis->allocated_summaries)
638 axis->summaries = x2nrealloc (axis->summaries,
639 &axis->allocated_summaries,
640 sizeof *axis->summaries);
642 const char *function_name = ctables_summary_function_name (function);
643 const char *var_name = ctables_var_name (&axis->var);
644 switch (ctables_function_availability (function))
647 if (!axis->var.is_mrset)
649 msg_at (SE, loc, _("Summary function %s applies only to multiple "
650 "response sets."), function_name);
651 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
661 _("Summary function %s applies only to scale variables."),
663 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
673 struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
674 *dst = (struct ctables_summary) {
675 .function = function,
676 .percentile = percentile,
677 .label = xstrdup (label),
678 .format = (format ? *format
679 : ctables_summary_default_format (function, &axis->var)),
685 for (size_t i = 0; i < 2; i++)
686 if (!add_summary (axis->subs[i], function, percentile, label, format,
693 static struct ctables_axis *ctables_axis_parse_stack (
694 struct ctables_axis_parse_ctx *);
697 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
698 struct ctables_var *var)
700 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
702 *var = (struct ctables_var) {
704 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
708 lex_error (lexer, _("'%s' does not name a multiple-response set "
709 "in the active file dictionary."),
710 lex_tokcstr (lexer));
718 *var = (struct ctables_var) {
720 .var = parse_variable (lexer, dict),
722 return var->var != NULL;
726 static struct ctables_axis *
727 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
729 if (lex_match (ctx->lexer, T_LPAREN))
731 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
732 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
734 ctables_axis_destroy (sub);
740 if (!lex_force_id (ctx->lexer))
743 int start_ofs = lex_ofs (ctx->lexer);
744 struct ctables_var var;
745 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
748 struct ctables_axis *axis = xmalloc (sizeof *axis);
749 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
751 /* XXX should figure out default measures by reading data */
752 axis->scale = (var.is_mrset ? false
753 : lex_match_phrase (ctx->lexer, "[S]") ? true
754 : lex_match_phrase (ctx->lexer, "[C]") ? false
755 : var_get_measure (var.var) == MEASURE_SCALE);
756 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
757 lex_ofs (ctx->lexer) - 1);
761 static struct ctables_axis *
762 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
764 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
765 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
770 int start_ofs = lex_ofs (ctx->lexer);
772 /* Parse function. */
773 enum ctables_summary_function function;
774 if (!parse_ctables_summary_function (ctx->lexer, &function))
777 /* Parse percentile. */
778 double percentile = 0;
779 if (function == CTSF_PTILE)
781 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
783 percentile = lex_number (ctx->lexer);
784 lex_get (ctx->lexer);
789 if (lex_is_string (ctx->lexer))
791 label = ss_xstrdup (lex_tokss (ctx->lexer));
792 lex_get (ctx->lexer);
794 else if (function == CTSF_PTILE)
795 label = xasprintf (_("Percentile %.2f"), percentile);
798 static const char *default_labels[] = {
799 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
803 label = xstrdup (gettext (default_labels[function]));
807 struct fmt_spec format;
808 const struct fmt_spec *formatp;
809 if (lex_token (ctx->lexer) == T_ID)
811 if (!parse_format_specifier (ctx->lexer, &format)
812 || !fmt_check_output (&format)
813 || !fmt_check_type_compat (&format, VAL_NUMERIC))
823 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
824 lex_ofs (ctx->lexer) - 1);
825 add_summary (sub, function, percentile, label, formatp, loc);
827 msg_location_destroy (loc);
829 lex_match (ctx->lexer, T_COMMA);
831 while (!lex_match (ctx->lexer, T_RBRACK));
836 ctables_axis_destroy (sub);
840 static const struct ctables_axis *
841 find_scale (const struct ctables_axis *axis)
845 else if (axis->op == CTAO_VAR)
849 assert (!axis->var.is_mrset);
857 for (size_t i = 0; i < 2; i++)
859 const struct ctables_axis *scale = find_scale (axis->subs[i]);
867 static const struct ctables_axis *
868 find_categorical_summary (const struct ctables_axis *axis)
872 else if (axis->op == CTAO_VAR)
873 return !axis->scale && axis->n_summaries ? axis : NULL;
876 for (size_t i = 0; i < 2; i++)
878 const struct ctables_axis *sum
879 = find_categorical_summary (axis->subs[i]);
887 static struct ctables_axis *
888 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
890 int start_ofs = lex_ofs (ctx->lexer);
891 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
895 while (lex_match (ctx->lexer, T_GT))
897 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
901 struct ctables_axis *nest = ctables_axis_new_nonterminal (
902 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
904 const struct ctables_axis *outer_scale = find_scale (lhs);
905 const struct ctables_axis *inner_scale = find_scale (rhs);
906 if (outer_scale && inner_scale)
908 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
909 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
910 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
911 ctables_axis_destroy (nest);
915 const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
918 msg_at (SE, nest->loc,
919 _("Summaries may only be requested for categorical variables "
920 "at the innermost nesting level."));
921 msg_at (SN, outer_sum->loc,
922 _("This outer categorical variable has a summary."));
923 ctables_axis_destroy (nest);
933 static struct ctables_axis *
934 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
936 int start_ofs = lex_ofs (ctx->lexer);
937 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
941 while (lex_match (ctx->lexer, T_PLUS))
943 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
947 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
948 ctx->lexer, start_ofs);
955 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
956 struct ctables *ct, struct ctables_table *t,
957 enum pivot_axis_type a)
959 if (lex_token (lexer) == T_BY
960 || lex_token (lexer) == T_SLASH
961 || lex_token (lexer) == T_ENDCMD)
964 struct ctables_axis_parse_ctx ctx = {
970 t->axes[a] = ctables_axis_parse_stack (&ctx);
971 return t->axes[a] != NULL;
975 ctables_chisq_destroy (struct ctables_chisq *chisq)
981 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
987 ctables_table_uninit (struct ctables_table *t)
992 for (size_t i = 0; i < t->n_categories; i++)
993 ctables_categories_unref (t->categories[i]);
994 free (t->categories);
996 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
997 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
998 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1002 ctables_chisq_destroy (t->chisq);
1003 ctables_pairwise_destroy (t->pairwise);
1007 ctables_destroy (struct ctables *ct)
1012 pivot_table_look_unref (ct->look);
1016 for (size_t i = 0; i < ct->n_tables; i++)
1017 ctables_table_uninit (&ct->tables[i]);
1022 static struct ctables_cat_value
1023 ccvt_range (double low, double high)
1025 return (struct ctables_cat_value) {
1027 .range = { low, high }
1032 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1033 struct ctables_table *t)
1035 if (!lex_match_id (lexer, "VARIABLES"))
1037 lex_match (lexer, T_EQUALS);
1039 struct variable **vars;
1041 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1044 struct ctables_categories *c = xmalloc (sizeof *c);
1045 *c = (struct ctables_categories) { .n_refs = n_vars };
1046 for (size_t i = 0; i < n_vars; i++)
1048 struct ctables_categories **cp
1049 = &t->categories[var_get_dict_index (vars[i])];
1050 ctables_categories_unref (*cp);
1055 if (lex_match (lexer, T_LBRACK))
1057 size_t allocated_values = 0;
1060 if (c->n_values >= allocated_values)
1061 c->values = x2nrealloc (c->values, &allocated_values,
1064 struct ctables_cat_value *v = &c->values[c->n_values];
1065 if (lex_match_id (lexer, "OTHERNM"))
1066 v->type = CCVT_OTHERNM;
1067 else if (lex_match_id (lexer, "MISSING"))
1068 v->type = CCVT_MISSING;
1069 else if (lex_match_id (lexer, "SUBTOTAL"))
1070 *v = (struct ctables_cat_value)
1071 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1072 else if (lex_match_id (lexer, "HSUBTOTAL"))
1073 *v = (struct ctables_cat_value)
1074 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1075 else if (lex_match_id (lexer, "LO"))
1077 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1079 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1082 else if (lex_is_number (lexer))
1084 double number = lex_number (lexer);
1086 if (lex_match_id (lexer, "THRU"))
1088 v->type = CCVT_RANGE;
1089 v->range[0] = number;
1090 if (lex_match_id (lexer, "HI"))
1091 *v = ccvt_range (number, DBL_MAX);
1094 if (!lex_force_num (lexer))
1096 *v = ccvt_range (number, lex_number (lexer));
1101 *v = (struct ctables_cat_value) {
1102 .type = CCVT_NUMBER,
1106 else if (lex_is_string (lexer))
1108 *v = (struct ctables_cat_value) {
1109 .type = CCVT_STRING,
1110 .string = ss_xstrdup (lex_tokss (lexer)),
1116 lex_error (lexer, NULL);
1120 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1121 && lex_match (lexer, T_EQUALS))
1123 if (!lex_force_string (lexer))
1126 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1131 lex_match (lexer, T_COMMA);
1133 while (!lex_match (lexer, T_RBRACK));
1136 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1138 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1140 lex_match (lexer, T_EQUALS);
1141 if (lex_match_id (lexer, "A"))
1142 c->sort_ascending = true;
1143 else if (lex_match_id (lexer, "D"))
1144 c->sort_ascending = false;
1147 lex_error_expecting (lexer, "A", "D");
1151 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1153 lex_match (lexer, T_EQUALS);
1154 if (lex_match_id (lexer, "VALUE"))
1155 c->key = CTCS_VALUE;
1156 else if (lex_match_id (lexer, "LABEL"))
1157 c->key = CTCS_LABEL;
1160 c->key = CTCS_FUNCTION;
1161 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1164 if (lex_match (lexer, T_LPAREN))
1166 c->sort_func_var = parse_variable (lexer, dict);
1167 if (!c->sort_func_var)
1170 if (c->sort_func == CTSF_PTILE)
1172 lex_match (lexer, T_COMMA);
1173 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1175 c->percentile = lex_number (lexer);
1179 if (!lex_force_match (lexer, T_RPAREN))
1182 else if (ctables_function_availability (c->sort_func)
1185 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1190 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1192 lex_match (lexer, T_EQUALS);
1193 if (lex_match_id (lexer, "INCLUDE"))
1194 c->include_missing = true;
1195 else if (lex_match_id (lexer, "EXCLUDE"))
1196 c->include_missing = false;
1199 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1203 else if (lex_match_id (lexer, "TOTAL"))
1205 lex_match (lexer, T_EQUALS);
1206 if (!parse_bool (lexer, &c->show_totals))
1209 else if (lex_match_id (lexer, "LABEL"))
1211 lex_match (lexer, T_EQUALS);
1212 if (!lex_force_string (lexer))
1214 free (c->total_label);
1215 c->total_label = ss_xstrdup (lex_tokss (lexer));
1218 else if (lex_match_id (lexer, "POSITION"))
1220 lex_match (lexer, T_EQUALS);
1221 if (lex_match_id (lexer, "BEFORE"))
1222 c->totals_before = true;
1223 else if (lex_match_id (lexer, "AFTER"))
1224 c->totals_before = false;
1227 lex_error_expecting (lexer, "BEFORE", "AFTER");
1231 else if (lex_match_id (lexer, "EMPTY"))
1233 lex_match (lexer, T_EQUALS);
1234 if (lex_match_id (lexer, "INCLUDE"))
1235 c->show_empty = true;
1236 else if (lex_match_id (lexer, "EXCLUDE"))
1237 c->show_empty = false;
1240 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1247 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1248 "TOTAL", "LABEL", "POSITION", "EMPTY");
1250 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1259 struct variable **vars;
1264 var_array_uninit (struct var_array *va)
1272 struct var_array *vas;
1277 var_array2_uninit (struct var_array2 *vaa)
1281 for (size_t i = 0; i < vaa->n; i++)
1282 var_array_uninit (&vaa->vas[i]);
1287 static struct var_array2
1288 nest_fts (struct var_array2 va0, struct var_array2 va1)
1295 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1296 for (size_t i = 0; i < va0.n; i++)
1297 for (size_t j = 0; j < va1.n; j++)
1299 size_t allocate = va0.vas[i].n + va1.vas[j].n;
1300 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1302 for (size_t k = 0; k < va0.vas[i].n; k++)
1303 vars[n++] = va0.vas[i].vars[k];
1304 for (size_t k = 0; k < va1.vas[j].n; k++)
1305 vars[n++] = va1.vas[j].vars[k];
1306 assert (n == allocate);
1308 vaa.vas[vaa.n++] = (struct var_array) { .vars = vars, n = n };
1310 var_array2_uninit (&va0);
1311 var_array2_uninit (&va1);
1315 static struct var_array2
1316 stack_fts (struct var_array2 va0, struct var_array2 va1)
1318 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1319 for (size_t i = 0; i < va0.n; i++)
1320 vaa.vas[vaa.n++] = va0.vas[i];
1321 for (size_t i = 0; i < va1.n; i++)
1322 vaa.vas[vaa.n++] = va1.vas[i];
1323 assert (vaa.n == va0.n + va1.n);
1329 static struct var_array2
1330 enumerate_fts (const struct ctables_axis *a)
1333 return (struct var_array2) { .n = 0 };
1338 assert (!a->var.is_mrset);
1339 struct variable **v = xmalloc (sizeof *v);
1341 struct var_array *va = xmalloc (sizeof *va);
1342 *va = (struct var_array) { .vars = v, .n = 1 };
1343 return (struct var_array2) { .vas = va, .n = 1 };
1346 return stack_fts (enumerate_fts (a->subs[0]),
1347 enumerate_fts (a->subs[1]));
1350 return nest_fts (enumerate_fts (a->subs[0]),
1351 enumerate_fts (a->subs[1]));
1357 struct ctables_freqtab
1359 struct var_array vars;
1360 struct hmap data; /* Contains "struct freq"s. */
1361 struct freq **sorted;
1365 compare_freq_3way (const void *a_, const void *b_, const void *vars_)
1367 const struct var_array *vars = vars_;
1368 struct freq *const *a = a_;
1369 struct freq *const *b = b_;
1371 for (size_t i = 0; i < vars->n; i++)
1373 int cmp = value_compare_3way (&(*a)->values[i], &(*b)->values[i],
1374 var_get_width (vars->vars[i]));
1383 ctables_execute (struct dataset *ds, struct ctables *ct)
1385 for (size_t i = 0; i < ct->n_tables; i++)
1387 size_t allocated_fts = 0;
1389 struct ctables_table *t = &ct->tables[i];
1390 struct var_array2 vaa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
1391 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
1392 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
1393 for (size_t i = 0; i < vaa.n; i++)
1395 for (size_t j = 0; j < vaa.vas[i].n; j++)
1398 fputs (", ", stdout);
1399 fputs (var_get_name (vaa.vas[i].vars[j]), stdout);
1404 for (size_t j = 0; j < vaa.n; j++)
1406 struct ctables_freqtab *ft = xmalloc (sizeof *ft);
1407 *ft = (struct ctables_freqtab) {
1409 .data = HMAP_INITIALIZER (ft->data),
1412 if (t->n_fts >= allocated_fts)
1413 t->fts = x2nrealloc (t->fts, &allocated_fts, sizeof *t->fts);
1414 t->fts[t->n_fts++] = ft;
1420 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
1423 bool warn_on_invalid = true;
1424 for (struct ccase *c = casereader_read (input); c;
1425 case_unref (c), c = casereader_read (input))
1427 double weight = dict_get_case_weight (dataset_dict (ds), c,
1430 for (size_t i = 0; i < ct->n_tables; i++)
1432 struct ctables_table *t = &ct->tables[i];
1434 for (size_t j = 0; j < t->n_fts; j++)
1436 struct ctables_freqtab *ft = t->fts[j];
1439 for (size_t k = 0; k < ft->vars.n; k++)
1441 const struct variable *var = ft->vars.vars[k];
1448 for (size_t k = 0; k < ft->vars.n; k++)
1450 const struct variable *var = ft->vars.vars[k];
1451 hash = value_hash (case_data (c, var), var_get_width (var), hash);
1455 HMAP_FOR_EACH_WITH_HASH (f, struct freq, node, hash, &ft->data)
1457 for (size_t k = 0; k < ft->vars.n; k++)
1459 const struct variable *var = ft->vars.vars[k];
1460 if (!value_equal (case_data (c, var), &f->values[k],
1461 var_get_width (var)))
1462 goto next_hash_node;
1471 f = xmalloc (table_entry_size (ft->vars.n));
1473 for (size_t k = 0; k < ft->vars.n; k++)
1475 const struct variable *var = ft->vars.vars[k];
1476 value_clone (&f->values[k], case_data (c, var),
1477 var_get_width (var));
1479 hmap_insert (&ft->data, &f->node, hash);
1485 casereader_destroy (input);
1487 for (size_t i = 0; i < ct->n_tables; i++)
1489 struct ctables_table *t = &ct->tables[i];
1491 struct pivot_table *pt = pivot_table_create (N_("Custom Tables"));
1492 struct pivot_dimension *d = pivot_dimension_create (
1493 pt, PIVOT_AXIS_ROW, N_("Rows"));
1494 for (size_t j = 0; j < t->n_fts; j++)
1496 struct ctables_freqtab *ft = t->fts[j];
1497 ft->sorted = xnmalloc (ft->data.count, sizeof *ft->sorted);
1501 HMAP_FOR_EACH (f, struct freq, node, &ft->data)
1502 ft->sorted[n++] = f;
1503 assert (n == ft->data.count);
1504 sort (ft->sorted, n, sizeof *ft->sorted,
1505 compare_freq_3way, &ft->vars);
1507 struct pivot_category **groups = xnmalloc (ft->vars.n,
1509 for (size_t k = 0; k < n; k++)
1511 struct freq *prev = k > 0 ? ft->sorted[k - 1] : NULL;
1512 struct freq *f = ft->sorted[k];
1514 size_t n_common = 0;
1516 for (; n_common + 1 < ft->vars.n; n_common++)
1517 if (!value_equal (&prev->values[n_common],
1518 &f->values[n_common],
1519 var_get_type (ft->vars.vars[n_common])))
1522 for (size_t m = n_common; m + 1 < ft->vars.n; m++)
1523 groups[m] = pivot_category_create_group__ (
1524 m > 0 ? groups[m - 1] : d->root,
1525 pivot_value_new_var_value (ft->vars.vars[m], &f->values[m]));
1527 int leaf = pivot_category_create_leaf (
1528 ft->vars.n > 1 ? groups[ft->vars.n - 2] : d->root,
1529 pivot_value_new_var_value (ft->vars.vars[ft->vars.n - 1],
1530 &f->values[ft->vars.n - 1]));
1532 pivot_table_put1 (pt, leaf, pivot_value_new_number (f->count));
1536 pivot_table_submit (pt);
1539 for (size_t i = 0; i < ct->n_tables; i++)
1541 struct ctables_table *t = &ct->tables[i];
1543 for (size_t j = 0; j < t->n_fts; j++)
1545 struct ctables_freqtab *ft = t->fts[j];
1546 struct freq *f, *next;
1547 HMAP_FOR_EACH_SAFE (f, next, struct freq, node, &ft->data)
1549 hmap_delete (&ft->data, &f->node);
1550 for (size_t k = 0; k < ft->vars.n; k++)
1552 const struct variable *var = ft->vars.vars[k];
1553 value_destroy (&f->values[k], var_get_width (var));
1557 hmap_destroy (&ft->data);
1559 var_array_uninit (&ft->vars);
1565 return proc_commit (ds);
1569 cmd_ctables (struct lexer *lexer, struct dataset *ds)
1571 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
1572 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
1573 for (size_t i = 0; i < n_vars; i++)
1574 vlabels[i] = CTVL_DEFAULT;
1576 struct ctables *ct = xmalloc (sizeof *ct);
1577 *ct = (struct ctables) {
1578 .look = pivot_table_look_unshare (pivot_table_look_ref (
1579 pivot_table_look_get_default ())),
1581 .hide_threshold = 5,
1584 if (!lex_force_match (lexer, T_SLASH))
1587 while (!lex_match_id (lexer, "TABLE"))
1589 if (lex_match_id (lexer, "FORMAT"))
1591 double widths[2] = { SYSMIS, SYSMIS };
1592 double units_per_inch = 72.0;
1594 while (lex_token (lexer) != T_SLASH)
1596 if (lex_match_id (lexer, "MINCOLWIDTH"))
1598 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1601 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1603 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1606 else if (lex_match_id (lexer, "UNITS"))
1608 lex_match (lexer, T_EQUALS);
1609 if (lex_match_id (lexer, "POINTS"))
1610 units_per_inch = 72.0;
1611 else if (lex_match_id (lexer, "INCHES"))
1612 units_per_inch = 1.0;
1613 else if (lex_match_id (lexer, "CM"))
1614 units_per_inch = 2.54;
1617 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1621 else if (lex_match_id (lexer, "EMPTY"))
1626 lex_match (lexer, T_EQUALS);
1627 if (lex_match_id (lexer, "ZERO"))
1629 /* Nothing to do. */
1631 else if (lex_match_id (lexer, "BLANK"))
1632 ct->zero = xstrdup ("");
1633 else if (lex_force_string (lexer))
1635 ct->zero = ss_xstrdup (lex_tokss (lexer));
1641 else if (lex_match_id (lexer, "MISSING"))
1643 lex_match (lexer, T_EQUALS);
1644 if (!lex_force_string (lexer))
1648 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1649 ? ss_xstrdup (lex_tokss (lexer))
1655 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1656 "UNITS", "EMPTY", "MISSING");
1661 if (widths[0] != SYSMIS && widths[1] != SYSMIS
1662 && widths[0] > widths[1])
1664 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1668 for (size_t i = 0; i < 2; i++)
1669 if (widths[i] != SYSMIS)
1671 int *wr = ct->look->width_ranges[TABLE_HORZ];
1672 wr[i] = widths[i] / units_per_inch * 96.0;
1677 else if (lex_match_id (lexer, "VLABELS"))
1679 if (!lex_force_match_id (lexer, "VARIABLES"))
1681 lex_match (lexer, T_EQUALS);
1683 struct variable **vars;
1685 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1689 if (!lex_force_match_id (lexer, "DISPLAY"))
1694 lex_match (lexer, T_EQUALS);
1696 enum ctables_vlabel vlabel;
1697 if (lex_match_id (lexer, "DEFAULT"))
1698 vlabel = CTVL_DEFAULT;
1699 else if (lex_match_id (lexer, "NAME"))
1701 else if (lex_match_id (lexer, "LABEL"))
1702 vlabel = CTVL_LABEL;
1703 else if (lex_match_id (lexer, "BOTH"))
1705 else if (lex_match_id (lexer, "NONE"))
1709 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1715 for (size_t i = 0; i < n_vars; i++)
1716 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1719 else if (lex_match_id (lexer, "MRSETS"))
1721 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1723 lex_match (lexer, T_EQUALS);
1724 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1727 else if (lex_match_id (lexer, "SMISSING"))
1729 if (lex_match_id (lexer, "VARIABLE"))
1730 ct->smissing_listwise = false;
1731 else if (lex_match_id (lexer, "LISTWISE"))
1732 ct->smissing_listwise = true;
1735 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1740 else if (lex_match_id (lexer, "WEIGHT"))
1742 if (!lex_force_match_id (lexer, "VARIABLE"))
1744 lex_match (lexer, T_EQUALS);
1745 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1746 if (!ct->base_weight)
1749 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1751 if (!lex_force_match_id (lexer, "COUNT"))
1753 lex_match (lexer, T_EQUALS);
1754 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1756 ct->hide_threshold = lex_integer (lexer);
1761 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1762 "SMISSING", "PCOMPUTE", "PPROPERTIES",
1763 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1767 if (!lex_force_match (lexer, T_SLASH))
1771 size_t allocated_tables = 0;
1774 if (ct->n_tables >= allocated_tables)
1775 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1776 sizeof *ct->tables);
1778 struct ctables_table *t = &ct->tables[ct->n_tables++];
1779 *t = (struct ctables_table) {
1780 .slabels_position = PIVOT_AXIS_COLUMN,
1781 .slabels_visible = true,
1782 .row_labels = CTLP_NORMAL,
1783 .col_labels = CTLP_NORMAL,
1784 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
1785 sizeof *t->categories),
1786 .n_categories = dict_get_n_vars (dataset_dict (ds)),
1790 lex_match (lexer, T_EQUALS);
1791 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1793 if (lex_match (lexer, T_BY))
1795 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1796 ct, t, PIVOT_AXIS_COLUMN))
1799 if (lex_match (lexer, T_BY))
1801 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1802 ct, t, PIVOT_AXIS_LAYER))
1807 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1808 && !t->axes[PIVOT_AXIS_LAYER])
1810 lex_error (lexer, _("At least one variable must be specified."));
1814 const struct ctables_axis *scales[PIVOT_N_AXES];
1815 size_t n_scales = 0;
1816 for (size_t i = 0; i < 3; i++)
1818 scales[i] = find_scale (t->axes[i]);
1824 msg (SE, _("Scale variables may appear only on one dimension."));
1825 if (scales[PIVOT_AXIS_ROW])
1826 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
1827 _("This scale variable appears in the rows dimension."));
1828 if (scales[PIVOT_AXIS_COLUMN])
1829 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
1830 _("This scale variable appears in the columns dimension."));
1831 if (scales[PIVOT_AXIS_LAYER])
1832 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
1833 _("This scale variable appears in the layer dimension."));
1837 if (lex_token (lexer) == T_ENDCMD)
1839 if (!lex_force_match (lexer, T_SLASH))
1842 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1844 if (lex_match_id (lexer, "SLABELS"))
1846 while (lex_token (lexer) != T_SLASH)
1848 if (lex_match_id (lexer, "POSITION"))
1850 lex_match (lexer, T_EQUALS);
1851 if (lex_match_id (lexer, "COLUMN"))
1852 t->slabels_position = PIVOT_AXIS_COLUMN;
1853 else if (lex_match_id (lexer, "ROW"))
1854 t->slabels_position = PIVOT_AXIS_ROW;
1855 else if (lex_match_id (lexer, "LAYER"))
1856 t->slabels_position = PIVOT_AXIS_LAYER;
1859 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1863 else if (lex_match_id (lexer, "VISIBLE"))
1865 lex_match (lexer, T_EQUALS);
1866 if (!parse_bool (lexer, &t->slabels_visible))
1871 lex_error_expecting (lexer, "POSITION", "VISIBLE");
1876 else if (lex_match_id (lexer, "CLABELS"))
1878 while (lex_token (lexer) != T_SLASH)
1880 if (lex_match_id (lexer, "AUTO"))
1881 t->row_labels = t->col_labels = CTLP_NORMAL;
1882 else if (lex_match_id (lexer, "ROWLABELS"))
1884 lex_match (lexer, T_EQUALS);
1885 if (lex_match_id (lexer, "OPPOSITE"))
1886 t->row_labels = CTLP_OPPOSITE;
1887 else if (lex_match_id (lexer, "LAYER"))
1888 t->row_labels = CTLP_LAYER;
1891 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1895 else if (lex_match_id (lexer, "COLLABELS"))
1897 lex_match (lexer, T_EQUALS);
1898 if (lex_match_id (lexer, "OPPOSITE"))
1899 t->col_labels = CTLP_OPPOSITE;
1900 else if (lex_match_id (lexer, "LAYER"))
1901 t->col_labels = CTLP_LAYER;
1904 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1910 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1916 else if (lex_match_id (lexer, "CRITERIA"))
1918 if (!lex_force_match_id (lexer, "CILEVEL"))
1920 lex_match (lexer, T_EQUALS);
1922 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1924 t->cilevel = lex_number (lexer);
1927 else if (lex_match_id (lexer, "CATEGORIES"))
1929 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1932 else if (lex_match_id (lexer, "TITLES"))
1937 if (lex_match_id (lexer, "CAPTION"))
1938 textp = &t->caption;
1939 else if (lex_match_id (lexer, "CORNER"))
1941 else if (lex_match_id (lexer, "TITLE"))
1945 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1948 lex_match (lexer, T_EQUALS);
1950 struct string s = DS_EMPTY_INITIALIZER;
1951 while (lex_is_string (lexer))
1953 if (!ds_is_empty (&s))
1954 ds_put_byte (&s, ' ');
1955 ds_put_substring (&s, lex_tokss (lexer));
1959 *textp = ds_steal_cstr (&s);
1961 while (lex_token (lexer) != T_SLASH
1962 && lex_token (lexer) != T_ENDCMD);
1964 else if (lex_match_id (lexer, "SIGTEST"))
1968 t->chisq = xmalloc (sizeof *t->chisq);
1969 *t->chisq = (struct ctables_chisq) {
1971 .include_mrsets = true,
1972 .all_visible = true,
1978 if (lex_match_id (lexer, "TYPE"))
1980 lex_match (lexer, T_EQUALS);
1981 if (!lex_force_match_id (lexer, "CHISQUARE"))
1984 else if (lex_match_id (lexer, "ALPHA"))
1986 lex_match (lexer, T_EQUALS);
1987 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
1989 t->chisq->alpha = lex_number (lexer);
1992 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1994 lex_match (lexer, T_EQUALS);
1995 if (parse_bool (lexer, &t->chisq->include_mrsets))
1998 else if (lex_match_id (lexer, "CATEGORIES"))
2000 lex_match (lexer, T_EQUALS);
2001 if (lex_match_id (lexer, "ALLVISIBLE"))
2002 t->chisq->all_visible = true;
2003 else if (lex_match_id (lexer, "SUBTOTALS"))
2004 t->chisq->all_visible = false;
2007 lex_error_expecting (lexer,
2008 "ALLVISIBLE", "SUBTOTALS");
2014 lex_error_expecting (lexer, "TYPE", "ALPHA",
2015 "INCLUDEMRSETS", "CATEGORIES");
2019 while (lex_token (lexer) != T_SLASH
2020 && lex_token (lexer) != T_ENDCMD);
2022 else if (lex_match_id (lexer, "COMPARETEST"))
2026 t->pairwise = xmalloc (sizeof *t->pairwise);
2027 *t->pairwise = (struct ctables_pairwise) {
2029 .alpha = { .05, .05 },
2030 .adjust = BONFERRONI,
2031 .include_mrsets = true,
2032 .meansvariance_allcats = true,
2033 .all_visible = true,
2042 if (lex_match_id (lexer, "TYPE"))
2044 lex_match (lexer, T_EQUALS);
2045 if (lex_match_id (lexer, "PROP"))
2046 t->pairwise->type = PROP;
2047 else if (lex_match_id (lexer, "MEAN"))
2048 t->pairwise->type = MEAN;
2051 lex_error_expecting (lexer, "PROP", "MEAN");
2055 else if (lex_match_id (lexer, "ALPHA"))
2057 lex_match (lexer, T_EQUALS);
2059 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2061 double a0 = lex_number (lexer);
2064 lex_match (lexer, T_COMMA);
2065 if (lex_is_number (lexer))
2067 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2069 double a1 = lex_number (lexer);
2072 t->pairwise->alpha[0] = MIN (a0, a1);
2073 t->pairwise->alpha[1] = MAX (a0, a1);
2076 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2078 else if (lex_match_id (lexer, "ADJUST"))
2080 lex_match (lexer, T_EQUALS);
2081 if (lex_match_id (lexer, "BONFERRONI"))
2082 t->pairwise->adjust = BONFERRONI;
2083 else if (lex_match_id (lexer, "BH"))
2084 t->pairwise->adjust = BH;
2085 else if (lex_match_id (lexer, "NONE"))
2086 t->pairwise->adjust = 0;
2089 lex_error_expecting (lexer, "BONFERRONI", "BH",
2094 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2096 lex_match (lexer, T_EQUALS);
2097 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2100 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2102 lex_match (lexer, T_EQUALS);
2103 if (lex_match_id (lexer, "ALLCATS"))
2104 t->pairwise->meansvariance_allcats = true;
2105 else if (lex_match_id (lexer, "TESTEDCATS"))
2106 t->pairwise->meansvariance_allcats = false;
2109 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2113 else if (lex_match_id (lexer, "CATEGORIES"))
2115 lex_match (lexer, T_EQUALS);
2116 if (lex_match_id (lexer, "ALLVISIBLE"))
2117 t->pairwise->all_visible = true;
2118 else if (lex_match_id (lexer, "SUBTOTALS"))
2119 t->pairwise->all_visible = false;
2122 lex_error_expecting (lexer, "ALLVISIBLE",
2127 else if (lex_match_id (lexer, "MERGE"))
2129 lex_match (lexer, T_EQUALS);
2130 if (!parse_bool (lexer, &t->pairwise->merge))
2133 else if (lex_match_id (lexer, "STYLE"))
2135 lex_match (lexer, T_EQUALS);
2136 if (lex_match_id (lexer, "APA"))
2137 t->pairwise->apa_style = true;
2138 else if (lex_match_id (lexer, "SIMPLE"))
2139 t->pairwise->apa_style = false;
2142 lex_error_expecting (lexer, "APA", "SIMPLE");
2146 else if (lex_match_id (lexer, "SHOWSIG"))
2148 lex_match (lexer, T_EQUALS);
2149 if (!parse_bool (lexer, &t->pairwise->show_sig))
2154 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2155 "INCLUDEMRSETS", "MEANSVARIANCE",
2156 "CATEGORIES", "MERGE", "STYLE",
2161 while (lex_token (lexer) != T_SLASH
2162 && lex_token (lexer) != T_ENDCMD);
2166 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2167 "CRITERIA", "CATEGORIES", "TITLES",
2168 "SIGTEST", "COMPARETEST");
2173 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2175 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2180 while (lex_token (lexer) != T_ENDCMD);
2182 bool ok = ctables_execute (ds, ct);
2183 ctables_destroy (ct);
2184 return ok ? CMD_SUCCESS : CMD_FAILURE;
2187 ctables_destroy (ct);