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 */
546 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
547 const struct ctables_summary_spec *src)
550 dst->label = xstrdup (src->label);
554 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
561 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
562 const struct ctables_summary_spec_set *src)
564 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
565 for (size_t i = 0; i < src->n; i++)
566 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
568 *dst = (struct ctables_summary_spec_set) {
577 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
579 for (size_t i = 0; i < set->n; i++)
580 ctables_summary_spec_uninit (&set->specs[i]);
585 parse_col_width (struct lexer *lexer, const char *name, double *width)
587 lex_match (lexer, T_EQUALS);
588 if (lex_match_id (lexer, "DEFAULT"))
590 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
592 *width = lex_number (lexer);
602 parse_bool (struct lexer *lexer, bool *b)
604 if (lex_match_id (lexer, "NO"))
606 else if (lex_match_id (lexer, "YES"))
610 lex_error_expecting (lexer, "YES", "NO");
616 static enum ctables_function_availability
617 ctables_function_availability (enum ctables_summary_function f)
619 static enum ctables_function_availability availability[] = {
620 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
625 return availability[f];
629 parse_ctables_summary_function (struct lexer *lexer,
630 enum ctables_summary_function *f)
634 enum ctables_summary_function function;
635 struct substring name;
637 static struct pair names[] = {
638 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
639 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
642 /* The .COUNT suffix may be omitted. */
643 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
644 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
645 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
646 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
647 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
648 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
649 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
653 if (!lex_force_id (lexer))
656 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
657 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
659 *f = names[i].function;
664 lex_error (lexer, _("Expecting summary function name."));
669 ctables_axis_destroy (struct ctables_axis *axis)
677 for (size_t i = 0; i < N_CSVS; i++)
678 ctables_summary_spec_set_uninit (&axis->specs[i]);
683 ctables_axis_destroy (axis->subs[0]);
684 ctables_axis_destroy (axis->subs[1]);
687 msg_location_destroy (axis->loc);
691 static struct ctables_axis *
692 ctables_axis_new_nonterminal (enum ctables_axis_op op,
693 struct ctables_axis *sub0,
694 struct ctables_axis *sub1,
695 struct lexer *lexer, int start_ofs)
697 struct ctables_axis *axis = xmalloc (sizeof *axis);
698 *axis = (struct ctables_axis) {
700 .subs = { sub0, sub1 },
701 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
706 struct ctables_axis_parse_ctx
709 struct dictionary *dict;
711 struct ctables_table *t;
714 static struct fmt_spec
715 ctables_summary_default_format (enum ctables_summary_function function,
716 const struct ctables_var *var)
718 static const enum ctables_format default_formats[] = {
719 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
723 switch (default_formats[function])
726 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
729 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
732 return *ctables_var_get_print_format (var);
740 ctables_summary_default_label (enum ctables_summary_function function,
743 static const char *default_labels[] = {
744 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
749 return (function == CTSF_PTILE
750 ? xasprintf (_("Percentile %.2f"), percentile)
751 : xstrdup (gettext (default_labels[function])));
755 ctables_summary_function_name (enum ctables_summary_function function)
757 static const char *names[] = {
758 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
762 return names[function];
766 add_summary_spec (struct ctables_axis *axis,
767 enum ctables_summary_function function, double percentile,
768 const char *label, const struct fmt_spec *format,
769 const struct msg_location *loc, enum ctables_summary_variant sv)
771 if (axis->op == CTAO_VAR)
773 const char *function_name = ctables_summary_function_name (function);
774 const char *var_name = ctables_var_name (&axis->var);
775 switch (ctables_function_availability (function))
778 if (!axis->var.is_mrset)
780 msg_at (SE, loc, _("Summary function %s applies only to multiple "
781 "response sets."), function_name);
782 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
792 _("Summary function %s applies only to scale variables."),
794 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
804 struct ctables_summary_spec_set *set = &axis->specs[sv];
805 if (set->n >= set->allocated)
806 set->specs = x2nrealloc (set->specs, &set->allocated,
809 struct ctables_summary_spec *dst = &set->specs[set->n++];
810 *dst = (struct ctables_summary_spec) {
811 .function = function,
812 .percentile = percentile,
813 .label = xstrdup (label),
814 .format = (format ? *format
815 : ctables_summary_default_format (function, &axis->var)),
821 for (size_t i = 0; i < 2; i++)
822 if (!add_summary_spec (axis->subs[i], function, percentile, label,
829 static struct ctables_axis *ctables_axis_parse_stack (
830 struct ctables_axis_parse_ctx *);
833 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
834 struct ctables_var *var)
836 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
838 *var = (struct ctables_var) {
840 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
844 lex_error (lexer, _("'%s' does not name a multiple-response set "
845 "in the active file dictionary."),
846 lex_tokcstr (lexer));
854 *var = (struct ctables_var) {
856 .var = parse_variable (lexer, dict),
858 return var->var != NULL;
862 static struct ctables_axis *
863 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
865 if (lex_match (ctx->lexer, T_LPAREN))
867 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
868 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
870 ctables_axis_destroy (sub);
876 if (!lex_force_id (ctx->lexer))
879 int start_ofs = lex_ofs (ctx->lexer);
880 struct ctables_var var;
881 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
884 struct ctables_axis *axis = xmalloc (sizeof *axis);
885 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
887 /* XXX should figure out default measures by reading data */
888 axis->scale = (var.is_mrset ? false
889 : lex_match_phrase (ctx->lexer, "[S]") ? true
890 : lex_match_phrase (ctx->lexer, "[C]") ? false
891 : var_get_measure (var.var) == MEASURE_SCALE);
892 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
893 lex_ofs (ctx->lexer) - 1);
897 static struct ctables_axis *
898 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
900 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
901 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
907 int start_ofs = lex_ofs (ctx->lexer);
909 /* Parse function. */
910 enum ctables_summary_function function;
911 if (!parse_ctables_summary_function (ctx->lexer, &function))
914 /* Parse percentile. */
915 double percentile = 0;
916 if (function == CTSF_PTILE)
918 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
920 percentile = lex_number (ctx->lexer);
921 lex_get (ctx->lexer);
926 if (lex_is_string (ctx->lexer))
928 label = ss_xstrdup (lex_tokss (ctx->lexer));
929 lex_get (ctx->lexer);
932 label = ctables_summary_default_label (function, percentile);
935 struct fmt_spec format;
936 const struct fmt_spec *formatp;
937 if (lex_token (ctx->lexer) == T_ID)
939 if (!parse_format_specifier (ctx->lexer, &format)
940 || !fmt_check_output (&format)
941 || !fmt_check_type_compat (&format, VAL_NUMERIC))
951 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
952 lex_ofs (ctx->lexer) - 1);
953 add_summary_spec (sub, function, percentile, label, formatp, loc,
956 msg_location_destroy (loc);
958 if (lex_match (ctx->lexer, T_COMMA))
960 if (!totals && lex_match_id (ctx->lexer, "TOTALS"))
962 if (!lex_force_match (ctx->lexer, T_LBRACK))
967 else if (lex_force_match (ctx->lexer, T_RBRACK))
969 if (totals && !lex_force_match (ctx->lexer, T_RBRACK))
978 ctables_axis_destroy (sub);
982 static const struct ctables_axis *
983 find_scale (const struct ctables_axis *axis)
987 else if (axis->op == CTAO_VAR)
991 assert (!axis->var.is_mrset);
999 for (size_t i = 0; i < 2; i++)
1001 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1009 static const struct ctables_axis *
1010 find_categorical_summary_spec (const struct ctables_axis *axis)
1014 else if (axis->op == CTAO_VAR)
1015 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1018 for (size_t i = 0; i < 2; i++)
1020 const struct ctables_axis *sum
1021 = find_categorical_summary_spec (axis->subs[i]);
1029 static struct ctables_axis *
1030 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1032 int start_ofs = lex_ofs (ctx->lexer);
1033 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1037 while (lex_match (ctx->lexer, T_GT))
1039 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1043 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1044 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1046 const struct ctables_axis *outer_scale = find_scale (lhs);
1047 const struct ctables_axis *inner_scale = find_scale (rhs);
1048 if (outer_scale && inner_scale)
1050 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1051 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1052 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1053 ctables_axis_destroy (nest);
1057 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1060 msg_at (SE, nest->loc,
1061 _("Summaries may only be requested for categorical variables "
1062 "at the innermost nesting level."));
1063 msg_at (SN, outer_sum->loc,
1064 _("This outer categorical variable has a summary."));
1065 ctables_axis_destroy (nest);
1075 static struct ctables_axis *
1076 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1078 int start_ofs = lex_ofs (ctx->lexer);
1079 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1083 while (lex_match (ctx->lexer, T_PLUS))
1085 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1089 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1090 ctx->lexer, start_ofs);
1097 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1098 struct ctables *ct, struct ctables_table *t,
1099 enum pivot_axis_type a)
1101 if (lex_token (lexer) == T_BY
1102 || lex_token (lexer) == T_SLASH
1103 || lex_token (lexer) == T_ENDCMD)
1106 struct ctables_axis_parse_ctx ctx = {
1112 t->axes[a] = ctables_axis_parse_stack (&ctx);
1113 return t->axes[a] != NULL;
1117 ctables_chisq_destroy (struct ctables_chisq *chisq)
1123 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1129 ctables_table_destroy (struct ctables_table *t)
1134 for (size_t i = 0; i < t->n_categories; i++)
1135 ctables_categories_unref (t->categories[i]);
1136 free (t->categories);
1138 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1139 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1140 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1144 ctables_chisq_destroy (t->chisq);
1145 ctables_pairwise_destroy (t->pairwise);
1150 ctables_destroy (struct ctables *ct)
1155 pivot_table_look_unref (ct->look);
1159 for (size_t i = 0; i < ct->n_tables; i++)
1160 ctables_table_destroy (ct->tables[i]);
1165 static struct ctables_category
1166 cct_range (double low, double high)
1168 return (struct ctables_category) {
1170 .range = { low, high }
1175 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1176 struct ctables_table *t)
1178 if (!lex_match_id (lexer, "VARIABLES"))
1180 lex_match (lexer, T_EQUALS);
1182 struct variable **vars;
1184 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1187 struct ctables_categories *c = xmalloc (sizeof *c);
1188 *c = (struct ctables_categories) { .n_refs = n_vars };
1189 for (size_t i = 0; i < n_vars; i++)
1191 struct ctables_categories **cp
1192 = &t->categories[var_get_dict_index (vars[i])];
1193 ctables_categories_unref (*cp);
1198 size_t allocated_cats = 0;
1199 if (lex_match (lexer, T_LBRACK))
1203 if (c->n_cats >= allocated_cats)
1204 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1206 struct ctables_category *cat = &c->cats[c->n_cats];
1207 if (lex_match_id (lexer, "OTHERNM"))
1208 cat->type = CCT_OTHERNM;
1209 else if (lex_match_id (lexer, "MISSING"))
1210 cat->type = CCT_MISSING;
1211 else if (lex_match_id (lexer, "SUBTOTAL"))
1212 *cat = (struct ctables_category)
1213 { .type = CCT_SUBTOTAL, .total_label = NULL };
1214 else if (lex_match_id (lexer, "HSUBTOTAL"))
1215 *cat = (struct ctables_category)
1216 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1217 else if (lex_match_id (lexer, "LO"))
1219 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1221 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1224 else if (lex_is_number (lexer))
1226 double number = lex_number (lexer);
1228 if (lex_match_id (lexer, "THRU"))
1230 cat->type = CCT_RANGE;
1231 cat->range[0] = number;
1232 if (lex_match_id (lexer, "HI"))
1233 *cat = cct_range (number, DBL_MAX);
1236 if (!lex_force_num (lexer))
1238 *cat = cct_range (number, lex_number (lexer));
1243 *cat = (struct ctables_category) {
1248 else if (lex_is_string (lexer))
1250 *cat = (struct ctables_category) {
1252 .string = ss_xstrdup (lex_tokss (lexer)),
1258 lex_error (lexer, NULL);
1262 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1264 if (lex_match (lexer, T_EQUALS))
1266 if (!lex_force_string (lexer))
1269 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1273 cat->total_label = xstrdup (_("Subtotal"));
1277 lex_match (lexer, T_COMMA);
1279 while (!lex_match (lexer, T_RBRACK));
1282 struct ctables_category cat = {
1284 .include_missing = false,
1285 .sort_ascending = true,
1287 bool show_totals = false;
1288 char *total_label = NULL;
1289 bool totals_before = false;
1290 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1292 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1294 lex_match (lexer, T_EQUALS);
1295 if (lex_match_id (lexer, "A"))
1296 cat.sort_ascending = true;
1297 else if (lex_match_id (lexer, "D"))
1298 cat.sort_ascending = false;
1301 lex_error_expecting (lexer, "A", "D");
1305 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1307 lex_match (lexer, T_EQUALS);
1308 if (lex_match_id (lexer, "VALUE"))
1309 cat.type = CCT_VALUE;
1310 else if (lex_match_id (lexer, "LABEL"))
1311 cat.type = CCT_LABEL;
1314 cat.type = CCT_FUNCTION;
1315 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1318 if (lex_match (lexer, T_LPAREN))
1320 cat.sort_var = parse_variable (lexer, dict);
1324 if (cat.sort_function == CTSF_PTILE)
1326 lex_match (lexer, T_COMMA);
1327 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1329 cat.percentile = lex_number (lexer);
1333 if (!lex_force_match (lexer, T_RPAREN))
1336 else if (ctables_function_availability (cat.sort_function)
1339 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1344 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1346 lex_match (lexer, T_EQUALS);
1347 if (lex_match_id (lexer, "INCLUDE"))
1348 cat.include_missing = true;
1349 else if (lex_match_id (lexer, "EXCLUDE"))
1350 cat.include_missing = false;
1353 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1357 else if (lex_match_id (lexer, "TOTAL"))
1359 lex_match (lexer, T_EQUALS);
1360 if (!parse_bool (lexer, &show_totals))
1363 else if (lex_match_id (lexer, "LABEL"))
1365 lex_match (lexer, T_EQUALS);
1366 if (!lex_force_string (lexer))
1369 total_label = ss_xstrdup (lex_tokss (lexer));
1372 else if (lex_match_id (lexer, "POSITION"))
1374 lex_match (lexer, T_EQUALS);
1375 if (lex_match_id (lexer, "BEFORE"))
1376 totals_before = true;
1377 else if (lex_match_id (lexer, "AFTER"))
1378 totals_before = false;
1381 lex_error_expecting (lexer, "BEFORE", "AFTER");
1385 else if (lex_match_id (lexer, "EMPTY"))
1387 lex_match (lexer, T_EQUALS);
1388 if (lex_match_id (lexer, "INCLUDE"))
1389 c->show_empty = true;
1390 else if (lex_match_id (lexer, "EXCLUDE"))
1391 c->show_empty = false;
1394 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1401 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1402 "TOTAL", "LABEL", "POSITION", "EMPTY");
1404 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1411 if (c->n_cats >= allocated_cats)
1412 c->cats = x2nrealloc (c->cats, &allocated_cats,
1414 c->cats[c->n_cats++] = cat;
1419 if (c->n_cats >= allocated_cats)
1420 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1422 struct ctables_category *totals;
1425 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1426 totals = &c->cats[0];
1429 totals = &c->cats[c->n_cats];
1432 *totals = (struct ctables_category) {
1434 .total_label = total_label ? total_label : xstrdup (_("Total")),
1438 struct ctables_category *subtotal = NULL;
1439 for (size_t i = totals_before ? 0 : c->n_cats;
1440 totals_before ? i < c->n_cats : i-- > 0;
1441 totals_before ? i++ : 0)
1443 struct ctables_category *cat = &c->cats[i];
1451 cat->subtotal = subtotal;
1471 ctables_nest_uninit (struct ctables_nest *nest)
1478 ctables_stack_uninit (struct ctables_stack *stack)
1482 for (size_t i = 0; i < stack->n; i++)
1483 ctables_nest_uninit (&stack->nests[i]);
1484 free (stack->nests);
1488 static struct ctables_stack
1489 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1496 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1497 for (size_t i = 0; i < s0.n; i++)
1498 for (size_t j = 0; j < s1.n; j++)
1500 const struct ctables_nest *a = &s0.nests[i];
1501 const struct ctables_nest *b = &s1.nests[j];
1503 size_t allocate = a->n + b->n;
1504 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1505 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1507 for (size_t k = 0; k < a->n; k++)
1508 vars[n++] = a->vars[k];
1509 for (size_t k = 0; k < b->n; k++)
1510 vars[n++] = b->vars[k];
1511 assert (n == allocate);
1513 const struct ctables_nest *summary_src;
1514 if (!a->specs[CSV_CELL].var)
1516 else if (!b->specs[CSV_CELL].var)
1521 struct ctables_nest *new = &stack.nests[stack.n++];
1522 *new = (struct ctables_nest) {
1524 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1525 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1529 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1530 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1532 ctables_stack_uninit (&s0);
1533 ctables_stack_uninit (&s1);
1537 static struct ctables_stack
1538 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1540 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1541 for (size_t i = 0; i < s0.n; i++)
1542 stack.nests[stack.n++] = s0.nests[i];
1543 for (size_t i = 0; i < s1.n; i++)
1544 stack.nests[stack.n++] = s1.nests[i];
1545 assert (stack.n == s0.n + s1.n);
1551 static struct ctables_stack
1552 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1555 return (struct ctables_stack) { .n = 0 };
1560 assert (!a->var.is_mrset);
1562 struct variable **vars = xmalloc (sizeof *vars);
1565 struct ctables_nest *nest = xmalloc (sizeof *nest);
1566 *nest = (struct ctables_nest) {
1569 .scale_idx = a->scale ? 0 : SIZE_MAX,
1571 if (a->specs[CSV_CELL].n || a->scale)
1572 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1574 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1575 nest->specs[sv].var = a->var.var;
1577 return (struct ctables_stack) { .nests = nest, .n = 1 };
1580 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1581 enumerate_fts (axis_type, a->subs[1]));
1584 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1585 enumerate_fts (axis_type, a->subs[1]));
1591 union ctables_summary
1593 /* COUNT, VALIDN, TOTALN. */
1600 /* MINIMUM, MAXIMUM, RANGE. */
1607 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1608 struct moments1 *moments;
1610 /* XXX percentiles, median, mode, multiple response */
1614 ctables_summary_init (union ctables_summary *s,
1615 const struct ctables_summary_spec *ss)
1617 switch (ss->function)
1621 case CTSF_ROWPCT_COUNT:
1622 case CTSF_COLPCT_COUNT:
1623 case CTSF_TABLEPCT_COUNT:
1624 case CTSF_SUBTABLEPCT_COUNT:
1625 case CTSF_LAYERPCT_COUNT:
1626 case CTSF_LAYERROWPCT_COUNT:
1627 case CTSF_LAYERCOLPCT_COUNT:
1628 case CTSF_ROWPCT_VALIDN:
1629 case CTSF_COLPCT_VALIDN:
1630 case CTSF_TABLEPCT_VALIDN:
1631 case CTSF_SUBTABLEPCT_VALIDN:
1632 case CTSF_LAYERPCT_VALIDN:
1633 case CTSF_LAYERROWPCT_VALIDN:
1634 case CTSF_LAYERCOLPCT_VALIDN:
1635 case CTSF_ROWPCT_TOTALN:
1636 case CTSF_COLPCT_TOTALN:
1637 case CTSF_TABLEPCT_TOTALN:
1638 case CTSF_SUBTABLEPCT_TOTALN:
1639 case CTSF_LAYERPCT_TOTALN:
1640 case CTSF_LAYERROWPCT_TOTALN:
1641 case CTSF_LAYERCOLPCT_TOTALN:
1646 s->missing = s->valid = 0;
1652 s->min = s->max = SYSMIS;
1660 case CTSF_ROWPCT_SUM:
1661 case CTSF_COLPCT_SUM:
1662 case CTSF_TABLEPCT_SUM:
1663 case CTSF_SUBTABLEPCT_SUM:
1664 case CTSF_LAYERPCT_SUM:
1665 case CTSF_LAYERROWPCT_SUM:
1666 case CTSF_LAYERCOLPCT_SUM:
1667 s->moments = moments1_create (MOMENT_VARIANCE);
1676 case CTSF_RESPONSES:
1677 case CTSF_ROWPCT_RESPONSES:
1678 case CTSF_COLPCT_RESPONSES:
1679 case CTSF_TABLEPCT_RESPONSES:
1680 case CTSF_SUBTABLEPCT_RESPONSES:
1681 case CTSF_LAYERPCT_RESPONSES:
1682 case CTSF_LAYERROWPCT_RESPONSES:
1683 case CTSF_LAYERCOLPCT_RESPONSES:
1684 case CTSF_ROWPCT_RESPONSES_COUNT:
1685 case CTSF_COLPCT_RESPONSES_COUNT:
1686 case CTSF_TABLEPCT_RESPONSES_COUNT:
1687 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1688 case CTSF_LAYERPCT_RESPONSES_COUNT:
1689 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1690 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1691 case CTSF_ROWPCT_COUNT_RESPONSES:
1692 case CTSF_COLPCT_COUNT_RESPONSES:
1693 case CTSF_TABLEPCT_COUNT_RESPONSES:
1694 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1695 case CTSF_LAYERPCT_COUNT_RESPONSES:
1696 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1697 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1703 ctables_summary_uninit (union ctables_summary *s,
1704 const struct ctables_summary_spec *ss)
1706 switch (ss->function)
1710 case CTSF_ROWPCT_COUNT:
1711 case CTSF_COLPCT_COUNT:
1712 case CTSF_TABLEPCT_COUNT:
1713 case CTSF_SUBTABLEPCT_COUNT:
1714 case CTSF_LAYERPCT_COUNT:
1715 case CTSF_LAYERROWPCT_COUNT:
1716 case CTSF_LAYERCOLPCT_COUNT:
1717 case CTSF_ROWPCT_VALIDN:
1718 case CTSF_COLPCT_VALIDN:
1719 case CTSF_TABLEPCT_VALIDN:
1720 case CTSF_SUBTABLEPCT_VALIDN:
1721 case CTSF_LAYERPCT_VALIDN:
1722 case CTSF_LAYERROWPCT_VALIDN:
1723 case CTSF_LAYERCOLPCT_VALIDN:
1724 case CTSF_ROWPCT_TOTALN:
1725 case CTSF_COLPCT_TOTALN:
1726 case CTSF_TABLEPCT_TOTALN:
1727 case CTSF_SUBTABLEPCT_TOTALN:
1728 case CTSF_LAYERPCT_TOTALN:
1729 case CTSF_LAYERROWPCT_TOTALN:
1730 case CTSF_LAYERCOLPCT_TOTALN:
1747 case CTSF_ROWPCT_SUM:
1748 case CTSF_COLPCT_SUM:
1749 case CTSF_TABLEPCT_SUM:
1750 case CTSF_SUBTABLEPCT_SUM:
1751 case CTSF_LAYERPCT_SUM:
1752 case CTSF_LAYERROWPCT_SUM:
1753 case CTSF_LAYERCOLPCT_SUM:
1754 moments1_destroy (s->moments);
1763 case CTSF_RESPONSES:
1764 case CTSF_ROWPCT_RESPONSES:
1765 case CTSF_COLPCT_RESPONSES:
1766 case CTSF_TABLEPCT_RESPONSES:
1767 case CTSF_SUBTABLEPCT_RESPONSES:
1768 case CTSF_LAYERPCT_RESPONSES:
1769 case CTSF_LAYERROWPCT_RESPONSES:
1770 case CTSF_LAYERCOLPCT_RESPONSES:
1771 case CTSF_ROWPCT_RESPONSES_COUNT:
1772 case CTSF_COLPCT_RESPONSES_COUNT:
1773 case CTSF_TABLEPCT_RESPONSES_COUNT:
1774 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1775 case CTSF_LAYERPCT_RESPONSES_COUNT:
1776 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1777 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1778 case CTSF_ROWPCT_COUNT_RESPONSES:
1779 case CTSF_COLPCT_COUNT_RESPONSES:
1780 case CTSF_TABLEPCT_COUNT_RESPONSES:
1781 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1782 case CTSF_LAYERPCT_COUNT_RESPONSES:
1783 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1784 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1790 ctables_summary_add (union ctables_summary *s,
1791 const struct ctables_summary_spec *ss,
1792 const struct variable *var, const union value *value,
1795 switch (ss->function)
1799 case CTSF_ROWPCT_COUNT:
1800 case CTSF_COLPCT_COUNT:
1801 case CTSF_TABLEPCT_COUNT:
1802 case CTSF_SUBTABLEPCT_COUNT:
1803 case CTSF_LAYERPCT_COUNT:
1804 case CTSF_LAYERROWPCT_COUNT:
1805 case CTSF_LAYERCOLPCT_COUNT:
1806 case CTSF_ROWPCT_VALIDN:
1807 case CTSF_COLPCT_VALIDN:
1808 case CTSF_TABLEPCT_VALIDN:
1809 case CTSF_SUBTABLEPCT_VALIDN:
1810 case CTSF_LAYERPCT_VALIDN:
1811 case CTSF_LAYERROWPCT_VALIDN:
1812 case CTSF_LAYERCOLPCT_VALIDN:
1813 case CTSF_ROWPCT_TOTALN:
1814 case CTSF_COLPCT_TOTALN:
1815 case CTSF_TABLEPCT_TOTALN:
1816 case CTSF_SUBTABLEPCT_TOTALN:
1817 case CTSF_LAYERPCT_TOTALN:
1818 case CTSF_LAYERROWPCT_TOTALN:
1819 case CTSF_LAYERCOLPCT_TOTALN:
1824 if (var_is_value_missing (var, value))
1825 s->missing += weight;
1833 if (!var_is_value_missing (var, value))
1835 assert (!var_is_alpha (var)); /* XXX? */
1836 if (s->min == SYSMIS || value->f < s->min)
1838 if (s->max == SYSMIS || value->f > s->max)
1848 case CTSF_ROWPCT_SUM:
1849 case CTSF_COLPCT_SUM:
1850 case CTSF_TABLEPCT_SUM:
1851 case CTSF_SUBTABLEPCT_SUM:
1852 case CTSF_LAYERPCT_SUM:
1853 case CTSF_LAYERROWPCT_SUM:
1854 case CTSF_LAYERCOLPCT_SUM:
1855 moments1_add (s->moments, value->f, weight);
1864 case CTSF_RESPONSES:
1865 case CTSF_ROWPCT_RESPONSES:
1866 case CTSF_COLPCT_RESPONSES:
1867 case CTSF_TABLEPCT_RESPONSES:
1868 case CTSF_SUBTABLEPCT_RESPONSES:
1869 case CTSF_LAYERPCT_RESPONSES:
1870 case CTSF_LAYERROWPCT_RESPONSES:
1871 case CTSF_LAYERCOLPCT_RESPONSES:
1872 case CTSF_ROWPCT_RESPONSES_COUNT:
1873 case CTSF_COLPCT_RESPONSES_COUNT:
1874 case CTSF_TABLEPCT_RESPONSES_COUNT:
1875 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1876 case CTSF_LAYERPCT_RESPONSES_COUNT:
1877 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1878 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1879 case CTSF_ROWPCT_COUNT_RESPONSES:
1880 case CTSF_COLPCT_COUNT_RESPONSES:
1881 case CTSF_TABLEPCT_COUNT_RESPONSES:
1882 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1883 case CTSF_LAYERPCT_COUNT_RESPONSES:
1884 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1885 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1891 ctables_summary_value (const struct ctables_cell *cell,
1892 union ctables_summary *s,
1893 const struct ctables_summary_spec *ss)
1895 switch (ss->function)
1901 case CTSF_SUBTABLEPCT_COUNT:
1902 return cell->domains[CTDT_SUBTABLE]->valid ? s->valid / cell->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1904 case CTSF_ROWPCT_COUNT:
1905 return cell->domains[CTDT_ROW]->valid ? s->valid / cell->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1907 case CTSF_COLPCT_COUNT:
1908 return cell->domains[CTDT_COL]->valid ? s->valid / cell->domains[CTDT_COL]->valid * 100 : SYSMIS;
1910 case CTSF_TABLEPCT_COUNT:
1911 return cell->domains[CTDT_TABLE]->valid ? s->valid / cell->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1913 case CTSF_LAYERPCT_COUNT:
1914 return cell->domains[CTDT_LAYER]->valid ? s->valid / cell->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1916 case CTSF_LAYERROWPCT_COUNT:
1917 return cell->domains[CTDT_LAYERROW]->valid ? s->valid / cell->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1919 case CTSF_LAYERCOLPCT_COUNT:
1920 return cell->domains[CTDT_LAYERCOL]->valid ? s->valid / cell->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1922 case CTSF_ROWPCT_VALIDN:
1923 case CTSF_COLPCT_VALIDN:
1924 case CTSF_TABLEPCT_VALIDN:
1925 case CTSF_SUBTABLEPCT_VALIDN:
1926 case CTSF_LAYERPCT_VALIDN:
1927 case CTSF_LAYERROWPCT_VALIDN:
1928 case CTSF_LAYERCOLPCT_VALIDN:
1929 case CTSF_ROWPCT_TOTALN:
1930 case CTSF_COLPCT_TOTALN:
1931 case CTSF_TABLEPCT_TOTALN:
1932 case CTSF_SUBTABLEPCT_TOTALN:
1933 case CTSF_LAYERPCT_TOTALN:
1934 case CTSF_LAYERROWPCT_TOTALN:
1935 case CTSF_LAYERCOLPCT_TOTALN:
1940 return s->valid + s->missing;
1953 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1958 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1964 double weight, variance;
1965 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1966 return calc_semean (variance, weight);
1972 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1973 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1978 double weight, mean;
1979 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1980 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1986 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1990 case CTSF_ROWPCT_SUM:
1991 case CTSF_COLPCT_SUM:
1992 case CTSF_TABLEPCT_SUM:
1993 case CTSF_SUBTABLEPCT_SUM:
1994 case CTSF_LAYERPCT_SUM:
1995 case CTSF_LAYERROWPCT_SUM:
1996 case CTSF_LAYERCOLPCT_SUM:
2005 case CTSF_RESPONSES:
2006 case CTSF_ROWPCT_RESPONSES:
2007 case CTSF_COLPCT_RESPONSES:
2008 case CTSF_TABLEPCT_RESPONSES:
2009 case CTSF_SUBTABLEPCT_RESPONSES:
2010 case CTSF_LAYERPCT_RESPONSES:
2011 case CTSF_LAYERROWPCT_RESPONSES:
2012 case CTSF_LAYERCOLPCT_RESPONSES:
2013 case CTSF_ROWPCT_RESPONSES_COUNT:
2014 case CTSF_COLPCT_RESPONSES_COUNT:
2015 case CTSF_TABLEPCT_RESPONSES_COUNT:
2016 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2017 case CTSF_LAYERPCT_RESPONSES_COUNT:
2018 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2019 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2020 case CTSF_ROWPCT_COUNT_RESPONSES:
2021 case CTSF_COLPCT_COUNT_RESPONSES:
2022 case CTSF_TABLEPCT_COUNT_RESPONSES:
2023 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2024 case CTSF_LAYERPCT_COUNT_RESPONSES:
2025 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2026 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2033 struct ctables_cell_sort_aux
2035 const struct ctables_table *t;
2036 enum pivot_axis_type a;
2040 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2042 const struct ctables_cell_sort_aux *aux = aux_;
2043 struct ctables_cell *const *ap = a_;
2044 struct ctables_cell *const *bp = b_;
2045 const struct ctables_cell *a = *ap;
2046 const struct ctables_cell *b = *bp;
2048 size_t a_idx = a->axes[aux->a].stack_idx;
2049 size_t b_idx = b->axes[aux->a].stack_idx;
2051 return a_idx < b_idx ? -1 : 1;
2053 const struct ctables_nest *nest = &aux->t->stacks[aux->a].nests[a_idx];
2054 for (size_t i = 0; i < nest->n; i++)
2055 if (i != nest->scale_idx)
2057 const struct variable *var = nest->vars[i];
2058 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2059 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2060 if (a_cv->category != b_cv->category)
2061 return a_cv->category > b_cv->category ? 1 : -1;
2063 const union value *a_val = &a_cv->value;
2064 const union value *b_val = &b_cv->value;
2065 switch (a_cv->category->type)
2072 /* Must be equal. */
2079 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2087 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2089 return a_cv->category->sort_ascending ? cmp : -cmp;
2095 const char *a_label = var_lookup_value_label (var, a_val);
2096 const char *b_label = var_lookup_value_label (var, b_val);
2098 ? (b_label ? strcmp (a_label, b_label) : 1)
2099 : (b_label ? -1 : value_compare_3way (
2100 a_val, b_val, var_get_width (var))));
2102 return a_cv->category->sort_ascending ? cmp : -cmp;
2116 For each ctables_table:
2117 For each combination of row vars:
2118 For each combination of column vars:
2119 For each combination of layer vars:
2121 Make a table of row values:
2122 Sort entries by row values
2123 Assign a 0-based index to each actual value
2124 Construct a dimension
2125 Make a table of column values
2126 Make a table of layer values
2128 Fill the table entry using the indexes from before.
2131 static struct ctables_domain *
2132 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *cell,
2133 enum ctables_domain_type domain)
2136 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2138 size_t idx = cell->axes[a].stack_idx;
2139 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2140 hash = hash_int (idx, hash);
2141 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2143 size_t v_idx = nest->domains[domain][i];
2144 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2145 var_get_width (nest->vars[v_idx]), hash);
2149 struct ctables_domain *d;
2150 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2152 const struct ctables_cell *df = d->example;
2153 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2155 size_t idx = cell->axes[a].stack_idx;
2156 if (idx != df->axes[a].stack_idx)
2159 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2160 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2162 size_t v_idx = nest->domains[domain][i];
2163 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2164 &cell->axes[a].cvs[v_idx].value,
2165 var_get_width (nest->vars[v_idx])))
2174 d = xmalloc (sizeof *d);
2175 *d = (struct ctables_domain) { .example = cell };
2176 hmap_insert (&t->domains[domain], &d->node, hash);
2180 static const struct ctables_category *
2181 ctables_categories_match (const struct ctables_categories *c,
2182 const union value *v, const struct variable *var)
2184 const struct ctables_category *othernm = NULL;
2185 for (size_t i = c->n_cats; i-- > 0; )
2187 const struct ctables_category *cat = &c->cats[i];
2191 if (cat->number == v->f)
2199 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2200 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2205 if (var_is_value_missing (var, v))
2222 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2227 return var_is_value_missing (var, v) ? NULL : othernm;
2230 static const struct ctables_category *
2231 ctables_categories_total (const struct ctables_categories *c)
2233 const struct ctables_category *first = &c->cats[0];
2234 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2235 return (first->type == CCT_TOTAL ? first
2236 : last->type == CCT_TOTAL ? last
2240 static struct ctables_cell *
2241 ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c,
2242 size_t ix[PIVOT_N_AXES],
2243 const struct ctables_category *cats[PIVOT_N_AXES][10])
2245 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2248 enum ctables_summary_variant sv = CSV_CELL;
2249 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2251 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2252 hash = hash_int (ix[a], hash);
2253 for (size_t i = 0; i < nest->n; i++)
2254 if (i != nest->scale_idx)
2256 hash = hash_pointer (cats[a][i], hash);
2257 if (cats[a][i]->type != CCT_TOTAL
2258 && cats[a][i]->type != CCT_SUBTOTAL
2259 && cats[a][i]->type != CCT_HSUBTOTAL)
2260 hash = value_hash (case_data (c, nest->vars[i]),
2261 var_get_width (nest->vars[i]), hash);
2267 struct ctables_cell *cell;
2268 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &t->cells)
2270 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2272 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2273 if (cell->axes[a].stack_idx != ix[a])
2275 for (size_t i = 0; i < nest->n; i++)
2276 if (i != nest->scale_idx
2277 && (cats[a][i] != cell->axes[a].cvs[i].category
2278 || (cats[a][i]->type != CCT_TOTAL
2279 && cats[a][i]->type != CCT_SUBTOTAL
2280 && cats[a][i]->type != CCT_HSUBTOTAL
2281 && !value_equal (case_data (c, nest->vars[i]),
2282 &cell->axes[a].cvs[i].value,
2283 var_get_width (nest->vars[i])))))
2292 cell = xmalloc (sizeof *cell);
2295 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2297 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2298 cell->axes[a].stack_idx = ix[a];
2299 cell->axes[a].cvs = (nest->n
2300 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2302 for (size_t i = 0; i < nest->n; i++)
2304 if (i != nest->scale_idx)
2306 const struct ctables_category *subtotal = cats[a][i]->subtotal;
2307 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2311 cell->axes[a].cvs[i].category = cats[a][i];
2312 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2313 var_get_width (nest->vars[i]));
2317 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2318 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2319 for (size_t i = 0; i < specs->n; i++)
2320 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2321 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2322 cell->domains[dt] = ctables_domain_insert (t, cell, dt);
2323 hmap_insert (&t->cells, &cell->node, hash);
2328 ctables_cell_add__ (struct ctables_table *t, const struct ccase *c,
2329 size_t ix[PIVOT_N_AXES],
2330 const struct ctables_category *cats[PIVOT_N_AXES][10],
2333 struct ctables_cell *cell = ctables_cell_insert__ (t, c, ix, cats);
2334 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2336 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2337 for (size_t i = 0; i < specs->n; i++)
2338 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2339 case_data (c, specs->var), weight);
2340 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2341 cell->domains[dt]->valid += weight;
2345 recurse_totals (struct ctables_table *t, const struct ccase *c,
2346 size_t ix[PIVOT_N_AXES],
2347 const struct ctables_category *cats[PIVOT_N_AXES][10],
2349 enum pivot_axis_type start_axis, size_t start_nest)
2351 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2353 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2354 for (size_t i = start_nest; i < nest->n; i++)
2356 if (i == nest->scale_idx)
2359 const struct variable *var = nest->vars[i];
2361 const struct ctables_category *total = ctables_categories_total (
2362 t->categories[var_get_dict_index (var)]);
2365 const struct ctables_category *save = cats[a][i];
2367 ctables_cell_add__ (t, c, ix, cats, weight);
2368 recurse_totals (t, c, ix, cats, weight, a, i + 1);
2377 ctables_cell_insert (struct ctables_table *t,
2378 const struct ccase *c,
2379 size_t ir, size_t ic, size_t il,
2382 size_t ix[PIVOT_N_AXES] = {
2383 [PIVOT_AXIS_ROW] = ir,
2384 [PIVOT_AXIS_COLUMN] = ic,
2385 [PIVOT_AXIS_LAYER] = il,
2388 const struct ctables_category *cats[PIVOT_N_AXES][10];
2389 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2391 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2392 for (size_t i = 0; i < nest->n; i++)
2394 if (i == nest->scale_idx)
2397 const struct variable *var = nest->vars[i];
2398 const union value *value = case_data (c, var);
2400 if (var_is_numeric (var) && value->f == SYSMIS)
2403 cats[a][i] = ctables_categories_match (
2404 t->categories[var_get_dict_index (var)], value, var);
2410 ctables_cell_add__ (t, c, ix, cats, weight);
2412 recurse_totals (t, c, ix, cats, weight, 0, 0);
2414 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2416 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2417 for (size_t i = 0; i < nest->n; i++)
2419 if (i == nest->scale_idx)
2422 const struct ctables_category *save = cats[a][i];
2425 cats[a][i] = save->subtotal;
2426 ctables_cell_add__ (t, c, ix, cats, weight);
2434 ctables_execute (struct dataset *ds, struct ctables *ct)
2436 for (size_t i = 0; i < ct->n_tables; i++)
2438 struct ctables_table *t = ct->tables[i];
2439 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2442 t->stacks[a] = enumerate_fts (a, t->axes[a]);
2444 for (size_t j = 0; j < t->stacks[a].n; j++)
2446 struct ctables_nest *nest = &t->stacks[a].nests[j];
2447 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2449 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
2450 nest->n_domains[dt] = 0;
2452 for (size_t k = 0; k < nest->n; k++)
2454 if (k == nest->scale_idx)
2463 if (a != PIVOT_AXIS_LAYER)
2470 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2471 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2472 : a == PIVOT_AXIS_ROW)
2474 if (k == nest->n - 1
2475 || (nest->scale_idx == nest->n - 1
2476 && k == nest->n - 2))
2482 if (a == PIVOT_AXIS_COLUMN)
2487 if (a == PIVOT_AXIS_ROW)
2492 nest->domains[dt][nest->n_domains[dt]++] = k;
2499 struct ctables_nest *nest = xmalloc (sizeof *nest);
2500 *nest = (struct ctables_nest) { .n = 0 };
2501 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
2504 for (size_t i = 0; i < t->stacks[t->summary_axis].n; i++)
2506 struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[i];
2507 if (!nest->specs[CSV_CELL].n)
2509 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
2510 specs->specs = xmalloc (sizeof *specs->specs);
2513 enum ctables_summary_function function
2514 = specs->var ? CTSF_MEAN : CTSF_COUNT;
2515 struct ctables_var var = { .is_mrset = false, .var = specs->var };
2517 *specs->specs = (struct ctables_summary_spec) {
2518 .function = function,
2519 .format = ctables_summary_default_format (function, &var),
2520 .label = ctables_summary_default_label (function, 0),
2523 specs->var = nest->vars[0];
2525 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
2526 &nest->specs[CSV_CELL]);
2528 else if (!nest->specs[CSV_TOTAL].n)
2529 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
2530 &nest->specs[CSV_CELL]);
2534 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2537 bool warn_on_invalid = true;
2538 double total_weight = 0;
2539 for (struct ccase *c = casereader_read (input); c;
2540 case_unref (c), c = casereader_read (input))
2542 double weight = dict_get_case_weight (dataset_dict (ds), c,
2544 total_weight += weight;
2546 for (size_t i = 0; i < ct->n_tables; i++)
2548 struct ctables_table *t = ct->tables[i];
2550 for (size_t ir = 0; ir < t->stacks[PIVOT_AXIS_ROW].n; ir++)
2551 for (size_t ic = 0; ic < t->stacks[PIVOT_AXIS_COLUMN].n; ic++)
2552 for (size_t il = 0; il < t->stacks[PIVOT_AXIS_LAYER].n; il++)
2553 ctables_cell_insert (t, c, ir, ic, il, weight);
2556 casereader_destroy (input);
2558 for (size_t i = 0; i < ct->n_tables; i++)
2560 struct ctables_table *t = ct->tables[i];
2562 struct pivot_table *pt = pivot_table_create__ (
2564 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2565 : pivot_value_new_text (N_("Custom Tables"))),
2568 pivot_table_set_caption (
2569 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2571 pivot_table_set_caption (
2572 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2574 pivot_table_set_look (pt, ct->look);
2575 struct pivot_dimension *d[PIVOT_N_AXES];
2576 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2578 static const char *names[] = {
2579 [PIVOT_AXIS_ROW] = N_("Rows"),
2580 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2581 [PIVOT_AXIS_LAYER] = N_("Layers"),
2583 d[a] = (t->axes[a] || a == t->summary_axis
2584 ? pivot_dimension_create (pt, a, names[a])
2589 assert (t->axes[a]);
2591 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2593 struct ctables_cell *cell;
2595 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2598 assert (n <= t->cells.count);
2600 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2601 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2603 size_t max_depth = 0;
2604 for (size_t j = 0; j < t->stacks[a].n; j++)
2605 if (t->stacks[a].nests[j].n > max_depth)
2606 max_depth = t->stacks[a].nests[j].n;
2608 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2609 struct pivot_category *top = NULL;
2611 for (size_t j = 0; j < n; j++)
2613 struct ctables_cell *cell = sorted[j];
2614 const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
2616 size_t n_common = 0;
2617 bool new_subtable = false;
2620 struct ctables_cell *prev = sorted[j - 1];
2621 if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
2623 for (; n_common < nest->n; n_common++)
2624 if (n_common != nest->scale_idx
2625 && (prev->axes[a].cvs[n_common].category
2626 != cell->axes[a].cvs[n_common].category
2627 || !value_equal (&prev->axes[a].cvs[n_common].value,
2628 &cell->axes[a].cvs[n_common].value,
2629 var_get_type (nest->vars[n_common]))))
2633 new_subtable = true;
2636 new_subtable = true;
2640 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
2642 if (vlabel != CTVL_NONE)
2643 top = pivot_category_create_group__ (
2644 top, pivot_value_new_variable (nest->vars[0]));
2646 if (n_common == nest->n)
2648 cell->axes[a].leaf = prev_leaf;
2652 for (size_t k = n_common; k < nest->n; k++)
2654 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2656 struct pivot_value *label
2657 = (k == nest->scale_idx ? NULL
2658 : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
2659 || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
2660 || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
2661 ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
2663 : pivot_value_new_var_value (nest->vars[k],
2664 &cell->axes[a].cvs[k].value));
2665 if (k == nest->n - 1)
2667 if (a == t->summary_axis)
2670 parent = pivot_category_create_group__ (parent, label);
2671 const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
2672 for (size_t m = 0; m < specs->n; m++)
2674 int leaf = pivot_category_create_leaf (
2675 parent, pivot_value_new_text (specs->specs[m].label));
2682 /* This assertion is true as long as the summary axis
2683 is the axis where the summaries are displayed. */
2686 prev_leaf = pivot_category_create_leaf (parent, label);
2692 parent = pivot_category_create_group__ (parent, label);
2694 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
2695 if (vlabel != CTVL_NONE)
2696 parent = pivot_category_create_group__ (
2697 parent, pivot_value_new_variable (nest->vars[k + 1]));
2701 cell->axes[a].leaf = prev_leaf;
2706 struct ctables_cell *cell;
2707 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2712 const struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].stack_idx];
2713 const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
2714 for (size_t j = 0; j < specs->n; j++)
2717 size_t n_dindexes = 0;
2719 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2722 int leaf = cell->axes[a].leaf;
2723 if (a == t->summary_axis)
2725 dindexes[n_dindexes++] = leaf;
2728 double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
2729 struct pivot_value *value = pivot_value_new_number (d);
2730 value->numeric.format = specs->specs[j].format;
2731 pivot_table_put (pt, dindexes, n_dindexes, value);
2735 pivot_table_submit (pt);
2738 return proc_commit (ds);
2742 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2744 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2745 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2746 enum settings_value_show tvars = settings_get_show_variables ();
2747 for (size_t i = 0; i < n_vars; i++)
2748 vlabels[i] = (enum ctables_vlabel) tvars;
2750 struct ctables *ct = xmalloc (sizeof *ct);
2751 *ct = (struct ctables) {
2752 .look = pivot_table_look_unshare (pivot_table_look_ref (
2753 pivot_table_look_get_default ())),
2755 .hide_threshold = 5,
2757 ct->look->omit_empty = false;
2759 if (!lex_force_match (lexer, T_SLASH))
2762 while (!lex_match_id (lexer, "TABLE"))
2764 if (lex_match_id (lexer, "FORMAT"))
2766 double widths[2] = { SYSMIS, SYSMIS };
2767 double units_per_inch = 72.0;
2769 while (lex_token (lexer) != T_SLASH)
2771 if (lex_match_id (lexer, "MINCOLWIDTH"))
2773 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2776 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2778 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2781 else if (lex_match_id (lexer, "UNITS"))
2783 lex_match (lexer, T_EQUALS);
2784 if (lex_match_id (lexer, "POINTS"))
2785 units_per_inch = 72.0;
2786 else if (lex_match_id (lexer, "INCHES"))
2787 units_per_inch = 1.0;
2788 else if (lex_match_id (lexer, "CM"))
2789 units_per_inch = 2.54;
2792 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2796 else if (lex_match_id (lexer, "EMPTY"))
2801 lex_match (lexer, T_EQUALS);
2802 if (lex_match_id (lexer, "ZERO"))
2804 /* Nothing to do. */
2806 else if (lex_match_id (lexer, "BLANK"))
2807 ct->zero = xstrdup ("");
2808 else if (lex_force_string (lexer))
2810 ct->zero = ss_xstrdup (lex_tokss (lexer));
2816 else if (lex_match_id (lexer, "MISSING"))
2818 lex_match (lexer, T_EQUALS);
2819 if (!lex_force_string (lexer))
2823 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2824 ? ss_xstrdup (lex_tokss (lexer))
2830 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2831 "UNITS", "EMPTY", "MISSING");
2836 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2837 && widths[0] > widths[1])
2839 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2843 for (size_t i = 0; i < 2; i++)
2844 if (widths[i] != SYSMIS)
2846 int *wr = ct->look->width_ranges[TABLE_HORZ];
2847 wr[i] = widths[i] / units_per_inch * 96.0;
2852 else if (lex_match_id (lexer, "VLABELS"))
2854 if (!lex_force_match_id (lexer, "VARIABLES"))
2856 lex_match (lexer, T_EQUALS);
2858 struct variable **vars;
2860 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2864 if (!lex_force_match_id (lexer, "DISPLAY"))
2869 lex_match (lexer, T_EQUALS);
2871 enum ctables_vlabel vlabel;
2872 if (lex_match_id (lexer, "DEFAULT"))
2873 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2874 else if (lex_match_id (lexer, "NAME"))
2876 else if (lex_match_id (lexer, "LABEL"))
2877 vlabel = CTVL_LABEL;
2878 else if (lex_match_id (lexer, "BOTH"))
2880 else if (lex_match_id (lexer, "NONE"))
2884 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2890 for (size_t i = 0; i < n_vars; i++)
2891 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2894 else if (lex_match_id (lexer, "MRSETS"))
2896 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2898 lex_match (lexer, T_EQUALS);
2899 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2902 else if (lex_match_id (lexer, "SMISSING"))
2904 if (lex_match_id (lexer, "VARIABLE"))
2905 ct->smissing_listwise = false;
2906 else if (lex_match_id (lexer, "LISTWISE"))
2907 ct->smissing_listwise = true;
2910 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2915 else if (lex_match_id (lexer, "WEIGHT"))
2917 if (!lex_force_match_id (lexer, "VARIABLE"))
2919 lex_match (lexer, T_EQUALS);
2920 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2921 if (!ct->base_weight)
2924 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2926 if (!lex_force_match_id (lexer, "COUNT"))
2928 lex_match (lexer, T_EQUALS);
2929 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2931 ct->hide_threshold = lex_integer (lexer);
2936 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2937 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2938 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2942 if (!lex_force_match (lexer, T_SLASH))
2946 size_t allocated_tables = 0;
2949 if (ct->n_tables >= allocated_tables)
2950 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2951 sizeof *ct->tables);
2953 struct ctables_category *cat = xmalloc (sizeof *cat);
2954 *cat = (struct ctables_category) {
2956 .include_missing = false,
2957 .sort_ascending = true,
2960 struct ctables_categories *c = xmalloc (sizeof *c);
2961 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2962 *c = (struct ctables_categories) {
2968 struct ctables_categories **categories = xnmalloc (n_vars,
2969 sizeof *categories);
2970 for (size_t i = 0; i < n_vars; i++)
2973 struct ctables_table *t = xmalloc (sizeof *t);
2974 *t = (struct ctables_table) {
2975 .cells = HMAP_INITIALIZER (t->cells),
2976 .slabels_position = PIVOT_AXIS_COLUMN,
2977 .slabels_visible = true,
2978 .row_labels = CTLP_NORMAL,
2979 .col_labels = CTLP_NORMAL,
2980 .categories = categories,
2981 .n_categories = n_vars,
2984 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2985 hmap_init (&t->domains[dt]);
2986 ct->tables[ct->n_tables++] = t;
2988 lex_match (lexer, T_EQUALS);
2989 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2991 if (lex_match (lexer, T_BY))
2993 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2994 ct, t, PIVOT_AXIS_COLUMN))
2997 if (lex_match (lexer, T_BY))
2999 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3000 ct, t, PIVOT_AXIS_LAYER))
3005 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
3006 && !t->axes[PIVOT_AXIS_LAYER])
3008 lex_error (lexer, _("At least one variable must be specified."));
3012 const struct ctables_axis *scales[PIVOT_N_AXES];
3013 size_t n_scales = 0;
3014 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3016 scales[a] = find_scale (t->axes[a]);
3022 msg (SE, _("Scale variables may appear only on one axis."));
3023 if (scales[PIVOT_AXIS_ROW])
3024 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
3025 _("This scale variable appears on the rows axis."));
3026 if (scales[PIVOT_AXIS_COLUMN])
3027 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3028 _("This scale variable appears on the columns axis."));
3029 if (scales[PIVOT_AXIS_LAYER])
3030 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3031 _("This scale variable appears on the layer axis."));
3035 const struct ctables_axis *summaries[PIVOT_N_AXES];
3036 size_t n_summaries = 0;
3037 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3039 summaries[a] = (scales[a]
3041 : find_categorical_summary_spec (t->axes[a]));
3045 if (n_summaries > 1)
3047 msg (SE, _("Summaries may appear only on one axis."));
3048 if (summaries[PIVOT_AXIS_ROW])
3049 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3050 _("This variable on the rows axis has a summary."));
3051 if (summaries[PIVOT_AXIS_COLUMN])
3052 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3053 _("This variable on the columns axis has a summary."));
3054 if (summaries[PIVOT_AXIS_LAYER])
3055 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3056 _("This variable on the layers axis has a summary."));
3059 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3060 if (n_summaries ? summaries[a] : t->axes[a])
3062 t->summary_axis = a;
3066 if (lex_token (lexer) == T_ENDCMD)
3068 if (!lex_force_match (lexer, T_SLASH))
3071 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3073 if (lex_match_id (lexer, "SLABELS"))
3075 while (lex_token (lexer) != T_SLASH)
3077 if (lex_match_id (lexer, "POSITION"))
3079 lex_match (lexer, T_EQUALS);
3080 if (lex_match_id (lexer, "COLUMN"))
3081 t->slabels_position = PIVOT_AXIS_COLUMN;
3082 else if (lex_match_id (lexer, "ROW"))
3083 t->slabels_position = PIVOT_AXIS_ROW;
3084 else if (lex_match_id (lexer, "LAYER"))
3085 t->slabels_position = PIVOT_AXIS_LAYER;
3088 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3092 else if (lex_match_id (lexer, "VISIBLE"))
3094 lex_match (lexer, T_EQUALS);
3095 if (!parse_bool (lexer, &t->slabels_visible))
3100 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3105 else if (lex_match_id (lexer, "CLABELS"))
3107 while (lex_token (lexer) != T_SLASH)
3109 if (lex_match_id (lexer, "AUTO"))
3110 t->row_labels = t->col_labels = CTLP_NORMAL;
3111 else if (lex_match_id (lexer, "ROWLABELS"))
3113 lex_match (lexer, T_EQUALS);
3114 if (lex_match_id (lexer, "OPPOSITE"))
3115 t->row_labels = CTLP_OPPOSITE;
3116 else if (lex_match_id (lexer, "LAYER"))
3117 t->row_labels = CTLP_LAYER;
3120 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3124 else if (lex_match_id (lexer, "COLLABELS"))
3126 lex_match (lexer, T_EQUALS);
3127 if (lex_match_id (lexer, "OPPOSITE"))
3128 t->col_labels = CTLP_OPPOSITE;
3129 else if (lex_match_id (lexer, "LAYER"))
3130 t->col_labels = CTLP_LAYER;
3133 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3139 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3145 else if (lex_match_id (lexer, "CRITERIA"))
3147 if (!lex_force_match_id (lexer, "CILEVEL"))
3149 lex_match (lexer, T_EQUALS);
3151 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3153 t->cilevel = lex_number (lexer);
3156 else if (lex_match_id (lexer, "CATEGORIES"))
3158 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3161 else if (lex_match_id (lexer, "TITLES"))
3166 if (lex_match_id (lexer, "CAPTION"))
3167 textp = &t->caption;
3168 else if (lex_match_id (lexer, "CORNER"))
3170 else if (lex_match_id (lexer, "TITLE"))
3174 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3177 lex_match (lexer, T_EQUALS);
3179 struct string s = DS_EMPTY_INITIALIZER;
3180 while (lex_is_string (lexer))
3182 if (!ds_is_empty (&s))
3183 ds_put_byte (&s, ' ');
3184 ds_put_substring (&s, lex_tokss (lexer));
3188 *textp = ds_steal_cstr (&s);
3190 while (lex_token (lexer) != T_SLASH
3191 && lex_token (lexer) != T_ENDCMD);
3193 else if (lex_match_id (lexer, "SIGTEST"))
3197 t->chisq = xmalloc (sizeof *t->chisq);
3198 *t->chisq = (struct ctables_chisq) {
3200 .include_mrsets = true,
3201 .all_visible = true,
3207 if (lex_match_id (lexer, "TYPE"))
3209 lex_match (lexer, T_EQUALS);
3210 if (!lex_force_match_id (lexer, "CHISQUARE"))
3213 else if (lex_match_id (lexer, "ALPHA"))
3215 lex_match (lexer, T_EQUALS);
3216 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3218 t->chisq->alpha = lex_number (lexer);
3221 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3223 lex_match (lexer, T_EQUALS);
3224 if (parse_bool (lexer, &t->chisq->include_mrsets))
3227 else if (lex_match_id (lexer, "CATEGORIES"))
3229 lex_match (lexer, T_EQUALS);
3230 if (lex_match_id (lexer, "ALLVISIBLE"))
3231 t->chisq->all_visible = true;
3232 else if (lex_match_id (lexer, "SUBTOTALS"))
3233 t->chisq->all_visible = false;
3236 lex_error_expecting (lexer,
3237 "ALLVISIBLE", "SUBTOTALS");
3243 lex_error_expecting (lexer, "TYPE", "ALPHA",
3244 "INCLUDEMRSETS", "CATEGORIES");
3248 while (lex_token (lexer) != T_SLASH
3249 && lex_token (lexer) != T_ENDCMD);
3251 else if (lex_match_id (lexer, "COMPARETEST"))
3255 t->pairwise = xmalloc (sizeof *t->pairwise);
3256 *t->pairwise = (struct ctables_pairwise) {
3258 .alpha = { .05, .05 },
3259 .adjust = BONFERRONI,
3260 .include_mrsets = true,
3261 .meansvariance_allcats = true,
3262 .all_visible = true,
3271 if (lex_match_id (lexer, "TYPE"))
3273 lex_match (lexer, T_EQUALS);
3274 if (lex_match_id (lexer, "PROP"))
3275 t->pairwise->type = PROP;
3276 else if (lex_match_id (lexer, "MEAN"))
3277 t->pairwise->type = MEAN;
3280 lex_error_expecting (lexer, "PROP", "MEAN");
3284 else if (lex_match_id (lexer, "ALPHA"))
3286 lex_match (lexer, T_EQUALS);
3288 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3290 double a0 = lex_number (lexer);
3293 lex_match (lexer, T_COMMA);
3294 if (lex_is_number (lexer))
3296 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3298 double a1 = lex_number (lexer);
3301 t->pairwise->alpha[0] = MIN (a0, a1);
3302 t->pairwise->alpha[1] = MAX (a0, a1);
3305 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3307 else if (lex_match_id (lexer, "ADJUST"))
3309 lex_match (lexer, T_EQUALS);
3310 if (lex_match_id (lexer, "BONFERRONI"))
3311 t->pairwise->adjust = BONFERRONI;
3312 else if (lex_match_id (lexer, "BH"))
3313 t->pairwise->adjust = BH;
3314 else if (lex_match_id (lexer, "NONE"))
3315 t->pairwise->adjust = 0;
3318 lex_error_expecting (lexer, "BONFERRONI", "BH",
3323 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3325 lex_match (lexer, T_EQUALS);
3326 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3329 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3331 lex_match (lexer, T_EQUALS);
3332 if (lex_match_id (lexer, "ALLCATS"))
3333 t->pairwise->meansvariance_allcats = true;
3334 else if (lex_match_id (lexer, "TESTEDCATS"))
3335 t->pairwise->meansvariance_allcats = false;
3338 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3342 else if (lex_match_id (lexer, "CATEGORIES"))
3344 lex_match (lexer, T_EQUALS);
3345 if (lex_match_id (lexer, "ALLVISIBLE"))
3346 t->pairwise->all_visible = true;
3347 else if (lex_match_id (lexer, "SUBTOTALS"))
3348 t->pairwise->all_visible = false;
3351 lex_error_expecting (lexer, "ALLVISIBLE",
3356 else if (lex_match_id (lexer, "MERGE"))
3358 lex_match (lexer, T_EQUALS);
3359 if (!parse_bool (lexer, &t->pairwise->merge))
3362 else if (lex_match_id (lexer, "STYLE"))
3364 lex_match (lexer, T_EQUALS);
3365 if (lex_match_id (lexer, "APA"))
3366 t->pairwise->apa_style = true;
3367 else if (lex_match_id (lexer, "SIMPLE"))
3368 t->pairwise->apa_style = false;
3371 lex_error_expecting (lexer, "APA", "SIMPLE");
3375 else if (lex_match_id (lexer, "SHOWSIG"))
3377 lex_match (lexer, T_EQUALS);
3378 if (!parse_bool (lexer, &t->pairwise->show_sig))
3383 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3384 "INCLUDEMRSETS", "MEANSVARIANCE",
3385 "CATEGORIES", "MERGE", "STYLE",
3390 while (lex_token (lexer) != T_SLASH
3391 && lex_token (lexer) != T_ENDCMD);
3395 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3396 "CRITERIA", "CATEGORIES", "TITLES",
3397 "SIGTEST", "COMPARETEST");
3401 if (!lex_match (lexer, T_SLASH))
3405 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3407 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3412 while (lex_token (lexer) != T_ENDCMD);
3414 bool ok = ctables_execute (ds, ct);
3415 ctables_destroy (ct);
3416 return ok ? CMD_SUCCESS : CMD_FAILURE;
3419 ctables_destroy (ct);