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_NONE = 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 - unweighted summaries (U*)
52 - lower confidence limits (*.LCL)
53 - upper confidence limits (*.UCL)
54 - standard error (*.SE)
57 /* All variables. */ \
58 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
59 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
60 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
61 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
62 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
63 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
64 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
82 /* Scale variables, totals, and subtotals. */ \
83 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
84 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
85 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
86 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
87 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
94 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
95 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
96 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
97 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
98 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
100 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
101 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
102 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
103 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
105 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
107 /* Multiple response sets. */ \
108 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
109 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
110 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
111 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
112 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
131 enum ctables_summary_function
133 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
139 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
140 N_CTSF_FUNCTIONS = SUMMARIES
146 struct pivot_table_look *look;
148 /* If this is NULL, zeros are displayed using the normal print format.
149 Otherwise, this string is displayed. */
152 /* If this is NULL, missing values are displayed using the normal print
153 format. Otherwise, this string is displayed. */
156 /* Indexed by variable dictionary index. */
157 enum ctables_vlabel *vlabels;
159 bool mrsets_count_duplicates; /* MRSETS. */
160 bool smissing_listwise; /* SMISSING. */
161 struct variable *base_weight; /* WEIGHT. */
162 int hide_threshold; /* HIDESMALLCOUNTS. */
164 struct ctables_table *tables;
168 struct ctables_postcompute
170 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
171 const char *name; /* Name, without leading &. */
173 struct ctables_postcompute_expr *expr;
176 bool hide_source_cats;
179 struct ctables_postcompute_expr
181 enum ctables_postcompute_op
189 /* XXX SUBTOTAL and HSUBTOTAL */
202 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
207 XXX what about string ranges? */
210 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
211 struct ctables_postcompute_expr *subs[2];
215 enum ctables_label_position
224 struct ctables_axis *axes[PIVOT_N_AXES];
226 enum pivot_axis_type slabels_position;
227 bool slabels_visible;
229 enum ctables_label_position row_labels;
230 enum ctables_label_position col_labels;
232 /* Indexed by variable dictionary index. */
233 struct ctables_categories **categories;
242 struct ctables_chisq *chisq;
243 struct ctables_pairwise *pairwise;
245 struct ctables_freqtab **fts;
254 struct variable *var;
255 const struct mrset *mrset;
259 static const struct fmt_spec *
260 ctables_var_get_print_format (const struct ctables_var *var)
262 return (var->is_mrset
263 ? var_get_print_format (var->mrset->vars[0])
264 : var_get_print_format (var->var));
268 ctables_var_name (const struct ctables_var *var)
270 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
273 struct ctables_categories
277 /* Explicit categories. */
278 struct ctables_cat_value *values;
281 /* Implicit categories. */
283 bool include_missing;
284 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
285 enum ctables_summary_function sort_func;
286 struct variable *sort_func_var;
294 /* Empty categories. */
298 struct ctables_cat_value
300 enum ctables_cat_value_type
314 double number; /* CCVT_NUMBER. */
315 char *string; /* CCVT_STRING. */
316 double range[2]; /* CCVT_RANGE. */
317 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
322 ctables_cat_value_uninit (struct ctables_cat_value *cv)
341 free (cv->subtotal_label);
346 ctables_categories_unref (struct ctables_categories *c)
351 assert (c->n_refs > 0);
355 for (size_t i = 0; i < c->n_values; i++)
356 ctables_cat_value_uninit (&c->values[i]);
358 free (c->total_label);
362 /* Chi-square test (SIGTEST). */
370 /* Pairwise comparison test (COMPARETEST). */
371 struct ctables_pairwise
373 enum { PROP, MEAN } type;
376 bool meansvariance_allcats;
378 enum { BONFERRONI = 1, BH } adjust;
402 struct ctables_var var;
404 struct ctables_summary *summaries;
406 size_t allocated_summaries;
410 struct ctables_axis *subs[2];
413 struct msg_location *loc;
416 static void ctables_axis_destroy (struct ctables_axis *);
425 enum ctables_function_availability
427 CTFA_ALL, /* Any variables. */
428 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
429 CTFA_MRSETS, /* Only multiple-response sets */
432 struct ctables_summary
434 enum ctables_summary_function function;
435 double percentile; /* CTSF_PTILE only. */
437 struct fmt_spec format; /* XXX extra CTABLES formats */
441 ctables_summary_uninit (struct ctables_summary *s)
448 parse_col_width (struct lexer *lexer, const char *name, double *width)
450 lex_match (lexer, T_EQUALS);
451 if (lex_match_id (lexer, "DEFAULT"))
453 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
455 *width = lex_number (lexer);
465 parse_bool (struct lexer *lexer, bool *b)
467 if (lex_match_id (lexer, "NO"))
469 else if (lex_match_id (lexer, "YES"))
473 lex_error_expecting (lexer, "YES", "NO");
479 static enum ctables_function_availability
480 ctables_function_availability (enum ctables_summary_function f)
482 static enum ctables_function_availability availability[] = {
483 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
488 return availability[f];
492 parse_ctables_summary_function (struct lexer *lexer,
493 enum ctables_summary_function *f)
497 enum ctables_summary_function function;
498 struct substring name;
500 static struct pair names[] = {
501 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
502 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
505 /* The .COUNT suffix may be omitted. */
506 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
507 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
508 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
509 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
510 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
511 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
512 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
516 if (!lex_force_id (lexer))
519 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
520 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
522 *f = names[i].function;
527 lex_error (lexer, _("Expecting summary function name."));
532 ctables_axis_destroy (struct ctables_axis *axis)
540 for (size_t i = 0; i < axis->n_summaries; i++)
541 ctables_summary_uninit (&axis->summaries[i]);
542 free (axis->summaries);
547 ctables_axis_destroy (axis->subs[0]);
548 ctables_axis_destroy (axis->subs[1]);
551 msg_location_destroy (axis->loc);
555 static struct ctables_axis *
556 ctables_axis_new_nonterminal (enum ctables_axis_op op,
557 struct ctables_axis *sub0,
558 struct ctables_axis *sub1,
559 struct lexer *lexer, int start_ofs)
561 struct ctables_axis *axis = xmalloc (sizeof *axis);
562 *axis = (struct ctables_axis) {
564 .subs = { sub0, sub1 },
565 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
570 struct ctables_axis_parse_ctx
573 struct dictionary *dict;
575 struct ctables_table *t;
578 static struct fmt_spec
579 ctables_summary_default_format (enum ctables_summary_function function,
580 const struct ctables_var *var)
582 static const enum ctables_format default_formats[] = {
583 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
587 switch (default_formats[function])
590 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
593 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
596 return *ctables_var_get_print_format (var);
604 ctables_summary_function_name (enum ctables_summary_function function)
606 static const char *names[] = {
607 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
611 return names[function];
615 add_summary (struct ctables_axis *axis,
616 enum ctables_summary_function function, double percentile,
617 const char *label, const struct fmt_spec *format,
618 const struct msg_location *loc)
620 if (axis->op == CTAO_VAR)
622 if (axis->n_summaries >= axis->allocated_summaries)
623 axis->summaries = x2nrealloc (axis->summaries,
624 &axis->allocated_summaries,
625 sizeof *axis->summaries);
627 const char *function_name = ctables_summary_function_name (function);
628 const char *var_name = ctables_var_name (&axis->var);
629 switch (ctables_function_availability (function))
632 if (!axis->var.is_mrset)
634 msg_at (SE, loc, _("Summary function %s applies only to multiple "
635 "response sets."), function_name);
636 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
646 _("Summary function %s applies only to scale variables."),
648 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
658 struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
659 *dst = (struct ctables_summary) {
660 .function = function,
661 .percentile = percentile,
662 .label = xstrdup (label),
663 .format = (format ? *format
664 : ctables_summary_default_format (function, &axis->var)),
670 for (size_t i = 0; i < 2; i++)
671 if (!add_summary (axis->subs[i], function, percentile, label, format,
678 static struct ctables_axis *ctables_axis_parse_stack (
679 struct ctables_axis_parse_ctx *);
682 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
683 struct ctables_var *var)
685 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
687 *var = (struct ctables_var) {
689 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
693 lex_error (lexer, _("'%s' does not name a multiple-response set "
694 "in the active file dictionary."),
695 lex_tokcstr (lexer));
703 *var = (struct ctables_var) {
705 .var = parse_variable (lexer, dict),
707 return var->var != NULL;
711 static struct ctables_axis *
712 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
714 if (lex_match (ctx->lexer, T_LPAREN))
716 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
717 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
719 ctables_axis_destroy (sub);
725 if (!lex_force_id (ctx->lexer))
728 int start_ofs = lex_ofs (ctx->lexer);
729 struct ctables_var var;
730 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
733 struct ctables_axis *axis = xmalloc (sizeof *axis);
734 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
736 /* XXX should figure out default measures by reading data */
737 axis->scale = (var.is_mrset ? false
738 : lex_match_phrase (ctx->lexer, "[S]") ? true
739 : lex_match_phrase (ctx->lexer, "[C]") ? false
740 : var_get_measure (var.var) == MEASURE_SCALE);
741 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
742 lex_ofs (ctx->lexer) - 1);
746 static struct ctables_axis *
747 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
749 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
750 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
755 int start_ofs = lex_ofs (ctx->lexer);
757 /* Parse function. */
758 enum ctables_summary_function function;
759 if (!parse_ctables_summary_function (ctx->lexer, &function))
762 /* Parse percentile. */
763 double percentile = 0;
764 if (function == CTSF_PTILE)
766 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
768 percentile = lex_number (ctx->lexer);
769 lex_get (ctx->lexer);
774 if (lex_is_string (ctx->lexer))
776 label = ss_xstrdup (lex_tokss (ctx->lexer));
777 lex_get (ctx->lexer);
779 else if (function == CTSF_PTILE)
780 label = xasprintf (_("Percentile %.2f"), percentile);
783 static const char *default_labels[] = {
784 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
788 label = xstrdup (gettext (default_labels[function]));
792 struct fmt_spec format;
793 const struct fmt_spec *formatp;
794 if (lex_token (ctx->lexer) == T_ID)
796 if (!parse_format_specifier (ctx->lexer, &format)
797 || !fmt_check_output (&format)
798 || !fmt_check_type_compat (&format, VAL_NUMERIC))
808 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
809 lex_ofs (ctx->lexer) - 1);
810 add_summary (sub, function, percentile, label, formatp, loc);
812 msg_location_destroy (loc);
814 lex_match (ctx->lexer, T_COMMA);
816 while (!lex_match (ctx->lexer, T_RBRACK));
821 ctables_axis_destroy (sub);
825 static const struct ctables_axis *
826 find_scale (const struct ctables_axis *axis)
830 else if (axis->op == CTAO_VAR)
834 assert (!axis->var.is_mrset);
842 for (size_t i = 0; i < 2; i++)
844 const struct ctables_axis *scale = find_scale (axis->subs[i]);
852 static const struct ctables_axis *
853 find_categorical_summary (const struct ctables_axis *axis)
857 else if (axis->op == CTAO_VAR)
858 return !axis->scale && axis->n_summaries ? axis : NULL;
861 for (size_t i = 0; i < 2; i++)
863 const struct ctables_axis *sum
864 = find_categorical_summary (axis->subs[i]);
872 static struct ctables_axis *
873 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
875 int start_ofs = lex_ofs (ctx->lexer);
876 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
880 while (lex_match (ctx->lexer, T_GT))
882 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
886 struct ctables_axis *nest = ctables_axis_new_nonterminal (
887 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
889 const struct ctables_axis *outer_scale = find_scale (lhs);
890 const struct ctables_axis *inner_scale = find_scale (rhs);
891 if (outer_scale && inner_scale)
893 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
894 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
895 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
896 ctables_axis_destroy (nest);
900 const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
903 msg_at (SE, nest->loc,
904 _("Summaries may only be requested for categorical variables "
905 "at the innermost nesting level."));
906 msg_at (SN, outer_sum->loc,
907 _("This outer categorical variable has a summary."));
908 ctables_axis_destroy (nest);
918 static struct ctables_axis *
919 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
921 int start_ofs = lex_ofs (ctx->lexer);
922 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
926 while (lex_match (ctx->lexer, T_PLUS))
928 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
932 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
933 ctx->lexer, start_ofs);
940 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
941 struct ctables *ct, struct ctables_table *t,
942 enum pivot_axis_type a)
944 if (lex_token (lexer) == T_BY
945 || lex_token (lexer) == T_SLASH
946 || lex_token (lexer) == T_ENDCMD)
949 struct ctables_axis_parse_ctx ctx = {
955 t->axes[a] = ctables_axis_parse_stack (&ctx);
956 return t->axes[a] != NULL;
960 ctables_chisq_destroy (struct ctables_chisq *chisq)
966 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
972 ctables_table_uninit (struct ctables_table *t)
977 for (size_t i = 0; i < t->n_categories; i++)
978 ctables_categories_unref (t->categories[i]);
979 free (t->categories);
981 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
982 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
983 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
987 ctables_chisq_destroy (t->chisq);
988 ctables_pairwise_destroy (t->pairwise);
992 ctables_destroy (struct ctables *ct)
997 pivot_table_look_unref (ct->look);
1001 for (size_t i = 0; i < ct->n_tables; i++)
1002 ctables_table_uninit (&ct->tables[i]);
1007 static struct ctables_cat_value
1008 ccvt_range (double low, double high)
1010 return (struct ctables_cat_value) {
1012 .range = { low, high }
1017 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1018 struct ctables_table *t)
1020 if (!lex_match_id (lexer, "VARIABLES"))
1022 lex_match (lexer, T_EQUALS);
1024 struct variable **vars;
1026 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1029 struct ctables_categories *c = xmalloc (sizeof *c);
1030 *c = (struct ctables_categories) { .n_refs = n_vars };
1031 for (size_t i = 0; i < n_vars; i++)
1033 struct ctables_categories **cp
1034 = &t->categories[var_get_dict_index (vars[i])];
1035 ctables_categories_unref (*cp);
1040 if (lex_match (lexer, T_LBRACK))
1042 size_t allocated_values = 0;
1045 if (c->n_values >= allocated_values)
1046 c->values = x2nrealloc (c->values, &allocated_values,
1049 struct ctables_cat_value *v = &c->values[c->n_values];
1050 if (lex_match_id (lexer, "OTHERNM"))
1051 v->type = CCVT_OTHERNM;
1052 else if (lex_match_id (lexer, "MISSING"))
1053 v->type = CCVT_MISSING;
1054 else if (lex_match_id (lexer, "SUBTOTAL"))
1055 *v = (struct ctables_cat_value)
1056 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1057 else if (lex_match_id (lexer, "HSUBTOTAL"))
1058 *v = (struct ctables_cat_value)
1059 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1060 else if (lex_match_id (lexer, "LO"))
1062 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1064 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1067 else if (lex_is_number (lexer))
1069 double number = lex_number (lexer);
1071 if (lex_match_id (lexer, "THRU"))
1073 v->type = CCVT_RANGE;
1074 v->range[0] = number;
1075 if (lex_match_id (lexer, "HI"))
1076 *v = ccvt_range (number, DBL_MAX);
1079 if (!lex_force_num (lexer))
1081 *v = ccvt_range (number, lex_number (lexer));
1086 *v = (struct ctables_cat_value) {
1087 .type = CCVT_NUMBER,
1091 else if (lex_is_string (lexer))
1093 *v = (struct ctables_cat_value) {
1094 .type = CCVT_STRING,
1095 .string = ss_xstrdup (lex_tokss (lexer)),
1101 lex_error (lexer, NULL);
1105 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1106 && lex_match (lexer, T_EQUALS))
1108 if (!lex_force_string (lexer))
1111 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1116 lex_match (lexer, T_COMMA);
1118 while (!lex_match (lexer, T_RBRACK));
1121 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1123 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1125 lex_match (lexer, T_EQUALS);
1126 if (lex_match_id (lexer, "A"))
1127 c->sort_ascending = true;
1128 else if (lex_match_id (lexer, "D"))
1129 c->sort_ascending = false;
1132 lex_error_expecting (lexer, "A", "D");
1136 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1138 lex_match (lexer, T_EQUALS);
1139 if (lex_match_id (lexer, "VALUE"))
1140 c->key = CTCS_VALUE;
1141 else if (lex_match_id (lexer, "LABEL"))
1142 c->key = CTCS_LABEL;
1145 c->key = CTCS_FUNCTION;
1146 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1149 if (lex_match (lexer, T_LPAREN))
1151 c->sort_func_var = parse_variable (lexer, dict);
1152 if (!c->sort_func_var)
1155 if (c->sort_func == CTSF_PTILE)
1157 lex_match (lexer, T_COMMA);
1158 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1160 c->percentile = lex_number (lexer);
1164 if (!lex_force_match (lexer, T_RPAREN))
1167 else if (ctables_function_availability (c->sort_func)
1170 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1175 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1177 lex_match (lexer, T_EQUALS);
1178 if (lex_match_id (lexer, "INCLUDE"))
1179 c->include_missing = true;
1180 else if (lex_match_id (lexer, "EXCLUDE"))
1181 c->include_missing = false;
1184 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1188 else if (lex_match_id (lexer, "TOTAL"))
1190 lex_match (lexer, T_EQUALS);
1191 if (!parse_bool (lexer, &c->show_totals))
1194 else if (lex_match_id (lexer, "LABEL"))
1196 lex_match (lexer, T_EQUALS);
1197 if (!lex_force_string (lexer))
1199 free (c->total_label);
1200 c->total_label = ss_xstrdup (lex_tokss (lexer));
1203 else if (lex_match_id (lexer, "POSITION"))
1205 lex_match (lexer, T_EQUALS);
1206 if (lex_match_id (lexer, "BEFORE"))
1207 c->totals_before = true;
1208 else if (lex_match_id (lexer, "AFTER"))
1209 c->totals_before = false;
1212 lex_error_expecting (lexer, "BEFORE", "AFTER");
1216 else if (lex_match_id (lexer, "EMPTY"))
1218 lex_match (lexer, T_EQUALS);
1219 if (lex_match_id (lexer, "INCLUDE"))
1220 c->show_empty = true;
1221 else if (lex_match_id (lexer, "EXCLUDE"))
1222 c->show_empty = false;
1225 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1232 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1233 "TOTAL", "LABEL", "POSITION", "EMPTY");
1235 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1244 struct variable **vars;
1249 var_array_uninit (struct var_array *va)
1257 struct var_array *vas;
1262 var_array2_uninit (struct var_array2 *vaa)
1266 for (size_t i = 0; i < vaa->n; i++)
1267 var_array_uninit (&vaa->vas[i]);
1272 static struct var_array2
1273 nest_fts (struct var_array2 va0, struct var_array2 va1)
1280 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1281 for (size_t i = 0; i < va0.n; i++)
1282 for (size_t j = 0; j < va1.n; j++)
1284 size_t allocate = va0.vas[i].n + va1.vas[j].n;
1285 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1287 for (size_t k = 0; k < va0.vas[i].n; k++)
1288 vars[n++] = va0.vas[i].vars[k];
1289 for (size_t k = 0; k < va1.vas[j].n; k++)
1290 vars[n++] = va1.vas[j].vars[k];
1291 assert (n == allocate);
1293 vaa.vas[vaa.n++] = (struct var_array) { .vars = vars, n = n };
1295 var_array2_uninit (&va0);
1296 var_array2_uninit (&va1);
1300 static struct var_array2
1301 stack_fts (struct var_array2 va0, struct var_array2 va1)
1303 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1304 for (size_t i = 0; i < va0.n; i++)
1305 vaa.vas[vaa.n++] = va0.vas[i];
1306 for (size_t i = 0; i < va1.n; i++)
1307 vaa.vas[vaa.n++] = va1.vas[i];
1308 assert (vaa.n == va0.n + va1.n);
1314 static struct var_array2
1315 enumerate_fts (const struct ctables_axis *a)
1318 return (struct var_array2) { .n = 0 };
1323 assert (!a->var.is_mrset);
1324 struct variable **v = xmalloc (sizeof *v);
1326 struct var_array *va = xmalloc (sizeof *va);
1327 *va = (struct var_array) { .vars = v, .n = 1 };
1328 return (struct var_array2) { .vas = va, .n = 1 };
1331 return stack_fts (enumerate_fts (a->subs[0]),
1332 enumerate_fts (a->subs[1]));
1335 return nest_fts (enumerate_fts (a->subs[0]),
1336 enumerate_fts (a->subs[1]));
1342 struct ctables_freqtab
1344 struct var_array vars;
1345 struct hmap data; /* Contains "struct freq"s. */
1346 struct freq **sorted;
1350 compare_freq_3way (const void *a_, const void *b_, const void *vars_)
1352 const struct var_array *vars = vars_;
1353 struct freq *const *a = a_;
1354 struct freq *const *b = b_;
1356 for (size_t i = 0; i < vars->n; i++)
1358 int cmp = value_compare_3way (&(*a)->values[i], &(*b)->values[i],
1359 var_get_width (vars->vars[i]));
1368 ctables_execute (struct dataset *ds, struct ctables *ct)
1370 for (size_t i = 0; i < ct->n_tables; i++)
1372 size_t allocated_fts = 0;
1374 struct ctables_table *t = &ct->tables[i];
1375 struct var_array2 vaa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
1376 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
1377 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
1378 for (size_t i = 0; i < vaa.n; i++)
1380 for (size_t j = 0; j < vaa.vas[i].n; j++)
1383 fputs (", ", stdout);
1384 fputs (var_get_name (vaa.vas[i].vars[j]), stdout);
1389 for (size_t j = 0; j < vaa.n; j++)
1391 struct ctables_freqtab *ft = xmalloc (sizeof *ft);
1392 *ft = (struct ctables_freqtab) {
1394 .data = HMAP_INITIALIZER (ft->data),
1397 if (t->n_fts >= allocated_fts)
1398 t->fts = x2nrealloc (t->fts, &allocated_fts, sizeof *t->fts);
1399 t->fts[t->n_fts++] = ft;
1405 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
1408 bool warn_on_invalid = true;
1409 for (struct ccase *c = casereader_read (input); c;
1410 case_unref (c), c = casereader_read (input))
1412 double weight = dict_get_case_weight (dataset_dict (ds), c,
1415 for (size_t i = 0; i < ct->n_tables; i++)
1417 struct ctables_table *t = &ct->tables[i];
1419 for (size_t j = 0; j < t->n_fts; j++)
1421 struct ctables_freqtab *ft = t->fts[j];
1423 for (size_t k = 0; k < ft->vars.n; k++)
1425 const struct variable *var = ft->vars.vars[k];
1426 switch (var_is_value_missing (var, case_data (c, var)))
1432 if (!t->categories[var_get_dict_index (var)]
1433 || !t->categories[var_get_dict_index (var)]->include_missing)
1439 for (size_t k = 0; k < ft->vars.n; k++)
1441 const struct variable *var = ft->vars.vars[k];
1442 hash = value_hash (case_data (c, var), var_get_width (var), hash);
1446 HMAP_FOR_EACH_WITH_HASH (f, struct freq, node, hash, &ft->data)
1448 for (size_t k = 0; k < ft->vars.n; k++)
1450 const struct variable *var = ft->vars.vars[k];
1451 if (!value_equal (case_data (c, var), &f->values[k],
1452 var_get_width (var)))
1453 goto next_hash_node;
1462 f = xmalloc (table_entry_size (ft->vars.n));
1464 for (size_t k = 0; k < ft->vars.n; k++)
1466 const struct variable *var = ft->vars.vars[k];
1467 value_clone (&f->values[k], case_data (c, var),
1468 var_get_width (var));
1470 hmap_insert (&ft->data, &f->node, hash);
1476 casereader_destroy (input);
1478 for (size_t i = 0; i < ct->n_tables; i++)
1480 struct ctables_table *t = &ct->tables[i];
1482 struct pivot_table *pt = pivot_table_create (N_("Custom Tables"));
1483 struct pivot_dimension *d = pivot_dimension_create (
1484 pt, PIVOT_AXIS_ROW, N_("Rows"));
1485 for (size_t j = 0; j < t->n_fts; j++)
1487 struct ctables_freqtab *ft = t->fts[j];
1488 ft->sorted = xnmalloc (ft->data.count, sizeof *ft->sorted);
1492 HMAP_FOR_EACH (f, struct freq, node, &ft->data)
1493 ft->sorted[n++] = f;
1494 assert (n == ft->data.count);
1495 sort (ft->sorted, n, sizeof *ft->sorted,
1496 compare_freq_3way, &ft->vars);
1498 struct pivot_category **groups = xnmalloc (ft->vars.n,
1500 for (size_t k = 0; k < n; k++)
1502 struct freq *prev = k > 0 ? ft->sorted[k - 1] : NULL;
1503 struct freq *f = ft->sorted[k];
1505 size_t n_common = 0;
1507 for (; n_common + 1 < ft->vars.n; n_common++)
1508 if (!value_equal (&prev->values[n_common],
1509 &f->values[n_common],
1510 var_get_type (ft->vars.vars[n_common])))
1513 for (size_t m = n_common; m < ft->vars.n; m++)
1515 struct pivot_category *parent = m > 0 ? groups[m - 1] : d->root;
1516 const struct variable *var = ft->vars.vars[m];
1517 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (var)];
1519 if (vlabel != CTVL_NONE)
1520 parent = pivot_category_create_group__ (
1521 parent, pivot_value_new_variable (ft->vars.vars[m]));
1523 if (m + 1 < ft->vars.n)
1524 parent = pivot_category_create_group__ (
1526 pivot_value_new_var_value (ft->vars.vars[m], &f->values[m]));
1529 if (m == ft->vars.n - 1)
1531 int leaf = pivot_category_create_leaf (
1533 pivot_value_new_var_value (ft->vars.vars[ft->vars.n - 1],
1534 &f->values[ft->vars.n - 1]));
1535 pivot_table_put1 (pt, leaf, pivot_value_new_number (f->count));
1541 pivot_table_submit (pt);
1544 for (size_t i = 0; i < ct->n_tables; i++)
1546 struct ctables_table *t = &ct->tables[i];
1548 for (size_t j = 0; j < t->n_fts; j++)
1550 struct ctables_freqtab *ft = t->fts[j];
1551 struct freq *f, *next;
1552 HMAP_FOR_EACH_SAFE (f, next, struct freq, node, &ft->data)
1554 hmap_delete (&ft->data, &f->node);
1555 for (size_t k = 0; k < ft->vars.n; k++)
1557 const struct variable *var = ft->vars.vars[k];
1558 value_destroy (&f->values[k], var_get_width (var));
1562 hmap_destroy (&ft->data);
1564 var_array_uninit (&ft->vars);
1570 return proc_commit (ds);
1574 cmd_ctables (struct lexer *lexer, struct dataset *ds)
1576 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
1577 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
1578 enum settings_value_show tvars = settings_get_show_variables ();
1579 for (size_t i = 0; i < n_vars; i++)
1580 vlabels[i] = (enum ctables_vlabel) tvars;
1582 struct ctables *ct = xmalloc (sizeof *ct);
1583 *ct = (struct ctables) {
1584 .look = pivot_table_look_unshare (pivot_table_look_ref (
1585 pivot_table_look_get_default ())),
1587 .hide_threshold = 5,
1590 if (!lex_force_match (lexer, T_SLASH))
1593 while (!lex_match_id (lexer, "TABLE"))
1595 if (lex_match_id (lexer, "FORMAT"))
1597 double widths[2] = { SYSMIS, SYSMIS };
1598 double units_per_inch = 72.0;
1600 while (lex_token (lexer) != T_SLASH)
1602 if (lex_match_id (lexer, "MINCOLWIDTH"))
1604 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1607 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1609 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1612 else if (lex_match_id (lexer, "UNITS"))
1614 lex_match (lexer, T_EQUALS);
1615 if (lex_match_id (lexer, "POINTS"))
1616 units_per_inch = 72.0;
1617 else if (lex_match_id (lexer, "INCHES"))
1618 units_per_inch = 1.0;
1619 else if (lex_match_id (lexer, "CM"))
1620 units_per_inch = 2.54;
1623 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1627 else if (lex_match_id (lexer, "EMPTY"))
1632 lex_match (lexer, T_EQUALS);
1633 if (lex_match_id (lexer, "ZERO"))
1635 /* Nothing to do. */
1637 else if (lex_match_id (lexer, "BLANK"))
1638 ct->zero = xstrdup ("");
1639 else if (lex_force_string (lexer))
1641 ct->zero = ss_xstrdup (lex_tokss (lexer));
1647 else if (lex_match_id (lexer, "MISSING"))
1649 lex_match (lexer, T_EQUALS);
1650 if (!lex_force_string (lexer))
1654 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1655 ? ss_xstrdup (lex_tokss (lexer))
1661 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1662 "UNITS", "EMPTY", "MISSING");
1667 if (widths[0] != SYSMIS && widths[1] != SYSMIS
1668 && widths[0] > widths[1])
1670 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1674 for (size_t i = 0; i < 2; i++)
1675 if (widths[i] != SYSMIS)
1677 int *wr = ct->look->width_ranges[TABLE_HORZ];
1678 wr[i] = widths[i] / units_per_inch * 96.0;
1683 else if (lex_match_id (lexer, "VLABELS"))
1685 if (!lex_force_match_id (lexer, "VARIABLES"))
1687 lex_match (lexer, T_EQUALS);
1689 struct variable **vars;
1691 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1695 if (!lex_force_match_id (lexer, "DISPLAY"))
1700 lex_match (lexer, T_EQUALS);
1702 enum ctables_vlabel vlabel;
1703 if (lex_match_id (lexer, "DEFAULT"))
1704 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
1705 else if (lex_match_id (lexer, "NAME"))
1707 else if (lex_match_id (lexer, "LABEL"))
1708 vlabel = CTVL_LABEL;
1709 else if (lex_match_id (lexer, "BOTH"))
1711 else if (lex_match_id (lexer, "NONE"))
1715 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1721 for (size_t i = 0; i < n_vars; i++)
1722 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1725 else if (lex_match_id (lexer, "MRSETS"))
1727 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1729 lex_match (lexer, T_EQUALS);
1730 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1733 else if (lex_match_id (lexer, "SMISSING"))
1735 if (lex_match_id (lexer, "VARIABLE"))
1736 ct->smissing_listwise = false;
1737 else if (lex_match_id (lexer, "LISTWISE"))
1738 ct->smissing_listwise = true;
1741 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1746 else if (lex_match_id (lexer, "WEIGHT"))
1748 if (!lex_force_match_id (lexer, "VARIABLE"))
1750 lex_match (lexer, T_EQUALS);
1751 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1752 if (!ct->base_weight)
1755 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1757 if (!lex_force_match_id (lexer, "COUNT"))
1759 lex_match (lexer, T_EQUALS);
1760 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1762 ct->hide_threshold = lex_integer (lexer);
1767 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1768 "SMISSING", "PCOMPUTE", "PPROPERTIES",
1769 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1773 if (!lex_force_match (lexer, T_SLASH))
1777 size_t allocated_tables = 0;
1780 if (ct->n_tables >= allocated_tables)
1781 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1782 sizeof *ct->tables);
1784 struct ctables_table *t = &ct->tables[ct->n_tables++];
1785 *t = (struct ctables_table) {
1786 .slabels_position = PIVOT_AXIS_COLUMN,
1787 .slabels_visible = true,
1788 .row_labels = CTLP_NORMAL,
1789 .col_labels = CTLP_NORMAL,
1790 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
1791 sizeof *t->categories),
1792 .n_categories = dict_get_n_vars (dataset_dict (ds)),
1796 lex_match (lexer, T_EQUALS);
1797 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1799 if (lex_match (lexer, T_BY))
1801 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1802 ct, t, PIVOT_AXIS_COLUMN))
1805 if (lex_match (lexer, T_BY))
1807 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1808 ct, t, PIVOT_AXIS_LAYER))
1813 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1814 && !t->axes[PIVOT_AXIS_LAYER])
1816 lex_error (lexer, _("At least one variable must be specified."));
1820 const struct ctables_axis *scales[PIVOT_N_AXES];
1821 size_t n_scales = 0;
1822 for (size_t i = 0; i < 3; i++)
1824 scales[i] = find_scale (t->axes[i]);
1830 msg (SE, _("Scale variables may appear only on one dimension."));
1831 if (scales[PIVOT_AXIS_ROW])
1832 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
1833 _("This scale variable appears in the rows dimension."));
1834 if (scales[PIVOT_AXIS_COLUMN])
1835 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
1836 _("This scale variable appears in the columns dimension."));
1837 if (scales[PIVOT_AXIS_LAYER])
1838 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
1839 _("This scale variable appears in the layer dimension."));
1843 if (lex_token (lexer) == T_ENDCMD)
1845 if (!lex_force_match (lexer, T_SLASH))
1848 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1850 if (lex_match_id (lexer, "SLABELS"))
1852 while (lex_token (lexer) != T_SLASH)
1854 if (lex_match_id (lexer, "POSITION"))
1856 lex_match (lexer, T_EQUALS);
1857 if (lex_match_id (lexer, "COLUMN"))
1858 t->slabels_position = PIVOT_AXIS_COLUMN;
1859 else if (lex_match_id (lexer, "ROW"))
1860 t->slabels_position = PIVOT_AXIS_ROW;
1861 else if (lex_match_id (lexer, "LAYER"))
1862 t->slabels_position = PIVOT_AXIS_LAYER;
1865 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1869 else if (lex_match_id (lexer, "VISIBLE"))
1871 lex_match (lexer, T_EQUALS);
1872 if (!parse_bool (lexer, &t->slabels_visible))
1877 lex_error_expecting (lexer, "POSITION", "VISIBLE");
1882 else if (lex_match_id (lexer, "CLABELS"))
1884 while (lex_token (lexer) != T_SLASH)
1886 if (lex_match_id (lexer, "AUTO"))
1887 t->row_labels = t->col_labels = CTLP_NORMAL;
1888 else if (lex_match_id (lexer, "ROWLABELS"))
1890 lex_match (lexer, T_EQUALS);
1891 if (lex_match_id (lexer, "OPPOSITE"))
1892 t->row_labels = CTLP_OPPOSITE;
1893 else if (lex_match_id (lexer, "LAYER"))
1894 t->row_labels = CTLP_LAYER;
1897 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1901 else if (lex_match_id (lexer, "COLLABELS"))
1903 lex_match (lexer, T_EQUALS);
1904 if (lex_match_id (lexer, "OPPOSITE"))
1905 t->col_labels = CTLP_OPPOSITE;
1906 else if (lex_match_id (lexer, "LAYER"))
1907 t->col_labels = CTLP_LAYER;
1910 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1916 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1922 else if (lex_match_id (lexer, "CRITERIA"))
1924 if (!lex_force_match_id (lexer, "CILEVEL"))
1926 lex_match (lexer, T_EQUALS);
1928 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1930 t->cilevel = lex_number (lexer);
1933 else if (lex_match_id (lexer, "CATEGORIES"))
1935 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1938 else if (lex_match_id (lexer, "TITLES"))
1943 if (lex_match_id (lexer, "CAPTION"))
1944 textp = &t->caption;
1945 else if (lex_match_id (lexer, "CORNER"))
1947 else if (lex_match_id (lexer, "TITLE"))
1951 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1954 lex_match (lexer, T_EQUALS);
1956 struct string s = DS_EMPTY_INITIALIZER;
1957 while (lex_is_string (lexer))
1959 if (!ds_is_empty (&s))
1960 ds_put_byte (&s, ' ');
1961 ds_put_substring (&s, lex_tokss (lexer));
1965 *textp = ds_steal_cstr (&s);
1967 while (lex_token (lexer) != T_SLASH
1968 && lex_token (lexer) != T_ENDCMD);
1970 else if (lex_match_id (lexer, "SIGTEST"))
1974 t->chisq = xmalloc (sizeof *t->chisq);
1975 *t->chisq = (struct ctables_chisq) {
1977 .include_mrsets = true,
1978 .all_visible = true,
1984 if (lex_match_id (lexer, "TYPE"))
1986 lex_match (lexer, T_EQUALS);
1987 if (!lex_force_match_id (lexer, "CHISQUARE"))
1990 else if (lex_match_id (lexer, "ALPHA"))
1992 lex_match (lexer, T_EQUALS);
1993 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
1995 t->chisq->alpha = lex_number (lexer);
1998 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2000 lex_match (lexer, T_EQUALS);
2001 if (parse_bool (lexer, &t->chisq->include_mrsets))
2004 else if (lex_match_id (lexer, "CATEGORIES"))
2006 lex_match (lexer, T_EQUALS);
2007 if (lex_match_id (lexer, "ALLVISIBLE"))
2008 t->chisq->all_visible = true;
2009 else if (lex_match_id (lexer, "SUBTOTALS"))
2010 t->chisq->all_visible = false;
2013 lex_error_expecting (lexer,
2014 "ALLVISIBLE", "SUBTOTALS");
2020 lex_error_expecting (lexer, "TYPE", "ALPHA",
2021 "INCLUDEMRSETS", "CATEGORIES");
2025 while (lex_token (lexer) != T_SLASH
2026 && lex_token (lexer) != T_ENDCMD);
2028 else if (lex_match_id (lexer, "COMPARETEST"))
2032 t->pairwise = xmalloc (sizeof *t->pairwise);
2033 *t->pairwise = (struct ctables_pairwise) {
2035 .alpha = { .05, .05 },
2036 .adjust = BONFERRONI,
2037 .include_mrsets = true,
2038 .meansvariance_allcats = true,
2039 .all_visible = true,
2048 if (lex_match_id (lexer, "TYPE"))
2050 lex_match (lexer, T_EQUALS);
2051 if (lex_match_id (lexer, "PROP"))
2052 t->pairwise->type = PROP;
2053 else if (lex_match_id (lexer, "MEAN"))
2054 t->pairwise->type = MEAN;
2057 lex_error_expecting (lexer, "PROP", "MEAN");
2061 else if (lex_match_id (lexer, "ALPHA"))
2063 lex_match (lexer, T_EQUALS);
2065 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2067 double a0 = lex_number (lexer);
2070 lex_match (lexer, T_COMMA);
2071 if (lex_is_number (lexer))
2073 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2075 double a1 = lex_number (lexer);
2078 t->pairwise->alpha[0] = MIN (a0, a1);
2079 t->pairwise->alpha[1] = MAX (a0, a1);
2082 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2084 else if (lex_match_id (lexer, "ADJUST"))
2086 lex_match (lexer, T_EQUALS);
2087 if (lex_match_id (lexer, "BONFERRONI"))
2088 t->pairwise->adjust = BONFERRONI;
2089 else if (lex_match_id (lexer, "BH"))
2090 t->pairwise->adjust = BH;
2091 else if (lex_match_id (lexer, "NONE"))
2092 t->pairwise->adjust = 0;
2095 lex_error_expecting (lexer, "BONFERRONI", "BH",
2100 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2102 lex_match (lexer, T_EQUALS);
2103 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2106 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2108 lex_match (lexer, T_EQUALS);
2109 if (lex_match_id (lexer, "ALLCATS"))
2110 t->pairwise->meansvariance_allcats = true;
2111 else if (lex_match_id (lexer, "TESTEDCATS"))
2112 t->pairwise->meansvariance_allcats = false;
2115 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2119 else if (lex_match_id (lexer, "CATEGORIES"))
2121 lex_match (lexer, T_EQUALS);
2122 if (lex_match_id (lexer, "ALLVISIBLE"))
2123 t->pairwise->all_visible = true;
2124 else if (lex_match_id (lexer, "SUBTOTALS"))
2125 t->pairwise->all_visible = false;
2128 lex_error_expecting (lexer, "ALLVISIBLE",
2133 else if (lex_match_id (lexer, "MERGE"))
2135 lex_match (lexer, T_EQUALS);
2136 if (!parse_bool (lexer, &t->pairwise->merge))
2139 else if (lex_match_id (lexer, "STYLE"))
2141 lex_match (lexer, T_EQUALS);
2142 if (lex_match_id (lexer, "APA"))
2143 t->pairwise->apa_style = true;
2144 else if (lex_match_id (lexer, "SIMPLE"))
2145 t->pairwise->apa_style = false;
2148 lex_error_expecting (lexer, "APA", "SIMPLE");
2152 else if (lex_match_id (lexer, "SHOWSIG"))
2154 lex_match (lexer, T_EQUALS);
2155 if (!parse_bool (lexer, &t->pairwise->show_sig))
2160 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2161 "INCLUDEMRSETS", "MEANSVARIANCE",
2162 "CATEGORIES", "MERGE", "STYLE",
2167 while (lex_token (lexer) != T_SLASH
2168 && lex_token (lexer) != T_ENDCMD);
2172 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2173 "CRITERIA", "CATEGORIES", "TITLES",
2174 "SIGTEST", "COMPARETEST");
2179 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2181 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2186 while (lex_token (lexer) != T_ENDCMD);
2188 bool ok = ctables_execute (ds, ct);
2189 ctables_destroy (ct);
2190 return ok ? CMD_SUCCESS : CMD_FAILURE;
2193 ctables_destroy (ct);