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_cell *example;
176 /* In struct ctables's 'cells' 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. */
391 static const struct ctables_cat_value *ctables_categories_match (
392 const struct ctables_categories *, const union value *,
393 const struct variable *);
396 ctables_cat_value_uninit (struct ctables_cat_value *cv)
415 free (cv->subtotal_label);
420 ctables_categories_unref (struct ctables_categories *c)
425 assert (c->n_refs > 0);
429 for (size_t i = 0; i < c->n_values; i++)
430 ctables_cat_value_uninit (&c->values[i]);
432 free (c->total_label);
436 /* Chi-square test (SIGTEST). */
444 /* Pairwise comparison test (COMPARETEST). */
445 struct ctables_pairwise
447 enum { PROP, MEAN } type;
450 bool meansvariance_allcats;
452 enum { BONFERRONI = 1, BH } adjust;
476 struct ctables_var var;
478 struct ctables_summary_spec *summaries;
480 size_t allocated_summaries;
484 struct ctables_axis *subs[2];
487 struct msg_location *loc;
490 static void ctables_axis_destroy (struct ctables_axis *);
499 enum ctables_function_availability
501 CTFA_ALL, /* Any variables. */
502 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
503 CTFA_MRSETS, /* Only multiple-response sets */
506 struct ctables_summary_spec
508 enum ctables_summary_function function;
509 double percentile; /* CTSF_PTILE only. */
511 struct fmt_spec format; /* XXX extra CTABLES formats */
515 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
522 parse_col_width (struct lexer *lexer, const char *name, double *width)
524 lex_match (lexer, T_EQUALS);
525 if (lex_match_id (lexer, "DEFAULT"))
527 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
529 *width = lex_number (lexer);
539 parse_bool (struct lexer *lexer, bool *b)
541 if (lex_match_id (lexer, "NO"))
543 else if (lex_match_id (lexer, "YES"))
547 lex_error_expecting (lexer, "YES", "NO");
553 static enum ctables_function_availability
554 ctables_function_availability (enum ctables_summary_function f)
556 static enum ctables_function_availability availability[] = {
557 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
562 return availability[f];
566 parse_ctables_summary_function (struct lexer *lexer,
567 enum ctables_summary_function *f)
571 enum ctables_summary_function function;
572 struct substring name;
574 static struct pair names[] = {
575 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
576 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
579 /* The .COUNT suffix may be omitted. */
580 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
581 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
582 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
583 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
584 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
585 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
586 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
590 if (!lex_force_id (lexer))
593 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
594 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
596 *f = names[i].function;
601 lex_error (lexer, _("Expecting summary function name."));
606 ctables_axis_destroy (struct ctables_axis *axis)
614 for (size_t i = 0; i < axis->n_summaries; i++)
615 ctables_summary_spec_uninit (&axis->summaries[i]);
616 free (axis->summaries);
621 ctables_axis_destroy (axis->subs[0]);
622 ctables_axis_destroy (axis->subs[1]);
625 msg_location_destroy (axis->loc);
629 static struct ctables_axis *
630 ctables_axis_new_nonterminal (enum ctables_axis_op op,
631 struct ctables_axis *sub0,
632 struct ctables_axis *sub1,
633 struct lexer *lexer, int start_ofs)
635 struct ctables_axis *axis = xmalloc (sizeof *axis);
636 *axis = (struct ctables_axis) {
638 .subs = { sub0, sub1 },
639 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
644 struct ctables_axis_parse_ctx
647 struct dictionary *dict;
649 struct ctables_table *t;
652 static struct fmt_spec
653 ctables_summary_default_format (enum ctables_summary_function function,
654 const struct ctables_var *var)
656 static const enum ctables_format default_formats[] = {
657 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
661 switch (default_formats[function])
664 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
667 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
670 return *ctables_var_get_print_format (var);
678 ctables_summary_default_label (enum ctables_summary_function function,
681 static const char *default_labels[] = {
682 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
687 return (function == CTSF_PTILE
688 ? xasprintf (_("Percentile %.2f"), percentile)
689 : xstrdup (gettext (default_labels[function])));
693 ctables_summary_function_name (enum ctables_summary_function function)
695 static const char *names[] = {
696 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
700 return names[function];
704 add_summary_spec (struct ctables_axis *axis,
705 enum ctables_summary_function function, double percentile,
706 const char *label, const struct fmt_spec *format,
707 const struct msg_location *loc)
709 if (axis->op == CTAO_VAR)
711 if (axis->n_summaries >= axis->allocated_summaries)
712 axis->summaries = x2nrealloc (axis->summaries,
713 &axis->allocated_summaries,
714 sizeof *axis->summaries);
716 const char *function_name = ctables_summary_function_name (function);
717 const char *var_name = ctables_var_name (&axis->var);
718 switch (ctables_function_availability (function))
721 if (!axis->var.is_mrset)
723 msg_at (SE, loc, _("Summary function %s applies only to multiple "
724 "response sets."), function_name);
725 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
735 _("Summary function %s applies only to scale variables."),
737 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
747 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
748 *dst = (struct ctables_summary_spec) {
749 .function = function,
750 .percentile = percentile,
751 .label = xstrdup (label),
752 .format = (format ? *format
753 : ctables_summary_default_format (function, &axis->var)),
759 for (size_t i = 0; i < 2; i++)
760 if (!add_summary_spec (axis->subs[i], function, percentile, label,
767 static struct ctables_axis *ctables_axis_parse_stack (
768 struct ctables_axis_parse_ctx *);
771 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
772 struct ctables_var *var)
774 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
776 *var = (struct ctables_var) {
778 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
782 lex_error (lexer, _("'%s' does not name a multiple-response set "
783 "in the active file dictionary."),
784 lex_tokcstr (lexer));
792 *var = (struct ctables_var) {
794 .var = parse_variable (lexer, dict),
796 return var->var != NULL;
800 static struct ctables_axis *
801 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
803 if (lex_match (ctx->lexer, T_LPAREN))
805 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
806 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
808 ctables_axis_destroy (sub);
814 if (!lex_force_id (ctx->lexer))
817 int start_ofs = lex_ofs (ctx->lexer);
818 struct ctables_var var;
819 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
822 struct ctables_axis *axis = xmalloc (sizeof *axis);
823 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
825 /* XXX should figure out default measures by reading data */
826 axis->scale = (var.is_mrset ? false
827 : lex_match_phrase (ctx->lexer, "[S]") ? true
828 : lex_match_phrase (ctx->lexer, "[C]") ? false
829 : var_get_measure (var.var) == MEASURE_SCALE);
830 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
831 lex_ofs (ctx->lexer) - 1);
835 static struct ctables_axis *
836 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
838 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
839 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
844 int start_ofs = lex_ofs (ctx->lexer);
846 /* Parse function. */
847 enum ctables_summary_function function;
848 if (!parse_ctables_summary_function (ctx->lexer, &function))
851 /* Parse percentile. */
852 double percentile = 0;
853 if (function == CTSF_PTILE)
855 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
857 percentile = lex_number (ctx->lexer);
858 lex_get (ctx->lexer);
863 if (lex_is_string (ctx->lexer))
865 label = ss_xstrdup (lex_tokss (ctx->lexer));
866 lex_get (ctx->lexer);
869 label = ctables_summary_default_label (function, percentile);
872 struct fmt_spec format;
873 const struct fmt_spec *formatp;
874 if (lex_token (ctx->lexer) == T_ID)
876 if (!parse_format_specifier (ctx->lexer, &format)
877 || !fmt_check_output (&format)
878 || !fmt_check_type_compat (&format, VAL_NUMERIC))
888 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
889 lex_ofs (ctx->lexer) - 1);
890 add_summary_spec (sub, function, percentile, label, formatp, loc);
892 msg_location_destroy (loc);
894 lex_match (ctx->lexer, T_COMMA);
896 while (!lex_match (ctx->lexer, T_RBRACK));
901 ctables_axis_destroy (sub);
905 static const struct ctables_axis *
906 find_scale (const struct ctables_axis *axis)
910 else if (axis->op == CTAO_VAR)
914 assert (!axis->var.is_mrset);
922 for (size_t i = 0; i < 2; i++)
924 const struct ctables_axis *scale = find_scale (axis->subs[i]);
932 static const struct ctables_axis *
933 find_categorical_summary_spec (const struct ctables_axis *axis)
937 else if (axis->op == CTAO_VAR)
938 return !axis->scale && axis->n_summaries ? axis : NULL;
941 for (size_t i = 0; i < 2; i++)
943 const struct ctables_axis *sum
944 = find_categorical_summary_spec (axis->subs[i]);
952 static struct ctables_axis *
953 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
955 int start_ofs = lex_ofs (ctx->lexer);
956 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
960 while (lex_match (ctx->lexer, T_GT))
962 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
966 struct ctables_axis *nest = ctables_axis_new_nonterminal (
967 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
969 const struct ctables_axis *outer_scale = find_scale (lhs);
970 const struct ctables_axis *inner_scale = find_scale (rhs);
971 if (outer_scale && inner_scale)
973 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
974 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
975 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
976 ctables_axis_destroy (nest);
980 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
983 msg_at (SE, nest->loc,
984 _("Summaries may only be requested for categorical variables "
985 "at the innermost nesting level."));
986 msg_at (SN, outer_sum->loc,
987 _("This outer categorical variable has a summary."));
988 ctables_axis_destroy (nest);
998 static struct ctables_axis *
999 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1001 int start_ofs = lex_ofs (ctx->lexer);
1002 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1006 while (lex_match (ctx->lexer, T_PLUS))
1008 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1012 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1013 ctx->lexer, start_ofs);
1020 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1021 struct ctables *ct, struct ctables_table *t,
1022 enum pivot_axis_type a)
1024 if (lex_token (lexer) == T_BY
1025 || lex_token (lexer) == T_SLASH
1026 || lex_token (lexer) == T_ENDCMD)
1029 struct ctables_axis_parse_ctx ctx = {
1035 t->axes[a] = ctables_axis_parse_stack (&ctx);
1036 return t->axes[a] != NULL;
1040 ctables_chisq_destroy (struct ctables_chisq *chisq)
1046 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1052 ctables_table_destroy (struct ctables_table *t)
1057 for (size_t i = 0; i < t->n_categories; i++)
1058 ctables_categories_unref (t->categories[i]);
1059 free (t->categories);
1061 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1062 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1063 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1067 ctables_chisq_destroy (t->chisq);
1068 ctables_pairwise_destroy (t->pairwise);
1073 ctables_destroy (struct ctables *ct)
1078 pivot_table_look_unref (ct->look);
1082 for (size_t i = 0; i < ct->n_tables; i++)
1083 ctables_table_destroy (ct->tables[i]);
1088 static struct ctables_cat_value
1089 ccvt_range (double low, double high)
1091 return (struct ctables_cat_value) {
1093 .range = { low, high }
1098 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1099 struct ctables_table *t)
1101 if (!lex_match_id (lexer, "VARIABLES"))
1103 lex_match (lexer, T_EQUALS);
1105 struct variable **vars;
1107 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1110 struct ctables_categories *c = xmalloc (sizeof *c);
1111 *c = (struct ctables_categories) { .n_refs = n_vars };
1112 for (size_t i = 0; i < n_vars; i++)
1114 struct ctables_categories **cp
1115 = &t->categories[var_get_dict_index (vars[i])];
1116 ctables_categories_unref (*cp);
1121 if (lex_match (lexer, T_LBRACK))
1123 size_t allocated_values = 0;
1126 if (c->n_values >= allocated_values)
1127 c->values = x2nrealloc (c->values, &allocated_values,
1130 struct ctables_cat_value *v = &c->values[c->n_values];
1131 if (lex_match_id (lexer, "OTHERNM"))
1132 v->type = CCVT_OTHERNM;
1133 else if (lex_match_id (lexer, "MISSING"))
1134 v->type = CCVT_MISSING;
1135 else if (lex_match_id (lexer, "SUBTOTAL"))
1136 *v = (struct ctables_cat_value)
1137 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1138 else if (lex_match_id (lexer, "HSUBTOTAL"))
1139 *v = (struct ctables_cat_value)
1140 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1141 else if (lex_match_id (lexer, "LO"))
1143 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1145 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1148 else if (lex_is_number (lexer))
1150 double number = lex_number (lexer);
1152 if (lex_match_id (lexer, "THRU"))
1154 v->type = CCVT_RANGE;
1155 v->range[0] = number;
1156 if (lex_match_id (lexer, "HI"))
1157 *v = ccvt_range (number, DBL_MAX);
1160 if (!lex_force_num (lexer))
1162 *v = ccvt_range (number, lex_number (lexer));
1167 *v = (struct ctables_cat_value) {
1168 .type = CCVT_NUMBER,
1172 else if (lex_is_string (lexer))
1174 *v = (struct ctables_cat_value) {
1175 .type = CCVT_STRING,
1176 .string = ss_xstrdup (lex_tokss (lexer)),
1182 lex_error (lexer, NULL);
1186 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1187 && lex_match (lexer, T_EQUALS))
1189 if (!lex_force_string (lexer))
1192 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1197 lex_match (lexer, T_COMMA);
1199 while (!lex_match (lexer, T_RBRACK));
1202 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1204 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1206 lex_match (lexer, T_EQUALS);
1207 if (lex_match_id (lexer, "A"))
1208 c->sort_ascending = true;
1209 else if (lex_match_id (lexer, "D"))
1210 c->sort_ascending = false;
1213 lex_error_expecting (lexer, "A", "D");
1217 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1219 lex_match (lexer, T_EQUALS);
1220 if (lex_match_id (lexer, "VALUE"))
1221 c->key = CTCS_VALUE;
1222 else if (lex_match_id (lexer, "LABEL"))
1223 c->key = CTCS_LABEL;
1226 c->key = CTCS_FUNCTION;
1227 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1230 if (lex_match (lexer, T_LPAREN))
1232 c->sort_func_var = parse_variable (lexer, dict);
1233 if (!c->sort_func_var)
1236 if (c->sort_func == CTSF_PTILE)
1238 lex_match (lexer, T_COMMA);
1239 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1241 c->percentile = lex_number (lexer);
1245 if (!lex_force_match (lexer, T_RPAREN))
1248 else if (ctables_function_availability (c->sort_func)
1251 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1256 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1258 lex_match (lexer, T_EQUALS);
1259 if (lex_match_id (lexer, "INCLUDE"))
1260 c->include_missing = true;
1261 else if (lex_match_id (lexer, "EXCLUDE"))
1262 c->include_missing = false;
1265 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1269 else if (lex_match_id (lexer, "TOTAL"))
1271 lex_match (lexer, T_EQUALS);
1272 if (!parse_bool (lexer, &c->show_totals))
1275 else if (lex_match_id (lexer, "LABEL"))
1277 lex_match (lexer, T_EQUALS);
1278 if (!lex_force_string (lexer))
1280 free (c->total_label);
1281 c->total_label = ss_xstrdup (lex_tokss (lexer));
1284 else if (lex_match_id (lexer, "POSITION"))
1286 lex_match (lexer, T_EQUALS);
1287 if (lex_match_id (lexer, "BEFORE"))
1288 c->totals_before = true;
1289 else if (lex_match_id (lexer, "AFTER"))
1290 c->totals_before = false;
1293 lex_error_expecting (lexer, "BEFORE", "AFTER");
1297 else if (lex_match_id (lexer, "EMPTY"))
1299 lex_match (lexer, T_EQUALS);
1300 if (lex_match_id (lexer, "INCLUDE"))
1301 c->show_empty = true;
1302 else if (lex_match_id (lexer, "EXCLUDE"))
1303 c->show_empty = false;
1306 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1313 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1314 "TOTAL", "LABEL", "POSITION", "EMPTY");
1316 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1324 var_array_uninit (struct var_array *va)
1331 var_array2_uninit (struct var_array2 *vaa)
1335 for (size_t i = 0; i < vaa->n; i++)
1336 var_array_uninit (&vaa->vas[i]);
1341 static struct var_array2
1342 nest_fts (struct var_array2 va0, struct var_array2 va1)
1349 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1350 for (size_t i = 0; i < va0.n; i++)
1351 for (size_t j = 0; j < va1.n; j++)
1353 const struct var_array *a = &va0.vas[i];
1354 const struct var_array *b = &va1.vas[j];
1356 size_t allocate = a->n + b->n;
1357 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1358 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1360 for (size_t k = 0; k < a->n; k++)
1361 vars[n++] = a->vars[k];
1362 for (size_t k = 0; k < b->n; k++)
1363 vars[n++] = b->vars[k];
1364 assert (n == allocate);
1366 const struct var_array *summary_src;
1367 if (!a->summary_var)
1369 else if (!b->summary_var)
1373 vaa.vas[vaa.n++] = (struct var_array) {
1375 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1376 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1379 .summaries = summary_src->summaries,
1380 .n_summaries = summary_src->n_summaries,
1381 .summary_var = summary_src->summary_var,
1384 var_array2_uninit (&va0);
1385 var_array2_uninit (&va1);
1389 static struct var_array2
1390 stack_fts (struct var_array2 va0, struct var_array2 va1)
1392 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1393 for (size_t i = 0; i < va0.n; i++)
1394 vaa.vas[vaa.n++] = va0.vas[i];
1395 for (size_t i = 0; i < va1.n; i++)
1396 vaa.vas[vaa.n++] = va1.vas[i];
1397 assert (vaa.n == va0.n + va1.n);
1403 static struct var_array2
1404 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1407 return (struct var_array2) { .n = 0 };
1412 assert (!a->var.is_mrset);
1414 struct variable **vars = xmalloc (sizeof *vars);
1417 struct var_array *va = xmalloc (sizeof *va);
1418 *va = (struct var_array) {
1421 .scale_idx = a->scale ? 0 : SIZE_MAX,
1423 if (a->n_summaries || a->scale)
1425 va->summaries = a->summaries;
1426 va->n_summaries = a->n_summaries;
1427 va->summary_var = a->var.var;
1429 return (struct var_array2) { .vas = va, .n = 1 };
1432 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1433 enumerate_fts (axis_type, a->subs[1]));
1436 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1437 enumerate_fts (axis_type, a->subs[1]));
1443 union ctables_summary
1445 /* COUNT, VALIDN, TOTALN. */
1452 /* MINIMUM, MAXIMUM, RANGE. */
1459 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1460 struct moments1 *moments;
1462 /* XXX percentiles, median, mode, multiple response */
1466 ctables_summary_init (union ctables_summary *s,
1467 const struct ctables_summary_spec *ss)
1469 switch (ss->function)
1473 case CTSF_ROWPCT_COUNT:
1474 case CTSF_COLPCT_COUNT:
1475 case CTSF_TABLEPCT_COUNT:
1476 case CTSF_SUBTABLEPCT_COUNT:
1477 case CTSF_LAYERPCT_COUNT:
1478 case CTSF_LAYERROWPCT_COUNT:
1479 case CTSF_LAYERCOLPCT_COUNT:
1480 case CTSF_ROWPCT_VALIDN:
1481 case CTSF_COLPCT_VALIDN:
1482 case CTSF_TABLEPCT_VALIDN:
1483 case CTSF_SUBTABLEPCT_VALIDN:
1484 case CTSF_LAYERPCT_VALIDN:
1485 case CTSF_LAYERROWPCT_VALIDN:
1486 case CTSF_LAYERCOLPCT_VALIDN:
1487 case CTSF_ROWPCT_TOTALN:
1488 case CTSF_COLPCT_TOTALN:
1489 case CTSF_TABLEPCT_TOTALN:
1490 case CTSF_SUBTABLEPCT_TOTALN:
1491 case CTSF_LAYERPCT_TOTALN:
1492 case CTSF_LAYERROWPCT_TOTALN:
1493 case CTSF_LAYERCOLPCT_TOTALN:
1498 s->missing = s->valid = 0;
1504 s->min = s->max = SYSMIS;
1512 case CTSF_ROWPCT_SUM:
1513 case CTSF_COLPCT_SUM:
1514 case CTSF_TABLEPCT_SUM:
1515 case CTSF_SUBTABLEPCT_SUM:
1516 case CTSF_LAYERPCT_SUM:
1517 case CTSF_LAYERROWPCT_SUM:
1518 case CTSF_LAYERCOLPCT_SUM:
1519 s->moments = moments1_create (MOMENT_VARIANCE);
1528 case CTSF_RESPONSES:
1529 case CTSF_ROWPCT_RESPONSES:
1530 case CTSF_COLPCT_RESPONSES:
1531 case CTSF_TABLEPCT_RESPONSES:
1532 case CTSF_SUBTABLEPCT_RESPONSES:
1533 case CTSF_LAYERPCT_RESPONSES:
1534 case CTSF_LAYERROWPCT_RESPONSES:
1535 case CTSF_LAYERCOLPCT_RESPONSES:
1536 case CTSF_ROWPCT_RESPONSES_COUNT:
1537 case CTSF_COLPCT_RESPONSES_COUNT:
1538 case CTSF_TABLEPCT_RESPONSES_COUNT:
1539 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1540 case CTSF_LAYERPCT_RESPONSES_COUNT:
1541 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1542 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1543 case CTSF_ROWPCT_COUNT_RESPONSES:
1544 case CTSF_COLPCT_COUNT_RESPONSES:
1545 case CTSF_TABLEPCT_COUNT_RESPONSES:
1546 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1547 case CTSF_LAYERPCT_COUNT_RESPONSES:
1548 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1549 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1555 ctables_summary_uninit (union ctables_summary *s,
1556 const struct ctables_summary_spec *ss)
1558 switch (ss->function)
1562 case CTSF_ROWPCT_COUNT:
1563 case CTSF_COLPCT_COUNT:
1564 case CTSF_TABLEPCT_COUNT:
1565 case CTSF_SUBTABLEPCT_COUNT:
1566 case CTSF_LAYERPCT_COUNT:
1567 case CTSF_LAYERROWPCT_COUNT:
1568 case CTSF_LAYERCOLPCT_COUNT:
1569 case CTSF_ROWPCT_VALIDN:
1570 case CTSF_COLPCT_VALIDN:
1571 case CTSF_TABLEPCT_VALIDN:
1572 case CTSF_SUBTABLEPCT_VALIDN:
1573 case CTSF_LAYERPCT_VALIDN:
1574 case CTSF_LAYERROWPCT_VALIDN:
1575 case CTSF_LAYERCOLPCT_VALIDN:
1576 case CTSF_ROWPCT_TOTALN:
1577 case CTSF_COLPCT_TOTALN:
1578 case CTSF_TABLEPCT_TOTALN:
1579 case CTSF_SUBTABLEPCT_TOTALN:
1580 case CTSF_LAYERPCT_TOTALN:
1581 case CTSF_LAYERROWPCT_TOTALN:
1582 case CTSF_LAYERCOLPCT_TOTALN:
1599 case CTSF_ROWPCT_SUM:
1600 case CTSF_COLPCT_SUM:
1601 case CTSF_TABLEPCT_SUM:
1602 case CTSF_SUBTABLEPCT_SUM:
1603 case CTSF_LAYERPCT_SUM:
1604 case CTSF_LAYERROWPCT_SUM:
1605 case CTSF_LAYERCOLPCT_SUM:
1606 moments1_destroy (s->moments);
1615 case CTSF_RESPONSES:
1616 case CTSF_ROWPCT_RESPONSES:
1617 case CTSF_COLPCT_RESPONSES:
1618 case CTSF_TABLEPCT_RESPONSES:
1619 case CTSF_SUBTABLEPCT_RESPONSES:
1620 case CTSF_LAYERPCT_RESPONSES:
1621 case CTSF_LAYERROWPCT_RESPONSES:
1622 case CTSF_LAYERCOLPCT_RESPONSES:
1623 case CTSF_ROWPCT_RESPONSES_COUNT:
1624 case CTSF_COLPCT_RESPONSES_COUNT:
1625 case CTSF_TABLEPCT_RESPONSES_COUNT:
1626 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1627 case CTSF_LAYERPCT_RESPONSES_COUNT:
1628 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1629 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1630 case CTSF_ROWPCT_COUNT_RESPONSES:
1631 case CTSF_COLPCT_COUNT_RESPONSES:
1632 case CTSF_TABLEPCT_COUNT_RESPONSES:
1633 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1634 case CTSF_LAYERPCT_COUNT_RESPONSES:
1635 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1636 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1642 ctables_summary_add (union ctables_summary *s,
1643 const struct ctables_summary_spec *ss,
1644 const struct variable *var, const union value *value,
1647 switch (ss->function)
1651 case CTSF_ROWPCT_COUNT:
1652 case CTSF_COLPCT_COUNT:
1653 case CTSF_TABLEPCT_COUNT:
1654 case CTSF_SUBTABLEPCT_COUNT:
1655 case CTSF_LAYERPCT_COUNT:
1656 case CTSF_LAYERROWPCT_COUNT:
1657 case CTSF_LAYERCOLPCT_COUNT:
1658 case CTSF_ROWPCT_VALIDN:
1659 case CTSF_COLPCT_VALIDN:
1660 case CTSF_TABLEPCT_VALIDN:
1661 case CTSF_SUBTABLEPCT_VALIDN:
1662 case CTSF_LAYERPCT_VALIDN:
1663 case CTSF_LAYERROWPCT_VALIDN:
1664 case CTSF_LAYERCOLPCT_VALIDN:
1665 case CTSF_ROWPCT_TOTALN:
1666 case CTSF_COLPCT_TOTALN:
1667 case CTSF_TABLEPCT_TOTALN:
1668 case CTSF_SUBTABLEPCT_TOTALN:
1669 case CTSF_LAYERPCT_TOTALN:
1670 case CTSF_LAYERROWPCT_TOTALN:
1671 case CTSF_LAYERCOLPCT_TOTALN:
1676 if (var_is_value_missing (var, value))
1677 s->missing += weight;
1685 if (!var_is_value_missing (var, value))
1687 assert (!var_is_alpha (var)); /* XXX? */
1688 if (s->min == SYSMIS || value->f < s->min)
1690 if (s->max == SYSMIS || value->f > s->max)
1700 case CTSF_ROWPCT_SUM:
1701 case CTSF_COLPCT_SUM:
1702 case CTSF_TABLEPCT_SUM:
1703 case CTSF_SUBTABLEPCT_SUM:
1704 case CTSF_LAYERPCT_SUM:
1705 case CTSF_LAYERROWPCT_SUM:
1706 case CTSF_LAYERCOLPCT_SUM:
1707 moments1_add (s->moments, value->f, weight);
1716 case CTSF_RESPONSES:
1717 case CTSF_ROWPCT_RESPONSES:
1718 case CTSF_COLPCT_RESPONSES:
1719 case CTSF_TABLEPCT_RESPONSES:
1720 case CTSF_SUBTABLEPCT_RESPONSES:
1721 case CTSF_LAYERPCT_RESPONSES:
1722 case CTSF_LAYERROWPCT_RESPONSES:
1723 case CTSF_LAYERCOLPCT_RESPONSES:
1724 case CTSF_ROWPCT_RESPONSES_COUNT:
1725 case CTSF_COLPCT_RESPONSES_COUNT:
1726 case CTSF_TABLEPCT_RESPONSES_COUNT:
1727 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1728 case CTSF_LAYERPCT_RESPONSES_COUNT:
1729 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1730 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1731 case CTSF_ROWPCT_COUNT_RESPONSES:
1732 case CTSF_COLPCT_COUNT_RESPONSES:
1733 case CTSF_TABLEPCT_COUNT_RESPONSES:
1734 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1735 case CTSF_LAYERPCT_COUNT_RESPONSES:
1736 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1737 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1743 ctables_summary_value (const struct ctables_cell *f,
1744 union ctables_summary *s,
1745 const struct ctables_summary_spec *ss)
1747 switch (ss->function)
1753 case CTSF_SUBTABLEPCT_COUNT:
1754 return f->domains[CTDT_SUBTABLE]->valid ? s->valid / f->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1756 case CTSF_ROWPCT_COUNT:
1757 return f->domains[CTDT_ROW]->valid ? s->valid / f->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1759 case CTSF_COLPCT_COUNT:
1760 return f->domains[CTDT_COL]->valid ? s->valid / f->domains[CTDT_COL]->valid * 100 : SYSMIS;
1762 case CTSF_TABLEPCT_COUNT:
1763 return f->domains[CTDT_TABLE]->valid ? s->valid / f->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1765 case CTSF_LAYERPCT_COUNT:
1766 return f->domains[CTDT_LAYER]->valid ? s->valid / f->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1768 case CTSF_LAYERROWPCT_COUNT:
1769 return f->domains[CTDT_LAYERROW]->valid ? s->valid / f->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1771 case CTSF_LAYERCOLPCT_COUNT:
1772 return f->domains[CTDT_LAYERCOL]->valid ? s->valid / f->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1774 case CTSF_ROWPCT_VALIDN:
1775 case CTSF_COLPCT_VALIDN:
1776 case CTSF_TABLEPCT_VALIDN:
1777 case CTSF_SUBTABLEPCT_VALIDN:
1778 case CTSF_LAYERPCT_VALIDN:
1779 case CTSF_LAYERROWPCT_VALIDN:
1780 case CTSF_LAYERCOLPCT_VALIDN:
1781 case CTSF_ROWPCT_TOTALN:
1782 case CTSF_COLPCT_TOTALN:
1783 case CTSF_TABLEPCT_TOTALN:
1784 case CTSF_SUBTABLEPCT_TOTALN:
1785 case CTSF_LAYERPCT_TOTALN:
1786 case CTSF_LAYERROWPCT_TOTALN:
1787 case CTSF_LAYERCOLPCT_TOTALN:
1792 return s->valid + s->missing;
1805 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1810 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1816 double weight, variance;
1817 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1818 return calc_semean (variance, weight);
1824 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1825 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1830 double weight, mean;
1831 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1832 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1838 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1842 case CTSF_ROWPCT_SUM:
1843 case CTSF_COLPCT_SUM:
1844 case CTSF_TABLEPCT_SUM:
1845 case CTSF_SUBTABLEPCT_SUM:
1846 case CTSF_LAYERPCT_SUM:
1847 case CTSF_LAYERROWPCT_SUM:
1848 case CTSF_LAYERCOLPCT_SUM:
1857 case CTSF_RESPONSES:
1858 case CTSF_ROWPCT_RESPONSES:
1859 case CTSF_COLPCT_RESPONSES:
1860 case CTSF_TABLEPCT_RESPONSES:
1861 case CTSF_SUBTABLEPCT_RESPONSES:
1862 case CTSF_LAYERPCT_RESPONSES:
1863 case CTSF_LAYERROWPCT_RESPONSES:
1864 case CTSF_LAYERCOLPCT_RESPONSES:
1865 case CTSF_ROWPCT_RESPONSES_COUNT:
1866 case CTSF_COLPCT_RESPONSES_COUNT:
1867 case CTSF_TABLEPCT_RESPONSES_COUNT:
1868 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1869 case CTSF_LAYERPCT_RESPONSES_COUNT:
1870 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1871 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1872 case CTSF_ROWPCT_COUNT_RESPONSES:
1873 case CTSF_COLPCT_COUNT_RESPONSES:
1874 case CTSF_TABLEPCT_COUNT_RESPONSES:
1875 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1876 case CTSF_LAYERPCT_COUNT_RESPONSES:
1877 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1878 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1885 struct ctables_cell_sort_aux
1887 const struct ctables_table *t;
1888 enum pivot_axis_type a;
1892 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
1894 const struct ctables_cell_sort_aux *aux = aux_;
1895 struct ctables_cell *const *ap = a_;
1896 struct ctables_cell *const *bp = b_;
1897 const struct ctables_cell *a = *ap;
1898 const struct ctables_cell *b = *bp;
1900 size_t a_idx = a->axes[aux->a].vaa_idx;
1901 size_t b_idx = b->axes[aux->a].vaa_idx;
1903 return a_idx < b_idx ? -1 : 1;
1905 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1906 for (size_t i = 0; i < va->n; i++)
1907 if (i != va->scale_idx)
1909 const struct variable *var = va->vars[i];
1910 const union value *val_a = &a->axes[aux->a].values[i];
1911 const union value *val_b = &b->axes[aux->a].values[i];
1912 int cmp = value_compare_3way (val_a, val_b, var_get_width (var));
1916 const struct ctables_categories *cats = aux->t->categories[var_get_dict_index (var)];
1919 else if (cats->n_values)
1921 const struct ctables_cat_value *a_cv = ctables_categories_match (cats, val_a, var);
1922 const struct ctables_cat_value *b_cv = ctables_categories_match (cats, val_b, var);
1923 assert (a_cv && b_cv);
1924 return (a_cv == b_cv ? cmp
1933 /* Nothing to do. */
1938 const char *a_label = var_lookup_value_label (var, val_a);
1939 const char *b_label = var_lookup_value_label (var, val_b);
1940 int label_cmp = (a_label
1941 ? (b_label ? strcmp (a_label, b_label) : 1)
1942 : (b_label ? -1 : 0));
1952 return cats->sort_ascending ? cmp : -cmp;
1961 For each ctables_table:
1962 For each combination of row vars:
1963 For each combination of column vars:
1964 For each combination of layer vars:
1966 Make a table of row values:
1967 Sort entries by row values
1968 Assign a 0-based index to each actual value
1969 Construct a dimension
1970 Make a table of column values
1971 Make a table of layer values
1973 Fill the table entry using the indexes from before.
1976 static struct ctables_domain *
1977 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *f,
1978 enum ctables_domain_type domain)
1981 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1983 size_t idx = f->axes[a].vaa_idx;
1984 const struct var_array *va = &t->vaas[a].vas[idx];
1985 hash = hash_int (idx, hash);
1986 for (size_t i = 0; i < va->n_domains[domain]; i++)
1988 size_t v_idx = va->domains[domain][i];
1989 hash = value_hash (&f->axes[a].values[v_idx],
1990 var_get_width (va->vars[v_idx]), hash);
1994 struct ctables_domain *d;
1995 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
1997 const struct ctables_cell *df = d->example;
1998 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2000 size_t idx = f->axes[a].vaa_idx;
2001 if (idx != df->axes[a].vaa_idx)
2004 const struct var_array *va = &t->vaas[a].vas[idx];
2005 for (size_t i = 0; i < va->n_domains[domain]; i++)
2007 size_t v_idx = va->domains[domain][i];
2008 if (!value_equal (&df->axes[a].values[v_idx],
2009 &f->axes[a].values[v_idx],
2010 var_get_width (va->vars[v_idx])))
2019 d = xmalloc (sizeof *d);
2020 *d = (struct ctables_domain) { .example = f };
2021 hmap_insert (&t->domains[domain], &d->node, hash);
2025 static const struct ctables_cat_value *
2026 ctables_categories_match (const struct ctables_categories *cats,
2027 const union value *v, const struct variable *var)
2029 const struct ctables_cat_value *othernm = NULL;
2030 for (size_t i = cats->n_values; i-- > 0; )
2032 const struct ctables_cat_value *cv = &cats->values[i];
2036 if (cv->number == v->f)
2044 if ((cv->range[0] == -DBL_MAX || v->f >= cv->range[0])
2045 && (cv->range[1] == DBL_MAX || v->f <= cv->range[1]))
2050 if (var_is_value_missing (var, v))
2060 case CCVT_HSUBTOTAL:
2065 return var_is_value_missing (var, v) ? NULL : othernm;
2069 ctables_cell_insert (struct ctables_table *t,
2070 const struct ccase *c,
2071 size_t ir, size_t ic, size_t il,
2074 size_t ix[PIVOT_N_AXES] = {
2075 [PIVOT_AXIS_ROW] = ir,
2076 [PIVOT_AXIS_COLUMN] = ic,
2077 [PIVOT_AXIS_LAYER] = il,
2079 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
2081 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2083 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2084 for (size_t i = 0; i < va->n; i++)
2086 if (i == va->scale_idx)
2089 const struct variable *var = va->vars[i];
2090 const union value *value = case_data (c, var);
2092 enum mv_class missing = var_is_value_missing (var, value);
2093 if (missing == MV_SYSTEM)
2096 const struct ctables_categories *cats = t->categories[var_get_dict_index (var)];
2102 else if (cats->n_values)
2104 if (!ctables_categories_match (cats, value, var))
2109 if (missing && !cats->include_missing)
2116 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2118 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2119 hash = hash_int (ix[a], hash);
2120 for (size_t i = 0; i < va->n; i++)
2121 if (i != va->scale_idx)
2122 hash = value_hash (case_data (c, va->vars[i]),
2123 var_get_width (va->vars[i]), hash);
2126 struct ctables_cell *f;
2127 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_cell, node, hash, &t->cells)
2129 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2131 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2132 if (f->axes[a].vaa_idx != ix[a])
2134 for (size_t i = 0; i < va->n; i++)
2135 if (i != va->scale_idx
2136 && !value_equal (case_data (c, va->vars[i]),
2137 &f->axes[a].values[i],
2138 var_get_width (va->vars[i])))
2147 f = xmalloc (sizeof *f);
2148 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2150 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2151 f->axes[a].vaa_idx = ix[a];
2152 f->axes[a].values = (va->n
2153 ? xnmalloc (va->n, sizeof *f->axes[a].values)
2155 for (size_t i = 0; i < va->n; i++)
2156 value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
2157 var_get_width (va->vars[i]));
2159 f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
2160 for (size_t i = 0; i < ss->n_summaries; i++)
2161 ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
2162 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2163 f->domains[dt] = ctables_domain_insert (t, f, dt);
2164 hmap_insert (&t->cells, &f->node, hash);
2167 for (size_t i = 0; i < ss->n_summaries; i++)
2168 ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
2169 case_data (c, ss->summary_var), weight);
2170 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2171 f->domains[dt]->valid += weight;
2175 ctables_execute (struct dataset *ds, struct ctables *ct)
2177 for (size_t i = 0; i < ct->n_tables; i++)
2179 struct ctables_table *t = ct->tables[i];
2180 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2183 t->vaas[a] = enumerate_fts (a, t->axes[a]);
2185 for (size_t j = 0; j < t->vaas[a].n; j++)
2187 struct var_array *va = &t->vaas[a].vas[j];
2188 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2190 va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
2191 va->n_domains[dt] = 0;
2193 for (size_t k = 0; k < va->n; k++)
2195 if (k == va->scale_idx)
2204 if (a != PIVOT_AXIS_LAYER)
2211 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2212 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2213 : a == PIVOT_AXIS_ROW)
2216 || (va->scale_idx == va->n - 1
2223 if (a == PIVOT_AXIS_COLUMN)
2228 if (a == PIVOT_AXIS_ROW)
2233 va->domains[dt][va->n_domains[dt]++] = k;
2240 struct var_array *va = xmalloc (sizeof *va);
2241 *va = (struct var_array) { .n = 0 };
2242 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
2245 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
2247 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
2248 if (!va->n_summaries)
2250 va->summaries = xmalloc (sizeof *va->summaries);
2251 va->n_summaries = 1;
2253 enum ctables_summary_function function
2254 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
2255 struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
2257 *va->summaries = (struct ctables_summary_spec) {
2258 .function = function,
2259 .format = ctables_summary_default_format (function, &var),
2260 .label = ctables_summary_default_label (function, 0),
2262 if (!va->summary_var)
2263 va->summary_var = va->vars[0];
2268 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2271 bool warn_on_invalid = true;
2272 double total_weight = 0;
2273 for (struct ccase *c = casereader_read (input); c;
2274 case_unref (c), c = casereader_read (input))
2276 double weight = dict_get_case_weight (dataset_dict (ds), c,
2278 total_weight += weight;
2280 for (size_t i = 0; i < ct->n_tables; i++)
2282 struct ctables_table *t = ct->tables[i];
2284 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2285 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2286 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2287 ctables_cell_insert (t, c, ir, ic, il, weight);
2290 casereader_destroy (input);
2292 for (size_t i = 0; i < ct->n_tables; i++)
2294 struct ctables_table *t = ct->tables[i];
2296 struct pivot_table *pt = pivot_table_create__ (
2298 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2299 : pivot_value_new_text (N_("Custom Tables"))),
2302 pivot_table_set_caption (
2303 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2305 pivot_table_set_caption (
2306 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2308 pivot_table_set_look (pt, ct->look);
2309 struct pivot_dimension *d[PIVOT_N_AXES];
2310 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2312 static const char *names[] = {
2313 [PIVOT_AXIS_ROW] = N_("Rows"),
2314 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2315 [PIVOT_AXIS_LAYER] = N_("Layers"),
2317 d[a] = (t->axes[a] || a == t->summary_axis
2318 ? pivot_dimension_create (pt, a, names[a])
2323 assert (t->axes[a]);
2325 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2327 struct ctables_cell *f;
2329 HMAP_FOR_EACH (f, struct ctables_cell, node, &t->cells)
2331 assert (n == t->cells.count);
2333 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2334 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2336 size_t max_depth = 0;
2337 for (size_t j = 0; j < t->vaas[a].n; j++)
2338 if (t->vaas[a].vas[j].n > max_depth)
2339 max_depth = t->vaas[a].vas[j].n;
2341 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2342 struct pivot_category *top = NULL;
2344 for (size_t j = 0; j < n; j++)
2346 struct ctables_cell *f = sorted[j];
2347 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2349 size_t n_common = 0;
2350 bool new_subtable = false;
2353 struct ctables_cell *prev = sorted[j - 1];
2354 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2356 for (; n_common < va->n; n_common++)
2357 if (n_common != va->scale_idx
2358 && !value_equal (&prev->axes[a].values[n_common],
2359 &f->axes[a].values[n_common],
2360 var_get_type (va->vars[n_common])))
2364 new_subtable = true;
2367 new_subtable = true;
2371 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2373 if (vlabel != CTVL_NONE)
2374 top = pivot_category_create_group__ (
2375 top, pivot_value_new_variable (va->vars[0]));
2377 if (n_common == va->n)
2379 f->axes[a].leaf = prev_leaf;
2383 for (size_t k = n_common; k < va->n; k++)
2385 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2387 struct pivot_value *label
2388 = (k != va->scale_idx
2389 ? pivot_value_new_var_value (va->vars[k],
2390 &f->axes[a].values[k])
2394 if (a == t->summary_axis)
2397 parent = pivot_category_create_group__ (parent, label);
2398 for (size_t m = 0; m < va->n_summaries; m++)
2400 int leaf = pivot_category_create_leaf (
2401 parent, pivot_value_new_text (va->summaries[m].label));
2408 /* This assertion is true as long as the summary axis
2409 is the axis where the summaries are displayed. */
2412 prev_leaf = pivot_category_create_leaf (parent, label);
2418 parent = pivot_category_create_group__ (parent, label);
2420 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2421 if (vlabel != CTVL_NONE)
2422 parent = pivot_category_create_group__ (
2423 parent, pivot_value_new_variable (va->vars[k + 1]));
2427 f->axes[a].leaf = prev_leaf;
2432 struct ctables_cell *f;
2433 HMAP_FOR_EACH (f, struct ctables_cell, node, &t->cells)
2435 const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2436 for (size_t j = 0; j < ss->n_summaries; j++)
2439 size_t n_dindexes = 0;
2441 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2444 int leaf = f->axes[a].leaf;
2445 if (a == t->summary_axis)
2447 dindexes[n_dindexes++] = leaf;
2450 double d = ctables_summary_value (f, &f->summaries[j], &ss->summaries[j]);
2451 struct pivot_value *value = pivot_value_new_number (d);
2452 value->numeric.format = ss->summaries[j].format;
2453 pivot_table_put (pt, dindexes, n_dindexes, value);
2457 pivot_table_submit (pt);
2460 return proc_commit (ds);
2464 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2466 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2467 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2468 enum settings_value_show tvars = settings_get_show_variables ();
2469 for (size_t i = 0; i < n_vars; i++)
2470 vlabels[i] = (enum ctables_vlabel) tvars;
2472 struct ctables *ct = xmalloc (sizeof *ct);
2473 *ct = (struct ctables) {
2474 .look = pivot_table_look_unshare (pivot_table_look_ref (
2475 pivot_table_look_get_default ())),
2477 .hide_threshold = 5,
2479 ct->look->omit_empty = false;
2481 if (!lex_force_match (lexer, T_SLASH))
2484 while (!lex_match_id (lexer, "TABLE"))
2486 if (lex_match_id (lexer, "FORMAT"))
2488 double widths[2] = { SYSMIS, SYSMIS };
2489 double units_per_inch = 72.0;
2491 while (lex_token (lexer) != T_SLASH)
2493 if (lex_match_id (lexer, "MINCOLWIDTH"))
2495 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2498 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2500 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2503 else if (lex_match_id (lexer, "UNITS"))
2505 lex_match (lexer, T_EQUALS);
2506 if (lex_match_id (lexer, "POINTS"))
2507 units_per_inch = 72.0;
2508 else if (lex_match_id (lexer, "INCHES"))
2509 units_per_inch = 1.0;
2510 else if (lex_match_id (lexer, "CM"))
2511 units_per_inch = 2.54;
2514 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2518 else if (lex_match_id (lexer, "EMPTY"))
2523 lex_match (lexer, T_EQUALS);
2524 if (lex_match_id (lexer, "ZERO"))
2526 /* Nothing to do. */
2528 else if (lex_match_id (lexer, "BLANK"))
2529 ct->zero = xstrdup ("");
2530 else if (lex_force_string (lexer))
2532 ct->zero = ss_xstrdup (lex_tokss (lexer));
2538 else if (lex_match_id (lexer, "MISSING"))
2540 lex_match (lexer, T_EQUALS);
2541 if (!lex_force_string (lexer))
2545 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2546 ? ss_xstrdup (lex_tokss (lexer))
2552 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2553 "UNITS", "EMPTY", "MISSING");
2558 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2559 && widths[0] > widths[1])
2561 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2565 for (size_t i = 0; i < 2; i++)
2566 if (widths[i] != SYSMIS)
2568 int *wr = ct->look->width_ranges[TABLE_HORZ];
2569 wr[i] = widths[i] / units_per_inch * 96.0;
2574 else if (lex_match_id (lexer, "VLABELS"))
2576 if (!lex_force_match_id (lexer, "VARIABLES"))
2578 lex_match (lexer, T_EQUALS);
2580 struct variable **vars;
2582 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2586 if (!lex_force_match_id (lexer, "DISPLAY"))
2591 lex_match (lexer, T_EQUALS);
2593 enum ctables_vlabel vlabel;
2594 if (lex_match_id (lexer, "DEFAULT"))
2595 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2596 else if (lex_match_id (lexer, "NAME"))
2598 else if (lex_match_id (lexer, "LABEL"))
2599 vlabel = CTVL_LABEL;
2600 else if (lex_match_id (lexer, "BOTH"))
2602 else if (lex_match_id (lexer, "NONE"))
2606 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2612 for (size_t i = 0; i < n_vars; i++)
2613 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2616 else if (lex_match_id (lexer, "MRSETS"))
2618 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2620 lex_match (lexer, T_EQUALS);
2621 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2624 else if (lex_match_id (lexer, "SMISSING"))
2626 if (lex_match_id (lexer, "VARIABLE"))
2627 ct->smissing_listwise = false;
2628 else if (lex_match_id (lexer, "LISTWISE"))
2629 ct->smissing_listwise = true;
2632 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2637 else if (lex_match_id (lexer, "WEIGHT"))
2639 if (!lex_force_match_id (lexer, "VARIABLE"))
2641 lex_match (lexer, T_EQUALS);
2642 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2643 if (!ct->base_weight)
2646 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2648 if (!lex_force_match_id (lexer, "COUNT"))
2650 lex_match (lexer, T_EQUALS);
2651 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2653 ct->hide_threshold = lex_integer (lexer);
2658 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2659 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2660 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2664 if (!lex_force_match (lexer, T_SLASH))
2668 size_t allocated_tables = 0;
2671 if (ct->n_tables >= allocated_tables)
2672 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2673 sizeof *ct->tables);
2675 struct ctables_table *t = xmalloc (sizeof *t);
2676 *t = (struct ctables_table) {
2677 .cells = HMAP_INITIALIZER (t->cells),
2678 .slabels_position = PIVOT_AXIS_COLUMN,
2679 .slabels_visible = true,
2680 .row_labels = CTLP_NORMAL,
2681 .col_labels = CTLP_NORMAL,
2682 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2683 sizeof *t->categories),
2684 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2687 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2688 hmap_init (&t->domains[dt]);
2689 ct->tables[ct->n_tables++] = t;
2691 lex_match (lexer, T_EQUALS);
2692 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2694 if (lex_match (lexer, T_BY))
2696 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2697 ct, t, PIVOT_AXIS_COLUMN))
2700 if (lex_match (lexer, T_BY))
2702 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2703 ct, t, PIVOT_AXIS_LAYER))
2708 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2709 && !t->axes[PIVOT_AXIS_LAYER])
2711 lex_error (lexer, _("At least one variable must be specified."));
2715 const struct ctables_axis *scales[PIVOT_N_AXES];
2716 size_t n_scales = 0;
2717 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2719 scales[a] = find_scale (t->axes[a]);
2725 msg (SE, _("Scale variables may appear only on one axis."));
2726 if (scales[PIVOT_AXIS_ROW])
2727 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2728 _("This scale variable appears on the rows axis."));
2729 if (scales[PIVOT_AXIS_COLUMN])
2730 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2731 _("This scale variable appears on the columns axis."));
2732 if (scales[PIVOT_AXIS_LAYER])
2733 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2734 _("This scale variable appears on the layer axis."));
2738 const struct ctables_axis *summaries[PIVOT_N_AXES];
2739 size_t n_summaries = 0;
2740 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2742 summaries[a] = (scales[a]
2744 : find_categorical_summary_spec (t->axes[a]));
2748 if (n_summaries > 1)
2750 msg (SE, _("Summaries may appear only on one axis."));
2751 if (summaries[PIVOT_AXIS_ROW])
2752 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2753 _("This variable on the rows axis has a summary."));
2754 if (summaries[PIVOT_AXIS_COLUMN])
2755 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2756 _("This variable on the columns axis has a summary."));
2757 if (summaries[PIVOT_AXIS_LAYER])
2758 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2759 _("This variable on the layers axis has a summary."));
2762 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2763 if (n_summaries ? summaries[a] : t->axes[a])
2765 t->summary_axis = a;
2769 if (lex_token (lexer) == T_ENDCMD)
2771 if (!lex_force_match (lexer, T_SLASH))
2774 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2776 if (lex_match_id (lexer, "SLABELS"))
2778 while (lex_token (lexer) != T_SLASH)
2780 if (lex_match_id (lexer, "POSITION"))
2782 lex_match (lexer, T_EQUALS);
2783 if (lex_match_id (lexer, "COLUMN"))
2784 t->slabels_position = PIVOT_AXIS_COLUMN;
2785 else if (lex_match_id (lexer, "ROW"))
2786 t->slabels_position = PIVOT_AXIS_ROW;
2787 else if (lex_match_id (lexer, "LAYER"))
2788 t->slabels_position = PIVOT_AXIS_LAYER;
2791 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2795 else if (lex_match_id (lexer, "VISIBLE"))
2797 lex_match (lexer, T_EQUALS);
2798 if (!parse_bool (lexer, &t->slabels_visible))
2803 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2808 else if (lex_match_id (lexer, "CLABELS"))
2810 while (lex_token (lexer) != T_SLASH)
2812 if (lex_match_id (lexer, "AUTO"))
2813 t->row_labels = t->col_labels = CTLP_NORMAL;
2814 else if (lex_match_id (lexer, "ROWLABELS"))
2816 lex_match (lexer, T_EQUALS);
2817 if (lex_match_id (lexer, "OPPOSITE"))
2818 t->row_labels = CTLP_OPPOSITE;
2819 else if (lex_match_id (lexer, "LAYER"))
2820 t->row_labels = CTLP_LAYER;
2823 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2827 else if (lex_match_id (lexer, "COLLABELS"))
2829 lex_match (lexer, T_EQUALS);
2830 if (lex_match_id (lexer, "OPPOSITE"))
2831 t->col_labels = CTLP_OPPOSITE;
2832 else if (lex_match_id (lexer, "LAYER"))
2833 t->col_labels = CTLP_LAYER;
2836 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2842 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2848 else if (lex_match_id (lexer, "CRITERIA"))
2850 if (!lex_force_match_id (lexer, "CILEVEL"))
2852 lex_match (lexer, T_EQUALS);
2854 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2856 t->cilevel = lex_number (lexer);
2859 else if (lex_match_id (lexer, "CATEGORIES"))
2861 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2864 else if (lex_match_id (lexer, "TITLES"))
2869 if (lex_match_id (lexer, "CAPTION"))
2870 textp = &t->caption;
2871 else if (lex_match_id (lexer, "CORNER"))
2873 else if (lex_match_id (lexer, "TITLE"))
2877 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2880 lex_match (lexer, T_EQUALS);
2882 struct string s = DS_EMPTY_INITIALIZER;
2883 while (lex_is_string (lexer))
2885 if (!ds_is_empty (&s))
2886 ds_put_byte (&s, ' ');
2887 ds_put_substring (&s, lex_tokss (lexer));
2891 *textp = ds_steal_cstr (&s);
2893 while (lex_token (lexer) != T_SLASH
2894 && lex_token (lexer) != T_ENDCMD);
2896 else if (lex_match_id (lexer, "SIGTEST"))
2900 t->chisq = xmalloc (sizeof *t->chisq);
2901 *t->chisq = (struct ctables_chisq) {
2903 .include_mrsets = true,
2904 .all_visible = true,
2910 if (lex_match_id (lexer, "TYPE"))
2912 lex_match (lexer, T_EQUALS);
2913 if (!lex_force_match_id (lexer, "CHISQUARE"))
2916 else if (lex_match_id (lexer, "ALPHA"))
2918 lex_match (lexer, T_EQUALS);
2919 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2921 t->chisq->alpha = lex_number (lexer);
2924 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2926 lex_match (lexer, T_EQUALS);
2927 if (parse_bool (lexer, &t->chisq->include_mrsets))
2930 else if (lex_match_id (lexer, "CATEGORIES"))
2932 lex_match (lexer, T_EQUALS);
2933 if (lex_match_id (lexer, "ALLVISIBLE"))
2934 t->chisq->all_visible = true;
2935 else if (lex_match_id (lexer, "SUBTOTALS"))
2936 t->chisq->all_visible = false;
2939 lex_error_expecting (lexer,
2940 "ALLVISIBLE", "SUBTOTALS");
2946 lex_error_expecting (lexer, "TYPE", "ALPHA",
2947 "INCLUDEMRSETS", "CATEGORIES");
2951 while (lex_token (lexer) != T_SLASH
2952 && lex_token (lexer) != T_ENDCMD);
2954 else if (lex_match_id (lexer, "COMPARETEST"))
2958 t->pairwise = xmalloc (sizeof *t->pairwise);
2959 *t->pairwise = (struct ctables_pairwise) {
2961 .alpha = { .05, .05 },
2962 .adjust = BONFERRONI,
2963 .include_mrsets = true,
2964 .meansvariance_allcats = true,
2965 .all_visible = true,
2974 if (lex_match_id (lexer, "TYPE"))
2976 lex_match (lexer, T_EQUALS);
2977 if (lex_match_id (lexer, "PROP"))
2978 t->pairwise->type = PROP;
2979 else if (lex_match_id (lexer, "MEAN"))
2980 t->pairwise->type = MEAN;
2983 lex_error_expecting (lexer, "PROP", "MEAN");
2987 else if (lex_match_id (lexer, "ALPHA"))
2989 lex_match (lexer, T_EQUALS);
2991 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2993 double a0 = lex_number (lexer);
2996 lex_match (lexer, T_COMMA);
2997 if (lex_is_number (lexer))
2999 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3001 double a1 = lex_number (lexer);
3004 t->pairwise->alpha[0] = MIN (a0, a1);
3005 t->pairwise->alpha[1] = MAX (a0, a1);
3008 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3010 else if (lex_match_id (lexer, "ADJUST"))
3012 lex_match (lexer, T_EQUALS);
3013 if (lex_match_id (lexer, "BONFERRONI"))
3014 t->pairwise->adjust = BONFERRONI;
3015 else if (lex_match_id (lexer, "BH"))
3016 t->pairwise->adjust = BH;
3017 else if (lex_match_id (lexer, "NONE"))
3018 t->pairwise->adjust = 0;
3021 lex_error_expecting (lexer, "BONFERRONI", "BH",
3026 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3028 lex_match (lexer, T_EQUALS);
3029 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3032 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3034 lex_match (lexer, T_EQUALS);
3035 if (lex_match_id (lexer, "ALLCATS"))
3036 t->pairwise->meansvariance_allcats = true;
3037 else if (lex_match_id (lexer, "TESTEDCATS"))
3038 t->pairwise->meansvariance_allcats = false;
3041 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3045 else if (lex_match_id (lexer, "CATEGORIES"))
3047 lex_match (lexer, T_EQUALS);
3048 if (lex_match_id (lexer, "ALLVISIBLE"))
3049 t->pairwise->all_visible = true;
3050 else if (lex_match_id (lexer, "SUBTOTALS"))
3051 t->pairwise->all_visible = false;
3054 lex_error_expecting (lexer, "ALLVISIBLE",
3059 else if (lex_match_id (lexer, "MERGE"))
3061 lex_match (lexer, T_EQUALS);
3062 if (!parse_bool (lexer, &t->pairwise->merge))
3065 else if (lex_match_id (lexer, "STYLE"))
3067 lex_match (lexer, T_EQUALS);
3068 if (lex_match_id (lexer, "APA"))
3069 t->pairwise->apa_style = true;
3070 else if (lex_match_id (lexer, "SIMPLE"))
3071 t->pairwise->apa_style = false;
3074 lex_error_expecting (lexer, "APA", "SIMPLE");
3078 else if (lex_match_id (lexer, "SHOWSIG"))
3080 lex_match (lexer, T_EQUALS);
3081 if (!parse_bool (lexer, &t->pairwise->show_sig))
3086 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3087 "INCLUDEMRSETS", "MEANSVARIANCE",
3088 "CATEGORIES", "MERGE", "STYLE",
3093 while (lex_token (lexer) != T_SLASH
3094 && lex_token (lexer) != T_ENDCMD);
3098 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3099 "CRITERIA", "CATEGORIES", "TITLES",
3100 "SIGTEST", "COMPARETEST");
3105 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3107 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3112 while (lex_token (lexer) != T_ENDCMD);
3114 bool ok = ctables_execute (ds, ct);
3115 ctables_destroy (ct);
3116 return ok ? CMD_SUCCESS : CMD_FAILURE;
3119 ctables_destroy (ct);