1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/mrset.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/message.h"
34 #include "libpspp/string-array.h"
35 #include "math/moments.h"
36 #include "output/pivot-table.h"
38 #include "gl/minmax.h"
39 #include "gl/xalloc.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) (msgid)
47 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
48 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
49 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
50 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
54 - unweighted summaries (U*)
55 - lower confidence limits (*.LCL)
56 - upper confidence limits (*.UCL)
57 - standard error (*.SE)
60 /* All variables. */ \
61 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
62 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
63 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
64 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
85 /* Scale variables, totals, and subtotals. */ \
86 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
87 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
97 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
98 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
99 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
100 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
101 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
103 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
105 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
106 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
107 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
108 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
110 /* Multiple response sets. */ \
111 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
112 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
134 enum ctables_summary_function
136 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
143 N_CTSF_FUNCTIONS = SUMMARIES
149 struct pivot_table_look *look;
151 /* If this is NULL, zeros are displayed using the normal print format.
152 Otherwise, this string is displayed. */
155 /* If this is NULL, missing values are displayed using the normal print
156 format. Otherwise, this string is displayed. */
159 /* Indexed by variable dictionary index. */
160 enum ctables_vlabel *vlabels;
162 bool mrsets_count_duplicates; /* MRSETS. */
163 bool smissing_listwise; /* SMISSING. */
164 struct variable *base_weight; /* WEIGHT. */
165 int hide_threshold; /* HIDESMALLCOUNTS. */
167 struct ctables_table **tables;
171 struct ctables_postcompute
173 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
174 const char *name; /* Name, without leading &. */
176 struct ctables_postcompute_expr *expr;
179 bool hide_source_cats;
182 struct ctables_postcompute_expr
184 enum ctables_postcompute_op
192 /* XXX SUBTOTAL and HSUBTOTAL */
205 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
210 XXX what about string ranges? */
213 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
214 struct ctables_postcompute_expr *subs[2];
218 enum ctables_label_position
227 struct variable **vars;
231 struct ctables_summary_spec *summaries;
233 struct variable *summary_var;
238 struct var_array *vas;
244 struct ctables_axis *axes[PIVOT_N_AXES];
245 struct var_array2 vaas[PIVOT_N_AXES];
246 enum pivot_axis_type summary_axis;
249 enum pivot_axis_type slabels_position;
250 bool slabels_visible;
252 enum ctables_label_position row_labels;
253 enum ctables_label_position col_labels;
255 /* Indexed by variable dictionary index. */
256 struct ctables_categories **categories;
265 struct ctables_chisq *chisq;
266 struct ctables_pairwise *pairwise;
275 struct variable *var;
276 const struct mrset *mrset;
280 static const struct fmt_spec *
281 ctables_var_get_print_format (const struct ctables_var *var)
283 return (var->is_mrset
284 ? var_get_print_format (var->mrset->vars[0])
285 : var_get_print_format (var->var));
289 ctables_var_name (const struct ctables_var *var)
291 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
294 struct ctables_categories
298 /* Explicit categories. */
299 struct ctables_cat_value *values;
302 /* Implicit categories. */
304 bool include_missing;
305 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
306 enum ctables_summary_function sort_func;
307 struct variable *sort_func_var;
315 /* Empty categories. */
319 struct ctables_cat_value
321 enum ctables_cat_value_type
335 double number; /* CCVT_NUMBER. */
336 char *string; /* CCVT_STRING. */
337 double range[2]; /* CCVT_RANGE. */
338 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
343 ctables_cat_value_uninit (struct ctables_cat_value *cv)
362 free (cv->subtotal_label);
367 ctables_categories_unref (struct ctables_categories *c)
372 assert (c->n_refs > 0);
376 for (size_t i = 0; i < c->n_values; i++)
377 ctables_cat_value_uninit (&c->values[i]);
379 free (c->total_label);
383 /* Chi-square test (SIGTEST). */
391 /* Pairwise comparison test (COMPARETEST). */
392 struct ctables_pairwise
394 enum { PROP, MEAN } type;
397 bool meansvariance_allcats;
399 enum { BONFERRONI = 1, BH } adjust;
423 struct ctables_var var;
425 struct ctables_summary_spec *summaries;
427 size_t allocated_summaries;
431 struct ctables_axis *subs[2];
434 struct msg_location *loc;
437 static void ctables_axis_destroy (struct ctables_axis *);
446 enum ctables_function_availability
448 CTFA_ALL, /* Any variables. */
449 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
450 CTFA_MRSETS, /* Only multiple-response sets */
453 struct ctables_summary_spec
455 enum ctables_summary_function function;
456 double percentile; /* CTSF_PTILE only. */
458 struct fmt_spec format; /* XXX extra CTABLES formats */
462 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
469 parse_col_width (struct lexer *lexer, const char *name, double *width)
471 lex_match (lexer, T_EQUALS);
472 if (lex_match_id (lexer, "DEFAULT"))
474 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
476 *width = lex_number (lexer);
486 parse_bool (struct lexer *lexer, bool *b)
488 if (lex_match_id (lexer, "NO"))
490 else if (lex_match_id (lexer, "YES"))
494 lex_error_expecting (lexer, "YES", "NO");
500 static enum ctables_function_availability
501 ctables_function_availability (enum ctables_summary_function f)
503 static enum ctables_function_availability availability[] = {
504 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
509 return availability[f];
513 parse_ctables_summary_function (struct lexer *lexer,
514 enum ctables_summary_function *f)
518 enum ctables_summary_function function;
519 struct substring name;
521 static struct pair names[] = {
522 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
523 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
526 /* The .COUNT suffix may be omitted. */
527 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
528 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
529 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
530 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
531 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
532 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
533 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
537 if (!lex_force_id (lexer))
540 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
541 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
543 *f = names[i].function;
548 lex_error (lexer, _("Expecting summary function name."));
553 ctables_axis_destroy (struct ctables_axis *axis)
561 for (size_t i = 0; i < axis->n_summaries; i++)
562 ctables_summary_spec_uninit (&axis->summaries[i]);
563 free (axis->summaries);
568 ctables_axis_destroy (axis->subs[0]);
569 ctables_axis_destroy (axis->subs[1]);
572 msg_location_destroy (axis->loc);
576 static struct ctables_axis *
577 ctables_axis_new_nonterminal (enum ctables_axis_op op,
578 struct ctables_axis *sub0,
579 struct ctables_axis *sub1,
580 struct lexer *lexer, int start_ofs)
582 struct ctables_axis *axis = xmalloc (sizeof *axis);
583 *axis = (struct ctables_axis) {
585 .subs = { sub0, sub1 },
586 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
591 struct ctables_axis_parse_ctx
594 struct dictionary *dict;
596 struct ctables_table *t;
599 static struct fmt_spec
600 ctables_summary_default_format (enum ctables_summary_function function,
601 const struct ctables_var *var)
603 static const enum ctables_format default_formats[] = {
604 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
608 switch (default_formats[function])
611 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
614 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
617 return *ctables_var_get_print_format (var);
625 ctables_summary_default_label (enum ctables_summary_function function,
628 static const char *default_labels[] = {
629 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
634 return (function == CTSF_PTILE
635 ? xasprintf (_("Percentile %.2f"), percentile)
636 : xstrdup (gettext (default_labels[function])));
640 ctables_summary_function_name (enum ctables_summary_function function)
642 static const char *names[] = {
643 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
647 return names[function];
651 add_summary_spec (struct ctables_axis *axis,
652 enum ctables_summary_function function, double percentile,
653 const char *label, const struct fmt_spec *format,
654 const struct msg_location *loc)
656 if (axis->op == CTAO_VAR)
658 if (axis->n_summaries >= axis->allocated_summaries)
659 axis->summaries = x2nrealloc (axis->summaries,
660 &axis->allocated_summaries,
661 sizeof *axis->summaries);
663 const char *function_name = ctables_summary_function_name (function);
664 const char *var_name = ctables_var_name (&axis->var);
665 switch (ctables_function_availability (function))
668 if (!axis->var.is_mrset)
670 msg_at (SE, loc, _("Summary function %s applies only to multiple "
671 "response sets."), function_name);
672 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
682 _("Summary function %s applies only to scale variables."),
684 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
694 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
695 *dst = (struct ctables_summary_spec) {
696 .function = function,
697 .percentile = percentile,
698 .label = xstrdup (label),
699 .format = (format ? *format
700 : ctables_summary_default_format (function, &axis->var)),
706 for (size_t i = 0; i < 2; i++)
707 if (!add_summary_spec (axis->subs[i], function, percentile, label,
714 static struct ctables_axis *ctables_axis_parse_stack (
715 struct ctables_axis_parse_ctx *);
718 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
719 struct ctables_var *var)
721 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
723 *var = (struct ctables_var) {
725 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
729 lex_error (lexer, _("'%s' does not name a multiple-response set "
730 "in the active file dictionary."),
731 lex_tokcstr (lexer));
739 *var = (struct ctables_var) {
741 .var = parse_variable (lexer, dict),
743 return var->var != NULL;
747 static struct ctables_axis *
748 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
750 if (lex_match (ctx->lexer, T_LPAREN))
752 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
753 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
755 ctables_axis_destroy (sub);
761 if (!lex_force_id (ctx->lexer))
764 int start_ofs = lex_ofs (ctx->lexer);
765 struct ctables_var var;
766 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
769 struct ctables_axis *axis = xmalloc (sizeof *axis);
770 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
772 /* XXX should figure out default measures by reading data */
773 axis->scale = (var.is_mrset ? false
774 : lex_match_phrase (ctx->lexer, "[S]") ? true
775 : lex_match_phrase (ctx->lexer, "[C]") ? false
776 : var_get_measure (var.var) == MEASURE_SCALE);
777 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
778 lex_ofs (ctx->lexer) - 1);
782 static struct ctables_axis *
783 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
785 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
786 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
791 int start_ofs = lex_ofs (ctx->lexer);
793 /* Parse function. */
794 enum ctables_summary_function function;
795 if (!parse_ctables_summary_function (ctx->lexer, &function))
798 /* Parse percentile. */
799 double percentile = 0;
800 if (function == CTSF_PTILE)
802 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
804 percentile = lex_number (ctx->lexer);
805 lex_get (ctx->lexer);
810 if (lex_is_string (ctx->lexer))
812 label = ss_xstrdup (lex_tokss (ctx->lexer));
813 lex_get (ctx->lexer);
816 label = ctables_summary_default_label (function, percentile);
819 struct fmt_spec format;
820 const struct fmt_spec *formatp;
821 if (lex_token (ctx->lexer) == T_ID)
823 if (!parse_format_specifier (ctx->lexer, &format)
824 || !fmt_check_output (&format)
825 || !fmt_check_type_compat (&format, VAL_NUMERIC))
835 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
836 lex_ofs (ctx->lexer) - 1);
837 add_summary_spec (sub, function, percentile, label, formatp, loc);
839 msg_location_destroy (loc);
841 lex_match (ctx->lexer, T_COMMA);
843 while (!lex_match (ctx->lexer, T_RBRACK));
848 ctables_axis_destroy (sub);
852 static const struct ctables_axis *
853 find_scale (const struct ctables_axis *axis)
857 else if (axis->op == CTAO_VAR)
861 assert (!axis->var.is_mrset);
869 for (size_t i = 0; i < 2; i++)
871 const struct ctables_axis *scale = find_scale (axis->subs[i]);
879 static const struct ctables_axis *
880 find_categorical_summary_spec (const struct ctables_axis *axis)
884 else if (axis->op == CTAO_VAR)
885 return !axis->scale && axis->n_summaries ? axis : NULL;
888 for (size_t i = 0; i < 2; i++)
890 const struct ctables_axis *sum
891 = find_categorical_summary_spec (axis->subs[i]);
899 static struct ctables_axis *
900 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
902 int start_ofs = lex_ofs (ctx->lexer);
903 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
907 while (lex_match (ctx->lexer, T_GT))
909 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
913 struct ctables_axis *nest = ctables_axis_new_nonterminal (
914 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
916 const struct ctables_axis *outer_scale = find_scale (lhs);
917 const struct ctables_axis *inner_scale = find_scale (rhs);
918 if (outer_scale && inner_scale)
920 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
921 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
922 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
923 ctables_axis_destroy (nest);
927 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
930 msg_at (SE, nest->loc,
931 _("Summaries may only be requested for categorical variables "
932 "at the innermost nesting level."));
933 msg_at (SN, outer_sum->loc,
934 _("This outer categorical variable has a summary."));
935 ctables_axis_destroy (nest);
945 static struct ctables_axis *
946 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
948 int start_ofs = lex_ofs (ctx->lexer);
949 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
953 while (lex_match (ctx->lexer, T_PLUS))
955 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
959 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
960 ctx->lexer, start_ofs);
967 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
968 struct ctables *ct, struct ctables_table *t,
969 enum pivot_axis_type a)
971 if (lex_token (lexer) == T_BY
972 || lex_token (lexer) == T_SLASH
973 || lex_token (lexer) == T_ENDCMD)
976 struct ctables_axis_parse_ctx ctx = {
982 t->axes[a] = ctables_axis_parse_stack (&ctx);
983 return t->axes[a] != NULL;
987 ctables_chisq_destroy (struct ctables_chisq *chisq)
993 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
999 ctables_table_destroy (struct ctables_table *t)
1004 for (size_t i = 0; i < t->n_categories; i++)
1005 ctables_categories_unref (t->categories[i]);
1006 free (t->categories);
1008 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1009 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1010 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1014 ctables_chisq_destroy (t->chisq);
1015 ctables_pairwise_destroy (t->pairwise);
1020 ctables_destroy (struct ctables *ct)
1025 pivot_table_look_unref (ct->look);
1029 for (size_t i = 0; i < ct->n_tables; i++)
1030 ctables_table_destroy (ct->tables[i]);
1035 static struct ctables_cat_value
1036 ccvt_range (double low, double high)
1038 return (struct ctables_cat_value) {
1040 .range = { low, high }
1045 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1046 struct ctables_table *t)
1048 if (!lex_match_id (lexer, "VARIABLES"))
1050 lex_match (lexer, T_EQUALS);
1052 struct variable **vars;
1054 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1057 struct ctables_categories *c = xmalloc (sizeof *c);
1058 *c = (struct ctables_categories) { .n_refs = n_vars };
1059 for (size_t i = 0; i < n_vars; i++)
1061 struct ctables_categories **cp
1062 = &t->categories[var_get_dict_index (vars[i])];
1063 ctables_categories_unref (*cp);
1068 if (lex_match (lexer, T_LBRACK))
1070 size_t allocated_values = 0;
1073 if (c->n_values >= allocated_values)
1074 c->values = x2nrealloc (c->values, &allocated_values,
1077 struct ctables_cat_value *v = &c->values[c->n_values];
1078 if (lex_match_id (lexer, "OTHERNM"))
1079 v->type = CCVT_OTHERNM;
1080 else if (lex_match_id (lexer, "MISSING"))
1081 v->type = CCVT_MISSING;
1082 else if (lex_match_id (lexer, "SUBTOTAL"))
1083 *v = (struct ctables_cat_value)
1084 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1085 else if (lex_match_id (lexer, "HSUBTOTAL"))
1086 *v = (struct ctables_cat_value)
1087 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1088 else if (lex_match_id (lexer, "LO"))
1090 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1092 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1095 else if (lex_is_number (lexer))
1097 double number = lex_number (lexer);
1099 if (lex_match_id (lexer, "THRU"))
1101 v->type = CCVT_RANGE;
1102 v->range[0] = number;
1103 if (lex_match_id (lexer, "HI"))
1104 *v = ccvt_range (number, DBL_MAX);
1107 if (!lex_force_num (lexer))
1109 *v = ccvt_range (number, lex_number (lexer));
1114 *v = (struct ctables_cat_value) {
1115 .type = CCVT_NUMBER,
1119 else if (lex_is_string (lexer))
1121 *v = (struct ctables_cat_value) {
1122 .type = CCVT_STRING,
1123 .string = ss_xstrdup (lex_tokss (lexer)),
1129 lex_error (lexer, NULL);
1133 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1134 && lex_match (lexer, T_EQUALS))
1136 if (!lex_force_string (lexer))
1139 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1144 lex_match (lexer, T_COMMA);
1146 while (!lex_match (lexer, T_RBRACK));
1149 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1151 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1153 lex_match (lexer, T_EQUALS);
1154 if (lex_match_id (lexer, "A"))
1155 c->sort_ascending = true;
1156 else if (lex_match_id (lexer, "D"))
1157 c->sort_ascending = false;
1160 lex_error_expecting (lexer, "A", "D");
1164 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1166 lex_match (lexer, T_EQUALS);
1167 if (lex_match_id (lexer, "VALUE"))
1168 c->key = CTCS_VALUE;
1169 else if (lex_match_id (lexer, "LABEL"))
1170 c->key = CTCS_LABEL;
1173 c->key = CTCS_FUNCTION;
1174 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1177 if (lex_match (lexer, T_LPAREN))
1179 c->sort_func_var = parse_variable (lexer, dict);
1180 if (!c->sort_func_var)
1183 if (c->sort_func == CTSF_PTILE)
1185 lex_match (lexer, T_COMMA);
1186 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1188 c->percentile = lex_number (lexer);
1192 if (!lex_force_match (lexer, T_RPAREN))
1195 else if (ctables_function_availability (c->sort_func)
1198 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1203 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1205 lex_match (lexer, T_EQUALS);
1206 if (lex_match_id (lexer, "INCLUDE"))
1207 c->include_missing = true;
1208 else if (lex_match_id (lexer, "EXCLUDE"))
1209 c->include_missing = false;
1212 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1216 else if (lex_match_id (lexer, "TOTAL"))
1218 lex_match (lexer, T_EQUALS);
1219 if (!parse_bool (lexer, &c->show_totals))
1222 else if (lex_match_id (lexer, "LABEL"))
1224 lex_match (lexer, T_EQUALS);
1225 if (!lex_force_string (lexer))
1227 free (c->total_label);
1228 c->total_label = ss_xstrdup (lex_tokss (lexer));
1231 else if (lex_match_id (lexer, "POSITION"))
1233 lex_match (lexer, T_EQUALS);
1234 if (lex_match_id (lexer, "BEFORE"))
1235 c->totals_before = true;
1236 else if (lex_match_id (lexer, "AFTER"))
1237 c->totals_before = false;
1240 lex_error_expecting (lexer, "BEFORE", "AFTER");
1244 else if (lex_match_id (lexer, "EMPTY"))
1246 lex_match (lexer, T_EQUALS);
1247 if (lex_match_id (lexer, "INCLUDE"))
1248 c->show_empty = true;
1249 else if (lex_match_id (lexer, "EXCLUDE"))
1250 c->show_empty = false;
1253 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1260 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1261 "TOTAL", "LABEL", "POSITION", "EMPTY");
1263 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1271 var_array_uninit (struct var_array *va)
1278 var_array2_uninit (struct var_array2 *vaa)
1282 for (size_t i = 0; i < vaa->n; i++)
1283 var_array_uninit (&vaa->vas[i]);
1288 static struct var_array2
1289 nest_fts (struct var_array2 va0, struct var_array2 va1)
1296 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1297 for (size_t i = 0; i < va0.n; i++)
1298 for (size_t j = 0; j < va1.n; j++)
1300 const struct var_array *a = &va0.vas[i];
1301 const struct var_array *b = &va1.vas[j];
1303 size_t allocate = a->n + b->n;
1304 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1305 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1307 for (size_t k = 0; k < a->n; k++)
1308 vars[n++] = a->vars[k];
1309 for (size_t k = 0; k < b->n; k++)
1310 vars[n++] = b->vars[k];
1311 assert (n == allocate);
1313 const struct var_array *summary_src;
1314 if (!a->summary_var)
1316 else if (!b->summary_var)
1320 vaa.vas[vaa.n++] = (struct var_array) {
1322 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1323 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1326 .summaries = summary_src->summaries,
1327 .n_summaries = summary_src->n_summaries,
1328 .summary_var = summary_src->summary_var,
1331 var_array2_uninit (&va0);
1332 var_array2_uninit (&va1);
1336 static struct var_array2
1337 stack_fts (struct var_array2 va0, struct var_array2 va1)
1339 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1340 for (size_t i = 0; i < va0.n; i++)
1341 vaa.vas[vaa.n++] = va0.vas[i];
1342 for (size_t i = 0; i < va1.n; i++)
1343 vaa.vas[vaa.n++] = va1.vas[i];
1344 assert (vaa.n == va0.n + va1.n);
1350 static struct var_array2
1351 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1354 return (struct var_array2) { .n = 0 };
1359 assert (!a->var.is_mrset);
1361 struct variable **vars = xmalloc (sizeof *vars);
1364 struct var_array *va = xmalloc (sizeof *va);
1365 *va = (struct var_array) {
1368 .scale_idx = a->scale ? 0 : SIZE_MAX,
1370 if (a->n_summaries || a->scale)
1372 va->summaries = a->summaries;
1373 va->n_summaries = a->n_summaries;
1374 va->summary_var = a->var.var;
1376 return (struct var_array2) { .vas = va, .n = 1 };
1379 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1380 enumerate_fts (axis_type, a->subs[1]));
1383 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1384 enumerate_fts (axis_type, a->subs[1]));
1390 union ctables_summary
1392 /* COUNT, VALIDN, TOTALN. */
1399 /* MINIMUM, MAXIMUM, RANGE. */
1406 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1407 struct moments1 *moments;
1409 /* XXX percentiles, median, mode, multiple response */
1413 ctables_summary_init (union ctables_summary *s,
1414 const struct ctables_summary_spec *ss)
1416 switch (ss->function)
1420 case CTSF_ROWPCT_COUNT:
1421 case CTSF_COLPCT_COUNT:
1422 case CTSF_TABLEPCT_COUNT:
1423 case CTSF_SUBTABLEPCT_COUNT:
1424 case CTSF_LAYERPCT_COUNT:
1425 case CTSF_LAYERROWPCT_COUNT:
1426 case CTSF_LAYERCOLPCT_COUNT:
1427 case CTSF_ROWPCT_VALIDN:
1428 case CTSF_COLPCT_VALIDN:
1429 case CTSF_TABLEPCT_VALIDN:
1430 case CTSF_SUBTABLEPCT_VALIDN:
1431 case CTSF_LAYERPCT_VALIDN:
1432 case CTSF_LAYERROWPCT_VALIDN:
1433 case CTSF_LAYERCOLPCT_VALIDN:
1434 case CTSF_ROWPCT_TOTALN:
1435 case CTSF_COLPCT_TOTALN:
1436 case CTSF_TABLEPCT_TOTALN:
1437 case CTSF_SUBTABLEPCT_TOTALN:
1438 case CTSF_LAYERPCT_TOTALN:
1439 case CTSF_LAYERROWPCT_TOTALN:
1440 case CTSF_LAYERCOLPCT_TOTALN:
1445 s->missing = s->valid = 0;
1451 s->min = s->max = SYSMIS;
1459 case CTSF_ROWPCT_SUM:
1460 case CTSF_COLPCT_SUM:
1461 case CTSF_TABLEPCT_SUM:
1462 case CTSF_SUBTABLEPCT_SUM:
1463 case CTSF_LAYERPCT_SUM:
1464 case CTSF_LAYERROWPCT_SUM:
1465 case CTSF_LAYERCOLPCT_SUM:
1466 s->moments = moments1_create (MOMENT_VARIANCE);
1475 case CTSF_RESPONSES:
1476 case CTSF_ROWPCT_RESPONSES:
1477 case CTSF_COLPCT_RESPONSES:
1478 case CTSF_TABLEPCT_RESPONSES:
1479 case CTSF_SUBTABLEPCT_RESPONSES:
1480 case CTSF_LAYERPCT_RESPONSES:
1481 case CTSF_LAYERROWPCT_RESPONSES:
1482 case CTSF_LAYERCOLPCT_RESPONSES:
1483 case CTSF_ROWPCT_RESPONSES_COUNT:
1484 case CTSF_COLPCT_RESPONSES_COUNT:
1485 case CTSF_TABLEPCT_RESPONSES_COUNT:
1486 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1487 case CTSF_LAYERPCT_RESPONSES_COUNT:
1488 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1489 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1490 case CTSF_ROWPCT_COUNT_RESPONSES:
1491 case CTSF_COLPCT_COUNT_RESPONSES:
1492 case CTSF_TABLEPCT_COUNT_RESPONSES:
1493 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1494 case CTSF_LAYERPCT_COUNT_RESPONSES:
1495 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1496 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1502 ctables_summary_uninit (union ctables_summary *s,
1503 const struct ctables_summary_spec *ss)
1505 switch (ss->function)
1509 case CTSF_ROWPCT_COUNT:
1510 case CTSF_COLPCT_COUNT:
1511 case CTSF_TABLEPCT_COUNT:
1512 case CTSF_SUBTABLEPCT_COUNT:
1513 case CTSF_LAYERPCT_COUNT:
1514 case CTSF_LAYERROWPCT_COUNT:
1515 case CTSF_LAYERCOLPCT_COUNT:
1516 case CTSF_ROWPCT_VALIDN:
1517 case CTSF_COLPCT_VALIDN:
1518 case CTSF_TABLEPCT_VALIDN:
1519 case CTSF_SUBTABLEPCT_VALIDN:
1520 case CTSF_LAYERPCT_VALIDN:
1521 case CTSF_LAYERROWPCT_VALIDN:
1522 case CTSF_LAYERCOLPCT_VALIDN:
1523 case CTSF_ROWPCT_TOTALN:
1524 case CTSF_COLPCT_TOTALN:
1525 case CTSF_TABLEPCT_TOTALN:
1526 case CTSF_SUBTABLEPCT_TOTALN:
1527 case CTSF_LAYERPCT_TOTALN:
1528 case CTSF_LAYERROWPCT_TOTALN:
1529 case CTSF_LAYERCOLPCT_TOTALN:
1546 case CTSF_ROWPCT_SUM:
1547 case CTSF_COLPCT_SUM:
1548 case CTSF_TABLEPCT_SUM:
1549 case CTSF_SUBTABLEPCT_SUM:
1550 case CTSF_LAYERPCT_SUM:
1551 case CTSF_LAYERROWPCT_SUM:
1552 case CTSF_LAYERCOLPCT_SUM:
1553 moments1_destroy (s->moments);
1562 case CTSF_RESPONSES:
1563 case CTSF_ROWPCT_RESPONSES:
1564 case CTSF_COLPCT_RESPONSES:
1565 case CTSF_TABLEPCT_RESPONSES:
1566 case CTSF_SUBTABLEPCT_RESPONSES:
1567 case CTSF_LAYERPCT_RESPONSES:
1568 case CTSF_LAYERROWPCT_RESPONSES:
1569 case CTSF_LAYERCOLPCT_RESPONSES:
1570 case CTSF_ROWPCT_RESPONSES_COUNT:
1571 case CTSF_COLPCT_RESPONSES_COUNT:
1572 case CTSF_TABLEPCT_RESPONSES_COUNT:
1573 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1574 case CTSF_LAYERPCT_RESPONSES_COUNT:
1575 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1576 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1577 case CTSF_ROWPCT_COUNT_RESPONSES:
1578 case CTSF_COLPCT_COUNT_RESPONSES:
1579 case CTSF_TABLEPCT_COUNT_RESPONSES:
1580 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1581 case CTSF_LAYERPCT_COUNT_RESPONSES:
1582 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1583 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1589 ctables_summary_add (union ctables_summary *s,
1590 const struct ctables_summary_spec *ss,
1591 const struct variable *var, const union value *value,
1594 switch (ss->function)
1598 case CTSF_ROWPCT_COUNT:
1599 case CTSF_COLPCT_COUNT:
1600 case CTSF_TABLEPCT_COUNT:
1601 case CTSF_SUBTABLEPCT_COUNT:
1602 case CTSF_LAYERPCT_COUNT:
1603 case CTSF_LAYERROWPCT_COUNT:
1604 case CTSF_LAYERCOLPCT_COUNT:
1605 case CTSF_ROWPCT_VALIDN:
1606 case CTSF_COLPCT_VALIDN:
1607 case CTSF_TABLEPCT_VALIDN:
1608 case CTSF_SUBTABLEPCT_VALIDN:
1609 case CTSF_LAYERPCT_VALIDN:
1610 case CTSF_LAYERROWPCT_VALIDN:
1611 case CTSF_LAYERCOLPCT_VALIDN:
1612 case CTSF_ROWPCT_TOTALN:
1613 case CTSF_COLPCT_TOTALN:
1614 case CTSF_TABLEPCT_TOTALN:
1615 case CTSF_SUBTABLEPCT_TOTALN:
1616 case CTSF_LAYERPCT_TOTALN:
1617 case CTSF_LAYERROWPCT_TOTALN:
1618 case CTSF_LAYERCOLPCT_TOTALN:
1623 if (var_is_value_missing (var, value))
1624 s->missing += weight;
1632 if (!var_is_value_missing (var, value))
1634 assert (!var_is_alpha (var)); /* XXX? */
1635 if (s->min == SYSMIS || value->f < s->min)
1637 if (s->max == SYSMIS || value->f > s->max)
1647 case CTSF_ROWPCT_SUM:
1648 case CTSF_COLPCT_SUM:
1649 case CTSF_TABLEPCT_SUM:
1650 case CTSF_SUBTABLEPCT_SUM:
1651 case CTSF_LAYERPCT_SUM:
1652 case CTSF_LAYERROWPCT_SUM:
1653 case CTSF_LAYERCOLPCT_SUM:
1654 moments1_add (s->moments, value->f, weight);
1663 case CTSF_RESPONSES:
1664 case CTSF_ROWPCT_RESPONSES:
1665 case CTSF_COLPCT_RESPONSES:
1666 case CTSF_TABLEPCT_RESPONSES:
1667 case CTSF_SUBTABLEPCT_RESPONSES:
1668 case CTSF_LAYERPCT_RESPONSES:
1669 case CTSF_LAYERROWPCT_RESPONSES:
1670 case CTSF_LAYERCOLPCT_RESPONSES:
1671 case CTSF_ROWPCT_RESPONSES_COUNT:
1672 case CTSF_COLPCT_RESPONSES_COUNT:
1673 case CTSF_TABLEPCT_RESPONSES_COUNT:
1674 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1675 case CTSF_LAYERPCT_RESPONSES_COUNT:
1676 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1677 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1678 case CTSF_ROWPCT_COUNT_RESPONSES:
1679 case CTSF_COLPCT_COUNT_RESPONSES:
1680 case CTSF_TABLEPCT_COUNT_RESPONSES:
1681 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1682 case CTSF_LAYERPCT_COUNT_RESPONSES:
1683 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1684 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1690 ctables_summary_value (union ctables_summary *s,
1691 const struct ctables_summary_spec *ss)
1693 switch (ss->function)
1699 case CTSF_ROWPCT_COUNT:
1700 case CTSF_COLPCT_COUNT:
1701 case CTSF_TABLEPCT_COUNT:
1702 case CTSF_SUBTABLEPCT_COUNT:
1703 case CTSF_LAYERPCT_COUNT:
1704 case CTSF_LAYERROWPCT_COUNT:
1705 case CTSF_LAYERCOLPCT_COUNT:
1706 case CTSF_ROWPCT_VALIDN:
1707 case CTSF_COLPCT_VALIDN:
1708 case CTSF_TABLEPCT_VALIDN:
1709 case CTSF_SUBTABLEPCT_VALIDN:
1710 case CTSF_LAYERPCT_VALIDN:
1711 case CTSF_LAYERROWPCT_VALIDN:
1712 case CTSF_LAYERCOLPCT_VALIDN:
1713 case CTSF_ROWPCT_TOTALN:
1714 case CTSF_COLPCT_TOTALN:
1715 case CTSF_TABLEPCT_TOTALN:
1716 case CTSF_SUBTABLEPCT_TOTALN:
1717 case CTSF_LAYERPCT_TOTALN:
1718 case CTSF_LAYERROWPCT_TOTALN:
1719 case CTSF_LAYERCOLPCT_TOTALN:
1724 return s->valid + s->missing;
1737 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1742 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1748 double weight, variance;
1749 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1750 return calc_semean (variance, weight);
1756 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1757 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1762 double weight, mean;
1763 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1764 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1770 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1774 case CTSF_ROWPCT_SUM:
1775 case CTSF_COLPCT_SUM:
1776 case CTSF_TABLEPCT_SUM:
1777 case CTSF_SUBTABLEPCT_SUM:
1778 case CTSF_LAYERPCT_SUM:
1779 case CTSF_LAYERROWPCT_SUM:
1780 case CTSF_LAYERCOLPCT_SUM:
1789 case CTSF_RESPONSES:
1790 case CTSF_ROWPCT_RESPONSES:
1791 case CTSF_COLPCT_RESPONSES:
1792 case CTSF_TABLEPCT_RESPONSES:
1793 case CTSF_SUBTABLEPCT_RESPONSES:
1794 case CTSF_LAYERPCT_RESPONSES:
1795 case CTSF_LAYERROWPCT_RESPONSES:
1796 case CTSF_LAYERCOLPCT_RESPONSES:
1797 case CTSF_ROWPCT_RESPONSES_COUNT:
1798 case CTSF_COLPCT_RESPONSES_COUNT:
1799 case CTSF_TABLEPCT_RESPONSES_COUNT:
1800 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1801 case CTSF_LAYERPCT_RESPONSES_COUNT:
1802 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1803 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1804 case CTSF_ROWPCT_COUNT_RESPONSES:
1805 case CTSF_COLPCT_COUNT_RESPONSES:
1806 case CTSF_TABLEPCT_COUNT_RESPONSES:
1807 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1808 case CTSF_LAYERPCT_COUNT_RESPONSES:
1809 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1810 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1819 struct hmap_node node; /* Element in hash table. */
1824 union value *values;
1829 union ctables_summary *summaries;
1833 static struct ctables_freq *
1834 ctables_freq_create (struct ctables_freqtab *ft)
1836 struct ctables_freq *f = xmalloc (sizeof *f + ft->vars.n * sizeof *f->values);
1837 f->summaries = xmalloc (ft->n_summaries * sizeof *f->summaries);
1838 for (size_t i = 0; i < ft->n_summaries; i++)
1839 ctables_summary_init (&f->summaries[i], &ft->summaries[i]);
1844 ctables_freq_add (struct ctables_freqtab *ft, struct ctables_freq *f,
1845 const struct variable *var, const union value *value,
1848 for (size_t i = 0; i < ft->n_summaries; i++)
1849 ctables_summary_add (&f->summaries[i], &ft->summaries[i],
1850 var, value, weight);
1854 struct ctables_freq_sort_aux
1856 const struct ctables_table *t;
1857 enum pivot_axis_type a;
1861 ctables_freq_compare_3way (const void *a_, const void *b_, const void *aux_)
1863 const struct ctables_freq_sort_aux *aux = aux_;
1864 struct ctables_freq *const *ap = a_;
1865 struct ctables_freq *const *bp = b_;
1866 const struct ctables_freq *a = *ap;
1867 const struct ctables_freq *b = *bp;
1869 size_t a_idx = a->axes[aux->a].vaa_idx;
1870 size_t b_idx = b->axes[aux->a].vaa_idx;
1872 return a_idx < b_idx ? -1 : 1;
1874 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1875 for (size_t i = 0; i < va->n; i++)
1876 if (i != va->scale_idx)
1878 int cmp = value_compare_3way (&a->axes[aux->a].values[i],
1879 &b->axes[aux->a].values[i],
1880 var_get_width (va->vars[i]));
1890 For each ctables_table:
1891 For each combination of row vars:
1892 For each combination of column vars:
1893 For each combination of layer vars:
1895 Make a table of row values:
1896 Sort entries by row values
1897 Assign a 0-based index to each actual value
1898 Construct a dimension
1899 Make a table of column values
1900 Make a table of layer values
1902 Fill the table entry using the indexes from before.
1906 ctables_freqtab_insert (struct ctables_table *t,
1907 const struct ccase *c,
1908 size_t ir, size_t ic, size_t il,
1911 size_t ix[PIVOT_N_AXES] = {
1912 [PIVOT_AXIS_ROW] = ir,
1913 [PIVOT_AXIS_COLUMN] = ic,
1914 [PIVOT_AXIS_LAYER] = il,
1916 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
1919 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1921 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1922 hash = hash_int (ix[a], hash);
1923 for (size_t i = 0; i < va->n; i++)
1924 if (i != va->scale_idx)
1925 hash = value_hash (case_data (c, va->vars[i]),
1926 var_get_width (va->vars[i]), hash);
1929 struct ctables_freq *f;
1930 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &t->ft)
1932 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1934 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1935 if (f->axes[a].vaa_idx != ix[a])
1937 for (size_t i = 0; i < va->n; i++)
1938 if (i != va->scale_idx
1939 && !value_equal (case_data (c, va->vars[i]),
1940 &f->axes[a].values[i],
1941 var_get_width (va->vars[i])))
1950 f = xmalloc (sizeof *f);
1951 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1953 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1954 f->axes[a].vaa_idx = ix[a];
1955 f->axes[a].values = (va->n
1956 ? xnmalloc (va->n, sizeof *f->axes[a].values)
1958 for (size_t i = 0; i < va->n; i++)
1959 value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
1960 var_get_width (va->vars[i]));
1962 f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
1963 for (size_t i = 0; i < ss->n_summaries; i++)
1964 ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
1965 hmap_insert (&t->ft, &f->node, hash);
1968 for (size_t i = 0; i < ss->n_summaries; i++)
1969 ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
1970 case_data (c, ss->summary_var), weight);
1974 ctables_execute (struct dataset *ds, struct ctables *ct)
1976 for (size_t i = 0; i < ct->n_tables; i++)
1978 struct ctables_table *t = ct->tables[i];
1979 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1981 t->vaas[a] = enumerate_fts (a, t->axes[a]);
1984 struct var_array *va = xmalloc (sizeof *va);
1985 *va = (struct var_array) { .n = 0 };
1986 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
1989 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
1991 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
1992 if (!va->n_summaries)
1994 va->summaries = xmalloc (sizeof *va->summaries);
1995 va->n_summaries = 1;
1997 enum ctables_summary_function function
1998 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
1999 struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
2001 *va->summaries = (struct ctables_summary_spec) {
2002 .function = function,
2003 .format = ctables_summary_default_format (function, &var),
2004 .label = ctables_summary_default_label (function, 0),
2006 if (!va->summary_var)
2007 va->summary_var = va->vars[0];
2012 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2015 bool warn_on_invalid = true;
2016 double total_weight = 0;
2017 for (struct ccase *c = casereader_read (input); c;
2018 case_unref (c), c = casereader_read (input))
2020 double weight = dict_get_case_weight (dataset_dict (ds), c,
2022 total_weight += weight;
2024 for (size_t i = 0; i < ct->n_tables; i++)
2026 struct ctables_table *t = ct->tables[i];
2028 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2029 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2030 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2031 ctables_freqtab_insert (t, c, ir, ic, il, weight);
2034 casereader_destroy (input);
2036 for (size_t i = 0; i < ct->n_tables; i++)
2038 struct ctables_table *t = ct->tables[i];
2040 struct pivot_table *pt = pivot_table_create__ (
2042 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2043 : pivot_value_new_text (N_("Custom Tables"))),
2046 pivot_table_set_caption (
2047 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2049 pivot_table_set_caption (
2050 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2052 pivot_table_set_look (pt, ct->look);
2053 struct pivot_dimension *d[PIVOT_N_AXES];
2054 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2056 static const char *names[] = {
2057 [PIVOT_AXIS_ROW] = N_("Rows"),
2058 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2059 [PIVOT_AXIS_LAYER] = N_("Layers"),
2061 d[a] = (t->axes[a] || a == t->summary_axis
2062 ? pivot_dimension_create (pt, a, names[a])
2067 assert (t->axes[a]);
2069 struct ctables_freq **sorted = xnmalloc (t->ft.count, sizeof *sorted);
2071 struct ctables_freq *f;
2073 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2075 assert (n == t->ft.count);
2077 struct ctables_freq_sort_aux aux = { .t = t, .a = a };
2078 sort (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
2080 size_t max_depth = 0;
2081 for (size_t j = 0; j < t->vaas[a].n; j++)
2082 if (t->vaas[a].vas[j].n > max_depth)
2083 max_depth = t->vaas[a].vas[j].n;
2085 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2086 struct pivot_category *top = NULL;
2088 for (size_t j = 0; j < n; j++)
2090 struct ctables_freq *f = sorted[j];
2091 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2093 size_t n_common = 0;
2094 bool new_subtable = false;
2097 struct ctables_freq *prev = sorted[j - 1];
2098 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2100 for (; n_common < va->n; n_common++)
2101 if (n_common != va->scale_idx
2102 && !value_equal (&prev->axes[a].values[n_common],
2103 &f->axes[a].values[n_common],
2104 var_get_type (va->vars[n_common])))
2108 new_subtable = true;
2111 new_subtable = true;
2115 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2117 if (vlabel != CTVL_NONE)
2118 top = pivot_category_create_group__ (
2119 top, pivot_value_new_variable (va->vars[0]));
2121 if (n_common == va->n)
2123 f->axes[a].leaf = prev_leaf;
2127 for (size_t k = n_common; k < va->n; k++)
2129 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2131 struct pivot_value *label
2132 = (k != va->scale_idx
2133 ? pivot_value_new_var_value (va->vars[k],
2134 &f->axes[a].values[k])
2138 if (a == t->summary_axis)
2141 parent = pivot_category_create_group__ (parent, label);
2142 for (size_t m = 0; m < va->n_summaries; m++)
2144 int leaf = pivot_category_create_leaf (
2145 parent, pivot_value_new_text (va->summaries[m].label));
2152 /* This assertion is true as long as the summary axis
2153 is the axis where the summaries are displayed. */
2156 prev_leaf = pivot_category_create_leaf (parent, label);
2162 parent = pivot_category_create_group__ (parent, label);
2164 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2165 if (vlabel != CTVL_NONE)
2166 parent = pivot_category_create_group__ (
2167 parent, pivot_value_new_variable (va->vars[k + 1]));
2171 f->axes[a].leaf = prev_leaf;
2176 struct ctables_freq *f;
2177 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2179 const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2180 for (size_t j = 0; j < ss->n_summaries; j++)
2183 size_t n_dindexes = 0;
2185 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2188 int leaf = f->axes[a].leaf;
2189 if (a == t->summary_axis)
2191 dindexes[n_dindexes++] = leaf;
2194 double d = ctables_summary_value (&f->summaries[j], &ss->summaries[j]);
2195 struct pivot_value *value = pivot_value_new_number (d);
2196 value->numeric.format = ss->summaries[j].format;
2197 pivot_table_put (pt, dindexes, n_dindexes, value);
2201 pivot_table_submit (pt);
2205 for (size_t i = 0; i < ct->n_tables; i++)
2207 struct ctables_table *t = ct->tables[i];
2209 for (size_t j = 0; j < t->n_fts; j++)
2211 struct ctables_freqtab *ft = t->fts[j];
2212 struct ctables_freq *f, *next;
2213 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2215 hmap_delete (&ft->data, &f->node);
2216 for (size_t k = 0; k < ft->n_summaries; k++)
2217 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2218 free (f->summaries);
2219 for (size_t k = 0; k < ft->vars.n; k++)
2221 const struct variable *var = ft->vars.vars[k];
2222 value_destroy (&f->values[k], var_get_width (var));
2226 hmap_destroy (&ft->data);
2227 var_array_uninit (&ft->vars);
2234 return proc_commit (ds);
2238 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2240 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2241 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2242 enum settings_value_show tvars = settings_get_show_variables ();
2243 for (size_t i = 0; i < n_vars; i++)
2244 vlabels[i] = (enum ctables_vlabel) tvars;
2246 struct ctables *ct = xmalloc (sizeof *ct);
2247 *ct = (struct ctables) {
2248 .look = pivot_table_look_unshare (pivot_table_look_ref (
2249 pivot_table_look_get_default ())),
2251 .hide_threshold = 5,
2253 ct->look->omit_empty = false;
2255 if (!lex_force_match (lexer, T_SLASH))
2258 while (!lex_match_id (lexer, "TABLE"))
2260 if (lex_match_id (lexer, "FORMAT"))
2262 double widths[2] = { SYSMIS, SYSMIS };
2263 double units_per_inch = 72.0;
2265 while (lex_token (lexer) != T_SLASH)
2267 if (lex_match_id (lexer, "MINCOLWIDTH"))
2269 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2272 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2274 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2277 else if (lex_match_id (lexer, "UNITS"))
2279 lex_match (lexer, T_EQUALS);
2280 if (lex_match_id (lexer, "POINTS"))
2281 units_per_inch = 72.0;
2282 else if (lex_match_id (lexer, "INCHES"))
2283 units_per_inch = 1.0;
2284 else if (lex_match_id (lexer, "CM"))
2285 units_per_inch = 2.54;
2288 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2292 else if (lex_match_id (lexer, "EMPTY"))
2297 lex_match (lexer, T_EQUALS);
2298 if (lex_match_id (lexer, "ZERO"))
2300 /* Nothing to do. */
2302 else if (lex_match_id (lexer, "BLANK"))
2303 ct->zero = xstrdup ("");
2304 else if (lex_force_string (lexer))
2306 ct->zero = ss_xstrdup (lex_tokss (lexer));
2312 else if (lex_match_id (lexer, "MISSING"))
2314 lex_match (lexer, T_EQUALS);
2315 if (!lex_force_string (lexer))
2319 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2320 ? ss_xstrdup (lex_tokss (lexer))
2326 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2327 "UNITS", "EMPTY", "MISSING");
2332 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2333 && widths[0] > widths[1])
2335 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2339 for (size_t i = 0; i < 2; i++)
2340 if (widths[i] != SYSMIS)
2342 int *wr = ct->look->width_ranges[TABLE_HORZ];
2343 wr[i] = widths[i] / units_per_inch * 96.0;
2348 else if (lex_match_id (lexer, "VLABELS"))
2350 if (!lex_force_match_id (lexer, "VARIABLES"))
2352 lex_match (lexer, T_EQUALS);
2354 struct variable **vars;
2356 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2360 if (!lex_force_match_id (lexer, "DISPLAY"))
2365 lex_match (lexer, T_EQUALS);
2367 enum ctables_vlabel vlabel;
2368 if (lex_match_id (lexer, "DEFAULT"))
2369 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2370 else if (lex_match_id (lexer, "NAME"))
2372 else if (lex_match_id (lexer, "LABEL"))
2373 vlabel = CTVL_LABEL;
2374 else if (lex_match_id (lexer, "BOTH"))
2376 else if (lex_match_id (lexer, "NONE"))
2380 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2386 for (size_t i = 0; i < n_vars; i++)
2387 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2390 else if (lex_match_id (lexer, "MRSETS"))
2392 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2394 lex_match (lexer, T_EQUALS);
2395 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2398 else if (lex_match_id (lexer, "SMISSING"))
2400 if (lex_match_id (lexer, "VARIABLE"))
2401 ct->smissing_listwise = false;
2402 else if (lex_match_id (lexer, "LISTWISE"))
2403 ct->smissing_listwise = true;
2406 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2411 else if (lex_match_id (lexer, "WEIGHT"))
2413 if (!lex_force_match_id (lexer, "VARIABLE"))
2415 lex_match (lexer, T_EQUALS);
2416 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2417 if (!ct->base_weight)
2420 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2422 if (!lex_force_match_id (lexer, "COUNT"))
2424 lex_match (lexer, T_EQUALS);
2425 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2427 ct->hide_threshold = lex_integer (lexer);
2432 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2433 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2434 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2438 if (!lex_force_match (lexer, T_SLASH))
2442 size_t allocated_tables = 0;
2445 if (ct->n_tables >= allocated_tables)
2446 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2447 sizeof *ct->tables);
2449 struct ctables_table *t = xmalloc (sizeof *t);
2450 *t = (struct ctables_table) {
2451 .ft = HMAP_INITIALIZER (t->ft),
2452 .slabels_position = PIVOT_AXIS_COLUMN,
2453 .slabels_visible = true,
2454 .row_labels = CTLP_NORMAL,
2455 .col_labels = CTLP_NORMAL,
2456 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2457 sizeof *t->categories),
2458 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2461 ct->tables[ct->n_tables++] = t;
2463 lex_match (lexer, T_EQUALS);
2464 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2466 if (lex_match (lexer, T_BY))
2468 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2469 ct, t, PIVOT_AXIS_COLUMN))
2472 if (lex_match (lexer, T_BY))
2474 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2475 ct, t, PIVOT_AXIS_LAYER))
2480 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2481 && !t->axes[PIVOT_AXIS_LAYER])
2483 lex_error (lexer, _("At least one variable must be specified."));
2487 const struct ctables_axis *scales[PIVOT_N_AXES];
2488 size_t n_scales = 0;
2489 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2491 scales[a] = find_scale (t->axes[a]);
2497 msg (SE, _("Scale variables may appear only on one axis."));
2498 if (scales[PIVOT_AXIS_ROW])
2499 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2500 _("This scale variable appears on the rows axis."));
2501 if (scales[PIVOT_AXIS_COLUMN])
2502 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2503 _("This scale variable appears on the columns axis."));
2504 if (scales[PIVOT_AXIS_LAYER])
2505 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2506 _("This scale variable appears on the layer axis."));
2510 const struct ctables_axis *summaries[PIVOT_N_AXES];
2511 size_t n_summaries = 0;
2512 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2514 summaries[a] = (scales[a]
2516 : find_categorical_summary_spec (t->axes[a]));
2520 if (n_summaries > 1)
2522 msg (SE, _("Summaries may appear only on one axis."));
2523 if (summaries[PIVOT_AXIS_ROW])
2524 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2525 _("This variable on the rows axis has a summary."));
2526 if (summaries[PIVOT_AXIS_COLUMN])
2527 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2528 _("This variable on the columns axis has a summary."));
2529 if (summaries[PIVOT_AXIS_LAYER])
2530 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2531 _("This variable on the layers axis has a summary."));
2534 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2535 if (n_summaries ? summaries[a] : t->axes[a])
2537 t->summary_axis = a;
2541 if (lex_token (lexer) == T_ENDCMD)
2543 if (!lex_force_match (lexer, T_SLASH))
2546 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2548 if (lex_match_id (lexer, "SLABELS"))
2550 while (lex_token (lexer) != T_SLASH)
2552 if (lex_match_id (lexer, "POSITION"))
2554 lex_match (lexer, T_EQUALS);
2555 if (lex_match_id (lexer, "COLUMN"))
2556 t->slabels_position = PIVOT_AXIS_COLUMN;
2557 else if (lex_match_id (lexer, "ROW"))
2558 t->slabels_position = PIVOT_AXIS_ROW;
2559 else if (lex_match_id (lexer, "LAYER"))
2560 t->slabels_position = PIVOT_AXIS_LAYER;
2563 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2567 else if (lex_match_id (lexer, "VISIBLE"))
2569 lex_match (lexer, T_EQUALS);
2570 if (!parse_bool (lexer, &t->slabels_visible))
2575 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2580 else if (lex_match_id (lexer, "CLABELS"))
2582 while (lex_token (lexer) != T_SLASH)
2584 if (lex_match_id (lexer, "AUTO"))
2585 t->row_labels = t->col_labels = CTLP_NORMAL;
2586 else if (lex_match_id (lexer, "ROWLABELS"))
2588 lex_match (lexer, T_EQUALS);
2589 if (lex_match_id (lexer, "OPPOSITE"))
2590 t->row_labels = CTLP_OPPOSITE;
2591 else if (lex_match_id (lexer, "LAYER"))
2592 t->row_labels = CTLP_LAYER;
2595 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2599 else if (lex_match_id (lexer, "COLLABELS"))
2601 lex_match (lexer, T_EQUALS);
2602 if (lex_match_id (lexer, "OPPOSITE"))
2603 t->col_labels = CTLP_OPPOSITE;
2604 else if (lex_match_id (lexer, "LAYER"))
2605 t->col_labels = CTLP_LAYER;
2608 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2614 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2620 else if (lex_match_id (lexer, "CRITERIA"))
2622 if (!lex_force_match_id (lexer, "CILEVEL"))
2624 lex_match (lexer, T_EQUALS);
2626 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2628 t->cilevel = lex_number (lexer);
2631 else if (lex_match_id (lexer, "CATEGORIES"))
2633 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2636 else if (lex_match_id (lexer, "TITLES"))
2641 if (lex_match_id (lexer, "CAPTION"))
2642 textp = &t->caption;
2643 else if (lex_match_id (lexer, "CORNER"))
2645 else if (lex_match_id (lexer, "TITLE"))
2649 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2652 lex_match (lexer, T_EQUALS);
2654 struct string s = DS_EMPTY_INITIALIZER;
2655 while (lex_is_string (lexer))
2657 if (!ds_is_empty (&s))
2658 ds_put_byte (&s, ' ');
2659 ds_put_substring (&s, lex_tokss (lexer));
2663 *textp = ds_steal_cstr (&s);
2665 while (lex_token (lexer) != T_SLASH
2666 && lex_token (lexer) != T_ENDCMD);
2668 else if (lex_match_id (lexer, "SIGTEST"))
2672 t->chisq = xmalloc (sizeof *t->chisq);
2673 *t->chisq = (struct ctables_chisq) {
2675 .include_mrsets = true,
2676 .all_visible = true,
2682 if (lex_match_id (lexer, "TYPE"))
2684 lex_match (lexer, T_EQUALS);
2685 if (!lex_force_match_id (lexer, "CHISQUARE"))
2688 else if (lex_match_id (lexer, "ALPHA"))
2690 lex_match (lexer, T_EQUALS);
2691 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2693 t->chisq->alpha = lex_number (lexer);
2696 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2698 lex_match (lexer, T_EQUALS);
2699 if (parse_bool (lexer, &t->chisq->include_mrsets))
2702 else if (lex_match_id (lexer, "CATEGORIES"))
2704 lex_match (lexer, T_EQUALS);
2705 if (lex_match_id (lexer, "ALLVISIBLE"))
2706 t->chisq->all_visible = true;
2707 else if (lex_match_id (lexer, "SUBTOTALS"))
2708 t->chisq->all_visible = false;
2711 lex_error_expecting (lexer,
2712 "ALLVISIBLE", "SUBTOTALS");
2718 lex_error_expecting (lexer, "TYPE", "ALPHA",
2719 "INCLUDEMRSETS", "CATEGORIES");
2723 while (lex_token (lexer) != T_SLASH
2724 && lex_token (lexer) != T_ENDCMD);
2726 else if (lex_match_id (lexer, "COMPARETEST"))
2730 t->pairwise = xmalloc (sizeof *t->pairwise);
2731 *t->pairwise = (struct ctables_pairwise) {
2733 .alpha = { .05, .05 },
2734 .adjust = BONFERRONI,
2735 .include_mrsets = true,
2736 .meansvariance_allcats = true,
2737 .all_visible = true,
2746 if (lex_match_id (lexer, "TYPE"))
2748 lex_match (lexer, T_EQUALS);
2749 if (lex_match_id (lexer, "PROP"))
2750 t->pairwise->type = PROP;
2751 else if (lex_match_id (lexer, "MEAN"))
2752 t->pairwise->type = MEAN;
2755 lex_error_expecting (lexer, "PROP", "MEAN");
2759 else if (lex_match_id (lexer, "ALPHA"))
2761 lex_match (lexer, T_EQUALS);
2763 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2765 double a0 = lex_number (lexer);
2768 lex_match (lexer, T_COMMA);
2769 if (lex_is_number (lexer))
2771 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2773 double a1 = lex_number (lexer);
2776 t->pairwise->alpha[0] = MIN (a0, a1);
2777 t->pairwise->alpha[1] = MAX (a0, a1);
2780 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2782 else if (lex_match_id (lexer, "ADJUST"))
2784 lex_match (lexer, T_EQUALS);
2785 if (lex_match_id (lexer, "BONFERRONI"))
2786 t->pairwise->adjust = BONFERRONI;
2787 else if (lex_match_id (lexer, "BH"))
2788 t->pairwise->adjust = BH;
2789 else if (lex_match_id (lexer, "NONE"))
2790 t->pairwise->adjust = 0;
2793 lex_error_expecting (lexer, "BONFERRONI", "BH",
2798 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2800 lex_match (lexer, T_EQUALS);
2801 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2804 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2806 lex_match (lexer, T_EQUALS);
2807 if (lex_match_id (lexer, "ALLCATS"))
2808 t->pairwise->meansvariance_allcats = true;
2809 else if (lex_match_id (lexer, "TESTEDCATS"))
2810 t->pairwise->meansvariance_allcats = false;
2813 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2817 else if (lex_match_id (lexer, "CATEGORIES"))
2819 lex_match (lexer, T_EQUALS);
2820 if (lex_match_id (lexer, "ALLVISIBLE"))
2821 t->pairwise->all_visible = true;
2822 else if (lex_match_id (lexer, "SUBTOTALS"))
2823 t->pairwise->all_visible = false;
2826 lex_error_expecting (lexer, "ALLVISIBLE",
2831 else if (lex_match_id (lexer, "MERGE"))
2833 lex_match (lexer, T_EQUALS);
2834 if (!parse_bool (lexer, &t->pairwise->merge))
2837 else if (lex_match_id (lexer, "STYLE"))
2839 lex_match (lexer, T_EQUALS);
2840 if (lex_match_id (lexer, "APA"))
2841 t->pairwise->apa_style = true;
2842 else if (lex_match_id (lexer, "SIMPLE"))
2843 t->pairwise->apa_style = false;
2846 lex_error_expecting (lexer, "APA", "SIMPLE");
2850 else if (lex_match_id (lexer, "SHOWSIG"))
2852 lex_match (lexer, T_EQUALS);
2853 if (!parse_bool (lexer, &t->pairwise->show_sig))
2858 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2859 "INCLUDEMRSETS", "MEANSVARIANCE",
2860 "CATEGORIES", "MERGE", "STYLE",
2865 while (lex_token (lexer) != T_SLASH
2866 && lex_token (lexer) != T_ENDCMD);
2870 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2871 "CRITERIA", "CATEGORIES", "TITLES",
2872 "SIGTEST", "COMPARETEST");
2877 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2879 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2884 while (lex_token (lexer) != T_ENDCMD);
2886 bool ok = ctables_execute (ds, ct);
2887 ctables_destroy (ct);
2888 return ok ? CMD_SUCCESS : CMD_FAILURE;
2891 ctables_destroy (ct);