1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/mrset.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/message.h"
34 #include "libpspp/string-array.h"
35 #include "math/moments.h"
36 #include "output/pivot-table.h"
38 #include "gl/minmax.h"
39 #include "gl/xalloc.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) (msgid)
47 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
48 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
49 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
50 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
54 - unweighted summaries (U*)
55 - lower confidence limits (*.LCL)
56 - upper confidence limits (*.UCL)
57 - standard error (*.SE)
60 /* All variables. */ \
61 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
62 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
63 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
64 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
85 /* Scale variables, totals, and subtotals. */ \
86 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
87 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
97 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
98 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
99 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
100 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
101 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
103 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
105 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
106 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
107 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
108 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
110 /* Multiple response sets. */ \
111 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
112 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
134 enum ctables_summary_function
136 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
143 N_CTSF_FUNCTIONS = SUMMARIES
147 enum ctables_domain_type
149 /* Within a section, where stacked variables divide one section from
151 CTDT_TABLE, /* All layers of a whole section. */
152 CTDT_LAYER, /* One layer within a section. */
153 CTDT_LAYERROW, /* Row in one layer within a section. */
154 CTDT_LAYERCOL, /* Column in one layer within a section. */
156 /* Within a subtable, where a subtable pairs an innermost row variable with
157 an innermost column variable within a single layer. */
158 CTDT_SUBTABLE, /* Whole subtable. */
159 CTDT_ROW, /* Row within a subtable. */
160 CTDT_COL, /* Column within a subtable. */
164 struct ctables_domain
166 struct hmap_node node;
168 const struct ctables_freq *example;
176 /* In struct ctables's 'ft' hmap. Indexed by all the values in all the
177 axes (except the scalar variable, if any). */
178 struct hmap_node node;
180 /* The domains that contains this cell. */
181 struct ctables_domain *domains[N_CTDTS];
191 union ctables_summary *summaries;
196 struct pivot_table_look *look;
198 /* If this is NULL, zeros are displayed using the normal print format.
199 Otherwise, this string is displayed. */
202 /* If this is NULL, missing values are displayed using the normal print
203 format. Otherwise, this string is displayed. */
206 /* Indexed by variable dictionary index. */
207 enum ctables_vlabel *vlabels;
209 bool mrsets_count_duplicates; /* MRSETS. */
210 bool smissing_listwise; /* SMISSING. */
211 struct variable *base_weight; /* WEIGHT. */
212 int hide_threshold; /* HIDESMALLCOUNTS. */
214 struct ctables_table **tables;
218 struct ctables_postcompute
220 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
221 const char *name; /* Name, without leading &. */
223 struct ctables_postcompute_expr *expr;
226 bool hide_source_cats;
229 struct ctables_postcompute_expr
231 enum ctables_postcompute_op
239 /* XXX SUBTOTAL and HSUBTOTAL */
252 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
257 XXX what about string ranges? */
260 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
261 struct ctables_postcompute_expr *subs[2];
265 enum ctables_label_position
274 struct variable **vars;
277 size_t *domains[N_CTDTS];
278 size_t n_domains[N_CTDTS];
280 struct ctables_summary_spec *summaries;
282 struct variable *summary_var;
287 struct var_array *vas;
293 struct ctables_axis *axes[PIVOT_N_AXES];
294 struct var_array2 vaas[PIVOT_N_AXES];
295 enum pivot_axis_type summary_axis;
297 struct hmap domains[N_CTDTS];
299 enum pivot_axis_type slabels_position;
300 bool slabels_visible;
302 enum ctables_label_position row_labels;
303 enum ctables_label_position col_labels;
305 /* Indexed by variable dictionary index. */
306 struct ctables_categories **categories;
315 struct ctables_chisq *chisq;
316 struct ctables_pairwise *pairwise;
324 struct variable *var;
325 const struct mrset *mrset;
329 static const struct fmt_spec *
330 ctables_var_get_print_format (const struct ctables_var *var)
332 return (var->is_mrset
333 ? var_get_print_format (var->mrset->vars[0])
334 : var_get_print_format (var->var));
338 ctables_var_name (const struct ctables_var *var)
340 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
343 struct ctables_categories
347 /* Explicit categories. */
348 struct ctables_cat_value *values;
351 /* Implicit categories. */
353 bool include_missing;
354 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
355 enum ctables_summary_function sort_func;
356 struct variable *sort_func_var;
364 /* Empty categories. */
368 struct ctables_cat_value
370 enum ctables_cat_value_type
384 double number; /* CCVT_NUMBER. */
385 char *string; /* CCVT_STRING. */
386 double range[2]; /* CCVT_RANGE. */
387 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
392 ctables_cat_value_uninit (struct ctables_cat_value *cv)
411 free (cv->subtotal_label);
416 ctables_categories_unref (struct ctables_categories *c)
421 assert (c->n_refs > 0);
425 for (size_t i = 0; i < c->n_values; i++)
426 ctables_cat_value_uninit (&c->values[i]);
428 free (c->total_label);
432 /* Chi-square test (SIGTEST). */
440 /* Pairwise comparison test (COMPARETEST). */
441 struct ctables_pairwise
443 enum { PROP, MEAN } type;
446 bool meansvariance_allcats;
448 enum { BONFERRONI = 1, BH } adjust;
472 struct ctables_var var;
474 struct ctables_summary_spec *summaries;
476 size_t allocated_summaries;
480 struct ctables_axis *subs[2];
483 struct msg_location *loc;
486 static void ctables_axis_destroy (struct ctables_axis *);
495 enum ctables_function_availability
497 CTFA_ALL, /* Any variables. */
498 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
499 CTFA_MRSETS, /* Only multiple-response sets */
502 struct ctables_summary_spec
504 enum ctables_summary_function function;
505 double percentile; /* CTSF_PTILE only. */
507 struct fmt_spec format; /* XXX extra CTABLES formats */
511 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
518 parse_col_width (struct lexer *lexer, const char *name, double *width)
520 lex_match (lexer, T_EQUALS);
521 if (lex_match_id (lexer, "DEFAULT"))
523 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
525 *width = lex_number (lexer);
535 parse_bool (struct lexer *lexer, bool *b)
537 if (lex_match_id (lexer, "NO"))
539 else if (lex_match_id (lexer, "YES"))
543 lex_error_expecting (lexer, "YES", "NO");
549 static enum ctables_function_availability
550 ctables_function_availability (enum ctables_summary_function f)
552 static enum ctables_function_availability availability[] = {
553 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
558 return availability[f];
562 parse_ctables_summary_function (struct lexer *lexer,
563 enum ctables_summary_function *f)
567 enum ctables_summary_function function;
568 struct substring name;
570 static struct pair names[] = {
571 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
572 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
575 /* The .COUNT suffix may be omitted. */
576 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
577 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
578 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
579 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
580 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
581 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
582 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
586 if (!lex_force_id (lexer))
589 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
590 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
592 *f = names[i].function;
597 lex_error (lexer, _("Expecting summary function name."));
602 ctables_axis_destroy (struct ctables_axis *axis)
610 for (size_t i = 0; i < axis->n_summaries; i++)
611 ctables_summary_spec_uninit (&axis->summaries[i]);
612 free (axis->summaries);
617 ctables_axis_destroy (axis->subs[0]);
618 ctables_axis_destroy (axis->subs[1]);
621 msg_location_destroy (axis->loc);
625 static struct ctables_axis *
626 ctables_axis_new_nonterminal (enum ctables_axis_op op,
627 struct ctables_axis *sub0,
628 struct ctables_axis *sub1,
629 struct lexer *lexer, int start_ofs)
631 struct ctables_axis *axis = xmalloc (sizeof *axis);
632 *axis = (struct ctables_axis) {
634 .subs = { sub0, sub1 },
635 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
640 struct ctables_axis_parse_ctx
643 struct dictionary *dict;
645 struct ctables_table *t;
648 static struct fmt_spec
649 ctables_summary_default_format (enum ctables_summary_function function,
650 const struct ctables_var *var)
652 static const enum ctables_format default_formats[] = {
653 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
657 switch (default_formats[function])
660 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
663 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
666 return *ctables_var_get_print_format (var);
674 ctables_summary_default_label (enum ctables_summary_function function,
677 static const char *default_labels[] = {
678 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
683 return (function == CTSF_PTILE
684 ? xasprintf (_("Percentile %.2f"), percentile)
685 : xstrdup (gettext (default_labels[function])));
689 ctables_summary_function_name (enum ctables_summary_function function)
691 static const char *names[] = {
692 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
696 return names[function];
700 add_summary_spec (struct ctables_axis *axis,
701 enum ctables_summary_function function, double percentile,
702 const char *label, const struct fmt_spec *format,
703 const struct msg_location *loc)
705 if (axis->op == CTAO_VAR)
707 if (axis->n_summaries >= axis->allocated_summaries)
708 axis->summaries = x2nrealloc (axis->summaries,
709 &axis->allocated_summaries,
710 sizeof *axis->summaries);
712 const char *function_name = ctables_summary_function_name (function);
713 const char *var_name = ctables_var_name (&axis->var);
714 switch (ctables_function_availability (function))
717 if (!axis->var.is_mrset)
719 msg_at (SE, loc, _("Summary function %s applies only to multiple "
720 "response sets."), function_name);
721 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
731 _("Summary function %s applies only to scale variables."),
733 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
743 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
744 *dst = (struct ctables_summary_spec) {
745 .function = function,
746 .percentile = percentile,
747 .label = xstrdup (label),
748 .format = (format ? *format
749 : ctables_summary_default_format (function, &axis->var)),
755 for (size_t i = 0; i < 2; i++)
756 if (!add_summary_spec (axis->subs[i], function, percentile, label,
763 static struct ctables_axis *ctables_axis_parse_stack (
764 struct ctables_axis_parse_ctx *);
767 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
768 struct ctables_var *var)
770 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
772 *var = (struct ctables_var) {
774 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
778 lex_error (lexer, _("'%s' does not name a multiple-response set "
779 "in the active file dictionary."),
780 lex_tokcstr (lexer));
788 *var = (struct ctables_var) {
790 .var = parse_variable (lexer, dict),
792 return var->var != NULL;
796 static struct ctables_axis *
797 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
799 if (lex_match (ctx->lexer, T_LPAREN))
801 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
802 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
804 ctables_axis_destroy (sub);
810 if (!lex_force_id (ctx->lexer))
813 int start_ofs = lex_ofs (ctx->lexer);
814 struct ctables_var var;
815 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
818 struct ctables_axis *axis = xmalloc (sizeof *axis);
819 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
821 /* XXX should figure out default measures by reading data */
822 axis->scale = (var.is_mrset ? false
823 : lex_match_phrase (ctx->lexer, "[S]") ? true
824 : lex_match_phrase (ctx->lexer, "[C]") ? false
825 : var_get_measure (var.var) == MEASURE_SCALE);
826 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
827 lex_ofs (ctx->lexer) - 1);
831 static struct ctables_axis *
832 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
834 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
835 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
840 int start_ofs = lex_ofs (ctx->lexer);
842 /* Parse function. */
843 enum ctables_summary_function function;
844 if (!parse_ctables_summary_function (ctx->lexer, &function))
847 /* Parse percentile. */
848 double percentile = 0;
849 if (function == CTSF_PTILE)
851 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
853 percentile = lex_number (ctx->lexer);
854 lex_get (ctx->lexer);
859 if (lex_is_string (ctx->lexer))
861 label = ss_xstrdup (lex_tokss (ctx->lexer));
862 lex_get (ctx->lexer);
865 label = ctables_summary_default_label (function, percentile);
868 struct fmt_spec format;
869 const struct fmt_spec *formatp;
870 if (lex_token (ctx->lexer) == T_ID)
872 if (!parse_format_specifier (ctx->lexer, &format)
873 || !fmt_check_output (&format)
874 || !fmt_check_type_compat (&format, VAL_NUMERIC))
884 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
885 lex_ofs (ctx->lexer) - 1);
886 add_summary_spec (sub, function, percentile, label, formatp, loc);
888 msg_location_destroy (loc);
890 lex_match (ctx->lexer, T_COMMA);
892 while (!lex_match (ctx->lexer, T_RBRACK));
897 ctables_axis_destroy (sub);
901 static const struct ctables_axis *
902 find_scale (const struct ctables_axis *axis)
906 else if (axis->op == CTAO_VAR)
910 assert (!axis->var.is_mrset);
918 for (size_t i = 0; i < 2; i++)
920 const struct ctables_axis *scale = find_scale (axis->subs[i]);
928 static const struct ctables_axis *
929 find_categorical_summary_spec (const struct ctables_axis *axis)
933 else if (axis->op == CTAO_VAR)
934 return !axis->scale && axis->n_summaries ? axis : NULL;
937 for (size_t i = 0; i < 2; i++)
939 const struct ctables_axis *sum
940 = find_categorical_summary_spec (axis->subs[i]);
948 static struct ctables_axis *
949 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
951 int start_ofs = lex_ofs (ctx->lexer);
952 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
956 while (lex_match (ctx->lexer, T_GT))
958 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
962 struct ctables_axis *nest = ctables_axis_new_nonterminal (
963 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
965 const struct ctables_axis *outer_scale = find_scale (lhs);
966 const struct ctables_axis *inner_scale = find_scale (rhs);
967 if (outer_scale && inner_scale)
969 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
970 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
971 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
972 ctables_axis_destroy (nest);
976 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
979 msg_at (SE, nest->loc,
980 _("Summaries may only be requested for categorical variables "
981 "at the innermost nesting level."));
982 msg_at (SN, outer_sum->loc,
983 _("This outer categorical variable has a summary."));
984 ctables_axis_destroy (nest);
994 static struct ctables_axis *
995 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
997 int start_ofs = lex_ofs (ctx->lexer);
998 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1002 while (lex_match (ctx->lexer, T_PLUS))
1004 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1008 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1009 ctx->lexer, start_ofs);
1016 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1017 struct ctables *ct, struct ctables_table *t,
1018 enum pivot_axis_type a)
1020 if (lex_token (lexer) == T_BY
1021 || lex_token (lexer) == T_SLASH
1022 || lex_token (lexer) == T_ENDCMD)
1025 struct ctables_axis_parse_ctx ctx = {
1031 t->axes[a] = ctables_axis_parse_stack (&ctx);
1032 return t->axes[a] != NULL;
1036 ctables_chisq_destroy (struct ctables_chisq *chisq)
1042 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1048 ctables_table_destroy (struct ctables_table *t)
1053 for (size_t i = 0; i < t->n_categories; i++)
1054 ctables_categories_unref (t->categories[i]);
1055 free (t->categories);
1057 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1058 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1059 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1063 ctables_chisq_destroy (t->chisq);
1064 ctables_pairwise_destroy (t->pairwise);
1069 ctables_destroy (struct ctables *ct)
1074 pivot_table_look_unref (ct->look);
1078 for (size_t i = 0; i < ct->n_tables; i++)
1079 ctables_table_destroy (ct->tables[i]);
1084 static struct ctables_cat_value
1085 ccvt_range (double low, double high)
1087 return (struct ctables_cat_value) {
1089 .range = { low, high }
1094 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1095 struct ctables_table *t)
1097 if (!lex_match_id (lexer, "VARIABLES"))
1099 lex_match (lexer, T_EQUALS);
1101 struct variable **vars;
1103 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1106 struct ctables_categories *c = xmalloc (sizeof *c);
1107 *c = (struct ctables_categories) { .n_refs = n_vars };
1108 for (size_t i = 0; i < n_vars; i++)
1110 struct ctables_categories **cp
1111 = &t->categories[var_get_dict_index (vars[i])];
1112 ctables_categories_unref (*cp);
1117 if (lex_match (lexer, T_LBRACK))
1119 size_t allocated_values = 0;
1122 if (c->n_values >= allocated_values)
1123 c->values = x2nrealloc (c->values, &allocated_values,
1126 struct ctables_cat_value *v = &c->values[c->n_values];
1127 if (lex_match_id (lexer, "OTHERNM"))
1128 v->type = CCVT_OTHERNM;
1129 else if (lex_match_id (lexer, "MISSING"))
1130 v->type = CCVT_MISSING;
1131 else if (lex_match_id (lexer, "SUBTOTAL"))
1132 *v = (struct ctables_cat_value)
1133 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1134 else if (lex_match_id (lexer, "HSUBTOTAL"))
1135 *v = (struct ctables_cat_value)
1136 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1137 else if (lex_match_id (lexer, "LO"))
1139 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1141 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1144 else if (lex_is_number (lexer))
1146 double number = lex_number (lexer);
1148 if (lex_match_id (lexer, "THRU"))
1150 v->type = CCVT_RANGE;
1151 v->range[0] = number;
1152 if (lex_match_id (lexer, "HI"))
1153 *v = ccvt_range (number, DBL_MAX);
1156 if (!lex_force_num (lexer))
1158 *v = ccvt_range (number, lex_number (lexer));
1163 *v = (struct ctables_cat_value) {
1164 .type = CCVT_NUMBER,
1168 else if (lex_is_string (lexer))
1170 *v = (struct ctables_cat_value) {
1171 .type = CCVT_STRING,
1172 .string = ss_xstrdup (lex_tokss (lexer)),
1178 lex_error (lexer, NULL);
1182 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1183 && lex_match (lexer, T_EQUALS))
1185 if (!lex_force_string (lexer))
1188 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1193 lex_match (lexer, T_COMMA);
1195 while (!lex_match (lexer, T_RBRACK));
1198 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1200 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1202 lex_match (lexer, T_EQUALS);
1203 if (lex_match_id (lexer, "A"))
1204 c->sort_ascending = true;
1205 else if (lex_match_id (lexer, "D"))
1206 c->sort_ascending = false;
1209 lex_error_expecting (lexer, "A", "D");
1213 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1215 lex_match (lexer, T_EQUALS);
1216 if (lex_match_id (lexer, "VALUE"))
1217 c->key = CTCS_VALUE;
1218 else if (lex_match_id (lexer, "LABEL"))
1219 c->key = CTCS_LABEL;
1222 c->key = CTCS_FUNCTION;
1223 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1226 if (lex_match (lexer, T_LPAREN))
1228 c->sort_func_var = parse_variable (lexer, dict);
1229 if (!c->sort_func_var)
1232 if (c->sort_func == CTSF_PTILE)
1234 lex_match (lexer, T_COMMA);
1235 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1237 c->percentile = lex_number (lexer);
1241 if (!lex_force_match (lexer, T_RPAREN))
1244 else if (ctables_function_availability (c->sort_func)
1247 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1252 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1254 lex_match (lexer, T_EQUALS);
1255 if (lex_match_id (lexer, "INCLUDE"))
1256 c->include_missing = true;
1257 else if (lex_match_id (lexer, "EXCLUDE"))
1258 c->include_missing = false;
1261 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1265 else if (lex_match_id (lexer, "TOTAL"))
1267 lex_match (lexer, T_EQUALS);
1268 if (!parse_bool (lexer, &c->show_totals))
1271 else if (lex_match_id (lexer, "LABEL"))
1273 lex_match (lexer, T_EQUALS);
1274 if (!lex_force_string (lexer))
1276 free (c->total_label);
1277 c->total_label = ss_xstrdup (lex_tokss (lexer));
1280 else if (lex_match_id (lexer, "POSITION"))
1282 lex_match (lexer, T_EQUALS);
1283 if (lex_match_id (lexer, "BEFORE"))
1284 c->totals_before = true;
1285 else if (lex_match_id (lexer, "AFTER"))
1286 c->totals_before = false;
1289 lex_error_expecting (lexer, "BEFORE", "AFTER");
1293 else if (lex_match_id (lexer, "EMPTY"))
1295 lex_match (lexer, T_EQUALS);
1296 if (lex_match_id (lexer, "INCLUDE"))
1297 c->show_empty = true;
1298 else if (lex_match_id (lexer, "EXCLUDE"))
1299 c->show_empty = false;
1302 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1309 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1310 "TOTAL", "LABEL", "POSITION", "EMPTY");
1312 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1320 var_array_uninit (struct var_array *va)
1327 var_array2_uninit (struct var_array2 *vaa)
1331 for (size_t i = 0; i < vaa->n; i++)
1332 var_array_uninit (&vaa->vas[i]);
1337 static struct var_array2
1338 nest_fts (struct var_array2 va0, struct var_array2 va1)
1345 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1346 for (size_t i = 0; i < va0.n; i++)
1347 for (size_t j = 0; j < va1.n; j++)
1349 const struct var_array *a = &va0.vas[i];
1350 const struct var_array *b = &va1.vas[j];
1352 size_t allocate = a->n + b->n;
1353 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1354 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1356 for (size_t k = 0; k < a->n; k++)
1357 vars[n++] = a->vars[k];
1358 for (size_t k = 0; k < b->n; k++)
1359 vars[n++] = b->vars[k];
1360 assert (n == allocate);
1362 const struct var_array *summary_src;
1363 if (!a->summary_var)
1365 else if (!b->summary_var)
1369 vaa.vas[vaa.n++] = (struct var_array) {
1371 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1372 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1375 .summaries = summary_src->summaries,
1376 .n_summaries = summary_src->n_summaries,
1377 .summary_var = summary_src->summary_var,
1380 var_array2_uninit (&va0);
1381 var_array2_uninit (&va1);
1385 static struct var_array2
1386 stack_fts (struct var_array2 va0, struct var_array2 va1)
1388 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1389 for (size_t i = 0; i < va0.n; i++)
1390 vaa.vas[vaa.n++] = va0.vas[i];
1391 for (size_t i = 0; i < va1.n; i++)
1392 vaa.vas[vaa.n++] = va1.vas[i];
1393 assert (vaa.n == va0.n + va1.n);
1399 static struct var_array2
1400 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1403 return (struct var_array2) { .n = 0 };
1408 assert (!a->var.is_mrset);
1410 struct variable **vars = xmalloc (sizeof *vars);
1413 struct var_array *va = xmalloc (sizeof *va);
1414 *va = (struct var_array) {
1417 .scale_idx = a->scale ? 0 : SIZE_MAX,
1419 if (a->n_summaries || a->scale)
1421 va->summaries = a->summaries;
1422 va->n_summaries = a->n_summaries;
1423 va->summary_var = a->var.var;
1425 return (struct var_array2) { .vas = va, .n = 1 };
1428 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1429 enumerate_fts (axis_type, a->subs[1]));
1432 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1433 enumerate_fts (axis_type, a->subs[1]));
1439 union ctables_summary
1441 /* COUNT, VALIDN, TOTALN. */
1448 /* MINIMUM, MAXIMUM, RANGE. */
1455 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1456 struct moments1 *moments;
1458 /* XXX percentiles, median, mode, multiple response */
1462 ctables_summary_init (union ctables_summary *s,
1463 const struct ctables_summary_spec *ss)
1465 switch (ss->function)
1469 case CTSF_ROWPCT_COUNT:
1470 case CTSF_COLPCT_COUNT:
1471 case CTSF_TABLEPCT_COUNT:
1472 case CTSF_SUBTABLEPCT_COUNT:
1473 case CTSF_LAYERPCT_COUNT:
1474 case CTSF_LAYERROWPCT_COUNT:
1475 case CTSF_LAYERCOLPCT_COUNT:
1476 case CTSF_ROWPCT_VALIDN:
1477 case CTSF_COLPCT_VALIDN:
1478 case CTSF_TABLEPCT_VALIDN:
1479 case CTSF_SUBTABLEPCT_VALIDN:
1480 case CTSF_LAYERPCT_VALIDN:
1481 case CTSF_LAYERROWPCT_VALIDN:
1482 case CTSF_LAYERCOLPCT_VALIDN:
1483 case CTSF_ROWPCT_TOTALN:
1484 case CTSF_COLPCT_TOTALN:
1485 case CTSF_TABLEPCT_TOTALN:
1486 case CTSF_SUBTABLEPCT_TOTALN:
1487 case CTSF_LAYERPCT_TOTALN:
1488 case CTSF_LAYERROWPCT_TOTALN:
1489 case CTSF_LAYERCOLPCT_TOTALN:
1494 s->missing = s->valid = 0;
1500 s->min = s->max = SYSMIS;
1508 case CTSF_ROWPCT_SUM:
1509 case CTSF_COLPCT_SUM:
1510 case CTSF_TABLEPCT_SUM:
1511 case CTSF_SUBTABLEPCT_SUM:
1512 case CTSF_LAYERPCT_SUM:
1513 case CTSF_LAYERROWPCT_SUM:
1514 case CTSF_LAYERCOLPCT_SUM:
1515 s->moments = moments1_create (MOMENT_VARIANCE);
1524 case CTSF_RESPONSES:
1525 case CTSF_ROWPCT_RESPONSES:
1526 case CTSF_COLPCT_RESPONSES:
1527 case CTSF_TABLEPCT_RESPONSES:
1528 case CTSF_SUBTABLEPCT_RESPONSES:
1529 case CTSF_LAYERPCT_RESPONSES:
1530 case CTSF_LAYERROWPCT_RESPONSES:
1531 case CTSF_LAYERCOLPCT_RESPONSES:
1532 case CTSF_ROWPCT_RESPONSES_COUNT:
1533 case CTSF_COLPCT_RESPONSES_COUNT:
1534 case CTSF_TABLEPCT_RESPONSES_COUNT:
1535 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1536 case CTSF_LAYERPCT_RESPONSES_COUNT:
1537 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1538 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1539 case CTSF_ROWPCT_COUNT_RESPONSES:
1540 case CTSF_COLPCT_COUNT_RESPONSES:
1541 case CTSF_TABLEPCT_COUNT_RESPONSES:
1542 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1543 case CTSF_LAYERPCT_COUNT_RESPONSES:
1544 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1545 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1551 ctables_summary_uninit (union ctables_summary *s,
1552 const struct ctables_summary_spec *ss)
1554 switch (ss->function)
1558 case CTSF_ROWPCT_COUNT:
1559 case CTSF_COLPCT_COUNT:
1560 case CTSF_TABLEPCT_COUNT:
1561 case CTSF_SUBTABLEPCT_COUNT:
1562 case CTSF_LAYERPCT_COUNT:
1563 case CTSF_LAYERROWPCT_COUNT:
1564 case CTSF_LAYERCOLPCT_COUNT:
1565 case CTSF_ROWPCT_VALIDN:
1566 case CTSF_COLPCT_VALIDN:
1567 case CTSF_TABLEPCT_VALIDN:
1568 case CTSF_SUBTABLEPCT_VALIDN:
1569 case CTSF_LAYERPCT_VALIDN:
1570 case CTSF_LAYERROWPCT_VALIDN:
1571 case CTSF_LAYERCOLPCT_VALIDN:
1572 case CTSF_ROWPCT_TOTALN:
1573 case CTSF_COLPCT_TOTALN:
1574 case CTSF_TABLEPCT_TOTALN:
1575 case CTSF_SUBTABLEPCT_TOTALN:
1576 case CTSF_LAYERPCT_TOTALN:
1577 case CTSF_LAYERROWPCT_TOTALN:
1578 case CTSF_LAYERCOLPCT_TOTALN:
1595 case CTSF_ROWPCT_SUM:
1596 case CTSF_COLPCT_SUM:
1597 case CTSF_TABLEPCT_SUM:
1598 case CTSF_SUBTABLEPCT_SUM:
1599 case CTSF_LAYERPCT_SUM:
1600 case CTSF_LAYERROWPCT_SUM:
1601 case CTSF_LAYERCOLPCT_SUM:
1602 moments1_destroy (s->moments);
1611 case CTSF_RESPONSES:
1612 case CTSF_ROWPCT_RESPONSES:
1613 case CTSF_COLPCT_RESPONSES:
1614 case CTSF_TABLEPCT_RESPONSES:
1615 case CTSF_SUBTABLEPCT_RESPONSES:
1616 case CTSF_LAYERPCT_RESPONSES:
1617 case CTSF_LAYERROWPCT_RESPONSES:
1618 case CTSF_LAYERCOLPCT_RESPONSES:
1619 case CTSF_ROWPCT_RESPONSES_COUNT:
1620 case CTSF_COLPCT_RESPONSES_COUNT:
1621 case CTSF_TABLEPCT_RESPONSES_COUNT:
1622 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1623 case CTSF_LAYERPCT_RESPONSES_COUNT:
1624 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1625 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1626 case CTSF_ROWPCT_COUNT_RESPONSES:
1627 case CTSF_COLPCT_COUNT_RESPONSES:
1628 case CTSF_TABLEPCT_COUNT_RESPONSES:
1629 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1630 case CTSF_LAYERPCT_COUNT_RESPONSES:
1631 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1632 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1638 ctables_summary_add (union ctables_summary *s,
1639 const struct ctables_summary_spec *ss,
1640 const struct variable *var, const union value *value,
1643 switch (ss->function)
1647 case CTSF_ROWPCT_COUNT:
1648 case CTSF_COLPCT_COUNT:
1649 case CTSF_TABLEPCT_COUNT:
1650 case CTSF_SUBTABLEPCT_COUNT:
1651 case CTSF_LAYERPCT_COUNT:
1652 case CTSF_LAYERROWPCT_COUNT:
1653 case CTSF_LAYERCOLPCT_COUNT:
1654 case CTSF_ROWPCT_VALIDN:
1655 case CTSF_COLPCT_VALIDN:
1656 case CTSF_TABLEPCT_VALIDN:
1657 case CTSF_SUBTABLEPCT_VALIDN:
1658 case CTSF_LAYERPCT_VALIDN:
1659 case CTSF_LAYERROWPCT_VALIDN:
1660 case CTSF_LAYERCOLPCT_VALIDN:
1661 case CTSF_ROWPCT_TOTALN:
1662 case CTSF_COLPCT_TOTALN:
1663 case CTSF_TABLEPCT_TOTALN:
1664 case CTSF_SUBTABLEPCT_TOTALN:
1665 case CTSF_LAYERPCT_TOTALN:
1666 case CTSF_LAYERROWPCT_TOTALN:
1667 case CTSF_LAYERCOLPCT_TOTALN:
1672 if (var_is_value_missing (var, value))
1673 s->missing += weight;
1681 if (!var_is_value_missing (var, value))
1683 assert (!var_is_alpha (var)); /* XXX? */
1684 if (s->min == SYSMIS || value->f < s->min)
1686 if (s->max == SYSMIS || value->f > s->max)
1696 case CTSF_ROWPCT_SUM:
1697 case CTSF_COLPCT_SUM:
1698 case CTSF_TABLEPCT_SUM:
1699 case CTSF_SUBTABLEPCT_SUM:
1700 case CTSF_LAYERPCT_SUM:
1701 case CTSF_LAYERROWPCT_SUM:
1702 case CTSF_LAYERCOLPCT_SUM:
1703 moments1_add (s->moments, value->f, weight);
1712 case CTSF_RESPONSES:
1713 case CTSF_ROWPCT_RESPONSES:
1714 case CTSF_COLPCT_RESPONSES:
1715 case CTSF_TABLEPCT_RESPONSES:
1716 case CTSF_SUBTABLEPCT_RESPONSES:
1717 case CTSF_LAYERPCT_RESPONSES:
1718 case CTSF_LAYERROWPCT_RESPONSES:
1719 case CTSF_LAYERCOLPCT_RESPONSES:
1720 case CTSF_ROWPCT_RESPONSES_COUNT:
1721 case CTSF_COLPCT_RESPONSES_COUNT:
1722 case CTSF_TABLEPCT_RESPONSES_COUNT:
1723 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1724 case CTSF_LAYERPCT_RESPONSES_COUNT:
1725 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1726 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1727 case CTSF_ROWPCT_COUNT_RESPONSES:
1728 case CTSF_COLPCT_COUNT_RESPONSES:
1729 case CTSF_TABLEPCT_COUNT_RESPONSES:
1730 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1731 case CTSF_LAYERPCT_COUNT_RESPONSES:
1732 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1733 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1739 ctables_summary_value (const struct ctables_freq *f,
1740 union ctables_summary *s,
1741 const struct ctables_summary_spec *ss)
1743 switch (ss->function)
1749 case CTSF_SUBTABLEPCT_COUNT:
1750 return f->domains[CTDT_SUBTABLE]->valid ? s->valid / f->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1752 case CTSF_ROWPCT_COUNT:
1753 return f->domains[CTDT_ROW]->valid ? s->valid / f->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1755 case CTSF_COLPCT_COUNT:
1756 return f->domains[CTDT_COL]->valid ? s->valid / f->domains[CTDT_COL]->valid * 100 : SYSMIS;
1758 case CTSF_TABLEPCT_COUNT:
1759 return f->domains[CTDT_TABLE]->valid ? s->valid / f->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1761 case CTSF_LAYERPCT_COUNT:
1762 return f->domains[CTDT_LAYER]->valid ? s->valid / f->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1764 case CTSF_LAYERROWPCT_COUNT:
1765 return f->domains[CTDT_LAYERROW]->valid ? s->valid / f->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1767 case CTSF_LAYERCOLPCT_COUNT:
1768 return f->domains[CTDT_LAYERCOL]->valid ? s->valid / f->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1770 case CTSF_ROWPCT_VALIDN:
1771 case CTSF_COLPCT_VALIDN:
1772 case CTSF_TABLEPCT_VALIDN:
1773 case CTSF_SUBTABLEPCT_VALIDN:
1774 case CTSF_LAYERPCT_VALIDN:
1775 case CTSF_LAYERROWPCT_VALIDN:
1776 case CTSF_LAYERCOLPCT_VALIDN:
1777 case CTSF_ROWPCT_TOTALN:
1778 case CTSF_COLPCT_TOTALN:
1779 case CTSF_TABLEPCT_TOTALN:
1780 case CTSF_SUBTABLEPCT_TOTALN:
1781 case CTSF_LAYERPCT_TOTALN:
1782 case CTSF_LAYERROWPCT_TOTALN:
1783 case CTSF_LAYERCOLPCT_TOTALN:
1788 return s->valid + s->missing;
1801 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1806 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1812 double weight, variance;
1813 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1814 return calc_semean (variance, weight);
1820 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1821 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1826 double weight, mean;
1827 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1828 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1834 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1838 case CTSF_ROWPCT_SUM:
1839 case CTSF_COLPCT_SUM:
1840 case CTSF_TABLEPCT_SUM:
1841 case CTSF_SUBTABLEPCT_SUM:
1842 case CTSF_LAYERPCT_SUM:
1843 case CTSF_LAYERROWPCT_SUM:
1844 case CTSF_LAYERCOLPCT_SUM:
1853 case CTSF_RESPONSES:
1854 case CTSF_ROWPCT_RESPONSES:
1855 case CTSF_COLPCT_RESPONSES:
1856 case CTSF_TABLEPCT_RESPONSES:
1857 case CTSF_SUBTABLEPCT_RESPONSES:
1858 case CTSF_LAYERPCT_RESPONSES:
1859 case CTSF_LAYERROWPCT_RESPONSES:
1860 case CTSF_LAYERCOLPCT_RESPONSES:
1861 case CTSF_ROWPCT_RESPONSES_COUNT:
1862 case CTSF_COLPCT_RESPONSES_COUNT:
1863 case CTSF_TABLEPCT_RESPONSES_COUNT:
1864 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1865 case CTSF_LAYERPCT_RESPONSES_COUNT:
1866 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1867 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1868 case CTSF_ROWPCT_COUNT_RESPONSES:
1869 case CTSF_COLPCT_COUNT_RESPONSES:
1870 case CTSF_TABLEPCT_COUNT_RESPONSES:
1871 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1872 case CTSF_LAYERPCT_COUNT_RESPONSES:
1873 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1874 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1881 struct ctables_freq_sort_aux
1883 const struct ctables_table *t;
1884 enum pivot_axis_type a;
1888 ctables_freq_compare_3way (const void *a_, const void *b_, const void *aux_)
1890 const struct ctables_freq_sort_aux *aux = aux_;
1891 struct ctables_freq *const *ap = a_;
1892 struct ctables_freq *const *bp = b_;
1893 const struct ctables_freq *a = *ap;
1894 const struct ctables_freq *b = *bp;
1896 size_t a_idx = a->axes[aux->a].vaa_idx;
1897 size_t b_idx = b->axes[aux->a].vaa_idx;
1899 return a_idx < b_idx ? -1 : 1;
1901 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1902 for (size_t i = 0; i < va->n; i++)
1903 if (i != va->scale_idx)
1905 int cmp = value_compare_3way (&a->axes[aux->a].values[i],
1906 &b->axes[aux->a].values[i],
1907 var_get_width (va->vars[i]));
1917 For each ctables_table:
1918 For each combination of row vars:
1919 For each combination of column vars:
1920 For each combination of layer vars:
1922 Make a table of row values:
1923 Sort entries by row values
1924 Assign a 0-based index to each actual value
1925 Construct a dimension
1926 Make a table of column values
1927 Make a table of layer values
1929 Fill the table entry using the indexes from before.
1932 static struct ctables_domain *
1933 ctables_domain_insert (struct ctables_table *t, struct ctables_freq *f,
1934 enum ctables_domain_type domain)
1937 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1939 size_t idx = f->axes[a].vaa_idx;
1940 const struct var_array *va = &t->vaas[a].vas[idx];
1941 hash = hash_int (idx, hash);
1942 for (size_t i = 0; i < va->n_domains[domain]; i++)
1944 size_t v_idx = va->domains[domain][i];
1945 hash = value_hash (&f->axes[a].values[v_idx],
1946 var_get_width (va->vars[v_idx]), hash);
1950 struct ctables_domain *d;
1951 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
1953 const struct ctables_freq *df = d->example;
1954 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1956 size_t idx = f->axes[a].vaa_idx;
1957 if (idx != df->axes[a].vaa_idx)
1960 const struct var_array *va = &t->vaas[a].vas[idx];
1961 for (size_t i = 0; i < va->n_domains[domain]; i++)
1963 size_t v_idx = va->domains[domain][i];
1964 if (!value_equal (&df->axes[a].values[v_idx],
1965 &f->axes[a].values[v_idx],
1966 var_get_width (va->vars[v_idx])))
1975 d = xmalloc (sizeof *d);
1976 *d = (struct ctables_domain) { .example = f };
1977 hmap_insert (&t->domains[domain], &d->node, hash);
1981 static const struct ctables_cat_value *
1982 ctables_categories_match (const struct ctables_categories *cats,
1983 const union value *v, const struct variable *var)
1985 const struct ctables_cat_value *othernm = NULL;
1986 for (size_t i = cats->n_values; i-- > 0; )
1988 const struct ctables_cat_value *cv = &cats->values[i];
1992 if (cv->number == v->f)
2000 if ((cv->range[0] == -DBL_MAX || v->f >= cv->range[0])
2001 && (cv->range[1] == DBL_MAX || v->f <= cv->range[1]))
2006 if (var_is_value_missing (var, v))
2016 case CCVT_HSUBTOTAL:
2021 return var_is_value_missing (var, v) ? NULL : othernm;
2025 ctables_freqtab_insert (struct ctables_table *t,
2026 const struct ccase *c,
2027 size_t ir, size_t ic, size_t il,
2030 size_t ix[PIVOT_N_AXES] = {
2031 [PIVOT_AXIS_ROW] = ir,
2032 [PIVOT_AXIS_COLUMN] = ic,
2033 [PIVOT_AXIS_LAYER] = il,
2035 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
2037 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2039 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2040 for (size_t i = 0; i < va->n; i++)
2042 if (i == va->scale_idx)
2045 const struct ctables_categories *cats = t->categories[var_get_dict_index (va->vars[i])];
2046 if (!cats || !cats->n_values)
2049 if (!ctables_categories_match (cats, case_data (c, va->vars[i]), va->vars[i]))
2055 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2057 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2058 hash = hash_int (ix[a], hash);
2059 for (size_t i = 0; i < va->n; i++)
2060 if (i != va->scale_idx)
2061 hash = value_hash (case_data (c, va->vars[i]),
2062 var_get_width (va->vars[i]), hash);
2065 struct ctables_freq *f;
2066 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &t->ft)
2068 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2070 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2071 if (f->axes[a].vaa_idx != ix[a])
2073 for (size_t i = 0; i < va->n; i++)
2074 if (i != va->scale_idx
2075 && !value_equal (case_data (c, va->vars[i]),
2076 &f->axes[a].values[i],
2077 var_get_width (va->vars[i])))
2086 f = xmalloc (sizeof *f);
2087 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2089 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2090 f->axes[a].vaa_idx = ix[a];
2091 f->axes[a].values = (va->n
2092 ? xnmalloc (va->n, sizeof *f->axes[a].values)
2094 for (size_t i = 0; i < va->n; i++)
2095 value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
2096 var_get_width (va->vars[i]));
2098 f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
2099 for (size_t i = 0; i < ss->n_summaries; i++)
2100 ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
2101 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2102 f->domains[dt] = ctables_domain_insert (t, f, dt);
2103 hmap_insert (&t->ft, &f->node, hash);
2106 for (size_t i = 0; i < ss->n_summaries; i++)
2107 ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
2108 case_data (c, ss->summary_var), weight);
2109 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2110 f->domains[dt]->valid += weight;
2114 ctables_execute (struct dataset *ds, struct ctables *ct)
2116 for (size_t i = 0; i < ct->n_tables; i++)
2118 struct ctables_table *t = ct->tables[i];
2119 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2122 t->vaas[a] = enumerate_fts (a, t->axes[a]);
2124 for (size_t j = 0; j < t->vaas[a].n; j++)
2126 struct var_array *va = &t->vaas[a].vas[j];
2127 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2129 va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
2130 va->n_domains[dt] = 0;
2132 for (size_t k = 0; k < va->n; k++)
2134 if (k == va->scale_idx)
2143 if (a != PIVOT_AXIS_LAYER)
2150 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2151 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2152 : a == PIVOT_AXIS_ROW)
2155 || (va->scale_idx == va->n - 1
2162 if (a == PIVOT_AXIS_COLUMN)
2167 if (a == PIVOT_AXIS_ROW)
2172 va->domains[dt][va->n_domains[dt]++] = k;
2179 struct var_array *va = xmalloc (sizeof *va);
2180 *va = (struct var_array) { .n = 0 };
2181 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
2184 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
2186 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
2187 if (!va->n_summaries)
2189 va->summaries = xmalloc (sizeof *va->summaries);
2190 va->n_summaries = 1;
2192 enum ctables_summary_function function
2193 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
2194 struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
2196 *va->summaries = (struct ctables_summary_spec) {
2197 .function = function,
2198 .format = ctables_summary_default_format (function, &var),
2199 .label = ctables_summary_default_label (function, 0),
2201 if (!va->summary_var)
2202 va->summary_var = va->vars[0];
2207 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2210 bool warn_on_invalid = true;
2211 double total_weight = 0;
2212 for (struct ccase *c = casereader_read (input); c;
2213 case_unref (c), c = casereader_read (input))
2215 double weight = dict_get_case_weight (dataset_dict (ds), c,
2217 total_weight += weight;
2219 for (size_t i = 0; i < ct->n_tables; i++)
2221 struct ctables_table *t = ct->tables[i];
2223 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2224 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2225 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2226 ctables_freqtab_insert (t, c, ir, ic, il, weight);
2229 casereader_destroy (input);
2231 for (size_t i = 0; i < ct->n_tables; i++)
2233 struct ctables_table *t = ct->tables[i];
2235 struct pivot_table *pt = pivot_table_create__ (
2237 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2238 : pivot_value_new_text (N_("Custom Tables"))),
2241 pivot_table_set_caption (
2242 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2244 pivot_table_set_caption (
2245 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2247 pivot_table_set_look (pt, ct->look);
2248 struct pivot_dimension *d[PIVOT_N_AXES];
2249 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2251 static const char *names[] = {
2252 [PIVOT_AXIS_ROW] = N_("Rows"),
2253 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2254 [PIVOT_AXIS_LAYER] = N_("Layers"),
2256 d[a] = (t->axes[a] || a == t->summary_axis
2257 ? pivot_dimension_create (pt, a, names[a])
2262 assert (t->axes[a]);
2264 struct ctables_freq **sorted = xnmalloc (t->ft.count, sizeof *sorted);
2266 struct ctables_freq *f;
2268 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2270 assert (n == t->ft.count);
2272 struct ctables_freq_sort_aux aux = { .t = t, .a = a };
2273 sort (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
2275 size_t max_depth = 0;
2276 for (size_t j = 0; j < t->vaas[a].n; j++)
2277 if (t->vaas[a].vas[j].n > max_depth)
2278 max_depth = t->vaas[a].vas[j].n;
2280 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2281 struct pivot_category *top = NULL;
2283 for (size_t j = 0; j < n; j++)
2285 struct ctables_freq *f = sorted[j];
2286 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2288 size_t n_common = 0;
2289 bool new_subtable = false;
2292 struct ctables_freq *prev = sorted[j - 1];
2293 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2295 for (; n_common < va->n; n_common++)
2296 if (n_common != va->scale_idx
2297 && !value_equal (&prev->axes[a].values[n_common],
2298 &f->axes[a].values[n_common],
2299 var_get_type (va->vars[n_common])))
2303 new_subtable = true;
2306 new_subtable = true;
2310 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2312 if (vlabel != CTVL_NONE)
2313 top = pivot_category_create_group__ (
2314 top, pivot_value_new_variable (va->vars[0]));
2316 if (n_common == va->n)
2318 f->axes[a].leaf = prev_leaf;
2322 for (size_t k = n_common; k < va->n; k++)
2324 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2326 struct pivot_value *label
2327 = (k != va->scale_idx
2328 ? pivot_value_new_var_value (va->vars[k],
2329 &f->axes[a].values[k])
2333 if (a == t->summary_axis)
2336 parent = pivot_category_create_group__ (parent, label);
2337 for (size_t m = 0; m < va->n_summaries; m++)
2339 int leaf = pivot_category_create_leaf (
2340 parent, pivot_value_new_text (va->summaries[m].label));
2347 /* This assertion is true as long as the summary axis
2348 is the axis where the summaries are displayed. */
2351 prev_leaf = pivot_category_create_leaf (parent, label);
2357 parent = pivot_category_create_group__ (parent, label);
2359 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2360 if (vlabel != CTVL_NONE)
2361 parent = pivot_category_create_group__ (
2362 parent, pivot_value_new_variable (va->vars[k + 1]));
2366 f->axes[a].leaf = prev_leaf;
2371 struct ctables_freq *f;
2372 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2374 const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2375 for (size_t j = 0; j < ss->n_summaries; j++)
2378 size_t n_dindexes = 0;
2380 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2383 int leaf = f->axes[a].leaf;
2384 if (a == t->summary_axis)
2386 dindexes[n_dindexes++] = leaf;
2389 double d = ctables_summary_value (f, &f->summaries[j], &ss->summaries[j]);
2390 struct pivot_value *value = pivot_value_new_number (d);
2391 value->numeric.format = ss->summaries[j].format;
2392 pivot_table_put (pt, dindexes, n_dindexes, value);
2396 pivot_table_submit (pt);
2400 for (size_t i = 0; i < ct->n_tables; i++)
2402 struct ctables_table *t = ct->tables[i];
2404 for (size_t j = 0; j < t->n_fts; j++)
2406 struct ctables_freqtab *ft = t->fts[j];
2407 struct ctables_freq *f, *next;
2408 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2410 hmap_delete (&ft->data, &f->node);
2411 for (size_t k = 0; k < ft->n_summaries; k++)
2412 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2413 free (f->summaries);
2414 for (size_t k = 0; k < ft->vars.n; k++)
2416 const struct variable *var = ft->vars.vars[k];
2417 value_destroy (&f->values[k], var_get_width (var));
2421 hmap_destroy (&ft->data);
2422 var_array_uninit (&ft->vars);
2429 return proc_commit (ds);
2433 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2435 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2436 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2437 enum settings_value_show tvars = settings_get_show_variables ();
2438 for (size_t i = 0; i < n_vars; i++)
2439 vlabels[i] = (enum ctables_vlabel) tvars;
2441 struct ctables *ct = xmalloc (sizeof *ct);
2442 *ct = (struct ctables) {
2443 .look = pivot_table_look_unshare (pivot_table_look_ref (
2444 pivot_table_look_get_default ())),
2446 .hide_threshold = 5,
2448 ct->look->omit_empty = false;
2450 if (!lex_force_match (lexer, T_SLASH))
2453 while (!lex_match_id (lexer, "TABLE"))
2455 if (lex_match_id (lexer, "FORMAT"))
2457 double widths[2] = { SYSMIS, SYSMIS };
2458 double units_per_inch = 72.0;
2460 while (lex_token (lexer) != T_SLASH)
2462 if (lex_match_id (lexer, "MINCOLWIDTH"))
2464 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2467 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2469 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2472 else if (lex_match_id (lexer, "UNITS"))
2474 lex_match (lexer, T_EQUALS);
2475 if (lex_match_id (lexer, "POINTS"))
2476 units_per_inch = 72.0;
2477 else if (lex_match_id (lexer, "INCHES"))
2478 units_per_inch = 1.0;
2479 else if (lex_match_id (lexer, "CM"))
2480 units_per_inch = 2.54;
2483 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2487 else if (lex_match_id (lexer, "EMPTY"))
2492 lex_match (lexer, T_EQUALS);
2493 if (lex_match_id (lexer, "ZERO"))
2495 /* Nothing to do. */
2497 else if (lex_match_id (lexer, "BLANK"))
2498 ct->zero = xstrdup ("");
2499 else if (lex_force_string (lexer))
2501 ct->zero = ss_xstrdup (lex_tokss (lexer));
2507 else if (lex_match_id (lexer, "MISSING"))
2509 lex_match (lexer, T_EQUALS);
2510 if (!lex_force_string (lexer))
2514 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2515 ? ss_xstrdup (lex_tokss (lexer))
2521 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2522 "UNITS", "EMPTY", "MISSING");
2527 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2528 && widths[0] > widths[1])
2530 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2534 for (size_t i = 0; i < 2; i++)
2535 if (widths[i] != SYSMIS)
2537 int *wr = ct->look->width_ranges[TABLE_HORZ];
2538 wr[i] = widths[i] / units_per_inch * 96.0;
2543 else if (lex_match_id (lexer, "VLABELS"))
2545 if (!lex_force_match_id (lexer, "VARIABLES"))
2547 lex_match (lexer, T_EQUALS);
2549 struct variable **vars;
2551 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2555 if (!lex_force_match_id (lexer, "DISPLAY"))
2560 lex_match (lexer, T_EQUALS);
2562 enum ctables_vlabel vlabel;
2563 if (lex_match_id (lexer, "DEFAULT"))
2564 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2565 else if (lex_match_id (lexer, "NAME"))
2567 else if (lex_match_id (lexer, "LABEL"))
2568 vlabel = CTVL_LABEL;
2569 else if (lex_match_id (lexer, "BOTH"))
2571 else if (lex_match_id (lexer, "NONE"))
2575 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2581 for (size_t i = 0; i < n_vars; i++)
2582 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2585 else if (lex_match_id (lexer, "MRSETS"))
2587 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2589 lex_match (lexer, T_EQUALS);
2590 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2593 else if (lex_match_id (lexer, "SMISSING"))
2595 if (lex_match_id (lexer, "VARIABLE"))
2596 ct->smissing_listwise = false;
2597 else if (lex_match_id (lexer, "LISTWISE"))
2598 ct->smissing_listwise = true;
2601 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2606 else if (lex_match_id (lexer, "WEIGHT"))
2608 if (!lex_force_match_id (lexer, "VARIABLE"))
2610 lex_match (lexer, T_EQUALS);
2611 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2612 if (!ct->base_weight)
2615 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2617 if (!lex_force_match_id (lexer, "COUNT"))
2619 lex_match (lexer, T_EQUALS);
2620 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2622 ct->hide_threshold = lex_integer (lexer);
2627 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2628 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2629 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2633 if (!lex_force_match (lexer, T_SLASH))
2637 size_t allocated_tables = 0;
2640 if (ct->n_tables >= allocated_tables)
2641 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2642 sizeof *ct->tables);
2644 struct ctables_table *t = xmalloc (sizeof *t);
2645 *t = (struct ctables_table) {
2646 .ft = HMAP_INITIALIZER (t->ft),
2647 .slabels_position = PIVOT_AXIS_COLUMN,
2648 .slabels_visible = true,
2649 .row_labels = CTLP_NORMAL,
2650 .col_labels = CTLP_NORMAL,
2651 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2652 sizeof *t->categories),
2653 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2656 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2657 hmap_init (&t->domains[dt]);
2658 ct->tables[ct->n_tables++] = t;
2660 lex_match (lexer, T_EQUALS);
2661 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2663 if (lex_match (lexer, T_BY))
2665 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2666 ct, t, PIVOT_AXIS_COLUMN))
2669 if (lex_match (lexer, T_BY))
2671 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2672 ct, t, PIVOT_AXIS_LAYER))
2677 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2678 && !t->axes[PIVOT_AXIS_LAYER])
2680 lex_error (lexer, _("At least one variable must be specified."));
2684 const struct ctables_axis *scales[PIVOT_N_AXES];
2685 size_t n_scales = 0;
2686 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2688 scales[a] = find_scale (t->axes[a]);
2694 msg (SE, _("Scale variables may appear only on one axis."));
2695 if (scales[PIVOT_AXIS_ROW])
2696 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2697 _("This scale variable appears on the rows axis."));
2698 if (scales[PIVOT_AXIS_COLUMN])
2699 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2700 _("This scale variable appears on the columns axis."));
2701 if (scales[PIVOT_AXIS_LAYER])
2702 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2703 _("This scale variable appears on the layer axis."));
2707 const struct ctables_axis *summaries[PIVOT_N_AXES];
2708 size_t n_summaries = 0;
2709 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2711 summaries[a] = (scales[a]
2713 : find_categorical_summary_spec (t->axes[a]));
2717 if (n_summaries > 1)
2719 msg (SE, _("Summaries may appear only on one axis."));
2720 if (summaries[PIVOT_AXIS_ROW])
2721 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2722 _("This variable on the rows axis has a summary."));
2723 if (summaries[PIVOT_AXIS_COLUMN])
2724 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2725 _("This variable on the columns axis has a summary."));
2726 if (summaries[PIVOT_AXIS_LAYER])
2727 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2728 _("This variable on the layers axis has a summary."));
2731 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2732 if (n_summaries ? summaries[a] : t->axes[a])
2734 t->summary_axis = a;
2738 if (lex_token (lexer) == T_ENDCMD)
2740 if (!lex_force_match (lexer, T_SLASH))
2743 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2745 if (lex_match_id (lexer, "SLABELS"))
2747 while (lex_token (lexer) != T_SLASH)
2749 if (lex_match_id (lexer, "POSITION"))
2751 lex_match (lexer, T_EQUALS);
2752 if (lex_match_id (lexer, "COLUMN"))
2753 t->slabels_position = PIVOT_AXIS_COLUMN;
2754 else if (lex_match_id (lexer, "ROW"))
2755 t->slabels_position = PIVOT_AXIS_ROW;
2756 else if (lex_match_id (lexer, "LAYER"))
2757 t->slabels_position = PIVOT_AXIS_LAYER;
2760 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2764 else if (lex_match_id (lexer, "VISIBLE"))
2766 lex_match (lexer, T_EQUALS);
2767 if (!parse_bool (lexer, &t->slabels_visible))
2772 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2777 else if (lex_match_id (lexer, "CLABELS"))
2779 while (lex_token (lexer) != T_SLASH)
2781 if (lex_match_id (lexer, "AUTO"))
2782 t->row_labels = t->col_labels = CTLP_NORMAL;
2783 else if (lex_match_id (lexer, "ROWLABELS"))
2785 lex_match (lexer, T_EQUALS);
2786 if (lex_match_id (lexer, "OPPOSITE"))
2787 t->row_labels = CTLP_OPPOSITE;
2788 else if (lex_match_id (lexer, "LAYER"))
2789 t->row_labels = CTLP_LAYER;
2792 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2796 else if (lex_match_id (lexer, "COLLABELS"))
2798 lex_match (lexer, T_EQUALS);
2799 if (lex_match_id (lexer, "OPPOSITE"))
2800 t->col_labels = CTLP_OPPOSITE;
2801 else if (lex_match_id (lexer, "LAYER"))
2802 t->col_labels = CTLP_LAYER;
2805 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2811 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2817 else if (lex_match_id (lexer, "CRITERIA"))
2819 if (!lex_force_match_id (lexer, "CILEVEL"))
2821 lex_match (lexer, T_EQUALS);
2823 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2825 t->cilevel = lex_number (lexer);
2828 else if (lex_match_id (lexer, "CATEGORIES"))
2830 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2833 else if (lex_match_id (lexer, "TITLES"))
2838 if (lex_match_id (lexer, "CAPTION"))
2839 textp = &t->caption;
2840 else if (lex_match_id (lexer, "CORNER"))
2842 else if (lex_match_id (lexer, "TITLE"))
2846 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2849 lex_match (lexer, T_EQUALS);
2851 struct string s = DS_EMPTY_INITIALIZER;
2852 while (lex_is_string (lexer))
2854 if (!ds_is_empty (&s))
2855 ds_put_byte (&s, ' ');
2856 ds_put_substring (&s, lex_tokss (lexer));
2860 *textp = ds_steal_cstr (&s);
2862 while (lex_token (lexer) != T_SLASH
2863 && lex_token (lexer) != T_ENDCMD);
2865 else if (lex_match_id (lexer, "SIGTEST"))
2869 t->chisq = xmalloc (sizeof *t->chisq);
2870 *t->chisq = (struct ctables_chisq) {
2872 .include_mrsets = true,
2873 .all_visible = true,
2879 if (lex_match_id (lexer, "TYPE"))
2881 lex_match (lexer, T_EQUALS);
2882 if (!lex_force_match_id (lexer, "CHISQUARE"))
2885 else if (lex_match_id (lexer, "ALPHA"))
2887 lex_match (lexer, T_EQUALS);
2888 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2890 t->chisq->alpha = lex_number (lexer);
2893 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2895 lex_match (lexer, T_EQUALS);
2896 if (parse_bool (lexer, &t->chisq->include_mrsets))
2899 else if (lex_match_id (lexer, "CATEGORIES"))
2901 lex_match (lexer, T_EQUALS);
2902 if (lex_match_id (lexer, "ALLVISIBLE"))
2903 t->chisq->all_visible = true;
2904 else if (lex_match_id (lexer, "SUBTOTALS"))
2905 t->chisq->all_visible = false;
2908 lex_error_expecting (lexer,
2909 "ALLVISIBLE", "SUBTOTALS");
2915 lex_error_expecting (lexer, "TYPE", "ALPHA",
2916 "INCLUDEMRSETS", "CATEGORIES");
2920 while (lex_token (lexer) != T_SLASH
2921 && lex_token (lexer) != T_ENDCMD);
2923 else if (lex_match_id (lexer, "COMPARETEST"))
2927 t->pairwise = xmalloc (sizeof *t->pairwise);
2928 *t->pairwise = (struct ctables_pairwise) {
2930 .alpha = { .05, .05 },
2931 .adjust = BONFERRONI,
2932 .include_mrsets = true,
2933 .meansvariance_allcats = true,
2934 .all_visible = true,
2943 if (lex_match_id (lexer, "TYPE"))
2945 lex_match (lexer, T_EQUALS);
2946 if (lex_match_id (lexer, "PROP"))
2947 t->pairwise->type = PROP;
2948 else if (lex_match_id (lexer, "MEAN"))
2949 t->pairwise->type = MEAN;
2952 lex_error_expecting (lexer, "PROP", "MEAN");
2956 else if (lex_match_id (lexer, "ALPHA"))
2958 lex_match (lexer, T_EQUALS);
2960 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2962 double a0 = lex_number (lexer);
2965 lex_match (lexer, T_COMMA);
2966 if (lex_is_number (lexer))
2968 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2970 double a1 = lex_number (lexer);
2973 t->pairwise->alpha[0] = MIN (a0, a1);
2974 t->pairwise->alpha[1] = MAX (a0, a1);
2977 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2979 else if (lex_match_id (lexer, "ADJUST"))
2981 lex_match (lexer, T_EQUALS);
2982 if (lex_match_id (lexer, "BONFERRONI"))
2983 t->pairwise->adjust = BONFERRONI;
2984 else if (lex_match_id (lexer, "BH"))
2985 t->pairwise->adjust = BH;
2986 else if (lex_match_id (lexer, "NONE"))
2987 t->pairwise->adjust = 0;
2990 lex_error_expecting (lexer, "BONFERRONI", "BH",
2995 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2997 lex_match (lexer, T_EQUALS);
2998 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3001 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3003 lex_match (lexer, T_EQUALS);
3004 if (lex_match_id (lexer, "ALLCATS"))
3005 t->pairwise->meansvariance_allcats = true;
3006 else if (lex_match_id (lexer, "TESTEDCATS"))
3007 t->pairwise->meansvariance_allcats = false;
3010 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3014 else if (lex_match_id (lexer, "CATEGORIES"))
3016 lex_match (lexer, T_EQUALS);
3017 if (lex_match_id (lexer, "ALLVISIBLE"))
3018 t->pairwise->all_visible = true;
3019 else if (lex_match_id (lexer, "SUBTOTALS"))
3020 t->pairwise->all_visible = false;
3023 lex_error_expecting (lexer, "ALLVISIBLE",
3028 else if (lex_match_id (lexer, "MERGE"))
3030 lex_match (lexer, T_EQUALS);
3031 if (!parse_bool (lexer, &t->pairwise->merge))
3034 else if (lex_match_id (lexer, "STYLE"))
3036 lex_match (lexer, T_EQUALS);
3037 if (lex_match_id (lexer, "APA"))
3038 t->pairwise->apa_style = true;
3039 else if (lex_match_id (lexer, "SIMPLE"))
3040 t->pairwise->apa_style = false;
3043 lex_error_expecting (lexer, "APA", "SIMPLE");
3047 else if (lex_match_id (lexer, "SHOWSIG"))
3049 lex_match (lexer, T_EQUALS);
3050 if (!parse_bool (lexer, &t->pairwise->show_sig))
3055 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3056 "INCLUDEMRSETS", "MEANSVARIANCE",
3057 "CATEGORIES", "MERGE", "STYLE",
3062 while (lex_token (lexer) != T_SLASH
3063 && lex_token (lexer) != T_ENDCMD);
3067 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3068 "CRITERIA", "CATEGORIES", "TITLES",
3069 "SIGTEST", "COMPARETEST");
3074 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3076 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3081 while (lex_token (lexer) != T_ENDCMD);
3083 bool ok = ctables_execute (ds, ct);
3084 ctables_destroy (ct);
3085 return ok ? CMD_SUCCESS : CMD_FAILURE;
3088 ctables_destroy (ct);