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;
174 enum ctables_summary_variant
183 /* In struct ctables's 'cells' hmap. Indexed by all the values in all the
184 axes (except the scalar variable, if any). */
185 struct hmap_node node;
187 /* The domains that contain this cell. */
188 struct ctables_domain *domains[N_CTDTS];
191 enum ctables_summary_variant sv;
196 struct ctables_cell_value
198 const struct ctables_category *category;
206 union ctables_summary *summaries;
211 struct pivot_table_look *look;
213 /* If this is NULL, zeros are displayed using the normal print format.
214 Otherwise, this string is displayed. */
217 /* If this is NULL, missing values are displayed using the normal print
218 format. Otherwise, this string is displayed. */
221 /* Indexed by variable dictionary index. */
222 enum ctables_vlabel *vlabels;
224 bool mrsets_count_duplicates; /* MRSETS. */
225 bool smissing_listwise; /* SMISSING. */
226 struct variable *base_weight; /* WEIGHT. */
227 int hide_threshold; /* HIDESMALLCOUNTS. */
229 struct ctables_table **tables;
233 struct ctables_postcompute
235 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
236 const char *name; /* Name, without leading &. */
238 struct ctables_postcompute_expr *expr;
241 bool hide_source_cats;
244 struct ctables_postcompute_expr
246 enum ctables_postcompute_op
254 /* XXX SUBTOTAL and HSUBTOTAL */
267 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
272 XXX what about string ranges? */
275 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
276 struct ctables_postcompute_expr *subs[2];
280 enum ctables_label_position
287 struct ctables_summary_spec_set
289 struct ctables_summary_spec *specs;
293 struct variable *var;
296 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
297 const struct ctables_summary_spec_set *);
298 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
300 /* A nested sequence of variables, e.g. a > b > c. */
303 struct variable **vars;
306 size_t *domains[N_CTDTS];
307 size_t n_domains[N_CTDTS];
309 struct ctables_summary_spec_set specs[N_CSVS];
312 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
315 struct ctables_nest *nests;
321 struct ctables_axis *axes[PIVOT_N_AXES];
322 struct ctables_stack stacks[PIVOT_N_AXES];
323 enum pivot_axis_type summary_axis;
325 struct hmap domains[N_CTDTS];
327 enum pivot_axis_type slabels_position;
328 bool slabels_visible;
330 enum ctables_label_position row_labels;
331 enum ctables_label_position col_labels;
333 /* Indexed by variable dictionary index. */
334 struct ctables_categories **categories;
343 struct ctables_chisq *chisq;
344 struct ctables_pairwise *pairwise;
352 struct variable *var;
353 const struct mrset *mrset;
357 static const struct fmt_spec *
358 ctables_var_get_print_format (const struct ctables_var *var)
360 return (var->is_mrset
361 ? var_get_print_format (var->mrset->vars[0])
362 : var_get_print_format (var->var));
366 ctables_var_name (const struct ctables_var *var)
368 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
371 struct ctables_categories
374 struct ctables_category *cats;
379 struct ctables_category
381 enum ctables_category_type
399 struct ctables_category *subtotal;
403 double number; /* CCT_NUMBER. */
404 char *string; /* CCT_STRING. */
405 double range[2]; /* CCT_RANGE. */
406 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
408 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
411 bool include_missing;
415 enum ctables_summary_function sort_function;
416 struct variable *sort_var;
423 ctables_category_uninit (struct ctables_category *cat)
443 free (cat->total_label);
454 ctables_categories_unref (struct ctables_categories *c)
459 assert (c->n_refs > 0);
463 for (size_t i = 0; i < c->n_cats; i++)
464 ctables_category_uninit (&c->cats[i]);
469 /* Chi-square test (SIGTEST). */
477 /* Pairwise comparison test (COMPARETEST). */
478 struct ctables_pairwise
480 enum { PROP, MEAN } type;
483 bool meansvariance_allcats;
485 enum { BONFERRONI = 1, BH } adjust;
509 struct ctables_var var;
511 struct ctables_summary_spec_set specs[N_CSVS];
515 struct ctables_axis *subs[2];
518 struct msg_location *loc;
521 static void ctables_axis_destroy (struct ctables_axis *);
530 enum ctables_function_availability
532 CTFA_ALL, /* Any variables. */
533 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
534 CTFA_MRSETS, /* Only multiple-response sets */
537 struct ctables_summary_spec
539 enum ctables_summary_function function;
540 double percentile; /* CTSF_PTILE only. */
542 struct fmt_spec format; /* XXX extra CTABLES formats */
547 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
548 const struct ctables_summary_spec *src)
551 dst->label = xstrdup (src->label);
555 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
562 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
563 const struct ctables_summary_spec_set *src)
565 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
566 for (size_t i = 0; i < src->n; i++)
567 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
569 *dst = (struct ctables_summary_spec_set) {
578 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
580 for (size_t i = 0; i < set->n; i++)
581 ctables_summary_spec_uninit (&set->specs[i]);
586 parse_col_width (struct lexer *lexer, const char *name, double *width)
588 lex_match (lexer, T_EQUALS);
589 if (lex_match_id (lexer, "DEFAULT"))
591 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
593 *width = lex_number (lexer);
603 parse_bool (struct lexer *lexer, bool *b)
605 if (lex_match_id (lexer, "NO"))
607 else if (lex_match_id (lexer, "YES"))
611 lex_error_expecting (lexer, "YES", "NO");
617 static enum ctables_function_availability
618 ctables_function_availability (enum ctables_summary_function f)
620 static enum ctables_function_availability availability[] = {
621 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
626 return availability[f];
630 parse_ctables_summary_function (struct lexer *lexer,
631 enum ctables_summary_function *f)
635 enum ctables_summary_function function;
636 struct substring name;
638 static struct pair names[] = {
639 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
640 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
643 /* The .COUNT suffix may be omitted. */
644 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
645 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
646 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
647 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
648 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
649 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
650 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
654 if (!lex_force_id (lexer))
657 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
658 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
660 *f = names[i].function;
665 lex_error (lexer, _("Expecting summary function name."));
670 ctables_axis_destroy (struct ctables_axis *axis)
678 for (size_t i = 0; i < N_CSVS; i++)
679 ctables_summary_spec_set_uninit (&axis->specs[i]);
684 ctables_axis_destroy (axis->subs[0]);
685 ctables_axis_destroy (axis->subs[1]);
688 msg_location_destroy (axis->loc);
692 static struct ctables_axis *
693 ctables_axis_new_nonterminal (enum ctables_axis_op op,
694 struct ctables_axis *sub0,
695 struct ctables_axis *sub1,
696 struct lexer *lexer, int start_ofs)
698 struct ctables_axis *axis = xmalloc (sizeof *axis);
699 *axis = (struct ctables_axis) {
701 .subs = { sub0, sub1 },
702 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
707 struct ctables_axis_parse_ctx
710 struct dictionary *dict;
712 struct ctables_table *t;
715 static struct fmt_spec
716 ctables_summary_default_format (enum ctables_summary_function function,
717 const struct ctables_var *var)
719 static const enum ctables_format default_formats[] = {
720 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
724 switch (default_formats[function])
727 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
730 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
733 return *ctables_var_get_print_format (var);
741 ctables_summary_default_label (enum ctables_summary_function function,
744 static const char *default_labels[] = {
745 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
750 return (function == CTSF_PTILE
751 ? xasprintf (_("Percentile %.2f"), percentile)
752 : xstrdup (gettext (default_labels[function])));
756 ctables_summary_function_name (enum ctables_summary_function function)
758 static const char *names[] = {
759 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
763 return names[function];
767 add_summary_spec (struct ctables_axis *axis,
768 enum ctables_summary_function function, double percentile,
769 const char *label, const struct fmt_spec *format,
770 const struct msg_location *loc, enum ctables_summary_variant sv)
772 if (axis->op == CTAO_VAR)
774 const char *function_name = ctables_summary_function_name (function);
775 const char *var_name = ctables_var_name (&axis->var);
776 switch (ctables_function_availability (function))
779 if (!axis->var.is_mrset)
781 msg_at (SE, loc, _("Summary function %s applies only to multiple "
782 "response sets."), function_name);
783 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
793 _("Summary function %s applies only to scale variables."),
795 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
805 struct ctables_summary_spec_set *set = &axis->specs[sv];
806 if (set->n >= set->allocated)
807 set->specs = x2nrealloc (set->specs, &set->allocated,
810 struct ctables_summary_spec *dst = &set->specs[set->n++];
811 *dst = (struct ctables_summary_spec) {
812 .function = function,
813 .percentile = percentile,
814 .label = xstrdup (label),
815 .format = (format ? *format
816 : ctables_summary_default_format (function, &axis->var)),
822 for (size_t i = 0; i < 2; i++)
823 if (!add_summary_spec (axis->subs[i], function, percentile, label,
830 static struct ctables_axis *ctables_axis_parse_stack (
831 struct ctables_axis_parse_ctx *);
834 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
835 struct ctables_var *var)
837 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
839 *var = (struct ctables_var) {
841 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
845 lex_error (lexer, _("'%s' does not name a multiple-response set "
846 "in the active file dictionary."),
847 lex_tokcstr (lexer));
855 *var = (struct ctables_var) {
857 .var = parse_variable (lexer, dict),
859 return var->var != NULL;
863 static struct ctables_axis *
864 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
866 if (lex_match (ctx->lexer, T_LPAREN))
868 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
869 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
871 ctables_axis_destroy (sub);
877 if (!lex_force_id (ctx->lexer))
880 int start_ofs = lex_ofs (ctx->lexer);
881 struct ctables_var var;
882 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
885 struct ctables_axis *axis = xmalloc (sizeof *axis);
886 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
888 /* XXX should figure out default measures by reading data */
889 axis->scale = (var.is_mrset ? false
890 : lex_match_phrase (ctx->lexer, "[S]") ? true
891 : lex_match_phrase (ctx->lexer, "[C]") ? false
892 : var_get_measure (var.var) == MEASURE_SCALE);
893 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
894 lex_ofs (ctx->lexer) - 1);
899 has_digit (const char *s)
901 return s[strcspn (s, "0123456789")] != '\0';
904 static struct ctables_axis *
905 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
907 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
908 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
911 enum ctables_summary_variant sv = CSV_CELL;
914 int start_ofs = lex_ofs (ctx->lexer);
916 /* Parse function. */
917 enum ctables_summary_function function;
918 if (!parse_ctables_summary_function (ctx->lexer, &function))
921 /* Parse percentile. */
922 double percentile = 0;
923 if (function == CTSF_PTILE)
925 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
927 percentile = lex_number (ctx->lexer);
928 lex_get (ctx->lexer);
933 if (lex_is_string (ctx->lexer))
935 label = ss_xstrdup (lex_tokss (ctx->lexer));
936 lex_get (ctx->lexer);
939 label = ctables_summary_default_label (function, percentile);
942 struct fmt_spec format;
943 const struct fmt_spec *formatp;
944 if (lex_token (ctx->lexer) == T_ID
945 && has_digit (lex_tokcstr (ctx->lexer)))
947 if (!parse_format_specifier (ctx->lexer, &format)
948 || !fmt_check_output (&format)
949 || !fmt_check_type_compat (&format, VAL_NUMERIC))
959 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
960 lex_ofs (ctx->lexer) - 1);
961 printf ("add %s\n", ctables_summary_function_name (function));
962 add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
964 msg_location_destroy (loc);
966 lex_match (ctx->lexer, T_COMMA);
967 if (sv == CSV_CELL && lex_match_id (ctx->lexer, "TOTALS"))
969 if (!lex_force_match (ctx->lexer, T_LBRACK))
973 else if (lex_match (ctx->lexer, T_RBRACK))
975 if (sv == CSV_TOTAL && !lex_force_match (ctx->lexer, T_RBRACK))
982 ctables_axis_destroy (sub);
986 static const struct ctables_axis *
987 find_scale (const struct ctables_axis *axis)
991 else if (axis->op == CTAO_VAR)
995 assert (!axis->var.is_mrset);
1003 for (size_t i = 0; i < 2; i++)
1005 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1013 static const struct ctables_axis *
1014 find_categorical_summary_spec (const struct ctables_axis *axis)
1018 else if (axis->op == CTAO_VAR)
1019 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1022 for (size_t i = 0; i < 2; i++)
1024 const struct ctables_axis *sum
1025 = find_categorical_summary_spec (axis->subs[i]);
1033 static struct ctables_axis *
1034 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1036 int start_ofs = lex_ofs (ctx->lexer);
1037 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1041 while (lex_match (ctx->lexer, T_GT))
1043 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1047 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1048 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1050 const struct ctables_axis *outer_scale = find_scale (lhs);
1051 const struct ctables_axis *inner_scale = find_scale (rhs);
1052 if (outer_scale && inner_scale)
1054 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1055 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1056 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1057 ctables_axis_destroy (nest);
1061 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1064 msg_at (SE, nest->loc,
1065 _("Summaries may only be requested for categorical variables "
1066 "at the innermost nesting level."));
1067 msg_at (SN, outer_sum->loc,
1068 _("This outer categorical variable has a summary."));
1069 ctables_axis_destroy (nest);
1079 static struct ctables_axis *
1080 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1082 int start_ofs = lex_ofs (ctx->lexer);
1083 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1087 while (lex_match (ctx->lexer, T_PLUS))
1089 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1093 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1094 ctx->lexer, start_ofs);
1101 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1102 struct ctables *ct, struct ctables_table *t,
1103 enum pivot_axis_type a)
1105 if (lex_token (lexer) == T_BY
1106 || lex_token (lexer) == T_SLASH
1107 || lex_token (lexer) == T_ENDCMD)
1110 struct ctables_axis_parse_ctx ctx = {
1116 t->axes[a] = ctables_axis_parse_stack (&ctx);
1117 return t->axes[a] != NULL;
1121 ctables_chisq_destroy (struct ctables_chisq *chisq)
1127 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1133 ctables_table_destroy (struct ctables_table *t)
1138 for (size_t i = 0; i < t->n_categories; i++)
1139 ctables_categories_unref (t->categories[i]);
1140 free (t->categories);
1142 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1143 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1144 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1148 ctables_chisq_destroy (t->chisq);
1149 ctables_pairwise_destroy (t->pairwise);
1154 ctables_destroy (struct ctables *ct)
1159 pivot_table_look_unref (ct->look);
1163 for (size_t i = 0; i < ct->n_tables; i++)
1164 ctables_table_destroy (ct->tables[i]);
1169 static struct ctables_category
1170 cct_range (double low, double high)
1172 return (struct ctables_category) {
1174 .range = { low, high }
1179 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1180 struct ctables_table *t)
1182 if (!lex_match_id (lexer, "VARIABLES"))
1184 lex_match (lexer, T_EQUALS);
1186 struct variable **vars;
1188 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1191 struct ctables_categories *c = xmalloc (sizeof *c);
1192 *c = (struct ctables_categories) { .n_refs = n_vars };
1193 for (size_t i = 0; i < n_vars; i++)
1195 struct ctables_categories **cp
1196 = &t->categories[var_get_dict_index (vars[i])];
1197 ctables_categories_unref (*cp);
1202 size_t allocated_cats = 0;
1203 if (lex_match (lexer, T_LBRACK))
1207 if (c->n_cats >= allocated_cats)
1208 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1210 struct ctables_category *cat = &c->cats[c->n_cats];
1211 if (lex_match_id (lexer, "OTHERNM"))
1212 cat->type = CCT_OTHERNM;
1213 else if (lex_match_id (lexer, "MISSING"))
1214 cat->type = CCT_MISSING;
1215 else if (lex_match_id (lexer, "SUBTOTAL"))
1216 *cat = (struct ctables_category)
1217 { .type = CCT_SUBTOTAL, .total_label = NULL };
1218 else if (lex_match_id (lexer, "HSUBTOTAL"))
1219 *cat = (struct ctables_category)
1220 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1221 else if (lex_match_id (lexer, "LO"))
1223 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1225 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1228 else if (lex_is_number (lexer))
1230 double number = lex_number (lexer);
1232 if (lex_match_id (lexer, "THRU"))
1234 cat->type = CCT_RANGE;
1235 cat->range[0] = number;
1236 if (lex_match_id (lexer, "HI"))
1237 *cat = cct_range (number, DBL_MAX);
1240 if (!lex_force_num (lexer))
1242 *cat = cct_range (number, lex_number (lexer));
1247 *cat = (struct ctables_category) {
1252 else if (lex_is_string (lexer))
1254 *cat = (struct ctables_category) {
1256 .string = ss_xstrdup (lex_tokss (lexer)),
1262 lex_error (lexer, NULL);
1266 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1268 if (lex_match (lexer, T_EQUALS))
1270 if (!lex_force_string (lexer))
1273 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1277 cat->total_label = xstrdup (_("Subtotal"));
1281 lex_match (lexer, T_COMMA);
1283 while (!lex_match (lexer, T_RBRACK));
1286 struct ctables_category cat = {
1288 .include_missing = false,
1289 .sort_ascending = true,
1291 bool show_totals = false;
1292 char *total_label = NULL;
1293 bool totals_before = false;
1294 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1296 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1298 lex_match (lexer, T_EQUALS);
1299 if (lex_match_id (lexer, "A"))
1300 cat.sort_ascending = true;
1301 else if (lex_match_id (lexer, "D"))
1302 cat.sort_ascending = false;
1305 lex_error_expecting (lexer, "A", "D");
1309 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1311 lex_match (lexer, T_EQUALS);
1312 if (lex_match_id (lexer, "VALUE"))
1313 cat.type = CCT_VALUE;
1314 else if (lex_match_id (lexer, "LABEL"))
1315 cat.type = CCT_LABEL;
1318 cat.type = CCT_FUNCTION;
1319 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1322 if (lex_match (lexer, T_LPAREN))
1324 cat.sort_var = parse_variable (lexer, dict);
1328 if (cat.sort_function == CTSF_PTILE)
1330 lex_match (lexer, T_COMMA);
1331 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1333 cat.percentile = lex_number (lexer);
1337 if (!lex_force_match (lexer, T_RPAREN))
1340 else if (ctables_function_availability (cat.sort_function)
1343 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1348 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1350 lex_match (lexer, T_EQUALS);
1351 if (lex_match_id (lexer, "INCLUDE"))
1352 cat.include_missing = true;
1353 else if (lex_match_id (lexer, "EXCLUDE"))
1354 cat.include_missing = false;
1357 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1361 else if (lex_match_id (lexer, "TOTAL"))
1363 lex_match (lexer, T_EQUALS);
1364 if (!parse_bool (lexer, &show_totals))
1367 else if (lex_match_id (lexer, "LABEL"))
1369 lex_match (lexer, T_EQUALS);
1370 if (!lex_force_string (lexer))
1373 total_label = ss_xstrdup (lex_tokss (lexer));
1376 else if (lex_match_id (lexer, "POSITION"))
1378 lex_match (lexer, T_EQUALS);
1379 if (lex_match_id (lexer, "BEFORE"))
1380 totals_before = true;
1381 else if (lex_match_id (lexer, "AFTER"))
1382 totals_before = false;
1385 lex_error_expecting (lexer, "BEFORE", "AFTER");
1389 else if (lex_match_id (lexer, "EMPTY"))
1391 lex_match (lexer, T_EQUALS);
1392 if (lex_match_id (lexer, "INCLUDE"))
1393 c->show_empty = true;
1394 else if (lex_match_id (lexer, "EXCLUDE"))
1395 c->show_empty = false;
1398 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1405 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1406 "TOTAL", "LABEL", "POSITION", "EMPTY");
1408 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1415 if (c->n_cats >= allocated_cats)
1416 c->cats = x2nrealloc (c->cats, &allocated_cats,
1418 c->cats[c->n_cats++] = cat;
1423 if (c->n_cats >= allocated_cats)
1424 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1426 struct ctables_category *totals;
1429 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1430 totals = &c->cats[0];
1433 totals = &c->cats[c->n_cats];
1436 *totals = (struct ctables_category) {
1438 .total_label = total_label ? total_label : xstrdup (_("Total")),
1442 struct ctables_category *subtotal = NULL;
1443 for (size_t i = totals_before ? 0 : c->n_cats;
1444 totals_before ? i < c->n_cats : i-- > 0;
1445 totals_before ? i++ : 0)
1447 struct ctables_category *cat = &c->cats[i];
1455 cat->subtotal = subtotal;
1475 ctables_nest_uninit (struct ctables_nest *nest)
1482 ctables_stack_uninit (struct ctables_stack *stack)
1486 for (size_t i = 0; i < stack->n; i++)
1487 ctables_nest_uninit (&stack->nests[i]);
1488 free (stack->nests);
1492 static struct ctables_stack
1493 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1500 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1501 for (size_t i = 0; i < s0.n; i++)
1502 for (size_t j = 0; j < s1.n; j++)
1504 const struct ctables_nest *a = &s0.nests[i];
1505 const struct ctables_nest *b = &s1.nests[j];
1507 size_t allocate = a->n + b->n;
1508 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1509 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1511 for (size_t k = 0; k < a->n; k++)
1512 vars[n++] = a->vars[k];
1513 for (size_t k = 0; k < b->n; k++)
1514 vars[n++] = b->vars[k];
1515 assert (n == allocate);
1517 const struct ctables_nest *summary_src;
1518 if (!a->specs[CSV_CELL].var)
1520 else if (!b->specs[CSV_CELL].var)
1525 struct ctables_nest *new = &stack.nests[stack.n++];
1526 *new = (struct ctables_nest) {
1528 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1529 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1533 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1534 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1536 ctables_stack_uninit (&s0);
1537 ctables_stack_uninit (&s1);
1541 static struct ctables_stack
1542 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1544 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1545 for (size_t i = 0; i < s0.n; i++)
1546 stack.nests[stack.n++] = s0.nests[i];
1547 for (size_t i = 0; i < s1.n; i++)
1548 stack.nests[stack.n++] = s1.nests[i];
1549 assert (stack.n == s0.n + s1.n);
1555 static struct ctables_stack
1556 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1559 return (struct ctables_stack) { .n = 0 };
1564 assert (!a->var.is_mrset);
1566 struct variable **vars = xmalloc (sizeof *vars);
1569 struct ctables_nest *nest = xmalloc (sizeof *nest);
1570 *nest = (struct ctables_nest) {
1573 .scale_idx = a->scale ? 0 : SIZE_MAX,
1575 if (a->specs[CSV_CELL].n || a->scale)
1576 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1578 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1579 nest->specs[sv].var = a->var.var;
1581 return (struct ctables_stack) { .nests = nest, .n = 1 };
1584 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1585 enumerate_fts (axis_type, a->subs[1]));
1588 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1589 enumerate_fts (axis_type, a->subs[1]));
1595 union ctables_summary
1597 /* COUNT, VALIDN, TOTALN. */
1604 /* MINIMUM, MAXIMUM, RANGE. */
1611 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1612 struct moments1 *moments;
1614 /* XXX percentiles, median, mode, multiple response */
1618 ctables_summary_init (union ctables_summary *s,
1619 const struct ctables_summary_spec *ss)
1621 switch (ss->function)
1625 case CTSF_ROWPCT_COUNT:
1626 case CTSF_COLPCT_COUNT:
1627 case CTSF_TABLEPCT_COUNT:
1628 case CTSF_SUBTABLEPCT_COUNT:
1629 case CTSF_LAYERPCT_COUNT:
1630 case CTSF_LAYERROWPCT_COUNT:
1631 case CTSF_LAYERCOLPCT_COUNT:
1632 case CTSF_ROWPCT_VALIDN:
1633 case CTSF_COLPCT_VALIDN:
1634 case CTSF_TABLEPCT_VALIDN:
1635 case CTSF_SUBTABLEPCT_VALIDN:
1636 case CTSF_LAYERPCT_VALIDN:
1637 case CTSF_LAYERROWPCT_VALIDN:
1638 case CTSF_LAYERCOLPCT_VALIDN:
1639 case CTSF_ROWPCT_TOTALN:
1640 case CTSF_COLPCT_TOTALN:
1641 case CTSF_TABLEPCT_TOTALN:
1642 case CTSF_SUBTABLEPCT_TOTALN:
1643 case CTSF_LAYERPCT_TOTALN:
1644 case CTSF_LAYERROWPCT_TOTALN:
1645 case CTSF_LAYERCOLPCT_TOTALN:
1650 s->missing = s->valid = 0;
1656 s->min = s->max = SYSMIS;
1664 case CTSF_ROWPCT_SUM:
1665 case CTSF_COLPCT_SUM:
1666 case CTSF_TABLEPCT_SUM:
1667 case CTSF_SUBTABLEPCT_SUM:
1668 case CTSF_LAYERPCT_SUM:
1669 case CTSF_LAYERROWPCT_SUM:
1670 case CTSF_LAYERCOLPCT_SUM:
1671 s->moments = moments1_create (MOMENT_VARIANCE);
1680 case CTSF_RESPONSES:
1681 case CTSF_ROWPCT_RESPONSES:
1682 case CTSF_COLPCT_RESPONSES:
1683 case CTSF_TABLEPCT_RESPONSES:
1684 case CTSF_SUBTABLEPCT_RESPONSES:
1685 case CTSF_LAYERPCT_RESPONSES:
1686 case CTSF_LAYERROWPCT_RESPONSES:
1687 case CTSF_LAYERCOLPCT_RESPONSES:
1688 case CTSF_ROWPCT_RESPONSES_COUNT:
1689 case CTSF_COLPCT_RESPONSES_COUNT:
1690 case CTSF_TABLEPCT_RESPONSES_COUNT:
1691 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1692 case CTSF_LAYERPCT_RESPONSES_COUNT:
1693 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1694 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1695 case CTSF_ROWPCT_COUNT_RESPONSES:
1696 case CTSF_COLPCT_COUNT_RESPONSES:
1697 case CTSF_TABLEPCT_COUNT_RESPONSES:
1698 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1699 case CTSF_LAYERPCT_COUNT_RESPONSES:
1700 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1701 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1707 ctables_summary_uninit (union ctables_summary *s,
1708 const struct ctables_summary_spec *ss)
1710 switch (ss->function)
1714 case CTSF_ROWPCT_COUNT:
1715 case CTSF_COLPCT_COUNT:
1716 case CTSF_TABLEPCT_COUNT:
1717 case CTSF_SUBTABLEPCT_COUNT:
1718 case CTSF_LAYERPCT_COUNT:
1719 case CTSF_LAYERROWPCT_COUNT:
1720 case CTSF_LAYERCOLPCT_COUNT:
1721 case CTSF_ROWPCT_VALIDN:
1722 case CTSF_COLPCT_VALIDN:
1723 case CTSF_TABLEPCT_VALIDN:
1724 case CTSF_SUBTABLEPCT_VALIDN:
1725 case CTSF_LAYERPCT_VALIDN:
1726 case CTSF_LAYERROWPCT_VALIDN:
1727 case CTSF_LAYERCOLPCT_VALIDN:
1728 case CTSF_ROWPCT_TOTALN:
1729 case CTSF_COLPCT_TOTALN:
1730 case CTSF_TABLEPCT_TOTALN:
1731 case CTSF_SUBTABLEPCT_TOTALN:
1732 case CTSF_LAYERPCT_TOTALN:
1733 case CTSF_LAYERROWPCT_TOTALN:
1734 case CTSF_LAYERCOLPCT_TOTALN:
1751 case CTSF_ROWPCT_SUM:
1752 case CTSF_COLPCT_SUM:
1753 case CTSF_TABLEPCT_SUM:
1754 case CTSF_SUBTABLEPCT_SUM:
1755 case CTSF_LAYERPCT_SUM:
1756 case CTSF_LAYERROWPCT_SUM:
1757 case CTSF_LAYERCOLPCT_SUM:
1758 moments1_destroy (s->moments);
1767 case CTSF_RESPONSES:
1768 case CTSF_ROWPCT_RESPONSES:
1769 case CTSF_COLPCT_RESPONSES:
1770 case CTSF_TABLEPCT_RESPONSES:
1771 case CTSF_SUBTABLEPCT_RESPONSES:
1772 case CTSF_LAYERPCT_RESPONSES:
1773 case CTSF_LAYERROWPCT_RESPONSES:
1774 case CTSF_LAYERCOLPCT_RESPONSES:
1775 case CTSF_ROWPCT_RESPONSES_COUNT:
1776 case CTSF_COLPCT_RESPONSES_COUNT:
1777 case CTSF_TABLEPCT_RESPONSES_COUNT:
1778 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1779 case CTSF_LAYERPCT_RESPONSES_COUNT:
1780 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1781 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1782 case CTSF_ROWPCT_COUNT_RESPONSES:
1783 case CTSF_COLPCT_COUNT_RESPONSES:
1784 case CTSF_TABLEPCT_COUNT_RESPONSES:
1785 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1786 case CTSF_LAYERPCT_COUNT_RESPONSES:
1787 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1788 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1794 ctables_summary_add (union ctables_summary *s,
1795 const struct ctables_summary_spec *ss,
1796 const struct variable *var, const union value *value,
1799 switch (ss->function)
1803 case CTSF_ROWPCT_COUNT:
1804 case CTSF_COLPCT_COUNT:
1805 case CTSF_TABLEPCT_COUNT:
1806 case CTSF_SUBTABLEPCT_COUNT:
1807 case CTSF_LAYERPCT_COUNT:
1808 case CTSF_LAYERROWPCT_COUNT:
1809 case CTSF_LAYERCOLPCT_COUNT:
1810 case CTSF_ROWPCT_VALIDN:
1811 case CTSF_COLPCT_VALIDN:
1812 case CTSF_TABLEPCT_VALIDN:
1813 case CTSF_SUBTABLEPCT_VALIDN:
1814 case CTSF_LAYERPCT_VALIDN:
1815 case CTSF_LAYERROWPCT_VALIDN:
1816 case CTSF_LAYERCOLPCT_VALIDN:
1817 case CTSF_ROWPCT_TOTALN:
1818 case CTSF_COLPCT_TOTALN:
1819 case CTSF_TABLEPCT_TOTALN:
1820 case CTSF_SUBTABLEPCT_TOTALN:
1821 case CTSF_LAYERPCT_TOTALN:
1822 case CTSF_LAYERROWPCT_TOTALN:
1823 case CTSF_LAYERCOLPCT_TOTALN:
1828 if (var_is_value_missing (var, value))
1829 s->missing += weight;
1837 if (!var_is_value_missing (var, value))
1839 assert (!var_is_alpha (var)); /* XXX? */
1840 if (s->min == SYSMIS || value->f < s->min)
1842 if (s->max == SYSMIS || value->f > s->max)
1852 case CTSF_ROWPCT_SUM:
1853 case CTSF_COLPCT_SUM:
1854 case CTSF_TABLEPCT_SUM:
1855 case CTSF_SUBTABLEPCT_SUM:
1856 case CTSF_LAYERPCT_SUM:
1857 case CTSF_LAYERROWPCT_SUM:
1858 case CTSF_LAYERCOLPCT_SUM:
1859 moments1_add (s->moments, value->f, weight);
1868 case CTSF_RESPONSES:
1869 case CTSF_ROWPCT_RESPONSES:
1870 case CTSF_COLPCT_RESPONSES:
1871 case CTSF_TABLEPCT_RESPONSES:
1872 case CTSF_SUBTABLEPCT_RESPONSES:
1873 case CTSF_LAYERPCT_RESPONSES:
1874 case CTSF_LAYERROWPCT_RESPONSES:
1875 case CTSF_LAYERCOLPCT_RESPONSES:
1876 case CTSF_ROWPCT_RESPONSES_COUNT:
1877 case CTSF_COLPCT_RESPONSES_COUNT:
1878 case CTSF_TABLEPCT_RESPONSES_COUNT:
1879 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1880 case CTSF_LAYERPCT_RESPONSES_COUNT:
1881 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1882 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1883 case CTSF_ROWPCT_COUNT_RESPONSES:
1884 case CTSF_COLPCT_COUNT_RESPONSES:
1885 case CTSF_TABLEPCT_COUNT_RESPONSES:
1886 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1887 case CTSF_LAYERPCT_COUNT_RESPONSES:
1888 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1889 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1895 ctables_summary_value (const struct ctables_cell *cell,
1896 union ctables_summary *s,
1897 const struct ctables_summary_spec *ss)
1899 switch (ss->function)
1905 case CTSF_SUBTABLEPCT_COUNT:
1906 return cell->domains[CTDT_SUBTABLE]->valid ? s->valid / cell->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1908 case CTSF_ROWPCT_COUNT:
1909 return cell->domains[CTDT_ROW]->valid ? s->valid / cell->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1911 case CTSF_COLPCT_COUNT:
1912 return cell->domains[CTDT_COL]->valid ? s->valid / cell->domains[CTDT_COL]->valid * 100 : SYSMIS;
1914 case CTSF_TABLEPCT_COUNT:
1915 return cell->domains[CTDT_TABLE]->valid ? s->valid / cell->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1917 case CTSF_LAYERPCT_COUNT:
1918 return cell->domains[CTDT_LAYER]->valid ? s->valid / cell->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1920 case CTSF_LAYERROWPCT_COUNT:
1921 return cell->domains[CTDT_LAYERROW]->valid ? s->valid / cell->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1923 case CTSF_LAYERCOLPCT_COUNT:
1924 return cell->domains[CTDT_LAYERCOL]->valid ? s->valid / cell->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1926 case CTSF_ROWPCT_VALIDN:
1927 case CTSF_COLPCT_VALIDN:
1928 case CTSF_TABLEPCT_VALIDN:
1929 case CTSF_SUBTABLEPCT_VALIDN:
1930 case CTSF_LAYERPCT_VALIDN:
1931 case CTSF_LAYERROWPCT_VALIDN:
1932 case CTSF_LAYERCOLPCT_VALIDN:
1933 case CTSF_ROWPCT_TOTALN:
1934 case CTSF_COLPCT_TOTALN:
1935 case CTSF_TABLEPCT_TOTALN:
1936 case CTSF_SUBTABLEPCT_TOTALN:
1937 case CTSF_LAYERPCT_TOTALN:
1938 case CTSF_LAYERROWPCT_TOTALN:
1939 case CTSF_LAYERCOLPCT_TOTALN:
1944 return s->valid + s->missing;
1957 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1962 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1968 double weight, variance;
1969 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1970 return calc_semean (variance, weight);
1976 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1977 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1982 double weight, mean;
1983 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1984 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1990 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1994 case CTSF_ROWPCT_SUM:
1995 case CTSF_COLPCT_SUM:
1996 case CTSF_TABLEPCT_SUM:
1997 case CTSF_SUBTABLEPCT_SUM:
1998 case CTSF_LAYERPCT_SUM:
1999 case CTSF_LAYERROWPCT_SUM:
2000 case CTSF_LAYERCOLPCT_SUM:
2009 case CTSF_RESPONSES:
2010 case CTSF_ROWPCT_RESPONSES:
2011 case CTSF_COLPCT_RESPONSES:
2012 case CTSF_TABLEPCT_RESPONSES:
2013 case CTSF_SUBTABLEPCT_RESPONSES:
2014 case CTSF_LAYERPCT_RESPONSES:
2015 case CTSF_LAYERROWPCT_RESPONSES:
2016 case CTSF_LAYERCOLPCT_RESPONSES:
2017 case CTSF_ROWPCT_RESPONSES_COUNT:
2018 case CTSF_COLPCT_RESPONSES_COUNT:
2019 case CTSF_TABLEPCT_RESPONSES_COUNT:
2020 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2021 case CTSF_LAYERPCT_RESPONSES_COUNT:
2022 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2023 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2024 case CTSF_ROWPCT_COUNT_RESPONSES:
2025 case CTSF_COLPCT_COUNT_RESPONSES:
2026 case CTSF_TABLEPCT_COUNT_RESPONSES:
2027 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2028 case CTSF_LAYERPCT_COUNT_RESPONSES:
2029 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2030 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2037 struct ctables_cell_sort_aux
2039 const struct ctables_table *t;
2040 enum pivot_axis_type a;
2044 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2046 const struct ctables_cell_sort_aux *aux = aux_;
2047 struct ctables_cell *const *ap = a_;
2048 struct ctables_cell *const *bp = b_;
2049 const struct ctables_cell *a = *ap;
2050 const struct ctables_cell *b = *bp;
2052 size_t a_idx = a->axes[aux->a].stack_idx;
2053 size_t b_idx = b->axes[aux->a].stack_idx;
2055 return a_idx < b_idx ? -1 : 1;
2057 const struct ctables_nest *nest = &aux->t->stacks[aux->a].nests[a_idx];
2058 for (size_t i = 0; i < nest->n; i++)
2059 if (i != nest->scale_idx)
2061 const struct variable *var = nest->vars[i];
2062 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2063 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2064 if (a_cv->category != b_cv->category)
2065 return a_cv->category > b_cv->category ? 1 : -1;
2067 const union value *a_val = &a_cv->value;
2068 const union value *b_val = &b_cv->value;
2069 switch (a_cv->category->type)
2076 /* Must be equal. */
2083 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2091 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2093 return a_cv->category->sort_ascending ? cmp : -cmp;
2099 const char *a_label = var_lookup_value_label (var, a_val);
2100 const char *b_label = var_lookup_value_label (var, b_val);
2102 ? (b_label ? strcmp (a_label, b_label) : 1)
2103 : (b_label ? -1 : value_compare_3way (
2104 a_val, b_val, var_get_width (var))));
2106 return a_cv->category->sort_ascending ? cmp : -cmp;
2120 For each ctables_table:
2121 For each combination of row vars:
2122 For each combination of column vars:
2123 For each combination of layer vars:
2125 Make a table of row values:
2126 Sort entries by row values
2127 Assign a 0-based index to each actual value
2128 Construct a dimension
2129 Make a table of column values
2130 Make a table of layer values
2132 Fill the table entry using the indexes from before.
2135 static struct ctables_domain *
2136 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *cell,
2137 enum ctables_domain_type domain)
2140 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2142 size_t idx = cell->axes[a].stack_idx;
2143 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2144 hash = hash_int (idx, hash);
2145 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2147 size_t v_idx = nest->domains[domain][i];
2148 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2149 var_get_width (nest->vars[v_idx]), hash);
2153 struct ctables_domain *d;
2154 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2156 const struct ctables_cell *df = d->example;
2157 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2159 size_t idx = cell->axes[a].stack_idx;
2160 if (idx != df->axes[a].stack_idx)
2163 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2164 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2166 size_t v_idx = nest->domains[domain][i];
2167 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2168 &cell->axes[a].cvs[v_idx].value,
2169 var_get_width (nest->vars[v_idx])))
2178 d = xmalloc (sizeof *d);
2179 *d = (struct ctables_domain) { .example = cell };
2180 hmap_insert (&t->domains[domain], &d->node, hash);
2184 static const struct ctables_category *
2185 ctables_categories_match (const struct ctables_categories *c,
2186 const union value *v, const struct variable *var)
2188 const struct ctables_category *othernm = NULL;
2189 for (size_t i = c->n_cats; i-- > 0; )
2191 const struct ctables_category *cat = &c->cats[i];
2195 if (cat->number == v->f)
2203 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2204 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2209 if (var_is_value_missing (var, v))
2226 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2231 return var_is_value_missing (var, v) ? NULL : othernm;
2234 static const struct ctables_category *
2235 ctables_categories_total (const struct ctables_categories *c)
2237 const struct ctables_category *first = &c->cats[0];
2238 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2239 return (first->type == CCT_TOTAL ? first
2240 : last->type == CCT_TOTAL ? last
2244 static struct ctables_cell *
2245 ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c,
2246 size_t ix[PIVOT_N_AXES],
2247 const struct ctables_category *cats[PIVOT_N_AXES][10])
2249 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2252 enum ctables_summary_variant sv = CSV_CELL;
2253 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2255 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2256 hash = hash_int (ix[a], hash);
2257 for (size_t i = 0; i < nest->n; i++)
2258 if (i != nest->scale_idx)
2260 hash = hash_pointer (cats[a][i], hash);
2261 if (cats[a][i]->type != CCT_TOTAL
2262 && cats[a][i]->type != CCT_SUBTOTAL
2263 && cats[a][i]->type != CCT_HSUBTOTAL)
2264 hash = value_hash (case_data (c, nest->vars[i]),
2265 var_get_width (nest->vars[i]), hash);
2271 struct ctables_cell *cell;
2272 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &t->cells)
2274 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2276 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2277 if (cell->axes[a].stack_idx != ix[a])
2279 for (size_t i = 0; i < nest->n; i++)
2280 if (i != nest->scale_idx
2281 && (cats[a][i] != cell->axes[a].cvs[i].category
2282 || (cats[a][i]->type != CCT_TOTAL
2283 && cats[a][i]->type != CCT_SUBTOTAL
2284 && cats[a][i]->type != CCT_HSUBTOTAL
2285 && !value_equal (case_data (c, nest->vars[i]),
2286 &cell->axes[a].cvs[i].value,
2287 var_get_width (nest->vars[i])))))
2296 cell = xmalloc (sizeof *cell);
2299 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2301 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2302 cell->axes[a].stack_idx = ix[a];
2303 cell->axes[a].cvs = (nest->n
2304 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2306 for (size_t i = 0; i < nest->n; i++)
2308 if (i != nest->scale_idx)
2310 const struct ctables_category *subtotal = cats[a][i]->subtotal;
2311 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2315 cell->axes[a].cvs[i].category = cats[a][i];
2316 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2317 var_get_width (nest->vars[i]));
2321 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2322 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2323 for (size_t i = 0; i < specs->n; i++)
2324 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2325 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2326 cell->domains[dt] = ctables_domain_insert (t, cell, dt);
2327 hmap_insert (&t->cells, &cell->node, hash);
2332 ctables_cell_add__ (struct ctables_table *t, const struct ccase *c,
2333 size_t ix[PIVOT_N_AXES],
2334 const struct ctables_category *cats[PIVOT_N_AXES][10],
2337 struct ctables_cell *cell = ctables_cell_insert__ (t, c, ix, cats);
2338 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2340 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2341 for (size_t i = 0; i < specs->n; i++)
2342 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2343 case_data (c, specs->var), weight);
2344 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2345 cell->domains[dt]->valid += weight;
2349 recurse_totals (struct ctables_table *t, const struct ccase *c,
2350 size_t ix[PIVOT_N_AXES],
2351 const struct ctables_category *cats[PIVOT_N_AXES][10],
2353 enum pivot_axis_type start_axis, size_t start_nest)
2355 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2357 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2358 for (size_t i = start_nest; i < nest->n; i++)
2360 if (i == nest->scale_idx)
2363 const struct variable *var = nest->vars[i];
2365 const struct ctables_category *total = ctables_categories_total (
2366 t->categories[var_get_dict_index (var)]);
2369 const struct ctables_category *save = cats[a][i];
2371 ctables_cell_add__ (t, c, ix, cats, weight);
2372 recurse_totals (t, c, ix, cats, weight, a, i + 1);
2381 ctables_cell_insert (struct ctables_table *t,
2382 const struct ccase *c,
2383 size_t ir, size_t ic, size_t il,
2386 size_t ix[PIVOT_N_AXES] = {
2387 [PIVOT_AXIS_ROW] = ir,
2388 [PIVOT_AXIS_COLUMN] = ic,
2389 [PIVOT_AXIS_LAYER] = il,
2392 const struct ctables_category *cats[PIVOT_N_AXES][10];
2393 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2395 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2396 for (size_t i = 0; i < nest->n; i++)
2398 if (i == nest->scale_idx)
2401 const struct variable *var = nest->vars[i];
2402 const union value *value = case_data (c, var);
2404 if (var_is_numeric (var) && value->f == SYSMIS)
2407 cats[a][i] = ctables_categories_match (
2408 t->categories[var_get_dict_index (var)], value, var);
2414 ctables_cell_add__ (t, c, ix, cats, weight);
2416 recurse_totals (t, c, ix, cats, weight, 0, 0);
2418 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2420 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2421 for (size_t i = 0; i < nest->n; i++)
2423 if (i == nest->scale_idx)
2426 const struct ctables_category *save = cats[a][i];
2429 cats[a][i] = save->subtotal;
2430 ctables_cell_add__ (t, c, ix, cats, weight);
2440 const struct ctables_summary_spec_set *set;
2445 merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
2447 const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
2448 const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
2449 if (as->function != bs->function)
2450 return as->function > bs->function ? 1 : -1;
2451 else if (as->percentile != bs->percentile)
2452 return as->percentile < bs->percentile ? 1 : -1;
2453 return strcmp (as->label, bs->label);
2457 ctables_execute (struct dataset *ds, struct ctables *ct)
2459 for (size_t i = 0; i < ct->n_tables; i++)
2461 struct ctables_table *t = ct->tables[i];
2462 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2465 t->stacks[a] = enumerate_fts (a, t->axes[a]);
2467 for (size_t j = 0; j < t->stacks[a].n; j++)
2469 struct ctables_nest *nest = &t->stacks[a].nests[j];
2470 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2472 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
2473 nest->n_domains[dt] = 0;
2475 for (size_t k = 0; k < nest->n; k++)
2477 if (k == nest->scale_idx)
2486 if (a != PIVOT_AXIS_LAYER)
2493 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2494 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2495 : a == PIVOT_AXIS_ROW)
2497 if (k == nest->n - 1
2498 || (nest->scale_idx == nest->n - 1
2499 && k == nest->n - 2))
2505 if (a == PIVOT_AXIS_COLUMN)
2510 if (a == PIVOT_AXIS_ROW)
2515 nest->domains[dt][nest->n_domains[dt]++] = k;
2522 struct ctables_nest *nest = xmalloc (sizeof *nest);
2523 *nest = (struct ctables_nest) { .n = 0 };
2524 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
2527 struct ctables_stack *stack = &t->stacks[t->summary_axis];
2528 for (size_t i = 0; i < stack->n; i++)
2530 struct ctables_nest *nest = &stack->nests[i];
2531 if (!nest->specs[CSV_CELL].n)
2533 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
2534 specs->specs = xmalloc (sizeof *specs->specs);
2537 enum ctables_summary_function function
2538 = specs->var ? CTSF_MEAN : CTSF_COUNT;
2539 struct ctables_var var = { .is_mrset = false, .var = specs->var };
2541 *specs->specs = (struct ctables_summary_spec) {
2542 .function = function,
2543 .format = ctables_summary_default_format (function, &var),
2544 .label = ctables_summary_default_label (function, 0),
2547 specs->var = nest->vars[0];
2549 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
2550 &nest->specs[CSV_CELL]);
2552 else if (!nest->specs[CSV_TOTAL].n)
2553 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
2554 &nest->specs[CSV_CELL]);
2557 struct ctables_summary_spec_set merged = { .n = 0 };
2558 struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
2560 for (size_t j = 0; j < stack->n; j++)
2562 const struct ctables_nest *nest = &stack->nests[j];
2566 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
2568 items[n_left] = (struct merge_item) {
2569 .tiebreaker = n_left,
2570 .set = &nest->specs[sv]
2578 struct merge_item min = items[0];
2579 for (size_t j = 1; j < n_left; j++)
2580 if (merge_item_compare_3way (&items[j], &min) < 0)
2583 /* XXX Add to 'merged' */
2584 if (merged.n >= merged.allocated)
2585 merged.specs = x2nrealloc (merged.specs, &merged.allocated,
2586 sizeof *merged.specs);
2587 merged.specs[merged.n++] = min.set->specs[min.ofs];
2589 for (size_t j = 0; j < n_left; )
2591 if (merge_item_compare_3way (&items[j], &min) == 0)
2593 struct merge_item *item = &items[j];
2594 item->set->specs[item->ofs].axis_idx = merged.n - 1;
2595 if (++item->ofs >= item->set->n)
2597 items[j] = items[--n_left];
2605 for (size_t j = 0; j < merged.n; j++)
2606 printf ("%s\n", ctables_summary_function_name (merged.specs[j].function));
2608 for (size_t j = 0; j < stack->n; j++)
2610 const struct ctables_nest *nest = &stack->nests[j];
2611 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
2613 const struct ctables_summary_spec_set *specs = &nest->specs[sv];
2614 for (size_t k = 0; k < specs->n; k++)
2615 printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
2616 specs->specs[k].axis_idx);
2622 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2625 bool warn_on_invalid = true;
2626 double total_weight = 0;
2627 for (struct ccase *c = casereader_read (input); c;
2628 case_unref (c), c = casereader_read (input))
2630 double weight = dict_get_case_weight (dataset_dict (ds), c,
2632 total_weight += weight;
2634 for (size_t i = 0; i < ct->n_tables; i++)
2636 struct ctables_table *t = ct->tables[i];
2638 for (size_t ir = 0; ir < t->stacks[PIVOT_AXIS_ROW].n; ir++)
2639 for (size_t ic = 0; ic < t->stacks[PIVOT_AXIS_COLUMN].n; ic++)
2640 for (size_t il = 0; il < t->stacks[PIVOT_AXIS_LAYER].n; il++)
2641 ctables_cell_insert (t, c, ir, ic, il, weight);
2644 casereader_destroy (input);
2646 for (size_t i = 0; i < ct->n_tables; i++)
2648 struct ctables_table *t = ct->tables[i];
2650 struct pivot_table *pt = pivot_table_create__ (
2652 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2653 : pivot_value_new_text (N_("Custom Tables"))),
2656 pivot_table_set_caption (
2657 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2659 pivot_table_set_caption (
2660 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2662 pivot_table_set_look (pt, ct->look);
2663 struct pivot_dimension *d[PIVOT_N_AXES];
2664 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2666 static const char *names[] = {
2667 [PIVOT_AXIS_ROW] = N_("Rows"),
2668 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2669 [PIVOT_AXIS_LAYER] = N_("Layers"),
2671 d[a] = (t->axes[a] || a == t->summary_axis
2672 ? pivot_dimension_create (pt, a, names[a])
2677 assert (t->axes[a]);
2679 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2681 struct ctables_cell *cell;
2683 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2686 assert (n <= t->cells.count);
2688 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2689 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2691 size_t max_depth = 0;
2692 for (size_t j = 0; j < t->stacks[a].n; j++)
2693 if (t->stacks[a].nests[j].n > max_depth)
2694 max_depth = t->stacks[a].nests[j].n;
2696 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2697 struct pivot_category *top = NULL;
2699 for (size_t j = 0; j < n; j++)
2701 struct ctables_cell *cell = sorted[j];
2702 const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
2704 size_t n_common = 0;
2705 bool new_subtable = false;
2708 struct ctables_cell *prev = sorted[j - 1];
2709 if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
2711 for (; n_common < nest->n; n_common++)
2712 if (n_common != nest->scale_idx
2713 && (prev->axes[a].cvs[n_common].category
2714 != cell->axes[a].cvs[n_common].category
2715 || !value_equal (&prev->axes[a].cvs[n_common].value,
2716 &cell->axes[a].cvs[n_common].value,
2717 var_get_type (nest->vars[n_common]))))
2721 new_subtable = true;
2724 new_subtable = true;
2728 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
2730 if (vlabel != CTVL_NONE)
2731 top = pivot_category_create_group__ (
2732 top, pivot_value_new_variable (nest->vars[0]));
2734 if (n_common == nest->n)
2736 cell->axes[a].leaf = prev_leaf;
2740 for (size_t k = n_common; k < nest->n; k++)
2742 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2744 struct pivot_value *label
2745 = (k == nest->scale_idx ? NULL
2746 : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
2747 || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
2748 || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
2749 ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
2751 : pivot_value_new_var_value (nest->vars[k],
2752 &cell->axes[a].cvs[k].value));
2753 if (k == nest->n - 1)
2755 if (a == t->summary_axis)
2758 parent = pivot_category_create_group__ (parent, label);
2759 const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
2760 for (size_t m = 0; m < specs->n; m++)
2762 int leaf = pivot_category_create_leaf (
2763 parent, pivot_value_new_text (specs->specs[m].label));
2770 /* This assertion is true as long as the summary axis
2771 is the axis where the summaries are displayed. */
2774 prev_leaf = pivot_category_create_leaf (parent, label);
2780 parent = pivot_category_create_group__ (parent, label);
2782 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
2783 if (vlabel != CTVL_NONE)
2784 parent = pivot_category_create_group__ (
2785 parent, pivot_value_new_variable (nest->vars[k + 1]));
2789 cell->axes[a].leaf = prev_leaf;
2794 struct ctables_cell *cell;
2795 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2800 const struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].stack_idx];
2801 const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
2802 for (size_t j = 0; j < specs->n; j++)
2805 size_t n_dindexes = 0;
2807 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2810 int leaf = cell->axes[a].leaf;
2811 if (a == t->summary_axis)
2813 dindexes[n_dindexes++] = leaf;
2816 double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
2817 struct pivot_value *value = pivot_value_new_number (d);
2818 value->numeric.format = specs->specs[j].format;
2819 pivot_table_put (pt, dindexes, n_dindexes, value);
2823 pivot_table_submit (pt);
2826 return proc_commit (ds);
2830 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2832 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2833 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2834 enum settings_value_show tvars = settings_get_show_variables ();
2835 for (size_t i = 0; i < n_vars; i++)
2836 vlabels[i] = (enum ctables_vlabel) tvars;
2838 struct ctables *ct = xmalloc (sizeof *ct);
2839 *ct = (struct ctables) {
2840 .look = pivot_table_look_unshare (pivot_table_look_ref (
2841 pivot_table_look_get_default ())),
2843 .hide_threshold = 5,
2845 ct->look->omit_empty = false;
2847 if (!lex_force_match (lexer, T_SLASH))
2850 while (!lex_match_id (lexer, "TABLE"))
2852 if (lex_match_id (lexer, "FORMAT"))
2854 double widths[2] = { SYSMIS, SYSMIS };
2855 double units_per_inch = 72.0;
2857 while (lex_token (lexer) != T_SLASH)
2859 if (lex_match_id (lexer, "MINCOLWIDTH"))
2861 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2864 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2866 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2869 else if (lex_match_id (lexer, "UNITS"))
2871 lex_match (lexer, T_EQUALS);
2872 if (lex_match_id (lexer, "POINTS"))
2873 units_per_inch = 72.0;
2874 else if (lex_match_id (lexer, "INCHES"))
2875 units_per_inch = 1.0;
2876 else if (lex_match_id (lexer, "CM"))
2877 units_per_inch = 2.54;
2880 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2884 else if (lex_match_id (lexer, "EMPTY"))
2889 lex_match (lexer, T_EQUALS);
2890 if (lex_match_id (lexer, "ZERO"))
2892 /* Nothing to do. */
2894 else if (lex_match_id (lexer, "BLANK"))
2895 ct->zero = xstrdup ("");
2896 else if (lex_force_string (lexer))
2898 ct->zero = ss_xstrdup (lex_tokss (lexer));
2904 else if (lex_match_id (lexer, "MISSING"))
2906 lex_match (lexer, T_EQUALS);
2907 if (!lex_force_string (lexer))
2911 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2912 ? ss_xstrdup (lex_tokss (lexer))
2918 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2919 "UNITS", "EMPTY", "MISSING");
2924 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2925 && widths[0] > widths[1])
2927 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2931 for (size_t i = 0; i < 2; i++)
2932 if (widths[i] != SYSMIS)
2934 int *wr = ct->look->width_ranges[TABLE_HORZ];
2935 wr[i] = widths[i] / units_per_inch * 96.0;
2940 else if (lex_match_id (lexer, "VLABELS"))
2942 if (!lex_force_match_id (lexer, "VARIABLES"))
2944 lex_match (lexer, T_EQUALS);
2946 struct variable **vars;
2948 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2952 if (!lex_force_match_id (lexer, "DISPLAY"))
2957 lex_match (lexer, T_EQUALS);
2959 enum ctables_vlabel vlabel;
2960 if (lex_match_id (lexer, "DEFAULT"))
2961 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2962 else if (lex_match_id (lexer, "NAME"))
2964 else if (lex_match_id (lexer, "LABEL"))
2965 vlabel = CTVL_LABEL;
2966 else if (lex_match_id (lexer, "BOTH"))
2968 else if (lex_match_id (lexer, "NONE"))
2972 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2978 for (size_t i = 0; i < n_vars; i++)
2979 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2982 else if (lex_match_id (lexer, "MRSETS"))
2984 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2986 lex_match (lexer, T_EQUALS);
2987 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2990 else if (lex_match_id (lexer, "SMISSING"))
2992 if (lex_match_id (lexer, "VARIABLE"))
2993 ct->smissing_listwise = false;
2994 else if (lex_match_id (lexer, "LISTWISE"))
2995 ct->smissing_listwise = true;
2998 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
3003 else if (lex_match_id (lexer, "WEIGHT"))
3005 if (!lex_force_match_id (lexer, "VARIABLE"))
3007 lex_match (lexer, T_EQUALS);
3008 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
3009 if (!ct->base_weight)
3012 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
3014 if (!lex_force_match_id (lexer, "COUNT"))
3016 lex_match (lexer, T_EQUALS);
3017 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
3019 ct->hide_threshold = lex_integer (lexer);
3024 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
3025 "SMISSING", "PCOMPUTE", "PPROPERTIES",
3026 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
3030 if (!lex_force_match (lexer, T_SLASH))
3034 size_t allocated_tables = 0;
3037 if (ct->n_tables >= allocated_tables)
3038 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
3039 sizeof *ct->tables);
3041 struct ctables_category *cat = xmalloc (sizeof *cat);
3042 *cat = (struct ctables_category) {
3044 .include_missing = false,
3045 .sort_ascending = true,
3048 struct ctables_categories *c = xmalloc (sizeof *c);
3049 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3050 *c = (struct ctables_categories) {
3056 struct ctables_categories **categories = xnmalloc (n_vars,
3057 sizeof *categories);
3058 for (size_t i = 0; i < n_vars; i++)
3061 struct ctables_table *t = xmalloc (sizeof *t);
3062 *t = (struct ctables_table) {
3063 .cells = HMAP_INITIALIZER (t->cells),
3064 .slabels_position = PIVOT_AXIS_COLUMN,
3065 .slabels_visible = true,
3066 .row_labels = CTLP_NORMAL,
3067 .col_labels = CTLP_NORMAL,
3068 .categories = categories,
3069 .n_categories = n_vars,
3072 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3073 hmap_init (&t->domains[dt]);
3074 ct->tables[ct->n_tables++] = t;
3076 lex_match (lexer, T_EQUALS);
3077 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
3079 if (lex_match (lexer, T_BY))
3081 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3082 ct, t, PIVOT_AXIS_COLUMN))
3085 if (lex_match (lexer, T_BY))
3087 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3088 ct, t, PIVOT_AXIS_LAYER))
3093 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
3094 && !t->axes[PIVOT_AXIS_LAYER])
3096 lex_error (lexer, _("At least one variable must be specified."));
3100 const struct ctables_axis *scales[PIVOT_N_AXES];
3101 size_t n_scales = 0;
3102 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3104 scales[a] = find_scale (t->axes[a]);
3110 msg (SE, _("Scale variables may appear only on one axis."));
3111 if (scales[PIVOT_AXIS_ROW])
3112 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
3113 _("This scale variable appears on the rows axis."));
3114 if (scales[PIVOT_AXIS_COLUMN])
3115 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3116 _("This scale variable appears on the columns axis."));
3117 if (scales[PIVOT_AXIS_LAYER])
3118 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3119 _("This scale variable appears on the layer axis."));
3123 const struct ctables_axis *summaries[PIVOT_N_AXES];
3124 size_t n_summaries = 0;
3125 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3127 summaries[a] = (scales[a]
3129 : find_categorical_summary_spec (t->axes[a]));
3133 if (n_summaries > 1)
3135 msg (SE, _("Summaries may appear only on one axis."));
3136 if (summaries[PIVOT_AXIS_ROW])
3137 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3138 _("This variable on the rows axis has a summary."));
3139 if (summaries[PIVOT_AXIS_COLUMN])
3140 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3141 _("This variable on the columns axis has a summary."));
3142 if (summaries[PIVOT_AXIS_LAYER])
3143 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3144 _("This variable on the layers axis has a summary."));
3147 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3148 if (n_summaries ? summaries[a] : t->axes[a])
3150 t->summary_axis = a;
3154 if (lex_token (lexer) == T_ENDCMD)
3156 if (!lex_force_match (lexer, T_SLASH))
3159 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3161 if (lex_match_id (lexer, "SLABELS"))
3163 while (lex_token (lexer) != T_SLASH)
3165 if (lex_match_id (lexer, "POSITION"))
3167 lex_match (lexer, T_EQUALS);
3168 if (lex_match_id (lexer, "COLUMN"))
3169 t->slabels_position = PIVOT_AXIS_COLUMN;
3170 else if (lex_match_id (lexer, "ROW"))
3171 t->slabels_position = PIVOT_AXIS_ROW;
3172 else if (lex_match_id (lexer, "LAYER"))
3173 t->slabels_position = PIVOT_AXIS_LAYER;
3176 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3180 else if (lex_match_id (lexer, "VISIBLE"))
3182 lex_match (lexer, T_EQUALS);
3183 if (!parse_bool (lexer, &t->slabels_visible))
3188 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3193 else if (lex_match_id (lexer, "CLABELS"))
3195 while (lex_token (lexer) != T_SLASH)
3197 if (lex_match_id (lexer, "AUTO"))
3198 t->row_labels = t->col_labels = CTLP_NORMAL;
3199 else if (lex_match_id (lexer, "ROWLABELS"))
3201 lex_match (lexer, T_EQUALS);
3202 if (lex_match_id (lexer, "OPPOSITE"))
3203 t->row_labels = CTLP_OPPOSITE;
3204 else if (lex_match_id (lexer, "LAYER"))
3205 t->row_labels = CTLP_LAYER;
3208 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3212 else if (lex_match_id (lexer, "COLLABELS"))
3214 lex_match (lexer, T_EQUALS);
3215 if (lex_match_id (lexer, "OPPOSITE"))
3216 t->col_labels = CTLP_OPPOSITE;
3217 else if (lex_match_id (lexer, "LAYER"))
3218 t->col_labels = CTLP_LAYER;
3221 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3227 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3233 else if (lex_match_id (lexer, "CRITERIA"))
3235 if (!lex_force_match_id (lexer, "CILEVEL"))
3237 lex_match (lexer, T_EQUALS);
3239 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3241 t->cilevel = lex_number (lexer);
3244 else if (lex_match_id (lexer, "CATEGORIES"))
3246 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3249 else if (lex_match_id (lexer, "TITLES"))
3254 if (lex_match_id (lexer, "CAPTION"))
3255 textp = &t->caption;
3256 else if (lex_match_id (lexer, "CORNER"))
3258 else if (lex_match_id (lexer, "TITLE"))
3262 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3265 lex_match (lexer, T_EQUALS);
3267 struct string s = DS_EMPTY_INITIALIZER;
3268 while (lex_is_string (lexer))
3270 if (!ds_is_empty (&s))
3271 ds_put_byte (&s, ' ');
3272 ds_put_substring (&s, lex_tokss (lexer));
3276 *textp = ds_steal_cstr (&s);
3278 while (lex_token (lexer) != T_SLASH
3279 && lex_token (lexer) != T_ENDCMD);
3281 else if (lex_match_id (lexer, "SIGTEST"))
3285 t->chisq = xmalloc (sizeof *t->chisq);
3286 *t->chisq = (struct ctables_chisq) {
3288 .include_mrsets = true,
3289 .all_visible = true,
3295 if (lex_match_id (lexer, "TYPE"))
3297 lex_match (lexer, T_EQUALS);
3298 if (!lex_force_match_id (lexer, "CHISQUARE"))
3301 else if (lex_match_id (lexer, "ALPHA"))
3303 lex_match (lexer, T_EQUALS);
3304 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3306 t->chisq->alpha = lex_number (lexer);
3309 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3311 lex_match (lexer, T_EQUALS);
3312 if (parse_bool (lexer, &t->chisq->include_mrsets))
3315 else if (lex_match_id (lexer, "CATEGORIES"))
3317 lex_match (lexer, T_EQUALS);
3318 if (lex_match_id (lexer, "ALLVISIBLE"))
3319 t->chisq->all_visible = true;
3320 else if (lex_match_id (lexer, "SUBTOTALS"))
3321 t->chisq->all_visible = false;
3324 lex_error_expecting (lexer,
3325 "ALLVISIBLE", "SUBTOTALS");
3331 lex_error_expecting (lexer, "TYPE", "ALPHA",
3332 "INCLUDEMRSETS", "CATEGORIES");
3336 while (lex_token (lexer) != T_SLASH
3337 && lex_token (lexer) != T_ENDCMD);
3339 else if (lex_match_id (lexer, "COMPARETEST"))
3343 t->pairwise = xmalloc (sizeof *t->pairwise);
3344 *t->pairwise = (struct ctables_pairwise) {
3346 .alpha = { .05, .05 },
3347 .adjust = BONFERRONI,
3348 .include_mrsets = true,
3349 .meansvariance_allcats = true,
3350 .all_visible = true,
3359 if (lex_match_id (lexer, "TYPE"))
3361 lex_match (lexer, T_EQUALS);
3362 if (lex_match_id (lexer, "PROP"))
3363 t->pairwise->type = PROP;
3364 else if (lex_match_id (lexer, "MEAN"))
3365 t->pairwise->type = MEAN;
3368 lex_error_expecting (lexer, "PROP", "MEAN");
3372 else if (lex_match_id (lexer, "ALPHA"))
3374 lex_match (lexer, T_EQUALS);
3376 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3378 double a0 = lex_number (lexer);
3381 lex_match (lexer, T_COMMA);
3382 if (lex_is_number (lexer))
3384 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3386 double a1 = lex_number (lexer);
3389 t->pairwise->alpha[0] = MIN (a0, a1);
3390 t->pairwise->alpha[1] = MAX (a0, a1);
3393 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3395 else if (lex_match_id (lexer, "ADJUST"))
3397 lex_match (lexer, T_EQUALS);
3398 if (lex_match_id (lexer, "BONFERRONI"))
3399 t->pairwise->adjust = BONFERRONI;
3400 else if (lex_match_id (lexer, "BH"))
3401 t->pairwise->adjust = BH;
3402 else if (lex_match_id (lexer, "NONE"))
3403 t->pairwise->adjust = 0;
3406 lex_error_expecting (lexer, "BONFERRONI", "BH",
3411 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3413 lex_match (lexer, T_EQUALS);
3414 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3417 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3419 lex_match (lexer, T_EQUALS);
3420 if (lex_match_id (lexer, "ALLCATS"))
3421 t->pairwise->meansvariance_allcats = true;
3422 else if (lex_match_id (lexer, "TESTEDCATS"))
3423 t->pairwise->meansvariance_allcats = false;
3426 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3430 else if (lex_match_id (lexer, "CATEGORIES"))
3432 lex_match (lexer, T_EQUALS);
3433 if (lex_match_id (lexer, "ALLVISIBLE"))
3434 t->pairwise->all_visible = true;
3435 else if (lex_match_id (lexer, "SUBTOTALS"))
3436 t->pairwise->all_visible = false;
3439 lex_error_expecting (lexer, "ALLVISIBLE",
3444 else if (lex_match_id (lexer, "MERGE"))
3446 lex_match (lexer, T_EQUALS);
3447 if (!parse_bool (lexer, &t->pairwise->merge))
3450 else if (lex_match_id (lexer, "STYLE"))
3452 lex_match (lexer, T_EQUALS);
3453 if (lex_match_id (lexer, "APA"))
3454 t->pairwise->apa_style = true;
3455 else if (lex_match_id (lexer, "SIMPLE"))
3456 t->pairwise->apa_style = false;
3459 lex_error_expecting (lexer, "APA", "SIMPLE");
3463 else if (lex_match_id (lexer, "SHOWSIG"))
3465 lex_match (lexer, T_EQUALS);
3466 if (!parse_bool (lexer, &t->pairwise->show_sig))
3471 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3472 "INCLUDEMRSETS", "MEANSVARIANCE",
3473 "CATEGORIES", "MERGE", "STYLE",
3478 while (lex_token (lexer) != T_SLASH
3479 && lex_token (lexer) != T_ENDCMD);
3483 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3484 "CRITERIA", "CATEGORIES", "TITLES",
3485 "SIGTEST", "COMPARETEST");
3489 if (!lex_match (lexer, T_SLASH))
3493 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3495 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3500 while (lex_token (lexer) != T_ENDCMD);
3502 bool ok = ctables_execute (ds, ct);
3503 ctables_destroy (ct);
3504 return ok ? CMD_SUCCESS : CMD_FAILURE;
3507 ctables_destroy (ct);