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];
186 struct ctables_cell_value
188 const struct ctables_category *category;
196 union ctables_summary *summaries;
201 struct pivot_table_look *look;
203 /* If this is NULL, zeros are displayed using the normal print format.
204 Otherwise, this string is displayed. */
207 /* If this is NULL, missing values are displayed using the normal print
208 format. Otherwise, this string is displayed. */
211 /* Indexed by variable dictionary index. */
212 enum ctables_vlabel *vlabels;
214 bool mrsets_count_duplicates; /* MRSETS. */
215 bool smissing_listwise; /* SMISSING. */
216 struct variable *base_weight; /* WEIGHT. */
217 int hide_threshold; /* HIDESMALLCOUNTS. */
219 struct ctables_table **tables;
223 struct ctables_postcompute
225 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
226 const char *name; /* Name, without leading &. */
228 struct ctables_postcompute_expr *expr;
231 bool hide_source_cats;
234 struct ctables_postcompute_expr
236 enum ctables_postcompute_op
244 /* XXX SUBTOTAL and HSUBTOTAL */
257 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
262 XXX what about string ranges? */
265 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
266 struct ctables_postcompute_expr *subs[2];
270 enum ctables_label_position
279 struct variable **vars;
282 size_t *domains[N_CTDTS];
283 size_t n_domains[N_CTDTS];
285 struct ctables_summary_spec *summaries;
287 struct variable *summary_var;
292 struct var_array *vas;
298 struct ctables_axis *axes[PIVOT_N_AXES];
299 struct var_array2 vaas[PIVOT_N_AXES];
300 enum pivot_axis_type summary_axis;
302 struct hmap domains[N_CTDTS];
304 enum pivot_axis_type slabels_position;
305 bool slabels_visible;
307 enum ctables_label_position row_labels;
308 enum ctables_label_position col_labels;
310 /* Indexed by variable dictionary index. */
311 struct ctables_categories **categories;
320 struct ctables_chisq *chisq;
321 struct ctables_pairwise *pairwise;
329 struct variable *var;
330 const struct mrset *mrset;
334 static const struct fmt_spec *
335 ctables_var_get_print_format (const struct ctables_var *var)
337 return (var->is_mrset
338 ? var_get_print_format (var->mrset->vars[0])
339 : var_get_print_format (var->var));
343 ctables_var_name (const struct ctables_var *var)
345 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
348 struct ctables_categories
351 struct ctables_category *cats;
356 struct ctables_category
358 enum ctables_category_type
378 double number; /* CCT_NUMBER. */
379 char *string; /* CCT_STRING. */
380 double range[2]; /* CCT_RANGE. */
381 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
383 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
386 bool include_missing;
390 enum ctables_summary_function sort_function;
391 struct variable *sort_var;
397 static const struct ctables_category *ctables_categories_match (
398 const struct ctables_categories *, const union value *,
399 const struct variable *);
402 ctables_category_uninit (struct ctables_category *cat)
422 free (cat->total_label);
433 ctables_categories_unref (struct ctables_categories *c)
438 assert (c->n_refs > 0);
442 for (size_t i = 0; i < c->n_cats; i++)
443 ctables_category_uninit (&c->cats[i]);
448 /* Chi-square test (SIGTEST). */
456 /* Pairwise comparison test (COMPARETEST). */
457 struct ctables_pairwise
459 enum { PROP, MEAN } type;
462 bool meansvariance_allcats;
464 enum { BONFERRONI = 1, BH } adjust;
488 struct ctables_var var;
490 struct ctables_summary_spec *summaries;
492 size_t allocated_summaries;
496 struct ctables_axis *subs[2];
499 struct msg_location *loc;
502 static void ctables_axis_destroy (struct ctables_axis *);
511 enum ctables_function_availability
513 CTFA_ALL, /* Any variables. */
514 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
515 CTFA_MRSETS, /* Only multiple-response sets */
518 struct ctables_summary_spec
520 enum ctables_summary_function function;
521 double percentile; /* CTSF_PTILE only. */
523 struct fmt_spec format; /* XXX extra CTABLES formats */
527 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
534 parse_col_width (struct lexer *lexer, const char *name, double *width)
536 lex_match (lexer, T_EQUALS);
537 if (lex_match_id (lexer, "DEFAULT"))
539 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
541 *width = lex_number (lexer);
551 parse_bool (struct lexer *lexer, bool *b)
553 if (lex_match_id (lexer, "NO"))
555 else if (lex_match_id (lexer, "YES"))
559 lex_error_expecting (lexer, "YES", "NO");
565 static enum ctables_function_availability
566 ctables_function_availability (enum ctables_summary_function f)
568 static enum ctables_function_availability availability[] = {
569 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
574 return availability[f];
578 parse_ctables_summary_function (struct lexer *lexer,
579 enum ctables_summary_function *f)
583 enum ctables_summary_function function;
584 struct substring name;
586 static struct pair names[] = {
587 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
588 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
591 /* The .COUNT suffix may be omitted. */
592 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
593 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
594 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
595 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
596 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
597 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
598 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
602 if (!lex_force_id (lexer))
605 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
606 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
608 *f = names[i].function;
613 lex_error (lexer, _("Expecting summary function name."));
618 ctables_axis_destroy (struct ctables_axis *axis)
626 for (size_t i = 0; i < axis->n_summaries; i++)
627 ctables_summary_spec_uninit (&axis->summaries[i]);
628 free (axis->summaries);
633 ctables_axis_destroy (axis->subs[0]);
634 ctables_axis_destroy (axis->subs[1]);
637 msg_location_destroy (axis->loc);
641 static struct ctables_axis *
642 ctables_axis_new_nonterminal (enum ctables_axis_op op,
643 struct ctables_axis *sub0,
644 struct ctables_axis *sub1,
645 struct lexer *lexer, int start_ofs)
647 struct ctables_axis *axis = xmalloc (sizeof *axis);
648 *axis = (struct ctables_axis) {
650 .subs = { sub0, sub1 },
651 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
656 struct ctables_axis_parse_ctx
659 struct dictionary *dict;
661 struct ctables_table *t;
664 static struct fmt_spec
665 ctables_summary_default_format (enum ctables_summary_function function,
666 const struct ctables_var *var)
668 static const enum ctables_format default_formats[] = {
669 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
673 switch (default_formats[function])
676 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
679 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
682 return *ctables_var_get_print_format (var);
690 ctables_summary_default_label (enum ctables_summary_function function,
693 static const char *default_labels[] = {
694 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
699 return (function == CTSF_PTILE
700 ? xasprintf (_("Percentile %.2f"), percentile)
701 : xstrdup (gettext (default_labels[function])));
705 ctables_summary_function_name (enum ctables_summary_function function)
707 static const char *names[] = {
708 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
712 return names[function];
716 add_summary_spec (struct ctables_axis *axis,
717 enum ctables_summary_function function, double percentile,
718 const char *label, const struct fmt_spec *format,
719 const struct msg_location *loc)
721 if (axis->op == CTAO_VAR)
723 if (axis->n_summaries >= axis->allocated_summaries)
724 axis->summaries = x2nrealloc (axis->summaries,
725 &axis->allocated_summaries,
726 sizeof *axis->summaries);
728 const char *function_name = ctables_summary_function_name (function);
729 const char *var_name = ctables_var_name (&axis->var);
730 switch (ctables_function_availability (function))
733 if (!axis->var.is_mrset)
735 msg_at (SE, loc, _("Summary function %s applies only to multiple "
736 "response sets."), function_name);
737 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
747 _("Summary function %s applies only to scale variables."),
749 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
759 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
760 *dst = (struct ctables_summary_spec) {
761 .function = function,
762 .percentile = percentile,
763 .label = xstrdup (label),
764 .format = (format ? *format
765 : ctables_summary_default_format (function, &axis->var)),
771 for (size_t i = 0; i < 2; i++)
772 if (!add_summary_spec (axis->subs[i], function, percentile, label,
779 static struct ctables_axis *ctables_axis_parse_stack (
780 struct ctables_axis_parse_ctx *);
783 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
784 struct ctables_var *var)
786 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
788 *var = (struct ctables_var) {
790 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
794 lex_error (lexer, _("'%s' does not name a multiple-response set "
795 "in the active file dictionary."),
796 lex_tokcstr (lexer));
804 *var = (struct ctables_var) {
806 .var = parse_variable (lexer, dict),
808 return var->var != NULL;
812 static struct ctables_axis *
813 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
815 if (lex_match (ctx->lexer, T_LPAREN))
817 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
818 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
820 ctables_axis_destroy (sub);
826 if (!lex_force_id (ctx->lexer))
829 int start_ofs = lex_ofs (ctx->lexer);
830 struct ctables_var var;
831 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
834 struct ctables_axis *axis = xmalloc (sizeof *axis);
835 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
837 /* XXX should figure out default measures by reading data */
838 axis->scale = (var.is_mrset ? false
839 : lex_match_phrase (ctx->lexer, "[S]") ? true
840 : lex_match_phrase (ctx->lexer, "[C]") ? false
841 : var_get_measure (var.var) == MEASURE_SCALE);
842 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
843 lex_ofs (ctx->lexer) - 1);
847 static struct ctables_axis *
848 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
850 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
851 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
856 int start_ofs = lex_ofs (ctx->lexer);
858 /* Parse function. */
859 enum ctables_summary_function function;
860 if (!parse_ctables_summary_function (ctx->lexer, &function))
863 /* Parse percentile. */
864 double percentile = 0;
865 if (function == CTSF_PTILE)
867 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
869 percentile = lex_number (ctx->lexer);
870 lex_get (ctx->lexer);
875 if (lex_is_string (ctx->lexer))
877 label = ss_xstrdup (lex_tokss (ctx->lexer));
878 lex_get (ctx->lexer);
881 label = ctables_summary_default_label (function, percentile);
884 struct fmt_spec format;
885 const struct fmt_spec *formatp;
886 if (lex_token (ctx->lexer) == T_ID)
888 if (!parse_format_specifier (ctx->lexer, &format)
889 || !fmt_check_output (&format)
890 || !fmt_check_type_compat (&format, VAL_NUMERIC))
900 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
901 lex_ofs (ctx->lexer) - 1);
902 add_summary_spec (sub, function, percentile, label, formatp, loc);
904 msg_location_destroy (loc);
906 lex_match (ctx->lexer, T_COMMA);
908 while (!lex_match (ctx->lexer, T_RBRACK));
913 ctables_axis_destroy (sub);
917 static const struct ctables_axis *
918 find_scale (const struct ctables_axis *axis)
922 else if (axis->op == CTAO_VAR)
926 assert (!axis->var.is_mrset);
934 for (size_t i = 0; i < 2; i++)
936 const struct ctables_axis *scale = find_scale (axis->subs[i]);
944 static const struct ctables_axis *
945 find_categorical_summary_spec (const struct ctables_axis *axis)
949 else if (axis->op == CTAO_VAR)
950 return !axis->scale && axis->n_summaries ? axis : NULL;
953 for (size_t i = 0; i < 2; i++)
955 const struct ctables_axis *sum
956 = find_categorical_summary_spec (axis->subs[i]);
964 static struct ctables_axis *
965 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
967 int start_ofs = lex_ofs (ctx->lexer);
968 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
972 while (lex_match (ctx->lexer, T_GT))
974 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
978 struct ctables_axis *nest = ctables_axis_new_nonterminal (
979 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
981 const struct ctables_axis *outer_scale = find_scale (lhs);
982 const struct ctables_axis *inner_scale = find_scale (rhs);
983 if (outer_scale && inner_scale)
985 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
986 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
987 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
988 ctables_axis_destroy (nest);
992 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
995 msg_at (SE, nest->loc,
996 _("Summaries may only be requested for categorical variables "
997 "at the innermost nesting level."));
998 msg_at (SN, outer_sum->loc,
999 _("This outer categorical variable has a summary."));
1000 ctables_axis_destroy (nest);
1010 static struct ctables_axis *
1011 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1013 int start_ofs = lex_ofs (ctx->lexer);
1014 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1018 while (lex_match (ctx->lexer, T_PLUS))
1020 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1024 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1025 ctx->lexer, start_ofs);
1032 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1033 struct ctables *ct, struct ctables_table *t,
1034 enum pivot_axis_type a)
1036 if (lex_token (lexer) == T_BY
1037 || lex_token (lexer) == T_SLASH
1038 || lex_token (lexer) == T_ENDCMD)
1041 struct ctables_axis_parse_ctx ctx = {
1047 t->axes[a] = ctables_axis_parse_stack (&ctx);
1048 return t->axes[a] != NULL;
1052 ctables_chisq_destroy (struct ctables_chisq *chisq)
1058 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1064 ctables_table_destroy (struct ctables_table *t)
1069 for (size_t i = 0; i < t->n_categories; i++)
1070 ctables_categories_unref (t->categories[i]);
1071 free (t->categories);
1073 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1074 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1075 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1079 ctables_chisq_destroy (t->chisq);
1080 ctables_pairwise_destroy (t->pairwise);
1085 ctables_destroy (struct ctables *ct)
1090 pivot_table_look_unref (ct->look);
1094 for (size_t i = 0; i < ct->n_tables; i++)
1095 ctables_table_destroy (ct->tables[i]);
1100 static struct ctables_category
1101 cct_range (double low, double high)
1103 return (struct ctables_category) {
1105 .range = { low, high }
1110 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1111 struct ctables_table *t)
1113 if (!lex_match_id (lexer, "VARIABLES"))
1115 lex_match (lexer, T_EQUALS);
1117 struct variable **vars;
1119 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1122 struct ctables_categories *c = xmalloc (sizeof *c);
1123 *c = (struct ctables_categories) { .n_refs = n_vars };
1124 for (size_t i = 0; i < n_vars; i++)
1126 struct ctables_categories **cp
1127 = &t->categories[var_get_dict_index (vars[i])];
1128 ctables_categories_unref (*cp);
1133 size_t allocated_cats = 0;
1134 if (lex_match (lexer, T_LBRACK))
1138 if (c->n_cats >= allocated_cats)
1139 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1141 struct ctables_category *cat = &c->cats[c->n_cats];
1142 if (lex_match_id (lexer, "OTHERNM"))
1143 cat->type = CCT_OTHERNM;
1144 else if (lex_match_id (lexer, "MISSING"))
1145 cat->type = CCT_MISSING;
1146 else if (lex_match_id (lexer, "SUBTOTAL"))
1147 *cat = (struct ctables_category)
1148 { .type = CCT_SUBTOTAL, .total_label = NULL };
1149 else if (lex_match_id (lexer, "HSUBTOTAL"))
1150 *cat = (struct ctables_category)
1151 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1152 else if (lex_match_id (lexer, "LO"))
1154 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1156 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1159 else if (lex_is_number (lexer))
1161 double number = lex_number (lexer);
1163 if (lex_match_id (lexer, "THRU"))
1165 cat->type = CCT_RANGE;
1166 cat->range[0] = number;
1167 if (lex_match_id (lexer, "HI"))
1168 *cat = cct_range (number, DBL_MAX);
1171 if (!lex_force_num (lexer))
1173 *cat = cct_range (number, lex_number (lexer));
1178 *cat = (struct ctables_category) {
1183 else if (lex_is_string (lexer))
1185 *cat = (struct ctables_category) {
1187 .string = ss_xstrdup (lex_tokss (lexer)),
1193 lex_error (lexer, NULL);
1197 if ((cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1198 && lex_match (lexer, T_EQUALS))
1200 if (!lex_force_string (lexer))
1203 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1208 lex_match (lexer, T_COMMA);
1210 while (!lex_match (lexer, T_RBRACK));
1213 struct ctables_category cat = {
1215 .include_missing = false,
1216 .sort_ascending = true,
1218 bool show_totals = false;
1219 char *total_label = NULL;
1220 bool totals_before = false;
1221 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1223 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1225 lex_match (lexer, T_EQUALS);
1226 if (lex_match_id (lexer, "A"))
1227 cat.sort_ascending = true;
1228 else if (lex_match_id (lexer, "D"))
1229 cat.sort_ascending = false;
1232 lex_error_expecting (lexer, "A", "D");
1236 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1238 lex_match (lexer, T_EQUALS);
1239 if (lex_match_id (lexer, "VALUE"))
1240 cat.type = CCT_VALUE;
1241 else if (lex_match_id (lexer, "LABEL"))
1242 cat.type = CCT_LABEL;
1245 cat.type = CCT_FUNCTION;
1246 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1249 if (lex_match (lexer, T_LPAREN))
1251 cat.sort_var = parse_variable (lexer, dict);
1255 if (cat.sort_function == CTSF_PTILE)
1257 lex_match (lexer, T_COMMA);
1258 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1260 cat.percentile = lex_number (lexer);
1264 if (!lex_force_match (lexer, T_RPAREN))
1267 else if (ctables_function_availability (cat.sort_function)
1270 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1275 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1277 lex_match (lexer, T_EQUALS);
1278 if (lex_match_id (lexer, "INCLUDE"))
1279 cat.include_missing = true;
1280 else if (lex_match_id (lexer, "EXCLUDE"))
1281 cat.include_missing = false;
1284 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1288 else if (lex_match_id (lexer, "TOTAL"))
1290 lex_match (lexer, T_EQUALS);
1291 if (!parse_bool (lexer, &show_totals))
1294 else if (lex_match_id (lexer, "LABEL"))
1296 lex_match (lexer, T_EQUALS);
1297 if (!lex_force_string (lexer))
1300 total_label = ss_xstrdup (lex_tokss (lexer));
1303 else if (lex_match_id (lexer, "POSITION"))
1305 lex_match (lexer, T_EQUALS);
1306 if (lex_match_id (lexer, "BEFORE"))
1307 totals_before = true;
1308 else if (lex_match_id (lexer, "AFTER"))
1309 totals_before = false;
1312 lex_error_expecting (lexer, "BEFORE", "AFTER");
1316 else if (lex_match_id (lexer, "EMPTY"))
1318 lex_match (lexer, T_EQUALS);
1319 if (lex_match_id (lexer, "INCLUDE"))
1320 c->show_empty = true;
1321 else if (lex_match_id (lexer, "EXCLUDE"))
1322 c->show_empty = false;
1325 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1332 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1333 "TOTAL", "LABEL", "POSITION", "EMPTY");
1335 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1342 if (c->n_cats >= allocated_cats)
1343 c->cats = x2nrealloc (c->cats, &allocated_cats,
1345 c->cats[c->n_cats++] = cat;
1350 if (c->n_cats >= allocated_cats)
1351 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1353 struct ctables_category *totals;
1356 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1357 totals = &c->cats[0];
1360 totals = &c->cats[c->n_cats];
1363 *totals = (struct ctables_category) {
1365 .total_label = total_label ? total_label : xstrdup (_("Total")),
1373 var_array_uninit (struct var_array *va)
1380 var_array2_uninit (struct var_array2 *vaa)
1384 for (size_t i = 0; i < vaa->n; i++)
1385 var_array_uninit (&vaa->vas[i]);
1390 static struct var_array2
1391 nest_fts (struct var_array2 va0, struct var_array2 va1)
1398 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1399 for (size_t i = 0; i < va0.n; i++)
1400 for (size_t j = 0; j < va1.n; j++)
1402 const struct var_array *a = &va0.vas[i];
1403 const struct var_array *b = &va1.vas[j];
1405 size_t allocate = a->n + b->n;
1406 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1407 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1409 for (size_t k = 0; k < a->n; k++)
1410 vars[n++] = a->vars[k];
1411 for (size_t k = 0; k < b->n; k++)
1412 vars[n++] = b->vars[k];
1413 assert (n == allocate);
1415 const struct var_array *summary_src;
1416 if (!a->summary_var)
1418 else if (!b->summary_var)
1422 vaa.vas[vaa.n++] = (struct var_array) {
1424 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1425 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1428 .summaries = summary_src->summaries,
1429 .n_summaries = summary_src->n_summaries,
1430 .summary_var = summary_src->summary_var,
1433 var_array2_uninit (&va0);
1434 var_array2_uninit (&va1);
1438 static struct var_array2
1439 stack_fts (struct var_array2 va0, struct var_array2 va1)
1441 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1442 for (size_t i = 0; i < va0.n; i++)
1443 vaa.vas[vaa.n++] = va0.vas[i];
1444 for (size_t i = 0; i < va1.n; i++)
1445 vaa.vas[vaa.n++] = va1.vas[i];
1446 assert (vaa.n == va0.n + va1.n);
1452 static struct var_array2
1453 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1456 return (struct var_array2) { .n = 0 };
1461 assert (!a->var.is_mrset);
1463 struct variable **vars = xmalloc (sizeof *vars);
1466 struct var_array *va = xmalloc (sizeof *va);
1467 *va = (struct var_array) {
1470 .scale_idx = a->scale ? 0 : SIZE_MAX,
1472 if (a->n_summaries || a->scale)
1474 va->summaries = a->summaries;
1475 va->n_summaries = a->n_summaries;
1476 va->summary_var = a->var.var;
1478 return (struct var_array2) { .vas = va, .n = 1 };
1481 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1482 enumerate_fts (axis_type, a->subs[1]));
1485 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1486 enumerate_fts (axis_type, a->subs[1]));
1492 union ctables_summary
1494 /* COUNT, VALIDN, TOTALN. */
1501 /* MINIMUM, MAXIMUM, RANGE. */
1508 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1509 struct moments1 *moments;
1511 /* XXX percentiles, median, mode, multiple response */
1515 ctables_summary_init (union ctables_summary *s,
1516 const struct ctables_summary_spec *ss)
1518 switch (ss->function)
1522 case CTSF_ROWPCT_COUNT:
1523 case CTSF_COLPCT_COUNT:
1524 case CTSF_TABLEPCT_COUNT:
1525 case CTSF_SUBTABLEPCT_COUNT:
1526 case CTSF_LAYERPCT_COUNT:
1527 case CTSF_LAYERROWPCT_COUNT:
1528 case CTSF_LAYERCOLPCT_COUNT:
1529 case CTSF_ROWPCT_VALIDN:
1530 case CTSF_COLPCT_VALIDN:
1531 case CTSF_TABLEPCT_VALIDN:
1532 case CTSF_SUBTABLEPCT_VALIDN:
1533 case CTSF_LAYERPCT_VALIDN:
1534 case CTSF_LAYERROWPCT_VALIDN:
1535 case CTSF_LAYERCOLPCT_VALIDN:
1536 case CTSF_ROWPCT_TOTALN:
1537 case CTSF_COLPCT_TOTALN:
1538 case CTSF_TABLEPCT_TOTALN:
1539 case CTSF_SUBTABLEPCT_TOTALN:
1540 case CTSF_LAYERPCT_TOTALN:
1541 case CTSF_LAYERROWPCT_TOTALN:
1542 case CTSF_LAYERCOLPCT_TOTALN:
1547 s->missing = s->valid = 0;
1553 s->min = s->max = SYSMIS;
1561 case CTSF_ROWPCT_SUM:
1562 case CTSF_COLPCT_SUM:
1563 case CTSF_TABLEPCT_SUM:
1564 case CTSF_SUBTABLEPCT_SUM:
1565 case CTSF_LAYERPCT_SUM:
1566 case CTSF_LAYERROWPCT_SUM:
1567 case CTSF_LAYERCOLPCT_SUM:
1568 s->moments = moments1_create (MOMENT_VARIANCE);
1577 case CTSF_RESPONSES:
1578 case CTSF_ROWPCT_RESPONSES:
1579 case CTSF_COLPCT_RESPONSES:
1580 case CTSF_TABLEPCT_RESPONSES:
1581 case CTSF_SUBTABLEPCT_RESPONSES:
1582 case CTSF_LAYERPCT_RESPONSES:
1583 case CTSF_LAYERROWPCT_RESPONSES:
1584 case CTSF_LAYERCOLPCT_RESPONSES:
1585 case CTSF_ROWPCT_RESPONSES_COUNT:
1586 case CTSF_COLPCT_RESPONSES_COUNT:
1587 case CTSF_TABLEPCT_RESPONSES_COUNT:
1588 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1589 case CTSF_LAYERPCT_RESPONSES_COUNT:
1590 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1591 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1592 case CTSF_ROWPCT_COUNT_RESPONSES:
1593 case CTSF_COLPCT_COUNT_RESPONSES:
1594 case CTSF_TABLEPCT_COUNT_RESPONSES:
1595 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1596 case CTSF_LAYERPCT_COUNT_RESPONSES:
1597 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1598 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1604 ctables_summary_uninit (union ctables_summary *s,
1605 const struct ctables_summary_spec *ss)
1607 switch (ss->function)
1611 case CTSF_ROWPCT_COUNT:
1612 case CTSF_COLPCT_COUNT:
1613 case CTSF_TABLEPCT_COUNT:
1614 case CTSF_SUBTABLEPCT_COUNT:
1615 case CTSF_LAYERPCT_COUNT:
1616 case CTSF_LAYERROWPCT_COUNT:
1617 case CTSF_LAYERCOLPCT_COUNT:
1618 case CTSF_ROWPCT_VALIDN:
1619 case CTSF_COLPCT_VALIDN:
1620 case CTSF_TABLEPCT_VALIDN:
1621 case CTSF_SUBTABLEPCT_VALIDN:
1622 case CTSF_LAYERPCT_VALIDN:
1623 case CTSF_LAYERROWPCT_VALIDN:
1624 case CTSF_LAYERCOLPCT_VALIDN:
1625 case CTSF_ROWPCT_TOTALN:
1626 case CTSF_COLPCT_TOTALN:
1627 case CTSF_TABLEPCT_TOTALN:
1628 case CTSF_SUBTABLEPCT_TOTALN:
1629 case CTSF_LAYERPCT_TOTALN:
1630 case CTSF_LAYERROWPCT_TOTALN:
1631 case CTSF_LAYERCOLPCT_TOTALN:
1648 case CTSF_ROWPCT_SUM:
1649 case CTSF_COLPCT_SUM:
1650 case CTSF_TABLEPCT_SUM:
1651 case CTSF_SUBTABLEPCT_SUM:
1652 case CTSF_LAYERPCT_SUM:
1653 case CTSF_LAYERROWPCT_SUM:
1654 case CTSF_LAYERCOLPCT_SUM:
1655 moments1_destroy (s->moments);
1664 case CTSF_RESPONSES:
1665 case CTSF_ROWPCT_RESPONSES:
1666 case CTSF_COLPCT_RESPONSES:
1667 case CTSF_TABLEPCT_RESPONSES:
1668 case CTSF_SUBTABLEPCT_RESPONSES:
1669 case CTSF_LAYERPCT_RESPONSES:
1670 case CTSF_LAYERROWPCT_RESPONSES:
1671 case CTSF_LAYERCOLPCT_RESPONSES:
1672 case CTSF_ROWPCT_RESPONSES_COUNT:
1673 case CTSF_COLPCT_RESPONSES_COUNT:
1674 case CTSF_TABLEPCT_RESPONSES_COUNT:
1675 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1676 case CTSF_LAYERPCT_RESPONSES_COUNT:
1677 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1678 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1679 case CTSF_ROWPCT_COUNT_RESPONSES:
1680 case CTSF_COLPCT_COUNT_RESPONSES:
1681 case CTSF_TABLEPCT_COUNT_RESPONSES:
1682 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1683 case CTSF_LAYERPCT_COUNT_RESPONSES:
1684 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1685 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1691 ctables_summary_add (union ctables_summary *s,
1692 const struct ctables_summary_spec *ss,
1693 const struct variable *var, const union value *value,
1696 switch (ss->function)
1700 case CTSF_ROWPCT_COUNT:
1701 case CTSF_COLPCT_COUNT:
1702 case CTSF_TABLEPCT_COUNT:
1703 case CTSF_SUBTABLEPCT_COUNT:
1704 case CTSF_LAYERPCT_COUNT:
1705 case CTSF_LAYERROWPCT_COUNT:
1706 case CTSF_LAYERCOLPCT_COUNT:
1707 case CTSF_ROWPCT_VALIDN:
1708 case CTSF_COLPCT_VALIDN:
1709 case CTSF_TABLEPCT_VALIDN:
1710 case CTSF_SUBTABLEPCT_VALIDN:
1711 case CTSF_LAYERPCT_VALIDN:
1712 case CTSF_LAYERROWPCT_VALIDN:
1713 case CTSF_LAYERCOLPCT_VALIDN:
1714 case CTSF_ROWPCT_TOTALN:
1715 case CTSF_COLPCT_TOTALN:
1716 case CTSF_TABLEPCT_TOTALN:
1717 case CTSF_SUBTABLEPCT_TOTALN:
1718 case CTSF_LAYERPCT_TOTALN:
1719 case CTSF_LAYERROWPCT_TOTALN:
1720 case CTSF_LAYERCOLPCT_TOTALN:
1725 if (var_is_value_missing (var, value))
1726 s->missing += weight;
1734 if (!var_is_value_missing (var, value))
1736 assert (!var_is_alpha (var)); /* XXX? */
1737 if (s->min == SYSMIS || value->f < s->min)
1739 if (s->max == SYSMIS || value->f > s->max)
1749 case CTSF_ROWPCT_SUM:
1750 case CTSF_COLPCT_SUM:
1751 case CTSF_TABLEPCT_SUM:
1752 case CTSF_SUBTABLEPCT_SUM:
1753 case CTSF_LAYERPCT_SUM:
1754 case CTSF_LAYERROWPCT_SUM:
1755 case CTSF_LAYERCOLPCT_SUM:
1756 moments1_add (s->moments, value->f, weight);
1765 case CTSF_RESPONSES:
1766 case CTSF_ROWPCT_RESPONSES:
1767 case CTSF_COLPCT_RESPONSES:
1768 case CTSF_TABLEPCT_RESPONSES:
1769 case CTSF_SUBTABLEPCT_RESPONSES:
1770 case CTSF_LAYERPCT_RESPONSES:
1771 case CTSF_LAYERROWPCT_RESPONSES:
1772 case CTSF_LAYERCOLPCT_RESPONSES:
1773 case CTSF_ROWPCT_RESPONSES_COUNT:
1774 case CTSF_COLPCT_RESPONSES_COUNT:
1775 case CTSF_TABLEPCT_RESPONSES_COUNT:
1776 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1777 case CTSF_LAYERPCT_RESPONSES_COUNT:
1778 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1779 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1780 case CTSF_ROWPCT_COUNT_RESPONSES:
1781 case CTSF_COLPCT_COUNT_RESPONSES:
1782 case CTSF_TABLEPCT_COUNT_RESPONSES:
1783 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1784 case CTSF_LAYERPCT_COUNT_RESPONSES:
1785 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1786 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1792 ctables_summary_value (const struct ctables_cell *f,
1793 union ctables_summary *s,
1794 const struct ctables_summary_spec *ss)
1796 switch (ss->function)
1802 case CTSF_SUBTABLEPCT_COUNT:
1803 return f->domains[CTDT_SUBTABLE]->valid ? s->valid / f->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1805 case CTSF_ROWPCT_COUNT:
1806 return f->domains[CTDT_ROW]->valid ? s->valid / f->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1808 case CTSF_COLPCT_COUNT:
1809 return f->domains[CTDT_COL]->valid ? s->valid / f->domains[CTDT_COL]->valid * 100 : SYSMIS;
1811 case CTSF_TABLEPCT_COUNT:
1812 return f->domains[CTDT_TABLE]->valid ? s->valid / f->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1814 case CTSF_LAYERPCT_COUNT:
1815 return f->domains[CTDT_LAYER]->valid ? s->valid / f->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1817 case CTSF_LAYERROWPCT_COUNT:
1818 return f->domains[CTDT_LAYERROW]->valid ? s->valid / f->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1820 case CTSF_LAYERCOLPCT_COUNT:
1821 return f->domains[CTDT_LAYERCOL]->valid ? s->valid / f->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1823 case CTSF_ROWPCT_VALIDN:
1824 case CTSF_COLPCT_VALIDN:
1825 case CTSF_TABLEPCT_VALIDN:
1826 case CTSF_SUBTABLEPCT_VALIDN:
1827 case CTSF_LAYERPCT_VALIDN:
1828 case CTSF_LAYERROWPCT_VALIDN:
1829 case CTSF_LAYERCOLPCT_VALIDN:
1830 case CTSF_ROWPCT_TOTALN:
1831 case CTSF_COLPCT_TOTALN:
1832 case CTSF_TABLEPCT_TOTALN:
1833 case CTSF_SUBTABLEPCT_TOTALN:
1834 case CTSF_LAYERPCT_TOTALN:
1835 case CTSF_LAYERROWPCT_TOTALN:
1836 case CTSF_LAYERCOLPCT_TOTALN:
1841 return s->valid + s->missing;
1854 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1859 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1865 double weight, variance;
1866 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1867 return calc_semean (variance, weight);
1873 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1874 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1879 double weight, mean;
1880 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1881 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1887 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1891 case CTSF_ROWPCT_SUM:
1892 case CTSF_COLPCT_SUM:
1893 case CTSF_TABLEPCT_SUM:
1894 case CTSF_SUBTABLEPCT_SUM:
1895 case CTSF_LAYERPCT_SUM:
1896 case CTSF_LAYERROWPCT_SUM:
1897 case CTSF_LAYERCOLPCT_SUM:
1906 case CTSF_RESPONSES:
1907 case CTSF_ROWPCT_RESPONSES:
1908 case CTSF_COLPCT_RESPONSES:
1909 case CTSF_TABLEPCT_RESPONSES:
1910 case CTSF_SUBTABLEPCT_RESPONSES:
1911 case CTSF_LAYERPCT_RESPONSES:
1912 case CTSF_LAYERROWPCT_RESPONSES:
1913 case CTSF_LAYERCOLPCT_RESPONSES:
1914 case CTSF_ROWPCT_RESPONSES_COUNT:
1915 case CTSF_COLPCT_RESPONSES_COUNT:
1916 case CTSF_TABLEPCT_RESPONSES_COUNT:
1917 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1918 case CTSF_LAYERPCT_RESPONSES_COUNT:
1919 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1920 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1921 case CTSF_ROWPCT_COUNT_RESPONSES:
1922 case CTSF_COLPCT_COUNT_RESPONSES:
1923 case CTSF_TABLEPCT_COUNT_RESPONSES:
1924 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1925 case CTSF_LAYERPCT_COUNT_RESPONSES:
1926 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1927 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1934 struct ctables_cell_sort_aux
1936 const struct ctables_table *t;
1937 enum pivot_axis_type a;
1941 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
1943 const struct ctables_cell_sort_aux *aux = aux_;
1944 struct ctables_cell *const *ap = a_;
1945 struct ctables_cell *const *bp = b_;
1946 const struct ctables_cell *a = *ap;
1947 const struct ctables_cell *b = *bp;
1949 size_t a_idx = a->axes[aux->a].vaa_idx;
1950 size_t b_idx = b->axes[aux->a].vaa_idx;
1952 return a_idx < b_idx ? -1 : 1;
1954 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1955 for (size_t i = 0; i < va->n; i++)
1956 if (i != va->scale_idx)
1958 const struct variable *var = va->vars[i];
1959 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
1960 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
1961 if (a_cv->category != b_cv->category)
1962 return a_cv->category > b_cv->category ? 1 : -1;
1964 const union value *a_val = &a_cv->value;
1965 const union value *b_val = &b_cv->value;
1966 switch (a_cv->category->type)
1973 /* Must be equal. */
1980 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
1988 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
1990 return a_cv->category->sort_ascending ? cmp : -cmp;
1996 const char *a_label = var_lookup_value_label (var, a_val);
1997 const char *b_label = var_lookup_value_label (var, b_val);
1999 ? (b_label ? strcmp (a_label, b_label) : 1)
2000 : (b_label ? -1 : value_compare_3way (
2001 a_val, b_val, var_get_width (var))));
2003 return a_cv->category->sort_ascending ? cmp : -cmp;
2017 For each ctables_table:
2018 For each combination of row vars:
2019 For each combination of column vars:
2020 For each combination of layer vars:
2022 Make a table of row values:
2023 Sort entries by row values
2024 Assign a 0-based index to each actual value
2025 Construct a dimension
2026 Make a table of column values
2027 Make a table of layer values
2029 Fill the table entry using the indexes from before.
2032 static struct ctables_domain *
2033 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *f,
2034 enum ctables_domain_type domain)
2037 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2039 size_t idx = f->axes[a].vaa_idx;
2040 const struct var_array *va = &t->vaas[a].vas[idx];
2041 hash = hash_int (idx, hash);
2042 for (size_t i = 0; i < va->n_domains[domain]; i++)
2044 size_t v_idx = va->domains[domain][i];
2045 hash = value_hash (&f->axes[a].cvs[v_idx].value,
2046 var_get_width (va->vars[v_idx]), hash);
2050 struct ctables_domain *d;
2051 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2053 const struct ctables_cell *df = d->example;
2054 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2056 size_t idx = f->axes[a].vaa_idx;
2057 if (idx != df->axes[a].vaa_idx)
2060 const struct var_array *va = &t->vaas[a].vas[idx];
2061 for (size_t i = 0; i < va->n_domains[domain]; i++)
2063 size_t v_idx = va->domains[domain][i];
2064 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2065 &f->axes[a].cvs[v_idx].value,
2066 var_get_width (va->vars[v_idx])))
2075 d = xmalloc (sizeof *d);
2076 *d = (struct ctables_domain) { .example = f };
2077 hmap_insert (&t->domains[domain], &d->node, hash);
2081 static const struct ctables_category *
2082 ctables_categories_match (const struct ctables_categories *c,
2083 const union value *v, const struct variable *var)
2085 const struct ctables_category *othernm = NULL;
2086 for (size_t i = c->n_cats; i-- > 0; )
2088 const struct ctables_category *cat = &c->cats[i];
2092 if (cat->number == v->f)
2100 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2101 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2106 if (var_is_value_missing (var, v))
2123 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2128 return var_is_value_missing (var, v) ? NULL : othernm;
2132 ctables_cell_insert (struct ctables_table *t,
2133 const struct ccase *c,
2134 size_t ir, size_t ic, size_t il,
2137 size_t ix[PIVOT_N_AXES] = {
2138 [PIVOT_AXIS_ROW] = ir,
2139 [PIVOT_AXIS_COLUMN] = ic,
2140 [PIVOT_AXIS_LAYER] = il,
2142 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
2144 const struct ctables_category *cats[PIVOT_N_AXES][10];
2145 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2147 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2148 for (size_t i = 0; i < va->n; i++)
2150 if (i == va->scale_idx)
2153 const struct variable *var = va->vars[i];
2154 const union value *value = case_data (c, var);
2156 if (var_is_numeric (var) && value->f == SYSMIS)
2159 cats[a][i] = ctables_categories_match (
2160 t->categories[var_get_dict_index (var)], value, var);
2167 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2169 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2170 hash = hash_int (ix[a], hash);
2171 for (size_t i = 0; i < va->n; i++)
2172 if (i != va->scale_idx)
2173 hash = value_hash (case_data (c, va->vars[i]),
2174 var_get_width (va->vars[i]), hash);
2177 struct ctables_cell *f;
2178 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_cell, node, hash, &t->cells)
2180 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2182 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2183 if (f->axes[a].vaa_idx != ix[a])
2185 for (size_t i = 0; i < va->n; i++)
2186 if (i != va->scale_idx
2187 && !value_equal (case_data (c, va->vars[i]),
2188 &f->axes[a].cvs[i].value,
2189 var_get_width (va->vars[i])))
2198 f = xmalloc (sizeof *f);
2199 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2201 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2202 f->axes[a].vaa_idx = ix[a];
2203 f->axes[a].cvs = (va->n
2204 ? xnmalloc (va->n, sizeof *f->axes[a].cvs)
2206 for (size_t i = 0; i < va->n; i++)
2208 f->axes[a].cvs[i].category = cats[a][i];
2209 value_clone (&f->axes[a].cvs[i].value, case_data (c, va->vars[i]),
2210 var_get_width (va->vars[i]));
2213 f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
2214 for (size_t i = 0; i < ss->n_summaries; i++)
2215 ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
2216 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2217 f->domains[dt] = ctables_domain_insert (t, f, dt);
2218 hmap_insert (&t->cells, &f->node, hash);
2221 for (size_t i = 0; i < ss->n_summaries; i++)
2222 ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
2223 case_data (c, ss->summary_var), weight);
2224 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2225 f->domains[dt]->valid += weight;
2229 ctables_execute (struct dataset *ds, struct ctables *ct)
2231 for (size_t i = 0; i < ct->n_tables; i++)
2233 struct ctables_table *t = ct->tables[i];
2234 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2237 t->vaas[a] = enumerate_fts (a, t->axes[a]);
2239 for (size_t j = 0; j < t->vaas[a].n; j++)
2241 struct var_array *va = &t->vaas[a].vas[j];
2242 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2244 va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
2245 va->n_domains[dt] = 0;
2247 for (size_t k = 0; k < va->n; k++)
2249 if (k == va->scale_idx)
2258 if (a != PIVOT_AXIS_LAYER)
2265 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2266 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2267 : a == PIVOT_AXIS_ROW)
2270 || (va->scale_idx == va->n - 1
2277 if (a == PIVOT_AXIS_COLUMN)
2282 if (a == PIVOT_AXIS_ROW)
2287 va->domains[dt][va->n_domains[dt]++] = k;
2294 struct var_array *va = xmalloc (sizeof *va);
2295 *va = (struct var_array) { .n = 0 };
2296 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
2299 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
2301 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
2302 if (!va->n_summaries)
2304 va->summaries = xmalloc (sizeof *va->summaries);
2305 va->n_summaries = 1;
2307 enum ctables_summary_function function
2308 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
2309 struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
2311 *va->summaries = (struct ctables_summary_spec) {
2312 .function = function,
2313 .format = ctables_summary_default_format (function, &var),
2314 .label = ctables_summary_default_label (function, 0),
2316 if (!va->summary_var)
2317 va->summary_var = va->vars[0];
2322 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2325 bool warn_on_invalid = true;
2326 double total_weight = 0;
2327 for (struct ccase *c = casereader_read (input); c;
2328 case_unref (c), c = casereader_read (input))
2330 double weight = dict_get_case_weight (dataset_dict (ds), c,
2332 total_weight += weight;
2334 for (size_t i = 0; i < ct->n_tables; i++)
2336 struct ctables_table *t = ct->tables[i];
2338 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2339 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2340 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2341 ctables_cell_insert (t, c, ir, ic, il, weight);
2344 casereader_destroy (input);
2346 for (size_t i = 0; i < ct->n_tables; i++)
2348 struct ctables_table *t = ct->tables[i];
2350 struct pivot_table *pt = pivot_table_create__ (
2352 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2353 : pivot_value_new_text (N_("Custom Tables"))),
2356 pivot_table_set_caption (
2357 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2359 pivot_table_set_caption (
2360 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2362 pivot_table_set_look (pt, ct->look);
2363 struct pivot_dimension *d[PIVOT_N_AXES];
2364 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2366 static const char *names[] = {
2367 [PIVOT_AXIS_ROW] = N_("Rows"),
2368 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2369 [PIVOT_AXIS_LAYER] = N_("Layers"),
2371 d[a] = (t->axes[a] || a == t->summary_axis
2372 ? pivot_dimension_create (pt, a, names[a])
2377 assert (t->axes[a]);
2379 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2381 struct ctables_cell *f;
2383 HMAP_FOR_EACH (f, struct ctables_cell, node, &t->cells)
2385 assert (n == t->cells.count);
2387 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2388 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2390 size_t max_depth = 0;
2391 for (size_t j = 0; j < t->vaas[a].n; j++)
2392 if (t->vaas[a].vas[j].n > max_depth)
2393 max_depth = t->vaas[a].vas[j].n;
2395 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2396 struct pivot_category *top = NULL;
2398 for (size_t j = 0; j < n; j++)
2400 struct ctables_cell *f = sorted[j];
2401 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2403 size_t n_common = 0;
2404 bool new_subtable = false;
2407 struct ctables_cell *prev = sorted[j - 1];
2408 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2410 for (; n_common < va->n; n_common++)
2411 if (n_common != va->scale_idx
2412 && !value_equal (&prev->axes[a].cvs[n_common].value,
2413 &f->axes[a].cvs[n_common].value,
2414 var_get_type (va->vars[n_common])))
2418 new_subtable = true;
2421 new_subtable = true;
2425 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2427 if (vlabel != CTVL_NONE)
2428 top = pivot_category_create_group__ (
2429 top, pivot_value_new_variable (va->vars[0]));
2431 if (n_common == va->n)
2433 f->axes[a].leaf = prev_leaf;
2437 for (size_t k = n_common; k < va->n; k++)
2439 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2441 struct pivot_value *label
2442 = (k != va->scale_idx
2443 ? pivot_value_new_var_value (va->vars[k],
2444 &f->axes[a].cvs[k].value)
2448 if (a == t->summary_axis)
2451 parent = pivot_category_create_group__ (parent, label);
2452 for (size_t m = 0; m < va->n_summaries; m++)
2454 int leaf = pivot_category_create_leaf (
2455 parent, pivot_value_new_text (va->summaries[m].label));
2462 /* This assertion is true as long as the summary axis
2463 is the axis where the summaries are displayed. */
2466 prev_leaf = pivot_category_create_leaf (parent, label);
2472 parent = pivot_category_create_group__ (parent, label);
2474 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2475 if (vlabel != CTVL_NONE)
2476 parent = pivot_category_create_group__ (
2477 parent, pivot_value_new_variable (va->vars[k + 1]));
2481 f->axes[a].leaf = prev_leaf;
2486 struct ctables_cell *f;
2487 HMAP_FOR_EACH (f, struct ctables_cell, node, &t->cells)
2489 const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2490 for (size_t j = 0; j < ss->n_summaries; j++)
2493 size_t n_dindexes = 0;
2495 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2498 int leaf = f->axes[a].leaf;
2499 if (a == t->summary_axis)
2501 dindexes[n_dindexes++] = leaf;
2504 double d = ctables_summary_value (f, &f->summaries[j], &ss->summaries[j]);
2505 struct pivot_value *value = pivot_value_new_number (d);
2506 value->numeric.format = ss->summaries[j].format;
2507 pivot_table_put (pt, dindexes, n_dindexes, value);
2511 pivot_table_submit (pt);
2514 return proc_commit (ds);
2518 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2520 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2521 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2522 enum settings_value_show tvars = settings_get_show_variables ();
2523 for (size_t i = 0; i < n_vars; i++)
2524 vlabels[i] = (enum ctables_vlabel) tvars;
2526 struct ctables *ct = xmalloc (sizeof *ct);
2527 *ct = (struct ctables) {
2528 .look = pivot_table_look_unshare (pivot_table_look_ref (
2529 pivot_table_look_get_default ())),
2531 .hide_threshold = 5,
2533 ct->look->omit_empty = false;
2535 if (!lex_force_match (lexer, T_SLASH))
2538 while (!lex_match_id (lexer, "TABLE"))
2540 if (lex_match_id (lexer, "FORMAT"))
2542 double widths[2] = { SYSMIS, SYSMIS };
2543 double units_per_inch = 72.0;
2545 while (lex_token (lexer) != T_SLASH)
2547 if (lex_match_id (lexer, "MINCOLWIDTH"))
2549 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2552 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2554 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2557 else if (lex_match_id (lexer, "UNITS"))
2559 lex_match (lexer, T_EQUALS);
2560 if (lex_match_id (lexer, "POINTS"))
2561 units_per_inch = 72.0;
2562 else if (lex_match_id (lexer, "INCHES"))
2563 units_per_inch = 1.0;
2564 else if (lex_match_id (lexer, "CM"))
2565 units_per_inch = 2.54;
2568 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2572 else if (lex_match_id (lexer, "EMPTY"))
2577 lex_match (lexer, T_EQUALS);
2578 if (lex_match_id (lexer, "ZERO"))
2580 /* Nothing to do. */
2582 else if (lex_match_id (lexer, "BLANK"))
2583 ct->zero = xstrdup ("");
2584 else if (lex_force_string (lexer))
2586 ct->zero = ss_xstrdup (lex_tokss (lexer));
2592 else if (lex_match_id (lexer, "MISSING"))
2594 lex_match (lexer, T_EQUALS);
2595 if (!lex_force_string (lexer))
2599 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2600 ? ss_xstrdup (lex_tokss (lexer))
2606 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2607 "UNITS", "EMPTY", "MISSING");
2612 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2613 && widths[0] > widths[1])
2615 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2619 for (size_t i = 0; i < 2; i++)
2620 if (widths[i] != SYSMIS)
2622 int *wr = ct->look->width_ranges[TABLE_HORZ];
2623 wr[i] = widths[i] / units_per_inch * 96.0;
2628 else if (lex_match_id (lexer, "VLABELS"))
2630 if (!lex_force_match_id (lexer, "VARIABLES"))
2632 lex_match (lexer, T_EQUALS);
2634 struct variable **vars;
2636 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2640 if (!lex_force_match_id (lexer, "DISPLAY"))
2645 lex_match (lexer, T_EQUALS);
2647 enum ctables_vlabel vlabel;
2648 if (lex_match_id (lexer, "DEFAULT"))
2649 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2650 else if (lex_match_id (lexer, "NAME"))
2652 else if (lex_match_id (lexer, "LABEL"))
2653 vlabel = CTVL_LABEL;
2654 else if (lex_match_id (lexer, "BOTH"))
2656 else if (lex_match_id (lexer, "NONE"))
2660 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2666 for (size_t i = 0; i < n_vars; i++)
2667 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2670 else if (lex_match_id (lexer, "MRSETS"))
2672 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2674 lex_match (lexer, T_EQUALS);
2675 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2678 else if (lex_match_id (lexer, "SMISSING"))
2680 if (lex_match_id (lexer, "VARIABLE"))
2681 ct->smissing_listwise = false;
2682 else if (lex_match_id (lexer, "LISTWISE"))
2683 ct->smissing_listwise = true;
2686 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2691 else if (lex_match_id (lexer, "WEIGHT"))
2693 if (!lex_force_match_id (lexer, "VARIABLE"))
2695 lex_match (lexer, T_EQUALS);
2696 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2697 if (!ct->base_weight)
2700 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2702 if (!lex_force_match_id (lexer, "COUNT"))
2704 lex_match (lexer, T_EQUALS);
2705 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2707 ct->hide_threshold = lex_integer (lexer);
2712 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2713 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2714 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2718 if (!lex_force_match (lexer, T_SLASH))
2722 size_t allocated_tables = 0;
2725 if (ct->n_tables >= allocated_tables)
2726 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2727 sizeof *ct->tables);
2729 struct ctables_category *cat = xmalloc (sizeof *cat);
2730 *cat = (struct ctables_category) {
2732 .include_missing = false,
2733 .sort_ascending = true,
2736 struct ctables_categories *c = xmalloc (sizeof *c);
2737 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2738 *c = (struct ctables_categories) {
2744 struct ctables_categories **categories = xnmalloc (n_vars,
2745 sizeof *categories);
2746 for (size_t i = 0; i < n_vars; i++)
2749 struct ctables_table *t = xmalloc (sizeof *t);
2750 *t = (struct ctables_table) {
2751 .cells = HMAP_INITIALIZER (t->cells),
2752 .slabels_position = PIVOT_AXIS_COLUMN,
2753 .slabels_visible = true,
2754 .row_labels = CTLP_NORMAL,
2755 .col_labels = CTLP_NORMAL,
2756 .categories = categories,
2757 .n_categories = n_vars,
2760 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2761 hmap_init (&t->domains[dt]);
2762 ct->tables[ct->n_tables++] = t;
2764 lex_match (lexer, T_EQUALS);
2765 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2767 if (lex_match (lexer, T_BY))
2769 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2770 ct, t, PIVOT_AXIS_COLUMN))
2773 if (lex_match (lexer, T_BY))
2775 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2776 ct, t, PIVOT_AXIS_LAYER))
2781 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2782 && !t->axes[PIVOT_AXIS_LAYER])
2784 lex_error (lexer, _("At least one variable must be specified."));
2788 const struct ctables_axis *scales[PIVOT_N_AXES];
2789 size_t n_scales = 0;
2790 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2792 scales[a] = find_scale (t->axes[a]);
2798 msg (SE, _("Scale variables may appear only on one axis."));
2799 if (scales[PIVOT_AXIS_ROW])
2800 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2801 _("This scale variable appears on the rows axis."));
2802 if (scales[PIVOT_AXIS_COLUMN])
2803 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2804 _("This scale variable appears on the columns axis."));
2805 if (scales[PIVOT_AXIS_LAYER])
2806 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2807 _("This scale variable appears on the layer axis."));
2811 const struct ctables_axis *summaries[PIVOT_N_AXES];
2812 size_t n_summaries = 0;
2813 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2815 summaries[a] = (scales[a]
2817 : find_categorical_summary_spec (t->axes[a]));
2821 if (n_summaries > 1)
2823 msg (SE, _("Summaries may appear only on one axis."));
2824 if (summaries[PIVOT_AXIS_ROW])
2825 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2826 _("This variable on the rows axis has a summary."));
2827 if (summaries[PIVOT_AXIS_COLUMN])
2828 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2829 _("This variable on the columns axis has a summary."));
2830 if (summaries[PIVOT_AXIS_LAYER])
2831 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2832 _("This variable on the layers axis has a summary."));
2835 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2836 if (n_summaries ? summaries[a] : t->axes[a])
2838 t->summary_axis = a;
2842 if (lex_token (lexer) == T_ENDCMD)
2844 if (!lex_force_match (lexer, T_SLASH))
2847 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2849 if (lex_match_id (lexer, "SLABELS"))
2851 while (lex_token (lexer) != T_SLASH)
2853 if (lex_match_id (lexer, "POSITION"))
2855 lex_match (lexer, T_EQUALS);
2856 if (lex_match_id (lexer, "COLUMN"))
2857 t->slabels_position = PIVOT_AXIS_COLUMN;
2858 else if (lex_match_id (lexer, "ROW"))
2859 t->slabels_position = PIVOT_AXIS_ROW;
2860 else if (lex_match_id (lexer, "LAYER"))
2861 t->slabels_position = PIVOT_AXIS_LAYER;
2864 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2868 else if (lex_match_id (lexer, "VISIBLE"))
2870 lex_match (lexer, T_EQUALS);
2871 if (!parse_bool (lexer, &t->slabels_visible))
2876 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2881 else if (lex_match_id (lexer, "CLABELS"))
2883 while (lex_token (lexer) != T_SLASH)
2885 if (lex_match_id (lexer, "AUTO"))
2886 t->row_labels = t->col_labels = CTLP_NORMAL;
2887 else if (lex_match_id (lexer, "ROWLABELS"))
2889 lex_match (lexer, T_EQUALS);
2890 if (lex_match_id (lexer, "OPPOSITE"))
2891 t->row_labels = CTLP_OPPOSITE;
2892 else if (lex_match_id (lexer, "LAYER"))
2893 t->row_labels = CTLP_LAYER;
2896 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2900 else if (lex_match_id (lexer, "COLLABELS"))
2902 lex_match (lexer, T_EQUALS);
2903 if (lex_match_id (lexer, "OPPOSITE"))
2904 t->col_labels = CTLP_OPPOSITE;
2905 else if (lex_match_id (lexer, "LAYER"))
2906 t->col_labels = CTLP_LAYER;
2909 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2915 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2921 else if (lex_match_id (lexer, "CRITERIA"))
2923 if (!lex_force_match_id (lexer, "CILEVEL"))
2925 lex_match (lexer, T_EQUALS);
2927 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2929 t->cilevel = lex_number (lexer);
2932 else if (lex_match_id (lexer, "CATEGORIES"))
2934 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2937 else if (lex_match_id (lexer, "TITLES"))
2942 if (lex_match_id (lexer, "CAPTION"))
2943 textp = &t->caption;
2944 else if (lex_match_id (lexer, "CORNER"))
2946 else if (lex_match_id (lexer, "TITLE"))
2950 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2953 lex_match (lexer, T_EQUALS);
2955 struct string s = DS_EMPTY_INITIALIZER;
2956 while (lex_is_string (lexer))
2958 if (!ds_is_empty (&s))
2959 ds_put_byte (&s, ' ');
2960 ds_put_substring (&s, lex_tokss (lexer));
2964 *textp = ds_steal_cstr (&s);
2966 while (lex_token (lexer) != T_SLASH
2967 && lex_token (lexer) != T_ENDCMD);
2969 else if (lex_match_id (lexer, "SIGTEST"))
2973 t->chisq = xmalloc (sizeof *t->chisq);
2974 *t->chisq = (struct ctables_chisq) {
2976 .include_mrsets = true,
2977 .all_visible = true,
2983 if (lex_match_id (lexer, "TYPE"))
2985 lex_match (lexer, T_EQUALS);
2986 if (!lex_force_match_id (lexer, "CHISQUARE"))
2989 else if (lex_match_id (lexer, "ALPHA"))
2991 lex_match (lexer, T_EQUALS);
2992 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2994 t->chisq->alpha = lex_number (lexer);
2997 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2999 lex_match (lexer, T_EQUALS);
3000 if (parse_bool (lexer, &t->chisq->include_mrsets))
3003 else if (lex_match_id (lexer, "CATEGORIES"))
3005 lex_match (lexer, T_EQUALS);
3006 if (lex_match_id (lexer, "ALLVISIBLE"))
3007 t->chisq->all_visible = true;
3008 else if (lex_match_id (lexer, "SUBTOTALS"))
3009 t->chisq->all_visible = false;
3012 lex_error_expecting (lexer,
3013 "ALLVISIBLE", "SUBTOTALS");
3019 lex_error_expecting (lexer, "TYPE", "ALPHA",
3020 "INCLUDEMRSETS", "CATEGORIES");
3024 while (lex_token (lexer) != T_SLASH
3025 && lex_token (lexer) != T_ENDCMD);
3027 else if (lex_match_id (lexer, "COMPARETEST"))
3031 t->pairwise = xmalloc (sizeof *t->pairwise);
3032 *t->pairwise = (struct ctables_pairwise) {
3034 .alpha = { .05, .05 },
3035 .adjust = BONFERRONI,
3036 .include_mrsets = true,
3037 .meansvariance_allcats = true,
3038 .all_visible = true,
3047 if (lex_match_id (lexer, "TYPE"))
3049 lex_match (lexer, T_EQUALS);
3050 if (lex_match_id (lexer, "PROP"))
3051 t->pairwise->type = PROP;
3052 else if (lex_match_id (lexer, "MEAN"))
3053 t->pairwise->type = MEAN;
3056 lex_error_expecting (lexer, "PROP", "MEAN");
3060 else if (lex_match_id (lexer, "ALPHA"))
3062 lex_match (lexer, T_EQUALS);
3064 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3066 double a0 = lex_number (lexer);
3069 lex_match (lexer, T_COMMA);
3070 if (lex_is_number (lexer))
3072 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3074 double a1 = lex_number (lexer);
3077 t->pairwise->alpha[0] = MIN (a0, a1);
3078 t->pairwise->alpha[1] = MAX (a0, a1);
3081 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3083 else if (lex_match_id (lexer, "ADJUST"))
3085 lex_match (lexer, T_EQUALS);
3086 if (lex_match_id (lexer, "BONFERRONI"))
3087 t->pairwise->adjust = BONFERRONI;
3088 else if (lex_match_id (lexer, "BH"))
3089 t->pairwise->adjust = BH;
3090 else if (lex_match_id (lexer, "NONE"))
3091 t->pairwise->adjust = 0;
3094 lex_error_expecting (lexer, "BONFERRONI", "BH",
3099 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3101 lex_match (lexer, T_EQUALS);
3102 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3105 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3107 lex_match (lexer, T_EQUALS);
3108 if (lex_match_id (lexer, "ALLCATS"))
3109 t->pairwise->meansvariance_allcats = true;
3110 else if (lex_match_id (lexer, "TESTEDCATS"))
3111 t->pairwise->meansvariance_allcats = false;
3114 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3118 else if (lex_match_id (lexer, "CATEGORIES"))
3120 lex_match (lexer, T_EQUALS);
3121 if (lex_match_id (lexer, "ALLVISIBLE"))
3122 t->pairwise->all_visible = true;
3123 else if (lex_match_id (lexer, "SUBTOTALS"))
3124 t->pairwise->all_visible = false;
3127 lex_error_expecting (lexer, "ALLVISIBLE",
3132 else if (lex_match_id (lexer, "MERGE"))
3134 lex_match (lexer, T_EQUALS);
3135 if (!parse_bool (lexer, &t->pairwise->merge))
3138 else if (lex_match_id (lexer, "STYLE"))
3140 lex_match (lexer, T_EQUALS);
3141 if (lex_match_id (lexer, "APA"))
3142 t->pairwise->apa_style = true;
3143 else if (lex_match_id (lexer, "SIMPLE"))
3144 t->pairwise->apa_style = false;
3147 lex_error_expecting (lexer, "APA", "SIMPLE");
3151 else if (lex_match_id (lexer, "SHOWSIG"))
3153 lex_match (lexer, T_EQUALS);
3154 if (!parse_bool (lexer, &t->pairwise->show_sig))
3159 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3160 "INCLUDEMRSETS", "MEANSVARIANCE",
3161 "CATEGORIES", "MERGE", "STYLE",
3166 while (lex_token (lexer) != T_SLASH
3167 && lex_token (lexer) != T_ENDCMD);
3171 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3172 "CRITERIA", "CATEGORIES", "TITLES",
3173 "SIGTEST", "COMPARETEST");
3178 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3180 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3185 while (lex_token (lexer) != T_ENDCMD);
3187 bool ok = ctables_execute (ds, ct);
3188 ctables_destroy (ct);
3189 return ok ? CMD_SUCCESS : CMD_FAILURE;
3192 ctables_destroy (ct);