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 "libpspp/array.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/hmap.h"
30 #include "libpspp/message.h"
31 #include "libpspp/string-array.h"
32 #include "output/pivot-table.h"
34 #include "gl/minmax.h"
35 #include "gl/xalloc.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) (msgid)
43 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
44 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
45 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
46 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
50 - unweighted summaries (U*)
51 - lower confidence limits (*.LCL)
52 - upper confidence limits (*.UCL)
53 - standard error (*.SE)
56 /* All variables. */ \
57 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
58 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
59 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
60 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
61 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
62 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
63 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
64 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
81 /* Scale variables, totals, and subtotals. */ \
82 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
83 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
84 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
85 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
86 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
87 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
93 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
94 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
95 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
96 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
97 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
99 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
100 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
101 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
102 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
103 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
106 /* Multiple response sets. */ \
107 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
108 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
109 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
110 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
111 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
112 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
130 enum ctables_summary_function
132 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
138 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
139 N_CTSF_FUNCTIONS = SUMMARIES
145 struct pivot_table_look *look;
147 /* If this is NULL, zeros are displayed using the normal print format.
148 Otherwise, this string is displayed. */
151 /* If this is NULL, missing values are displayed using the normal print
152 format. Otherwise, this string is displayed. */
155 /* Indexed by variable dictionary index. */
156 enum ctables_vlabel *vlabels;
158 bool mrsets_count_duplicates; /* MRSETS. */
159 bool smissing_listwise; /* SMISSING. */
160 struct variable *base_weight; /* WEIGHT. */
161 int hide_threshold; /* HIDESMALLCOUNTS. */
163 struct ctables_table *tables;
167 struct ctables_postcompute
169 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
170 const char *name; /* Name, without leading &. */
172 struct ctables_postcompute_expr *expr;
175 bool hide_source_cats;
178 struct ctables_postcompute_expr
180 enum ctables_postcompute_op
188 /* XXX SUBTOTAL and HSUBTOTAL */
201 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
206 XXX what about string ranges? */
209 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
210 struct ctables_postcompute_expr *subs[2];
214 enum ctables_label_position
223 struct ctables_axis *axes[PIVOT_N_AXES];
225 enum pivot_axis_type slabels_position;
226 bool slabels_visible;
228 enum ctables_label_position row_labels;
229 enum ctables_label_position col_labels;
231 /* Indexed by variable dictionary index. */
232 struct ctables_categories **categories;
241 struct ctables_chisq *chisq;
242 struct ctables_pairwise *pairwise;
244 struct ctables_freqtab **fts;
253 struct variable *var;
254 const struct mrset *mrset;
258 static const struct fmt_spec *
259 ctables_var_get_print_format (const struct ctables_var *var)
261 return (var->is_mrset
262 ? var_get_print_format (var->mrset->vars[0])
263 : var_get_print_format (var->var));
267 ctables_var_name (const struct ctables_var *var)
269 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
272 struct ctables_categories
276 /* Explicit categories. */
277 struct ctables_cat_value *values;
280 /* Implicit categories. */
282 bool include_missing;
283 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
284 enum ctables_summary_function sort_func;
285 struct variable *sort_func_var;
293 /* Empty categories. */
297 struct ctables_cat_value
299 enum ctables_cat_value_type
313 double number; /* CCVT_NUMBER. */
314 char *string; /* CCVT_STRING. */
315 double range[2]; /* CCVT_RANGE. */
316 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
321 ctables_cat_value_uninit (struct ctables_cat_value *cv)
340 free (cv->subtotal_label);
345 ctables_categories_unref (struct ctables_categories *c)
350 assert (c->n_refs > 0);
354 for (size_t i = 0; i < c->n_values; i++)
355 ctables_cat_value_uninit (&c->values[i]);
357 free (c->total_label);
361 /* Chi-square test (SIGTEST). */
369 /* Pairwise comparison test (COMPARETEST). */
370 struct ctables_pairwise
372 enum { PROP, MEAN } type;
375 bool meansvariance_allcats;
377 enum { BONFERRONI = 1, BH } adjust;
401 struct ctables_var var;
403 struct ctables_summary *summaries;
405 size_t allocated_summaries;
409 struct ctables_axis *subs[2];
412 struct msg_location *loc;
415 static void ctables_axis_destroy (struct ctables_axis *);
424 enum ctables_function_availability
426 CTFA_ALL, /* Any variables. */
427 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
428 CTFA_MRSETS, /* Only multiple-response sets */
431 struct ctables_summary
433 enum ctables_summary_function function;
434 double percentile; /* CTSF_PTILE only. */
436 struct fmt_spec format; /* XXX extra CTABLES formats */
440 ctables_summary_uninit (struct ctables_summary *s)
447 parse_col_width (struct lexer *lexer, const char *name, double *width)
449 lex_match (lexer, T_EQUALS);
450 if (lex_match_id (lexer, "DEFAULT"))
452 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
454 *width = lex_number (lexer);
464 parse_bool (struct lexer *lexer, bool *b)
466 if (lex_match_id (lexer, "NO"))
468 else if (lex_match_id (lexer, "YES"))
472 lex_error_expecting (lexer, "YES", "NO");
478 static enum ctables_function_availability
479 ctables_function_availability (enum ctables_summary_function f)
481 static enum ctables_function_availability availability[] = {
482 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
487 return availability[f];
491 parse_ctables_summary_function (struct lexer *lexer,
492 enum ctables_summary_function *f)
496 enum ctables_summary_function function;
497 struct substring name;
499 static struct pair names[] = {
500 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
501 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
504 /* The .COUNT suffix may be omitted. */
505 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
506 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
507 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
508 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
509 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
510 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
511 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
515 if (!lex_force_id (lexer))
518 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
519 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
521 *f = names[i].function;
526 lex_error (lexer, _("Expecting summary function name."));
531 ctables_axis_destroy (struct ctables_axis *axis)
539 for (size_t i = 0; i < axis->n_summaries; i++)
540 ctables_summary_uninit (&axis->summaries[i]);
541 free (axis->summaries);
546 ctables_axis_destroy (axis->subs[0]);
547 ctables_axis_destroy (axis->subs[1]);
550 msg_location_destroy (axis->loc);
554 static struct ctables_axis *
555 ctables_axis_new_nonterminal (enum ctables_axis_op op,
556 struct ctables_axis *sub0,
557 struct ctables_axis *sub1,
558 struct lexer *lexer, int start_ofs)
560 struct ctables_axis *axis = xmalloc (sizeof *axis);
561 *axis = (struct ctables_axis) {
563 .subs = { sub0, sub1 },
564 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
569 struct ctables_axis_parse_ctx
572 struct dictionary *dict;
574 struct ctables_table *t;
577 static struct fmt_spec
578 ctables_summary_default_format (enum ctables_summary_function function,
579 const struct ctables_var *var)
581 static const enum ctables_format default_formats[] = {
582 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
586 switch (default_formats[function])
589 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
592 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
595 return *ctables_var_get_print_format (var);
603 ctables_summary_function_name (enum ctables_summary_function function)
605 static const char *names[] = {
606 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
610 return names[function];
614 add_summary (struct ctables_axis *axis,
615 enum ctables_summary_function function, double percentile,
616 const char *label, const struct fmt_spec *format,
617 const struct msg_location *loc)
619 if (axis->op == CTAO_VAR)
621 if (axis->n_summaries >= axis->allocated_summaries)
622 axis->summaries = x2nrealloc (axis->summaries,
623 &axis->allocated_summaries,
624 sizeof *axis->summaries);
626 const char *function_name = ctables_summary_function_name (function);
627 const char *var_name = ctables_var_name (&axis->var);
628 switch (ctables_function_availability (function))
631 if (!axis->var.is_mrset)
633 msg_at (SE, loc, _("Summary function %s applies only to multiple "
634 "response sets."), function_name);
635 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
645 _("Summary function %s applies only to scale variables."),
647 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
657 struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
658 *dst = (struct ctables_summary) {
659 .function = function,
660 .percentile = percentile,
661 .label = xstrdup (label),
662 .format = (format ? *format
663 : ctables_summary_default_format (function, &axis->var)),
669 for (size_t i = 0; i < 2; i++)
670 if (!add_summary (axis->subs[i], function, percentile, label, format,
677 static struct ctables_axis *ctables_axis_parse_stack (
678 struct ctables_axis_parse_ctx *);
681 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
682 struct ctables_var *var)
684 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
686 *var = (struct ctables_var) {
688 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
692 lex_error (lexer, _("'%s' does not name a multiple-response set "
693 "in the active file dictionary."),
694 lex_tokcstr (lexer));
702 *var = (struct ctables_var) {
704 .var = parse_variable (lexer, dict),
706 return var->var != NULL;
710 static struct ctables_axis *
711 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
713 if (lex_match (ctx->lexer, T_LPAREN))
715 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
716 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
718 ctables_axis_destroy (sub);
724 if (!lex_force_id (ctx->lexer))
727 int start_ofs = lex_ofs (ctx->lexer);
728 struct ctables_var var;
729 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
732 struct ctables_axis *axis = xmalloc (sizeof *axis);
733 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
735 /* XXX should figure out default measures by reading data */
736 axis->scale = (var.is_mrset ? false
737 : lex_match_phrase (ctx->lexer, "[S]") ? true
738 : lex_match_phrase (ctx->lexer, "[C]") ? false
739 : var_get_measure (var.var) == MEASURE_SCALE);
740 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
741 lex_ofs (ctx->lexer) - 1);
745 static struct ctables_axis *
746 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
748 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
749 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
754 int start_ofs = lex_ofs (ctx->lexer);
756 /* Parse function. */
757 enum ctables_summary_function function;
758 if (!parse_ctables_summary_function (ctx->lexer, &function))
761 /* Parse percentile. */
762 double percentile = 0;
763 if (function == CTSF_PTILE)
765 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
767 percentile = lex_number (ctx->lexer);
768 lex_get (ctx->lexer);
773 if (lex_is_string (ctx->lexer))
775 label = ss_xstrdup (lex_tokss (ctx->lexer));
776 lex_get (ctx->lexer);
778 else if (function == CTSF_PTILE)
779 label = xasprintf (_("Percentile %.2f"), percentile);
782 static const char *default_labels[] = {
783 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
787 label = xstrdup (gettext (default_labels[function]));
791 struct fmt_spec format;
792 const struct fmt_spec *formatp;
793 if (lex_token (ctx->lexer) == T_ID)
795 if (!parse_format_specifier (ctx->lexer, &format)
796 || !fmt_check_output (&format)
797 || !fmt_check_type_compat (&format, VAL_NUMERIC))
807 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
808 lex_ofs (ctx->lexer) - 1);
809 add_summary (sub, function, percentile, label, formatp, loc);
811 msg_location_destroy (loc);
813 lex_match (ctx->lexer, T_COMMA);
815 while (!lex_match (ctx->lexer, T_RBRACK));
820 ctables_axis_destroy (sub);
824 static const struct ctables_axis *
825 find_scale (const struct ctables_axis *axis)
829 else if (axis->op == CTAO_VAR)
833 assert (!axis->var.is_mrset);
841 for (size_t i = 0; i < 2; i++)
843 const struct ctables_axis *scale = find_scale (axis->subs[i]);
851 static const struct ctables_axis *
852 find_categorical_summary (const struct ctables_axis *axis)
856 else if (axis->op == CTAO_VAR)
857 return !axis->scale && axis->n_summaries ? axis : NULL;
860 for (size_t i = 0; i < 2; i++)
862 const struct ctables_axis *sum
863 = find_categorical_summary (axis->subs[i]);
871 static struct ctables_axis *
872 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
874 int start_ofs = lex_ofs (ctx->lexer);
875 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
879 while (lex_match (ctx->lexer, T_GT))
881 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
885 struct ctables_axis *nest = ctables_axis_new_nonterminal (
886 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
888 const struct ctables_axis *outer_scale = find_scale (lhs);
889 const struct ctables_axis *inner_scale = find_scale (rhs);
890 if (outer_scale && inner_scale)
892 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
893 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
894 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
895 ctables_axis_destroy (nest);
899 const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
902 msg_at (SE, nest->loc,
903 _("Summaries may only be requested for categorical variables "
904 "at the innermost nesting level."));
905 msg_at (SN, outer_sum->loc,
906 _("This outer categorical variable has a summary."));
907 ctables_axis_destroy (nest);
917 static struct ctables_axis *
918 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
920 int start_ofs = lex_ofs (ctx->lexer);
921 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
925 while (lex_match (ctx->lexer, T_PLUS))
927 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
931 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
932 ctx->lexer, start_ofs);
939 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
940 struct ctables *ct, struct ctables_table *t,
941 enum pivot_axis_type a)
943 if (lex_token (lexer) == T_BY
944 || lex_token (lexer) == T_SLASH
945 || lex_token (lexer) == T_ENDCMD)
948 struct ctables_axis_parse_ctx ctx = {
954 t->axes[a] = ctables_axis_parse_stack (&ctx);
955 return t->axes[a] != NULL;
959 ctables_chisq_destroy (struct ctables_chisq *chisq)
965 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
971 ctables_table_uninit (struct ctables_table *t)
976 for (size_t i = 0; i < t->n_categories; i++)
977 ctables_categories_unref (t->categories[i]);
978 free (t->categories);
980 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
981 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
982 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
986 ctables_chisq_destroy (t->chisq);
987 ctables_pairwise_destroy (t->pairwise);
991 ctables_destroy (struct ctables *ct)
996 pivot_table_look_unref (ct->look);
1000 for (size_t i = 0; i < ct->n_tables; i++)
1001 ctables_table_uninit (&ct->tables[i]);
1006 static struct ctables_cat_value
1007 ccvt_range (double low, double high)
1009 return (struct ctables_cat_value) {
1011 .range = { low, high }
1016 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1017 struct ctables_table *t)
1019 if (!lex_match_id (lexer, "VARIABLES"))
1021 lex_match (lexer, T_EQUALS);
1023 struct variable **vars;
1025 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1028 struct ctables_categories *c = xmalloc (sizeof *c);
1029 *c = (struct ctables_categories) { .n_refs = n_vars };
1030 for (size_t i = 0; i < n_vars; i++)
1032 struct ctables_categories **cp
1033 = &t->categories[var_get_dict_index (vars[i])];
1034 ctables_categories_unref (*cp);
1039 if (lex_match (lexer, T_LBRACK))
1041 size_t allocated_values = 0;
1044 if (c->n_values >= allocated_values)
1045 c->values = x2nrealloc (c->values, &allocated_values,
1048 struct ctables_cat_value *v = &c->values[c->n_values];
1049 if (lex_match_id (lexer, "OTHERNM"))
1050 v->type = CCVT_OTHERNM;
1051 else if (lex_match_id (lexer, "MISSING"))
1052 v->type = CCVT_MISSING;
1053 else if (lex_match_id (lexer, "SUBTOTAL"))
1054 *v = (struct ctables_cat_value)
1055 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1056 else if (lex_match_id (lexer, "HSUBTOTAL"))
1057 *v = (struct ctables_cat_value)
1058 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1059 else if (lex_match_id (lexer, "LO"))
1061 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1063 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1066 else if (lex_is_number (lexer))
1068 double number = lex_number (lexer);
1070 if (lex_match_id (lexer, "THRU"))
1072 v->type = CCVT_RANGE;
1073 v->range[0] = number;
1074 if (lex_match_id (lexer, "HI"))
1075 *v = ccvt_range (number, DBL_MAX);
1078 if (!lex_force_num (lexer))
1080 *v = ccvt_range (number, lex_number (lexer));
1085 *v = (struct ctables_cat_value) {
1086 .type = CCVT_NUMBER,
1090 else if (lex_is_string (lexer))
1092 *v = (struct ctables_cat_value) {
1093 .type = CCVT_STRING,
1094 .string = ss_xstrdup (lex_tokss (lexer)),
1100 lex_error (lexer, NULL);
1104 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1105 && lex_match (lexer, T_EQUALS))
1107 if (!lex_force_string (lexer))
1110 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1115 lex_match (lexer, T_COMMA);
1117 while (!lex_match (lexer, T_RBRACK));
1120 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1122 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1124 lex_match (lexer, T_EQUALS);
1125 if (lex_match_id (lexer, "A"))
1126 c->sort_ascending = true;
1127 else if (lex_match_id (lexer, "D"))
1128 c->sort_ascending = false;
1131 lex_error_expecting (lexer, "A", "D");
1135 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1137 lex_match (lexer, T_EQUALS);
1138 if (lex_match_id (lexer, "VALUE"))
1139 c->key = CTCS_VALUE;
1140 else if (lex_match_id (lexer, "LABEL"))
1141 c->key = CTCS_LABEL;
1144 c->key = CTCS_FUNCTION;
1145 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1148 if (lex_match (lexer, T_LPAREN))
1150 c->sort_func_var = parse_variable (lexer, dict);
1151 if (!c->sort_func_var)
1154 if (c->sort_func == CTSF_PTILE)
1156 lex_match (lexer, T_COMMA);
1157 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1159 c->percentile = lex_number (lexer);
1163 if (!lex_force_match (lexer, T_RPAREN))
1166 else if (ctables_function_availability (c->sort_func)
1169 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1174 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1176 lex_match (lexer, T_EQUALS);
1177 if (lex_match_id (lexer, "INCLUDE"))
1178 c->include_missing = true;
1179 else if (lex_match_id (lexer, "EXCLUDE"))
1180 c->include_missing = false;
1183 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1187 else if (lex_match_id (lexer, "TOTAL"))
1189 lex_match (lexer, T_EQUALS);
1190 if (!parse_bool (lexer, &c->show_totals))
1193 else if (lex_match_id (lexer, "LABEL"))
1195 lex_match (lexer, T_EQUALS);
1196 if (!lex_force_string (lexer))
1198 free (c->total_label);
1199 c->total_label = ss_xstrdup (lex_tokss (lexer));
1202 else if (lex_match_id (lexer, "POSITION"))
1204 lex_match (lexer, T_EQUALS);
1205 if (lex_match_id (lexer, "BEFORE"))
1206 c->totals_before = true;
1207 else if (lex_match_id (lexer, "AFTER"))
1208 c->totals_before = false;
1211 lex_error_expecting (lexer, "BEFORE", "AFTER");
1215 else if (lex_match_id (lexer, "EMPTY"))
1217 lex_match (lexer, T_EQUALS);
1218 if (lex_match_id (lexer, "INCLUDE"))
1219 c->show_empty = true;
1220 else if (lex_match_id (lexer, "EXCLUDE"))
1221 c->show_empty = false;
1224 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1231 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1232 "TOTAL", "LABEL", "POSITION", "EMPTY");
1234 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1243 struct variable **vars;
1248 var_array_uninit (struct var_array *va)
1256 struct var_array *vas;
1261 var_array2_uninit (struct var_array2 *vaa)
1265 for (size_t i = 0; i < vaa->n; i++)
1266 var_array_uninit (&vaa->vas[i]);
1271 static struct var_array2
1272 nest_fts (struct var_array2 va0, struct var_array2 va1)
1279 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1280 for (size_t i = 0; i < va0.n; i++)
1281 for (size_t j = 0; j < va1.n; j++)
1283 size_t allocate = va0.vas[i].n + va1.vas[j].n;
1284 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1286 for (size_t k = 0; k < va0.vas[i].n; k++)
1287 vars[n++] = va0.vas[i].vars[k];
1288 for (size_t k = 0; k < va1.vas[j].n; k++)
1289 vars[n++] = va1.vas[j].vars[k];
1290 assert (n == allocate);
1292 vaa.vas[vaa.n++] = (struct var_array) { .vars = vars, n = n };
1294 var_array2_uninit (&va0);
1295 var_array2_uninit (&va1);
1299 static struct var_array2
1300 stack_fts (struct var_array2 va0, struct var_array2 va1)
1302 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1303 for (size_t i = 0; i < va0.n; i++)
1304 vaa.vas[vaa.n++] = va0.vas[i];
1305 for (size_t i = 0; i < va1.n; i++)
1306 vaa.vas[vaa.n++] = va1.vas[i];
1307 assert (vaa.n == va0.n + va1.n);
1313 static struct var_array2
1314 enumerate_fts (const struct ctables_axis *a)
1317 return (struct var_array2) { .n = 0 };
1322 assert (!a->var.is_mrset);
1323 struct variable **v = xmalloc (sizeof *v);
1325 struct var_array *va = xmalloc (sizeof *va);
1326 *va = (struct var_array) { .vars = v, .n = 1 };
1327 return (struct var_array2) { .vas = va, .n = 1 };
1330 return stack_fts (enumerate_fts (a->subs[0]),
1331 enumerate_fts (a->subs[1]));
1334 return nest_fts (enumerate_fts (a->subs[0]),
1335 enumerate_fts (a->subs[1]));
1343 struct hmap_node node; /* Element in hash table. */
1345 union value values[]; /* The value. */
1348 static struct ctables_freq *
1349 ctables_freq_allocate (size_t n_values)
1351 struct ctables_freq *f;
1352 return xmalloc (sizeof *f + n_values * sizeof *f->values);
1355 struct ctables_freqtab
1357 struct var_array vars;
1358 struct hmap data; /* Contains "struct ctables_freq"s. */
1359 struct ctables_freq **sorted;
1363 ctables_freq_compare_3way (const void *a_, const void *b_, const void *vars_)
1365 const struct var_array *vars = vars_;
1366 struct ctables_freq *const *a = a_;
1367 struct ctables_freq *const *b = b_;
1369 for (size_t i = 0; i < vars->n; i++)
1371 int cmp = value_compare_3way (&(*a)->values[i], &(*b)->values[i],
1372 var_get_width (vars->vars[i]));
1380 ctables_execute (struct dataset *ds, struct ctables *ct)
1382 for (size_t i = 0; i < ct->n_tables; i++)
1384 size_t allocated_fts = 0;
1386 struct ctables_table *t = &ct->tables[i];
1387 struct var_array2 vaa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
1388 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
1389 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
1390 for (size_t i = 0; i < vaa.n; i++)
1392 for (size_t j = 0; j < vaa.vas[i].n; j++)
1395 fputs (", ", stdout);
1396 fputs (var_get_name (vaa.vas[i].vars[j]), stdout);
1401 for (size_t j = 0; j < vaa.n; j++)
1403 struct ctables_freqtab *ft = xmalloc (sizeof *ft);
1404 *ft = (struct ctables_freqtab) {
1406 .data = HMAP_INITIALIZER (ft->data),
1409 if (t->n_fts >= allocated_fts)
1410 t->fts = x2nrealloc (t->fts, &allocated_fts, sizeof *t->fts);
1411 t->fts[t->n_fts++] = ft;
1417 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
1420 bool warn_on_invalid = true;
1421 for (struct ccase *c = casereader_read (input); c;
1422 case_unref (c), c = casereader_read (input))
1424 double weight = dict_get_case_weight (dataset_dict (ds), c,
1427 for (size_t i = 0; i < ct->n_tables; i++)
1429 struct ctables_table *t = &ct->tables[i];
1431 for (size_t j = 0; j < t->n_fts; j++)
1433 struct ctables_freqtab *ft = t->fts[j];
1435 for (size_t k = 0; k < ft->vars.n; k++)
1437 const struct variable *var = ft->vars.vars[k];
1438 switch (var_is_value_missing (var, case_data (c, var)))
1444 if (!t->categories[var_get_dict_index (var)]
1445 || !t->categories[var_get_dict_index (var)]->include_missing)
1451 for (size_t k = 0; k < ft->vars.n; k++)
1453 const struct variable *var = ft->vars.vars[k];
1454 hash = value_hash (case_data (c, var), var_get_width (var), hash);
1457 struct ctables_freq *f;
1458 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &ft->data)
1460 for (size_t k = 0; k < ft->vars.n; k++)
1462 const struct variable *var = ft->vars.vars[k];
1463 if (!value_equal (case_data (c, var), &f->values[k],
1464 var_get_width (var)))
1465 goto next_hash_node;
1474 f = ctables_freq_allocate (ft->vars.n);
1476 for (size_t k = 0; k < ft->vars.n; k++)
1478 const struct variable *var = ft->vars.vars[k];
1479 value_clone (&f->values[k], case_data (c, var),
1480 var_get_width (var));
1482 hmap_insert (&ft->data, &f->node, hash);
1488 casereader_destroy (input);
1490 for (size_t i = 0; i < ct->n_tables; i++)
1492 struct ctables_table *t = &ct->tables[i];
1494 struct pivot_table *pt = pivot_table_create (N_("Custom Tables"));
1495 struct pivot_dimension *d = pivot_dimension_create (
1496 pt, PIVOT_AXIS_ROW, N_("Rows"));
1497 for (size_t j = 0; j < t->n_fts; j++)
1499 struct ctables_freqtab *ft = t->fts[j];
1500 ft->sorted = xnmalloc (ft->data.count, sizeof *ft->sorted);
1502 struct ctables_freq *f;
1504 HMAP_FOR_EACH (f, struct ctables_freq, node, &ft->data)
1505 ft->sorted[n++] = f;
1506 assert (n == ft->data.count);
1507 sort (ft->sorted, n, sizeof *ft->sorted,
1508 ctables_freq_compare_3way, &ft->vars);
1510 struct pivot_category **groups = xnmalloc (ft->vars.n,
1512 for (size_t k = 0; k < n; k++)
1514 struct ctables_freq *prev = k > 0 ? ft->sorted[k - 1] : NULL;
1515 struct ctables_freq *f = ft->sorted[k];
1517 size_t n_common = 0;
1519 for (; n_common + 1 < ft->vars.n; n_common++)
1520 if (!value_equal (&prev->values[n_common],
1521 &f->values[n_common],
1522 var_get_type (ft->vars.vars[n_common])))
1525 for (size_t m = n_common; m < ft->vars.n; m++)
1527 struct pivot_category *parent = m > 0 ? groups[m - 1] : d->root;
1528 const struct variable *var = ft->vars.vars[m];
1529 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (var)];
1531 if (vlabel != CTVL_NONE)
1532 parent = pivot_category_create_group__ (
1533 parent, pivot_value_new_variable (ft->vars.vars[m]));
1535 if (m + 1 < ft->vars.n)
1536 parent = pivot_category_create_group__ (
1538 pivot_value_new_var_value (ft->vars.vars[m], &f->values[m]));
1541 if (m == ft->vars.n - 1)
1543 int leaf = pivot_category_create_leaf (
1545 pivot_value_new_var_value (ft->vars.vars[ft->vars.n - 1],
1546 &f->values[ft->vars.n - 1]));
1547 pivot_table_put1 (pt, leaf, pivot_value_new_number (f->count));
1553 pivot_table_submit (pt);
1556 for (size_t i = 0; i < ct->n_tables; i++)
1558 struct ctables_table *t = &ct->tables[i];
1560 for (size_t j = 0; j < t->n_fts; j++)
1562 struct ctables_freqtab *ft = t->fts[j];
1563 struct ctables_freq *f, *next;
1564 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
1566 hmap_delete (&ft->data, &f->node);
1567 for (size_t k = 0; k < ft->vars.n; k++)
1569 const struct variable *var = ft->vars.vars[k];
1570 value_destroy (&f->values[k], var_get_width (var));
1574 hmap_destroy (&ft->data);
1576 var_array_uninit (&ft->vars);
1582 return proc_commit (ds);
1586 cmd_ctables (struct lexer *lexer, struct dataset *ds)
1588 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
1589 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
1590 enum settings_value_show tvars = settings_get_show_variables ();
1591 for (size_t i = 0; i < n_vars; i++)
1592 vlabels[i] = (enum ctables_vlabel) tvars;
1594 struct ctables *ct = xmalloc (sizeof *ct);
1595 *ct = (struct ctables) {
1596 .look = pivot_table_look_unshare (pivot_table_look_ref (
1597 pivot_table_look_get_default ())),
1599 .hide_threshold = 5,
1602 if (!lex_force_match (lexer, T_SLASH))
1605 while (!lex_match_id (lexer, "TABLE"))
1607 if (lex_match_id (lexer, "FORMAT"))
1609 double widths[2] = { SYSMIS, SYSMIS };
1610 double units_per_inch = 72.0;
1612 while (lex_token (lexer) != T_SLASH)
1614 if (lex_match_id (lexer, "MINCOLWIDTH"))
1616 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1619 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1621 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1624 else if (lex_match_id (lexer, "UNITS"))
1626 lex_match (lexer, T_EQUALS);
1627 if (lex_match_id (lexer, "POINTS"))
1628 units_per_inch = 72.0;
1629 else if (lex_match_id (lexer, "INCHES"))
1630 units_per_inch = 1.0;
1631 else if (lex_match_id (lexer, "CM"))
1632 units_per_inch = 2.54;
1635 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1639 else if (lex_match_id (lexer, "EMPTY"))
1644 lex_match (lexer, T_EQUALS);
1645 if (lex_match_id (lexer, "ZERO"))
1647 /* Nothing to do. */
1649 else if (lex_match_id (lexer, "BLANK"))
1650 ct->zero = xstrdup ("");
1651 else if (lex_force_string (lexer))
1653 ct->zero = ss_xstrdup (lex_tokss (lexer));
1659 else if (lex_match_id (lexer, "MISSING"))
1661 lex_match (lexer, T_EQUALS);
1662 if (!lex_force_string (lexer))
1666 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1667 ? ss_xstrdup (lex_tokss (lexer))
1673 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1674 "UNITS", "EMPTY", "MISSING");
1679 if (widths[0] != SYSMIS && widths[1] != SYSMIS
1680 && widths[0] > widths[1])
1682 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1686 for (size_t i = 0; i < 2; i++)
1687 if (widths[i] != SYSMIS)
1689 int *wr = ct->look->width_ranges[TABLE_HORZ];
1690 wr[i] = widths[i] / units_per_inch * 96.0;
1695 else if (lex_match_id (lexer, "VLABELS"))
1697 if (!lex_force_match_id (lexer, "VARIABLES"))
1699 lex_match (lexer, T_EQUALS);
1701 struct variable **vars;
1703 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1707 if (!lex_force_match_id (lexer, "DISPLAY"))
1712 lex_match (lexer, T_EQUALS);
1714 enum ctables_vlabel vlabel;
1715 if (lex_match_id (lexer, "DEFAULT"))
1716 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
1717 else if (lex_match_id (lexer, "NAME"))
1719 else if (lex_match_id (lexer, "LABEL"))
1720 vlabel = CTVL_LABEL;
1721 else if (lex_match_id (lexer, "BOTH"))
1723 else if (lex_match_id (lexer, "NONE"))
1727 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1733 for (size_t i = 0; i < n_vars; i++)
1734 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1737 else if (lex_match_id (lexer, "MRSETS"))
1739 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1741 lex_match (lexer, T_EQUALS);
1742 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1745 else if (lex_match_id (lexer, "SMISSING"))
1747 if (lex_match_id (lexer, "VARIABLE"))
1748 ct->smissing_listwise = false;
1749 else if (lex_match_id (lexer, "LISTWISE"))
1750 ct->smissing_listwise = true;
1753 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1758 else if (lex_match_id (lexer, "WEIGHT"))
1760 if (!lex_force_match_id (lexer, "VARIABLE"))
1762 lex_match (lexer, T_EQUALS);
1763 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1764 if (!ct->base_weight)
1767 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1769 if (!lex_force_match_id (lexer, "COUNT"))
1771 lex_match (lexer, T_EQUALS);
1772 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1774 ct->hide_threshold = lex_integer (lexer);
1779 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1780 "SMISSING", "PCOMPUTE", "PPROPERTIES",
1781 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1785 if (!lex_force_match (lexer, T_SLASH))
1789 size_t allocated_tables = 0;
1792 if (ct->n_tables >= allocated_tables)
1793 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1794 sizeof *ct->tables);
1796 struct ctables_table *t = &ct->tables[ct->n_tables++];
1797 *t = (struct ctables_table) {
1798 .slabels_position = PIVOT_AXIS_COLUMN,
1799 .slabels_visible = true,
1800 .row_labels = CTLP_NORMAL,
1801 .col_labels = CTLP_NORMAL,
1802 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
1803 sizeof *t->categories),
1804 .n_categories = dict_get_n_vars (dataset_dict (ds)),
1808 lex_match (lexer, T_EQUALS);
1809 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1811 if (lex_match (lexer, T_BY))
1813 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1814 ct, t, PIVOT_AXIS_COLUMN))
1817 if (lex_match (lexer, T_BY))
1819 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1820 ct, t, PIVOT_AXIS_LAYER))
1825 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1826 && !t->axes[PIVOT_AXIS_LAYER])
1828 lex_error (lexer, _("At least one variable must be specified."));
1832 const struct ctables_axis *scales[PIVOT_N_AXES];
1833 size_t n_scales = 0;
1834 for (size_t i = 0; i < 3; i++)
1836 scales[i] = find_scale (t->axes[i]);
1842 msg (SE, _("Scale variables may appear only on one dimension."));
1843 if (scales[PIVOT_AXIS_ROW])
1844 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
1845 _("This scale variable appears in the rows dimension."));
1846 if (scales[PIVOT_AXIS_COLUMN])
1847 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
1848 _("This scale variable appears in the columns dimension."));
1849 if (scales[PIVOT_AXIS_LAYER])
1850 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
1851 _("This scale variable appears in the layer dimension."));
1855 if (lex_token (lexer) == T_ENDCMD)
1857 if (!lex_force_match (lexer, T_SLASH))
1860 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1862 if (lex_match_id (lexer, "SLABELS"))
1864 while (lex_token (lexer) != T_SLASH)
1866 if (lex_match_id (lexer, "POSITION"))
1868 lex_match (lexer, T_EQUALS);
1869 if (lex_match_id (lexer, "COLUMN"))
1870 t->slabels_position = PIVOT_AXIS_COLUMN;
1871 else if (lex_match_id (lexer, "ROW"))
1872 t->slabels_position = PIVOT_AXIS_ROW;
1873 else if (lex_match_id (lexer, "LAYER"))
1874 t->slabels_position = PIVOT_AXIS_LAYER;
1877 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1881 else if (lex_match_id (lexer, "VISIBLE"))
1883 lex_match (lexer, T_EQUALS);
1884 if (!parse_bool (lexer, &t->slabels_visible))
1889 lex_error_expecting (lexer, "POSITION", "VISIBLE");
1894 else if (lex_match_id (lexer, "CLABELS"))
1896 while (lex_token (lexer) != T_SLASH)
1898 if (lex_match_id (lexer, "AUTO"))
1899 t->row_labels = t->col_labels = CTLP_NORMAL;
1900 else if (lex_match_id (lexer, "ROWLABELS"))
1902 lex_match (lexer, T_EQUALS);
1903 if (lex_match_id (lexer, "OPPOSITE"))
1904 t->row_labels = CTLP_OPPOSITE;
1905 else if (lex_match_id (lexer, "LAYER"))
1906 t->row_labels = CTLP_LAYER;
1909 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1913 else if (lex_match_id (lexer, "COLLABELS"))
1915 lex_match (lexer, T_EQUALS);
1916 if (lex_match_id (lexer, "OPPOSITE"))
1917 t->col_labels = CTLP_OPPOSITE;
1918 else if (lex_match_id (lexer, "LAYER"))
1919 t->col_labels = CTLP_LAYER;
1922 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1928 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1934 else if (lex_match_id (lexer, "CRITERIA"))
1936 if (!lex_force_match_id (lexer, "CILEVEL"))
1938 lex_match (lexer, T_EQUALS);
1940 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1942 t->cilevel = lex_number (lexer);
1945 else if (lex_match_id (lexer, "CATEGORIES"))
1947 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1950 else if (lex_match_id (lexer, "TITLES"))
1955 if (lex_match_id (lexer, "CAPTION"))
1956 textp = &t->caption;
1957 else if (lex_match_id (lexer, "CORNER"))
1959 else if (lex_match_id (lexer, "TITLE"))
1963 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1966 lex_match (lexer, T_EQUALS);
1968 struct string s = DS_EMPTY_INITIALIZER;
1969 while (lex_is_string (lexer))
1971 if (!ds_is_empty (&s))
1972 ds_put_byte (&s, ' ');
1973 ds_put_substring (&s, lex_tokss (lexer));
1977 *textp = ds_steal_cstr (&s);
1979 while (lex_token (lexer) != T_SLASH
1980 && lex_token (lexer) != T_ENDCMD);
1982 else if (lex_match_id (lexer, "SIGTEST"))
1986 t->chisq = xmalloc (sizeof *t->chisq);
1987 *t->chisq = (struct ctables_chisq) {
1989 .include_mrsets = true,
1990 .all_visible = true,
1996 if (lex_match_id (lexer, "TYPE"))
1998 lex_match (lexer, T_EQUALS);
1999 if (!lex_force_match_id (lexer, "CHISQUARE"))
2002 else if (lex_match_id (lexer, "ALPHA"))
2004 lex_match (lexer, T_EQUALS);
2005 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2007 t->chisq->alpha = lex_number (lexer);
2010 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2012 lex_match (lexer, T_EQUALS);
2013 if (parse_bool (lexer, &t->chisq->include_mrsets))
2016 else if (lex_match_id (lexer, "CATEGORIES"))
2018 lex_match (lexer, T_EQUALS);
2019 if (lex_match_id (lexer, "ALLVISIBLE"))
2020 t->chisq->all_visible = true;
2021 else if (lex_match_id (lexer, "SUBTOTALS"))
2022 t->chisq->all_visible = false;
2025 lex_error_expecting (lexer,
2026 "ALLVISIBLE", "SUBTOTALS");
2032 lex_error_expecting (lexer, "TYPE", "ALPHA",
2033 "INCLUDEMRSETS", "CATEGORIES");
2037 while (lex_token (lexer) != T_SLASH
2038 && lex_token (lexer) != T_ENDCMD);
2040 else if (lex_match_id (lexer, "COMPARETEST"))
2044 t->pairwise = xmalloc (sizeof *t->pairwise);
2045 *t->pairwise = (struct ctables_pairwise) {
2047 .alpha = { .05, .05 },
2048 .adjust = BONFERRONI,
2049 .include_mrsets = true,
2050 .meansvariance_allcats = true,
2051 .all_visible = true,
2060 if (lex_match_id (lexer, "TYPE"))
2062 lex_match (lexer, T_EQUALS);
2063 if (lex_match_id (lexer, "PROP"))
2064 t->pairwise->type = PROP;
2065 else if (lex_match_id (lexer, "MEAN"))
2066 t->pairwise->type = MEAN;
2069 lex_error_expecting (lexer, "PROP", "MEAN");
2073 else if (lex_match_id (lexer, "ALPHA"))
2075 lex_match (lexer, T_EQUALS);
2077 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2079 double a0 = lex_number (lexer);
2082 lex_match (lexer, T_COMMA);
2083 if (lex_is_number (lexer))
2085 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2087 double a1 = lex_number (lexer);
2090 t->pairwise->alpha[0] = MIN (a0, a1);
2091 t->pairwise->alpha[1] = MAX (a0, a1);
2094 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2096 else if (lex_match_id (lexer, "ADJUST"))
2098 lex_match (lexer, T_EQUALS);
2099 if (lex_match_id (lexer, "BONFERRONI"))
2100 t->pairwise->adjust = BONFERRONI;
2101 else if (lex_match_id (lexer, "BH"))
2102 t->pairwise->adjust = BH;
2103 else if (lex_match_id (lexer, "NONE"))
2104 t->pairwise->adjust = 0;
2107 lex_error_expecting (lexer, "BONFERRONI", "BH",
2112 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2114 lex_match (lexer, T_EQUALS);
2115 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2118 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2120 lex_match (lexer, T_EQUALS);
2121 if (lex_match_id (lexer, "ALLCATS"))
2122 t->pairwise->meansvariance_allcats = true;
2123 else if (lex_match_id (lexer, "TESTEDCATS"))
2124 t->pairwise->meansvariance_allcats = false;
2127 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2131 else if (lex_match_id (lexer, "CATEGORIES"))
2133 lex_match (lexer, T_EQUALS);
2134 if (lex_match_id (lexer, "ALLVISIBLE"))
2135 t->pairwise->all_visible = true;
2136 else if (lex_match_id (lexer, "SUBTOTALS"))
2137 t->pairwise->all_visible = false;
2140 lex_error_expecting (lexer, "ALLVISIBLE",
2145 else if (lex_match_id (lexer, "MERGE"))
2147 lex_match (lexer, T_EQUALS);
2148 if (!parse_bool (lexer, &t->pairwise->merge))
2151 else if (lex_match_id (lexer, "STYLE"))
2153 lex_match (lexer, T_EQUALS);
2154 if (lex_match_id (lexer, "APA"))
2155 t->pairwise->apa_style = true;
2156 else if (lex_match_id (lexer, "SIMPLE"))
2157 t->pairwise->apa_style = false;
2160 lex_error_expecting (lexer, "APA", "SIMPLE");
2164 else if (lex_match_id (lexer, "SHOWSIG"))
2166 lex_match (lexer, T_EQUALS);
2167 if (!parse_bool (lexer, &t->pairwise->show_sig))
2172 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2173 "INCLUDEMRSETS", "MEANSVARIANCE",
2174 "CATEGORIES", "MERGE", "STYLE",
2179 while (lex_token (lexer) != T_SLASH
2180 && lex_token (lexer) != T_ENDCMD);
2184 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2185 "CRITERIA", "CATEGORIES", "TITLES",
2186 "SIGTEST", "COMPARETEST");
2191 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2193 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2198 while (lex_token (lexer) != T_ENDCMD);
2200 bool ok = ctables_execute (ds, ct);
2201 ctables_destroy (ct);
2202 return ok ? CMD_SUCCESS : CMD_FAILURE;
2205 ctables_destroy (ct);