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 CTDT_TABLE, /* Entire table. */
150 CTDT_SUBTABLE, /* Innermost variable in a single layer. */
151 CTDT_ROW, /* Row within a subtable. */
152 CTDT_COL, /* Column within a subtable. */
153 CTDT_LAYER, /* Entire layer. */
154 CTDT_LAYERROW, /* Row within a layer. */
155 CTDT_LAYERCOL, /* Column within a layer. */
159 struct ctables_domain
161 struct hmap_node node;
163 const struct ctables_freq *example;
171 /* In struct ctables's 'ft' hmap. Indexed by all the values in all the
172 axes (except the scalar variable, if any). */
173 struct hmap_node node;
175 /* The domains that contains this cell. */
176 struct ctables_domain *domains[N_CTDTS];
186 union ctables_summary *summaries;
191 struct pivot_table_look *look;
193 /* If this is NULL, zeros are displayed using the normal print format.
194 Otherwise, this string is displayed. */
197 /* If this is NULL, missing values are displayed using the normal print
198 format. Otherwise, this string is displayed. */
201 /* Indexed by variable dictionary index. */
202 enum ctables_vlabel *vlabels;
204 bool mrsets_count_duplicates; /* MRSETS. */
205 bool smissing_listwise; /* SMISSING. */
206 struct variable *base_weight; /* WEIGHT. */
207 int hide_threshold; /* HIDESMALLCOUNTS. */
209 struct ctables_table **tables;
213 struct ctables_postcompute
215 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
216 const char *name; /* Name, without leading &. */
218 struct ctables_postcompute_expr *expr;
221 bool hide_source_cats;
224 struct ctables_postcompute_expr
226 enum ctables_postcompute_op
234 /* XXX SUBTOTAL and HSUBTOTAL */
247 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
252 XXX what about string ranges? */
255 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
256 struct ctables_postcompute_expr *subs[2];
260 enum ctables_label_position
269 struct variable **vars;
272 size_t *domains[N_CTDTS];
273 size_t n_domains[N_CTDTS];
275 struct ctables_summary_spec *summaries;
277 struct variable *summary_var;
282 struct var_array *vas;
288 struct ctables_axis *axes[PIVOT_N_AXES];
289 struct var_array2 vaas[PIVOT_N_AXES];
290 enum pivot_axis_type summary_axis;
292 struct hmap domains[N_CTDTS];
294 enum pivot_axis_type slabels_position;
295 bool slabels_visible;
297 enum ctables_label_position row_labels;
298 enum ctables_label_position col_labels;
300 /* Indexed by variable dictionary index. */
301 struct ctables_categories **categories;
310 struct ctables_chisq *chisq;
311 struct ctables_pairwise *pairwise;
320 struct variable *var;
321 const struct mrset *mrset;
325 static const struct fmt_spec *
326 ctables_var_get_print_format (const struct ctables_var *var)
328 return (var->is_mrset
329 ? var_get_print_format (var->mrset->vars[0])
330 : var_get_print_format (var->var));
334 ctables_var_name (const struct ctables_var *var)
336 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
339 struct ctables_categories
343 /* Explicit categories. */
344 struct ctables_cat_value *values;
347 /* Implicit categories. */
349 bool include_missing;
350 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
351 enum ctables_summary_function sort_func;
352 struct variable *sort_func_var;
360 /* Empty categories. */
364 struct ctables_cat_value
366 enum ctables_cat_value_type
380 double number; /* CCVT_NUMBER. */
381 char *string; /* CCVT_STRING. */
382 double range[2]; /* CCVT_RANGE. */
383 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
388 ctables_cat_value_uninit (struct ctables_cat_value *cv)
407 free (cv->subtotal_label);
412 ctables_categories_unref (struct ctables_categories *c)
417 assert (c->n_refs > 0);
421 for (size_t i = 0; i < c->n_values; i++)
422 ctables_cat_value_uninit (&c->values[i]);
424 free (c->total_label);
428 /* Chi-square test (SIGTEST). */
436 /* Pairwise comparison test (COMPARETEST). */
437 struct ctables_pairwise
439 enum { PROP, MEAN } type;
442 bool meansvariance_allcats;
444 enum { BONFERRONI = 1, BH } adjust;
468 struct ctables_var var;
470 struct ctables_summary_spec *summaries;
472 size_t allocated_summaries;
476 struct ctables_axis *subs[2];
479 struct msg_location *loc;
482 static void ctables_axis_destroy (struct ctables_axis *);
491 enum ctables_function_availability
493 CTFA_ALL, /* Any variables. */
494 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
495 CTFA_MRSETS, /* Only multiple-response sets */
498 struct ctables_summary_spec
500 enum ctables_summary_function function;
501 double percentile; /* CTSF_PTILE only. */
503 struct fmt_spec format; /* XXX extra CTABLES formats */
507 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
514 parse_col_width (struct lexer *lexer, const char *name, double *width)
516 lex_match (lexer, T_EQUALS);
517 if (lex_match_id (lexer, "DEFAULT"))
519 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
521 *width = lex_number (lexer);
531 parse_bool (struct lexer *lexer, bool *b)
533 if (lex_match_id (lexer, "NO"))
535 else if (lex_match_id (lexer, "YES"))
539 lex_error_expecting (lexer, "YES", "NO");
545 static enum ctables_function_availability
546 ctables_function_availability (enum ctables_summary_function f)
548 static enum ctables_function_availability availability[] = {
549 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
554 return availability[f];
558 parse_ctables_summary_function (struct lexer *lexer,
559 enum ctables_summary_function *f)
563 enum ctables_summary_function function;
564 struct substring name;
566 static struct pair names[] = {
567 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
568 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
571 /* The .COUNT suffix may be omitted. */
572 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
573 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
574 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
575 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
576 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
577 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
578 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
582 if (!lex_force_id (lexer))
585 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
586 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
588 *f = names[i].function;
593 lex_error (lexer, _("Expecting summary function name."));
598 ctables_axis_destroy (struct ctables_axis *axis)
606 for (size_t i = 0; i < axis->n_summaries; i++)
607 ctables_summary_spec_uninit (&axis->summaries[i]);
608 free (axis->summaries);
613 ctables_axis_destroy (axis->subs[0]);
614 ctables_axis_destroy (axis->subs[1]);
617 msg_location_destroy (axis->loc);
621 static struct ctables_axis *
622 ctables_axis_new_nonterminal (enum ctables_axis_op op,
623 struct ctables_axis *sub0,
624 struct ctables_axis *sub1,
625 struct lexer *lexer, int start_ofs)
627 struct ctables_axis *axis = xmalloc (sizeof *axis);
628 *axis = (struct ctables_axis) {
630 .subs = { sub0, sub1 },
631 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
636 struct ctables_axis_parse_ctx
639 struct dictionary *dict;
641 struct ctables_table *t;
644 static struct fmt_spec
645 ctables_summary_default_format (enum ctables_summary_function function,
646 const struct ctables_var *var)
648 static const enum ctables_format default_formats[] = {
649 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
653 switch (default_formats[function])
656 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
659 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
662 return *ctables_var_get_print_format (var);
670 ctables_summary_default_label (enum ctables_summary_function function,
673 static const char *default_labels[] = {
674 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
679 return (function == CTSF_PTILE
680 ? xasprintf (_("Percentile %.2f"), percentile)
681 : xstrdup (gettext (default_labels[function])));
685 ctables_summary_function_name (enum ctables_summary_function function)
687 static const char *names[] = {
688 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
692 return names[function];
696 add_summary_spec (struct ctables_axis *axis,
697 enum ctables_summary_function function, double percentile,
698 const char *label, const struct fmt_spec *format,
699 const struct msg_location *loc)
701 if (axis->op == CTAO_VAR)
703 if (axis->n_summaries >= axis->allocated_summaries)
704 axis->summaries = x2nrealloc (axis->summaries,
705 &axis->allocated_summaries,
706 sizeof *axis->summaries);
708 const char *function_name = ctables_summary_function_name (function);
709 const char *var_name = ctables_var_name (&axis->var);
710 switch (ctables_function_availability (function))
713 if (!axis->var.is_mrset)
715 msg_at (SE, loc, _("Summary function %s applies only to multiple "
716 "response sets."), function_name);
717 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
727 _("Summary function %s applies only to scale variables."),
729 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
739 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
740 *dst = (struct ctables_summary_spec) {
741 .function = function,
742 .percentile = percentile,
743 .label = xstrdup (label),
744 .format = (format ? *format
745 : ctables_summary_default_format (function, &axis->var)),
751 for (size_t i = 0; i < 2; i++)
752 if (!add_summary_spec (axis->subs[i], function, percentile, label,
759 static struct ctables_axis *ctables_axis_parse_stack (
760 struct ctables_axis_parse_ctx *);
763 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
764 struct ctables_var *var)
766 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
768 *var = (struct ctables_var) {
770 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
774 lex_error (lexer, _("'%s' does not name a multiple-response set "
775 "in the active file dictionary."),
776 lex_tokcstr (lexer));
784 *var = (struct ctables_var) {
786 .var = parse_variable (lexer, dict),
788 return var->var != NULL;
792 static struct ctables_axis *
793 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
795 if (lex_match (ctx->lexer, T_LPAREN))
797 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
798 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
800 ctables_axis_destroy (sub);
806 if (!lex_force_id (ctx->lexer))
809 int start_ofs = lex_ofs (ctx->lexer);
810 struct ctables_var var;
811 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
814 struct ctables_axis *axis = xmalloc (sizeof *axis);
815 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
817 /* XXX should figure out default measures by reading data */
818 axis->scale = (var.is_mrset ? false
819 : lex_match_phrase (ctx->lexer, "[S]") ? true
820 : lex_match_phrase (ctx->lexer, "[C]") ? false
821 : var_get_measure (var.var) == MEASURE_SCALE);
822 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
823 lex_ofs (ctx->lexer) - 1);
827 static struct ctables_axis *
828 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
830 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
831 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
836 int start_ofs = lex_ofs (ctx->lexer);
838 /* Parse function. */
839 enum ctables_summary_function function;
840 if (!parse_ctables_summary_function (ctx->lexer, &function))
843 /* Parse percentile. */
844 double percentile = 0;
845 if (function == CTSF_PTILE)
847 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
849 percentile = lex_number (ctx->lexer);
850 lex_get (ctx->lexer);
855 if (lex_is_string (ctx->lexer))
857 label = ss_xstrdup (lex_tokss (ctx->lexer));
858 lex_get (ctx->lexer);
861 label = ctables_summary_default_label (function, percentile);
864 struct fmt_spec format;
865 const struct fmt_spec *formatp;
866 if (lex_token (ctx->lexer) == T_ID)
868 if (!parse_format_specifier (ctx->lexer, &format)
869 || !fmt_check_output (&format)
870 || !fmt_check_type_compat (&format, VAL_NUMERIC))
880 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
881 lex_ofs (ctx->lexer) - 1);
882 add_summary_spec (sub, function, percentile, label, formatp, loc);
884 msg_location_destroy (loc);
886 lex_match (ctx->lexer, T_COMMA);
888 while (!lex_match (ctx->lexer, T_RBRACK));
893 ctables_axis_destroy (sub);
897 static const struct ctables_axis *
898 find_scale (const struct ctables_axis *axis)
902 else if (axis->op == CTAO_VAR)
906 assert (!axis->var.is_mrset);
914 for (size_t i = 0; i < 2; i++)
916 const struct ctables_axis *scale = find_scale (axis->subs[i]);
924 static const struct ctables_axis *
925 find_categorical_summary_spec (const struct ctables_axis *axis)
929 else if (axis->op == CTAO_VAR)
930 return !axis->scale && axis->n_summaries ? axis : NULL;
933 for (size_t i = 0; i < 2; i++)
935 const struct ctables_axis *sum
936 = find_categorical_summary_spec (axis->subs[i]);
944 static struct ctables_axis *
945 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
947 int start_ofs = lex_ofs (ctx->lexer);
948 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
952 while (lex_match (ctx->lexer, T_GT))
954 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
958 struct ctables_axis *nest = ctables_axis_new_nonterminal (
959 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
961 const struct ctables_axis *outer_scale = find_scale (lhs);
962 const struct ctables_axis *inner_scale = find_scale (rhs);
963 if (outer_scale && inner_scale)
965 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
966 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
967 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
968 ctables_axis_destroy (nest);
972 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
975 msg_at (SE, nest->loc,
976 _("Summaries may only be requested for categorical variables "
977 "at the innermost nesting level."));
978 msg_at (SN, outer_sum->loc,
979 _("This outer categorical variable has a summary."));
980 ctables_axis_destroy (nest);
990 static struct ctables_axis *
991 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
993 int start_ofs = lex_ofs (ctx->lexer);
994 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
998 while (lex_match (ctx->lexer, T_PLUS))
1000 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1004 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1005 ctx->lexer, start_ofs);
1012 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1013 struct ctables *ct, struct ctables_table *t,
1014 enum pivot_axis_type a)
1016 if (lex_token (lexer) == T_BY
1017 || lex_token (lexer) == T_SLASH
1018 || lex_token (lexer) == T_ENDCMD)
1021 struct ctables_axis_parse_ctx ctx = {
1027 t->axes[a] = ctables_axis_parse_stack (&ctx);
1028 return t->axes[a] != NULL;
1032 ctables_chisq_destroy (struct ctables_chisq *chisq)
1038 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1044 ctables_table_destroy (struct ctables_table *t)
1049 for (size_t i = 0; i < t->n_categories; i++)
1050 ctables_categories_unref (t->categories[i]);
1051 free (t->categories);
1053 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1054 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1055 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1059 ctables_chisq_destroy (t->chisq);
1060 ctables_pairwise_destroy (t->pairwise);
1065 ctables_destroy (struct ctables *ct)
1070 pivot_table_look_unref (ct->look);
1074 for (size_t i = 0; i < ct->n_tables; i++)
1075 ctables_table_destroy (ct->tables[i]);
1080 static struct ctables_cat_value
1081 ccvt_range (double low, double high)
1083 return (struct ctables_cat_value) {
1085 .range = { low, high }
1090 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1091 struct ctables_table *t)
1093 if (!lex_match_id (lexer, "VARIABLES"))
1095 lex_match (lexer, T_EQUALS);
1097 struct variable **vars;
1099 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1102 struct ctables_categories *c = xmalloc (sizeof *c);
1103 *c = (struct ctables_categories) { .n_refs = n_vars };
1104 for (size_t i = 0; i < n_vars; i++)
1106 struct ctables_categories **cp
1107 = &t->categories[var_get_dict_index (vars[i])];
1108 ctables_categories_unref (*cp);
1113 if (lex_match (lexer, T_LBRACK))
1115 size_t allocated_values = 0;
1118 if (c->n_values >= allocated_values)
1119 c->values = x2nrealloc (c->values, &allocated_values,
1122 struct ctables_cat_value *v = &c->values[c->n_values];
1123 if (lex_match_id (lexer, "OTHERNM"))
1124 v->type = CCVT_OTHERNM;
1125 else if (lex_match_id (lexer, "MISSING"))
1126 v->type = CCVT_MISSING;
1127 else if (lex_match_id (lexer, "SUBTOTAL"))
1128 *v = (struct ctables_cat_value)
1129 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1130 else if (lex_match_id (lexer, "HSUBTOTAL"))
1131 *v = (struct ctables_cat_value)
1132 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1133 else if (lex_match_id (lexer, "LO"))
1135 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1137 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1140 else if (lex_is_number (lexer))
1142 double number = lex_number (lexer);
1144 if (lex_match_id (lexer, "THRU"))
1146 v->type = CCVT_RANGE;
1147 v->range[0] = number;
1148 if (lex_match_id (lexer, "HI"))
1149 *v = ccvt_range (number, DBL_MAX);
1152 if (!lex_force_num (lexer))
1154 *v = ccvt_range (number, lex_number (lexer));
1159 *v = (struct ctables_cat_value) {
1160 .type = CCVT_NUMBER,
1164 else if (lex_is_string (lexer))
1166 *v = (struct ctables_cat_value) {
1167 .type = CCVT_STRING,
1168 .string = ss_xstrdup (lex_tokss (lexer)),
1174 lex_error (lexer, NULL);
1178 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1179 && lex_match (lexer, T_EQUALS))
1181 if (!lex_force_string (lexer))
1184 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1189 lex_match (lexer, T_COMMA);
1191 while (!lex_match (lexer, T_RBRACK));
1194 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1196 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1198 lex_match (lexer, T_EQUALS);
1199 if (lex_match_id (lexer, "A"))
1200 c->sort_ascending = true;
1201 else if (lex_match_id (lexer, "D"))
1202 c->sort_ascending = false;
1205 lex_error_expecting (lexer, "A", "D");
1209 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1211 lex_match (lexer, T_EQUALS);
1212 if (lex_match_id (lexer, "VALUE"))
1213 c->key = CTCS_VALUE;
1214 else if (lex_match_id (lexer, "LABEL"))
1215 c->key = CTCS_LABEL;
1218 c->key = CTCS_FUNCTION;
1219 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1222 if (lex_match (lexer, T_LPAREN))
1224 c->sort_func_var = parse_variable (lexer, dict);
1225 if (!c->sort_func_var)
1228 if (c->sort_func == CTSF_PTILE)
1230 lex_match (lexer, T_COMMA);
1231 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1233 c->percentile = lex_number (lexer);
1237 if (!lex_force_match (lexer, T_RPAREN))
1240 else if (ctables_function_availability (c->sort_func)
1243 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1248 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1250 lex_match (lexer, T_EQUALS);
1251 if (lex_match_id (lexer, "INCLUDE"))
1252 c->include_missing = true;
1253 else if (lex_match_id (lexer, "EXCLUDE"))
1254 c->include_missing = false;
1257 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1261 else if (lex_match_id (lexer, "TOTAL"))
1263 lex_match (lexer, T_EQUALS);
1264 if (!parse_bool (lexer, &c->show_totals))
1267 else if (lex_match_id (lexer, "LABEL"))
1269 lex_match (lexer, T_EQUALS);
1270 if (!lex_force_string (lexer))
1272 free (c->total_label);
1273 c->total_label = ss_xstrdup (lex_tokss (lexer));
1276 else if (lex_match_id (lexer, "POSITION"))
1278 lex_match (lexer, T_EQUALS);
1279 if (lex_match_id (lexer, "BEFORE"))
1280 c->totals_before = true;
1281 else if (lex_match_id (lexer, "AFTER"))
1282 c->totals_before = false;
1285 lex_error_expecting (lexer, "BEFORE", "AFTER");
1289 else if (lex_match_id (lexer, "EMPTY"))
1291 lex_match (lexer, T_EQUALS);
1292 if (lex_match_id (lexer, "INCLUDE"))
1293 c->show_empty = true;
1294 else if (lex_match_id (lexer, "EXCLUDE"))
1295 c->show_empty = false;
1298 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1305 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1306 "TOTAL", "LABEL", "POSITION", "EMPTY");
1308 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1316 var_array_uninit (struct var_array *va)
1323 var_array2_uninit (struct var_array2 *vaa)
1327 for (size_t i = 0; i < vaa->n; i++)
1328 var_array_uninit (&vaa->vas[i]);
1333 static struct var_array2
1334 nest_fts (struct var_array2 va0, struct var_array2 va1)
1341 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1342 for (size_t i = 0; i < va0.n; i++)
1343 for (size_t j = 0; j < va1.n; j++)
1345 const struct var_array *a = &va0.vas[i];
1346 const struct var_array *b = &va1.vas[j];
1348 size_t allocate = a->n + b->n;
1349 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1350 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1352 for (size_t k = 0; k < a->n; k++)
1353 vars[n++] = a->vars[k];
1354 for (size_t k = 0; k < b->n; k++)
1355 vars[n++] = b->vars[k];
1356 assert (n == allocate);
1358 const struct var_array *summary_src;
1359 if (!a->summary_var)
1361 else if (!b->summary_var)
1365 vaa.vas[vaa.n++] = (struct var_array) {
1367 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1368 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1371 .summaries = summary_src->summaries,
1372 .n_summaries = summary_src->n_summaries,
1373 .summary_var = summary_src->summary_var,
1376 var_array2_uninit (&va0);
1377 var_array2_uninit (&va1);
1381 static struct var_array2
1382 stack_fts (struct var_array2 va0, struct var_array2 va1)
1384 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1385 for (size_t i = 0; i < va0.n; i++)
1386 vaa.vas[vaa.n++] = va0.vas[i];
1387 for (size_t i = 0; i < va1.n; i++)
1388 vaa.vas[vaa.n++] = va1.vas[i];
1389 assert (vaa.n == va0.n + va1.n);
1395 static struct var_array2
1396 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1399 return (struct var_array2) { .n = 0 };
1404 assert (!a->var.is_mrset);
1406 struct variable **vars = xmalloc (sizeof *vars);
1409 struct var_array *va = xmalloc (sizeof *va);
1410 *va = (struct var_array) {
1413 .scale_idx = a->scale ? 0 : SIZE_MAX,
1415 if (a->n_summaries || a->scale)
1417 va->summaries = a->summaries;
1418 va->n_summaries = a->n_summaries;
1419 va->summary_var = a->var.var;
1421 return (struct var_array2) { .vas = va, .n = 1 };
1424 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1425 enumerate_fts (axis_type, a->subs[1]));
1428 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1429 enumerate_fts (axis_type, a->subs[1]));
1435 union ctables_summary
1437 /* COUNT, VALIDN, TOTALN. */
1444 /* MINIMUM, MAXIMUM, RANGE. */
1451 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1452 struct moments1 *moments;
1454 /* XXX percentiles, median, mode, multiple response */
1458 ctables_summary_init (union ctables_summary *s,
1459 const struct ctables_summary_spec *ss)
1461 switch (ss->function)
1465 case CTSF_ROWPCT_COUNT:
1466 case CTSF_COLPCT_COUNT:
1467 case CTSF_TABLEPCT_COUNT:
1468 case CTSF_SUBTABLEPCT_COUNT:
1469 case CTSF_LAYERPCT_COUNT:
1470 case CTSF_LAYERROWPCT_COUNT:
1471 case CTSF_LAYERCOLPCT_COUNT:
1472 case CTSF_ROWPCT_VALIDN:
1473 case CTSF_COLPCT_VALIDN:
1474 case CTSF_TABLEPCT_VALIDN:
1475 case CTSF_SUBTABLEPCT_VALIDN:
1476 case CTSF_LAYERPCT_VALIDN:
1477 case CTSF_LAYERROWPCT_VALIDN:
1478 case CTSF_LAYERCOLPCT_VALIDN:
1479 case CTSF_ROWPCT_TOTALN:
1480 case CTSF_COLPCT_TOTALN:
1481 case CTSF_TABLEPCT_TOTALN:
1482 case CTSF_SUBTABLEPCT_TOTALN:
1483 case CTSF_LAYERPCT_TOTALN:
1484 case CTSF_LAYERROWPCT_TOTALN:
1485 case CTSF_LAYERCOLPCT_TOTALN:
1490 s->missing = s->valid = 0;
1496 s->min = s->max = SYSMIS;
1504 case CTSF_ROWPCT_SUM:
1505 case CTSF_COLPCT_SUM:
1506 case CTSF_TABLEPCT_SUM:
1507 case CTSF_SUBTABLEPCT_SUM:
1508 case CTSF_LAYERPCT_SUM:
1509 case CTSF_LAYERROWPCT_SUM:
1510 case CTSF_LAYERCOLPCT_SUM:
1511 s->moments = moments1_create (MOMENT_VARIANCE);
1520 case CTSF_RESPONSES:
1521 case CTSF_ROWPCT_RESPONSES:
1522 case CTSF_COLPCT_RESPONSES:
1523 case CTSF_TABLEPCT_RESPONSES:
1524 case CTSF_SUBTABLEPCT_RESPONSES:
1525 case CTSF_LAYERPCT_RESPONSES:
1526 case CTSF_LAYERROWPCT_RESPONSES:
1527 case CTSF_LAYERCOLPCT_RESPONSES:
1528 case CTSF_ROWPCT_RESPONSES_COUNT:
1529 case CTSF_COLPCT_RESPONSES_COUNT:
1530 case CTSF_TABLEPCT_RESPONSES_COUNT:
1531 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1532 case CTSF_LAYERPCT_RESPONSES_COUNT:
1533 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1534 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1535 case CTSF_ROWPCT_COUNT_RESPONSES:
1536 case CTSF_COLPCT_COUNT_RESPONSES:
1537 case CTSF_TABLEPCT_COUNT_RESPONSES:
1538 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1539 case CTSF_LAYERPCT_COUNT_RESPONSES:
1540 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1541 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1547 ctables_summary_uninit (union ctables_summary *s,
1548 const struct ctables_summary_spec *ss)
1550 switch (ss->function)
1554 case CTSF_ROWPCT_COUNT:
1555 case CTSF_COLPCT_COUNT:
1556 case CTSF_TABLEPCT_COUNT:
1557 case CTSF_SUBTABLEPCT_COUNT:
1558 case CTSF_LAYERPCT_COUNT:
1559 case CTSF_LAYERROWPCT_COUNT:
1560 case CTSF_LAYERCOLPCT_COUNT:
1561 case CTSF_ROWPCT_VALIDN:
1562 case CTSF_COLPCT_VALIDN:
1563 case CTSF_TABLEPCT_VALIDN:
1564 case CTSF_SUBTABLEPCT_VALIDN:
1565 case CTSF_LAYERPCT_VALIDN:
1566 case CTSF_LAYERROWPCT_VALIDN:
1567 case CTSF_LAYERCOLPCT_VALIDN:
1568 case CTSF_ROWPCT_TOTALN:
1569 case CTSF_COLPCT_TOTALN:
1570 case CTSF_TABLEPCT_TOTALN:
1571 case CTSF_SUBTABLEPCT_TOTALN:
1572 case CTSF_LAYERPCT_TOTALN:
1573 case CTSF_LAYERROWPCT_TOTALN:
1574 case CTSF_LAYERCOLPCT_TOTALN:
1591 case CTSF_ROWPCT_SUM:
1592 case CTSF_COLPCT_SUM:
1593 case CTSF_TABLEPCT_SUM:
1594 case CTSF_SUBTABLEPCT_SUM:
1595 case CTSF_LAYERPCT_SUM:
1596 case CTSF_LAYERROWPCT_SUM:
1597 case CTSF_LAYERCOLPCT_SUM:
1598 moments1_destroy (s->moments);
1607 case CTSF_RESPONSES:
1608 case CTSF_ROWPCT_RESPONSES:
1609 case CTSF_COLPCT_RESPONSES:
1610 case CTSF_TABLEPCT_RESPONSES:
1611 case CTSF_SUBTABLEPCT_RESPONSES:
1612 case CTSF_LAYERPCT_RESPONSES:
1613 case CTSF_LAYERROWPCT_RESPONSES:
1614 case CTSF_LAYERCOLPCT_RESPONSES:
1615 case CTSF_ROWPCT_RESPONSES_COUNT:
1616 case CTSF_COLPCT_RESPONSES_COUNT:
1617 case CTSF_TABLEPCT_RESPONSES_COUNT:
1618 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1619 case CTSF_LAYERPCT_RESPONSES_COUNT:
1620 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1621 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1622 case CTSF_ROWPCT_COUNT_RESPONSES:
1623 case CTSF_COLPCT_COUNT_RESPONSES:
1624 case CTSF_TABLEPCT_COUNT_RESPONSES:
1625 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1626 case CTSF_LAYERPCT_COUNT_RESPONSES:
1627 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1628 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1634 ctables_summary_add (union ctables_summary *s,
1635 const struct ctables_summary_spec *ss,
1636 const struct variable *var, const union value *value,
1639 switch (ss->function)
1643 case CTSF_ROWPCT_COUNT:
1644 case CTSF_COLPCT_COUNT:
1645 case CTSF_TABLEPCT_COUNT:
1646 case CTSF_SUBTABLEPCT_COUNT:
1647 case CTSF_LAYERPCT_COUNT:
1648 case CTSF_LAYERROWPCT_COUNT:
1649 case CTSF_LAYERCOLPCT_COUNT:
1650 case CTSF_ROWPCT_VALIDN:
1651 case CTSF_COLPCT_VALIDN:
1652 case CTSF_TABLEPCT_VALIDN:
1653 case CTSF_SUBTABLEPCT_VALIDN:
1654 case CTSF_LAYERPCT_VALIDN:
1655 case CTSF_LAYERROWPCT_VALIDN:
1656 case CTSF_LAYERCOLPCT_VALIDN:
1657 case CTSF_ROWPCT_TOTALN:
1658 case CTSF_COLPCT_TOTALN:
1659 case CTSF_TABLEPCT_TOTALN:
1660 case CTSF_SUBTABLEPCT_TOTALN:
1661 case CTSF_LAYERPCT_TOTALN:
1662 case CTSF_LAYERROWPCT_TOTALN:
1663 case CTSF_LAYERCOLPCT_TOTALN:
1668 if (var_is_value_missing (var, value))
1669 s->missing += weight;
1677 if (!var_is_value_missing (var, value))
1679 assert (!var_is_alpha (var)); /* XXX? */
1680 if (s->min == SYSMIS || value->f < s->min)
1682 if (s->max == SYSMIS || value->f > s->max)
1692 case CTSF_ROWPCT_SUM:
1693 case CTSF_COLPCT_SUM:
1694 case CTSF_TABLEPCT_SUM:
1695 case CTSF_SUBTABLEPCT_SUM:
1696 case CTSF_LAYERPCT_SUM:
1697 case CTSF_LAYERROWPCT_SUM:
1698 case CTSF_LAYERCOLPCT_SUM:
1699 moments1_add (s->moments, value->f, weight);
1708 case CTSF_RESPONSES:
1709 case CTSF_ROWPCT_RESPONSES:
1710 case CTSF_COLPCT_RESPONSES:
1711 case CTSF_TABLEPCT_RESPONSES:
1712 case CTSF_SUBTABLEPCT_RESPONSES:
1713 case CTSF_LAYERPCT_RESPONSES:
1714 case CTSF_LAYERROWPCT_RESPONSES:
1715 case CTSF_LAYERCOLPCT_RESPONSES:
1716 case CTSF_ROWPCT_RESPONSES_COUNT:
1717 case CTSF_COLPCT_RESPONSES_COUNT:
1718 case CTSF_TABLEPCT_RESPONSES_COUNT:
1719 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1720 case CTSF_LAYERPCT_RESPONSES_COUNT:
1721 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1722 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1723 case CTSF_ROWPCT_COUNT_RESPONSES:
1724 case CTSF_COLPCT_COUNT_RESPONSES:
1725 case CTSF_TABLEPCT_COUNT_RESPONSES:
1726 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1727 case CTSF_LAYERPCT_COUNT_RESPONSES:
1728 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1729 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1735 ctables_summary_value (const struct ctables_freq *f,
1736 union ctables_summary *s,
1737 const struct ctables_summary_spec *ss)
1739 switch (ss->function)
1745 case CTSF_SUBTABLEPCT_COUNT:
1746 return f->domains[CTDT_SUBTABLE]->valid ? s->valid / f->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1748 case CTSF_ROWPCT_COUNT:
1749 return f->domains[CTDT_ROW]->valid ? s->valid / f->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1751 case CTSF_COLPCT_COUNT:
1752 return f->domains[CTDT_COL]->valid ? s->valid / f->domains[CTDT_COL]->valid * 100 : SYSMIS;
1754 case CTSF_TABLEPCT_COUNT:
1755 return f->domains[CTDT_TABLE]->valid ? s->valid / f->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1757 case CTSF_LAYERPCT_COUNT:
1758 return f->domains[CTDT_LAYER]->valid ? s->valid / f->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1760 case CTSF_LAYERROWPCT_COUNT:
1761 return f->domains[CTDT_LAYERROW]->valid ? s->valid / f->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1763 case CTSF_LAYERCOLPCT_COUNT:
1764 return f->domains[CTDT_LAYERCOL]->valid ? s->valid / f->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1766 case CTSF_ROWPCT_VALIDN:
1767 case CTSF_COLPCT_VALIDN:
1768 case CTSF_TABLEPCT_VALIDN:
1769 case CTSF_SUBTABLEPCT_VALIDN:
1770 case CTSF_LAYERPCT_VALIDN:
1771 case CTSF_LAYERROWPCT_VALIDN:
1772 case CTSF_LAYERCOLPCT_VALIDN:
1773 case CTSF_ROWPCT_TOTALN:
1774 case CTSF_COLPCT_TOTALN:
1775 case CTSF_TABLEPCT_TOTALN:
1776 case CTSF_SUBTABLEPCT_TOTALN:
1777 case CTSF_LAYERPCT_TOTALN:
1778 case CTSF_LAYERROWPCT_TOTALN:
1779 case CTSF_LAYERCOLPCT_TOTALN:
1784 return s->valid + s->missing;
1797 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1802 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1808 double weight, variance;
1809 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1810 return calc_semean (variance, weight);
1816 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1817 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1822 double weight, mean;
1823 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1824 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1830 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1834 case CTSF_ROWPCT_SUM:
1835 case CTSF_COLPCT_SUM:
1836 case CTSF_TABLEPCT_SUM:
1837 case CTSF_SUBTABLEPCT_SUM:
1838 case CTSF_LAYERPCT_SUM:
1839 case CTSF_LAYERROWPCT_SUM:
1840 case CTSF_LAYERCOLPCT_SUM:
1849 case CTSF_RESPONSES:
1850 case CTSF_ROWPCT_RESPONSES:
1851 case CTSF_COLPCT_RESPONSES:
1852 case CTSF_TABLEPCT_RESPONSES:
1853 case CTSF_SUBTABLEPCT_RESPONSES:
1854 case CTSF_LAYERPCT_RESPONSES:
1855 case CTSF_LAYERROWPCT_RESPONSES:
1856 case CTSF_LAYERCOLPCT_RESPONSES:
1857 case CTSF_ROWPCT_RESPONSES_COUNT:
1858 case CTSF_COLPCT_RESPONSES_COUNT:
1859 case CTSF_TABLEPCT_RESPONSES_COUNT:
1860 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1861 case CTSF_LAYERPCT_RESPONSES_COUNT:
1862 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1863 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1864 case CTSF_ROWPCT_COUNT_RESPONSES:
1865 case CTSF_COLPCT_COUNT_RESPONSES:
1866 case CTSF_TABLEPCT_COUNT_RESPONSES:
1867 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1868 case CTSF_LAYERPCT_COUNT_RESPONSES:
1869 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1870 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1877 struct ctables_freq_sort_aux
1879 const struct ctables_table *t;
1880 enum pivot_axis_type a;
1884 ctables_freq_compare_3way (const void *a_, const void *b_, const void *aux_)
1886 const struct ctables_freq_sort_aux *aux = aux_;
1887 struct ctables_freq *const *ap = a_;
1888 struct ctables_freq *const *bp = b_;
1889 const struct ctables_freq *a = *ap;
1890 const struct ctables_freq *b = *bp;
1892 size_t a_idx = a->axes[aux->a].vaa_idx;
1893 size_t b_idx = b->axes[aux->a].vaa_idx;
1895 return a_idx < b_idx ? -1 : 1;
1897 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1898 for (size_t i = 0; i < va->n; i++)
1899 if (i != va->scale_idx)
1901 int cmp = value_compare_3way (&a->axes[aux->a].values[i],
1902 &b->axes[aux->a].values[i],
1903 var_get_width (va->vars[i]));
1913 For each ctables_table:
1914 For each combination of row vars:
1915 For each combination of column vars:
1916 For each combination of layer vars:
1918 Make a table of row values:
1919 Sort entries by row values
1920 Assign a 0-based index to each actual value
1921 Construct a dimension
1922 Make a table of column values
1923 Make a table of layer values
1925 Fill the table entry using the indexes from before.
1928 static struct ctables_domain *
1929 ctables_domain_insert (struct ctables_table *t, struct ctables_freq *f,
1930 enum ctables_domain_type domain)
1932 /* XXX how can we handle CTDT_LAYERROW and CTDT_LAYERCOL? Crossing subtables
1935 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1937 size_t idx = f->axes[a].vaa_idx;
1938 const struct var_array *va = &t->vaas[a].vas[idx];
1939 hash = hash_int (idx, hash);
1940 for (size_t i = 0; i < va->n_domains[domain]; i++)
1942 size_t v_idx = va->domains[domain][i];
1943 hash = value_hash (&f->axes[a].values[v_idx],
1944 var_get_width (va->vars[v_idx]), hash);
1948 struct ctables_domain *d;
1949 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
1951 const struct ctables_freq *df = d->example;
1952 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1954 size_t idx = f->axes[a].vaa_idx;
1955 if (idx != df->axes[a].vaa_idx)
1958 const struct var_array *va = &t->vaas[a].vas[idx];
1959 for (size_t i = 0; i < va->n_domains[domain]; i++)
1961 size_t v_idx = va->domains[domain][i];
1962 if (!value_equal (&df->axes[a].values[v_idx],
1963 &f->axes[a].values[v_idx],
1964 var_get_width (va->vars[v_idx])))
1973 d = xmalloc (sizeof *d);
1974 *d = (struct ctables_domain) { .example = f };
1975 hmap_insert (&t->domains[domain], &d->node, hash);
1980 ctables_freqtab_insert (struct ctables_table *t,
1981 const struct ccase *c,
1982 size_t ir, size_t ic, size_t il,
1985 size_t ix[PIVOT_N_AXES] = {
1986 [PIVOT_AXIS_ROW] = ir,
1987 [PIVOT_AXIS_COLUMN] = ic,
1988 [PIVOT_AXIS_LAYER] = il,
1990 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
1993 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1995 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1996 hash = hash_int (ix[a], hash);
1997 for (size_t i = 0; i < va->n; i++)
1998 if (i != va->scale_idx)
1999 hash = value_hash (case_data (c, va->vars[i]),
2000 var_get_width (va->vars[i]), hash);
2003 struct ctables_freq *f;
2004 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &t->ft)
2006 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2008 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2009 if (f->axes[a].vaa_idx != ix[a])
2011 for (size_t i = 0; i < va->n; i++)
2012 if (i != va->scale_idx
2013 && !value_equal (case_data (c, va->vars[i]),
2014 &f->axes[a].values[i],
2015 var_get_width (va->vars[i])))
2024 f = xmalloc (sizeof *f);
2025 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2027 const struct var_array *va = &t->vaas[a].vas[ix[a]];
2028 f->axes[a].vaa_idx = ix[a];
2029 f->axes[a].values = (va->n
2030 ? xnmalloc (va->n, sizeof *f->axes[a].values)
2032 for (size_t i = 0; i < va->n; i++)
2033 value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
2034 var_get_width (va->vars[i]));
2036 f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
2037 for (size_t i = 0; i < ss->n_summaries; i++)
2038 ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
2039 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2040 f->domains[dt] = ctables_domain_insert (t, f, dt);
2041 hmap_insert (&t->ft, &f->node, hash);
2044 for (size_t i = 0; i < ss->n_summaries; i++)
2045 ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
2046 case_data (c, ss->summary_var), weight);
2047 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2048 f->domains[dt]->valid += weight;
2052 ctables_execute (struct dataset *ds, struct ctables *ct)
2054 for (size_t i = 0; i < ct->n_tables; i++)
2056 struct ctables_table *t = ct->tables[i];
2057 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2060 t->vaas[a] = enumerate_fts (a, t->axes[a]);
2062 for (size_t j = 0; j < t->vaas[a].n; j++)
2064 struct var_array *va = &t->vaas[a].vas[j];
2065 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2067 va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
2068 va->n_domains[dt] = 0;
2070 for (size_t k = 0; k < va->n; k++)
2072 if (k == va->scale_idx)
2081 if (a != PIVOT_AXIS_LAYER)
2088 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2089 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2090 : a == PIVOT_AXIS_ROW)
2093 || (va->scale_idx == va->n - 1
2100 if (a == PIVOT_AXIS_COLUMN)
2105 if (a == PIVOT_AXIS_ROW)
2110 va->domains[dt][va->n_domains[dt]++] = k;
2117 struct var_array *va = xmalloc (sizeof *va);
2118 *va = (struct var_array) { .n = 0 };
2119 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
2122 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
2124 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
2125 if (!va->n_summaries)
2127 va->summaries = xmalloc (sizeof *va->summaries);
2128 va->n_summaries = 1;
2130 enum ctables_summary_function function
2131 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
2132 struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
2134 *va->summaries = (struct ctables_summary_spec) {
2135 .function = function,
2136 .format = ctables_summary_default_format (function, &var),
2137 .label = ctables_summary_default_label (function, 0),
2139 if (!va->summary_var)
2140 va->summary_var = va->vars[0];
2145 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2148 bool warn_on_invalid = true;
2149 double total_weight = 0;
2150 for (struct ccase *c = casereader_read (input); c;
2151 case_unref (c), c = casereader_read (input))
2153 double weight = dict_get_case_weight (dataset_dict (ds), c,
2155 total_weight += weight;
2157 for (size_t i = 0; i < ct->n_tables; i++)
2159 struct ctables_table *t = ct->tables[i];
2161 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2162 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2163 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2164 ctables_freqtab_insert (t, c, ir, ic, il, weight);
2167 casereader_destroy (input);
2169 for (size_t i = 0; i < ct->n_tables; i++)
2171 struct ctables_table *t = ct->tables[i];
2173 struct pivot_table *pt = pivot_table_create__ (
2175 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2176 : pivot_value_new_text (N_("Custom Tables"))),
2179 pivot_table_set_caption (
2180 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2182 pivot_table_set_caption (
2183 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2185 pivot_table_set_look (pt, ct->look);
2186 struct pivot_dimension *d[PIVOT_N_AXES];
2187 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2189 static const char *names[] = {
2190 [PIVOT_AXIS_ROW] = N_("Rows"),
2191 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2192 [PIVOT_AXIS_LAYER] = N_("Layers"),
2194 d[a] = (t->axes[a] || a == t->summary_axis
2195 ? pivot_dimension_create (pt, a, names[a])
2200 assert (t->axes[a]);
2202 struct ctables_freq **sorted = xnmalloc (t->ft.count, sizeof *sorted);
2204 struct ctables_freq *f;
2206 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2208 assert (n == t->ft.count);
2210 struct ctables_freq_sort_aux aux = { .t = t, .a = a };
2211 sort (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
2213 size_t max_depth = 0;
2214 for (size_t j = 0; j < t->vaas[a].n; j++)
2215 if (t->vaas[a].vas[j].n > max_depth)
2216 max_depth = t->vaas[a].vas[j].n;
2218 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2219 struct pivot_category *top = NULL;
2221 for (size_t j = 0; j < n; j++)
2223 struct ctables_freq *f = sorted[j];
2224 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2226 size_t n_common = 0;
2227 bool new_subtable = false;
2230 struct ctables_freq *prev = sorted[j - 1];
2231 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2233 for (; n_common < va->n; n_common++)
2234 if (n_common != va->scale_idx
2235 && !value_equal (&prev->axes[a].values[n_common],
2236 &f->axes[a].values[n_common],
2237 var_get_type (va->vars[n_common])))
2241 new_subtable = true;
2244 new_subtable = true;
2248 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2250 if (vlabel != CTVL_NONE)
2251 top = pivot_category_create_group__ (
2252 top, pivot_value_new_variable (va->vars[0]));
2254 if (n_common == va->n)
2256 f->axes[a].leaf = prev_leaf;
2260 for (size_t k = n_common; k < va->n; k++)
2262 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2264 struct pivot_value *label
2265 = (k != va->scale_idx
2266 ? pivot_value_new_var_value (va->vars[k],
2267 &f->axes[a].values[k])
2271 if (a == t->summary_axis)
2274 parent = pivot_category_create_group__ (parent, label);
2275 for (size_t m = 0; m < va->n_summaries; m++)
2277 int leaf = pivot_category_create_leaf (
2278 parent, pivot_value_new_text (va->summaries[m].label));
2285 /* This assertion is true as long as the summary axis
2286 is the axis where the summaries are displayed. */
2289 prev_leaf = pivot_category_create_leaf (parent, label);
2295 parent = pivot_category_create_group__ (parent, label);
2297 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2298 if (vlabel != CTVL_NONE)
2299 parent = pivot_category_create_group__ (
2300 parent, pivot_value_new_variable (va->vars[k + 1]));
2304 f->axes[a].leaf = prev_leaf;
2309 struct ctables_freq *f;
2310 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2312 const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2313 for (size_t j = 0; j < ss->n_summaries; j++)
2316 size_t n_dindexes = 0;
2318 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2321 int leaf = f->axes[a].leaf;
2322 if (a == t->summary_axis)
2324 dindexes[n_dindexes++] = leaf;
2327 double d = ctables_summary_value (f, &f->summaries[j], &ss->summaries[j]);
2328 struct pivot_value *value = pivot_value_new_number (d);
2329 value->numeric.format = ss->summaries[j].format;
2330 pivot_table_put (pt, dindexes, n_dindexes, value);
2334 pivot_table_submit (pt);
2338 for (size_t i = 0; i < ct->n_tables; i++)
2340 struct ctables_table *t = ct->tables[i];
2342 for (size_t j = 0; j < t->n_fts; j++)
2344 struct ctables_freqtab *ft = t->fts[j];
2345 struct ctables_freq *f, *next;
2346 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2348 hmap_delete (&ft->data, &f->node);
2349 for (size_t k = 0; k < ft->n_summaries; k++)
2350 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2351 free (f->summaries);
2352 for (size_t k = 0; k < ft->vars.n; k++)
2354 const struct variable *var = ft->vars.vars[k];
2355 value_destroy (&f->values[k], var_get_width (var));
2359 hmap_destroy (&ft->data);
2360 var_array_uninit (&ft->vars);
2367 return proc_commit (ds);
2371 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2373 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2374 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2375 enum settings_value_show tvars = settings_get_show_variables ();
2376 for (size_t i = 0; i < n_vars; i++)
2377 vlabels[i] = (enum ctables_vlabel) tvars;
2379 struct ctables *ct = xmalloc (sizeof *ct);
2380 *ct = (struct ctables) {
2381 .look = pivot_table_look_unshare (pivot_table_look_ref (
2382 pivot_table_look_get_default ())),
2384 .hide_threshold = 5,
2386 ct->look->omit_empty = false;
2388 if (!lex_force_match (lexer, T_SLASH))
2391 while (!lex_match_id (lexer, "TABLE"))
2393 if (lex_match_id (lexer, "FORMAT"))
2395 double widths[2] = { SYSMIS, SYSMIS };
2396 double units_per_inch = 72.0;
2398 while (lex_token (lexer) != T_SLASH)
2400 if (lex_match_id (lexer, "MINCOLWIDTH"))
2402 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2405 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2407 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2410 else if (lex_match_id (lexer, "UNITS"))
2412 lex_match (lexer, T_EQUALS);
2413 if (lex_match_id (lexer, "POINTS"))
2414 units_per_inch = 72.0;
2415 else if (lex_match_id (lexer, "INCHES"))
2416 units_per_inch = 1.0;
2417 else if (lex_match_id (lexer, "CM"))
2418 units_per_inch = 2.54;
2421 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2425 else if (lex_match_id (lexer, "EMPTY"))
2430 lex_match (lexer, T_EQUALS);
2431 if (lex_match_id (lexer, "ZERO"))
2433 /* Nothing to do. */
2435 else if (lex_match_id (lexer, "BLANK"))
2436 ct->zero = xstrdup ("");
2437 else if (lex_force_string (lexer))
2439 ct->zero = ss_xstrdup (lex_tokss (lexer));
2445 else if (lex_match_id (lexer, "MISSING"))
2447 lex_match (lexer, T_EQUALS);
2448 if (!lex_force_string (lexer))
2452 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2453 ? ss_xstrdup (lex_tokss (lexer))
2459 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2460 "UNITS", "EMPTY", "MISSING");
2465 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2466 && widths[0] > widths[1])
2468 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2472 for (size_t i = 0; i < 2; i++)
2473 if (widths[i] != SYSMIS)
2475 int *wr = ct->look->width_ranges[TABLE_HORZ];
2476 wr[i] = widths[i] / units_per_inch * 96.0;
2481 else if (lex_match_id (lexer, "VLABELS"))
2483 if (!lex_force_match_id (lexer, "VARIABLES"))
2485 lex_match (lexer, T_EQUALS);
2487 struct variable **vars;
2489 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2493 if (!lex_force_match_id (lexer, "DISPLAY"))
2498 lex_match (lexer, T_EQUALS);
2500 enum ctables_vlabel vlabel;
2501 if (lex_match_id (lexer, "DEFAULT"))
2502 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2503 else if (lex_match_id (lexer, "NAME"))
2505 else if (lex_match_id (lexer, "LABEL"))
2506 vlabel = CTVL_LABEL;
2507 else if (lex_match_id (lexer, "BOTH"))
2509 else if (lex_match_id (lexer, "NONE"))
2513 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2519 for (size_t i = 0; i < n_vars; i++)
2520 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2523 else if (lex_match_id (lexer, "MRSETS"))
2525 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2527 lex_match (lexer, T_EQUALS);
2528 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2531 else if (lex_match_id (lexer, "SMISSING"))
2533 if (lex_match_id (lexer, "VARIABLE"))
2534 ct->smissing_listwise = false;
2535 else if (lex_match_id (lexer, "LISTWISE"))
2536 ct->smissing_listwise = true;
2539 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2544 else if (lex_match_id (lexer, "WEIGHT"))
2546 if (!lex_force_match_id (lexer, "VARIABLE"))
2548 lex_match (lexer, T_EQUALS);
2549 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2550 if (!ct->base_weight)
2553 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2555 if (!lex_force_match_id (lexer, "COUNT"))
2557 lex_match (lexer, T_EQUALS);
2558 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2560 ct->hide_threshold = lex_integer (lexer);
2565 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2566 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2567 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2571 if (!lex_force_match (lexer, T_SLASH))
2575 size_t allocated_tables = 0;
2578 if (ct->n_tables >= allocated_tables)
2579 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2580 sizeof *ct->tables);
2582 struct ctables_table *t = xmalloc (sizeof *t);
2583 *t = (struct ctables_table) {
2584 .ft = HMAP_INITIALIZER (t->ft),
2585 .slabels_position = PIVOT_AXIS_COLUMN,
2586 .slabels_visible = true,
2587 .row_labels = CTLP_NORMAL,
2588 .col_labels = CTLP_NORMAL,
2589 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2590 sizeof *t->categories),
2591 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2594 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2595 hmap_init (&t->domains[dt]);
2596 ct->tables[ct->n_tables++] = t;
2598 lex_match (lexer, T_EQUALS);
2599 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2601 if (lex_match (lexer, T_BY))
2603 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2604 ct, t, PIVOT_AXIS_COLUMN))
2607 if (lex_match (lexer, T_BY))
2609 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2610 ct, t, PIVOT_AXIS_LAYER))
2615 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2616 && !t->axes[PIVOT_AXIS_LAYER])
2618 lex_error (lexer, _("At least one variable must be specified."));
2622 const struct ctables_axis *scales[PIVOT_N_AXES];
2623 size_t n_scales = 0;
2624 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2626 scales[a] = find_scale (t->axes[a]);
2632 msg (SE, _("Scale variables may appear only on one axis."));
2633 if (scales[PIVOT_AXIS_ROW])
2634 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2635 _("This scale variable appears on the rows axis."));
2636 if (scales[PIVOT_AXIS_COLUMN])
2637 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2638 _("This scale variable appears on the columns axis."));
2639 if (scales[PIVOT_AXIS_LAYER])
2640 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2641 _("This scale variable appears on the layer axis."));
2645 const struct ctables_axis *summaries[PIVOT_N_AXES];
2646 size_t n_summaries = 0;
2647 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2649 summaries[a] = (scales[a]
2651 : find_categorical_summary_spec (t->axes[a]));
2655 if (n_summaries > 1)
2657 msg (SE, _("Summaries may appear only on one axis."));
2658 if (summaries[PIVOT_AXIS_ROW])
2659 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2660 _("This variable on the rows axis has a summary."));
2661 if (summaries[PIVOT_AXIS_COLUMN])
2662 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2663 _("This variable on the columns axis has a summary."));
2664 if (summaries[PIVOT_AXIS_LAYER])
2665 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2666 _("This variable on the layers axis has a summary."));
2669 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2670 if (n_summaries ? summaries[a] : t->axes[a])
2672 t->summary_axis = a;
2676 if (lex_token (lexer) == T_ENDCMD)
2678 if (!lex_force_match (lexer, T_SLASH))
2681 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2683 if (lex_match_id (lexer, "SLABELS"))
2685 while (lex_token (lexer) != T_SLASH)
2687 if (lex_match_id (lexer, "POSITION"))
2689 lex_match (lexer, T_EQUALS);
2690 if (lex_match_id (lexer, "COLUMN"))
2691 t->slabels_position = PIVOT_AXIS_COLUMN;
2692 else if (lex_match_id (lexer, "ROW"))
2693 t->slabels_position = PIVOT_AXIS_ROW;
2694 else if (lex_match_id (lexer, "LAYER"))
2695 t->slabels_position = PIVOT_AXIS_LAYER;
2698 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2702 else if (lex_match_id (lexer, "VISIBLE"))
2704 lex_match (lexer, T_EQUALS);
2705 if (!parse_bool (lexer, &t->slabels_visible))
2710 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2715 else if (lex_match_id (lexer, "CLABELS"))
2717 while (lex_token (lexer) != T_SLASH)
2719 if (lex_match_id (lexer, "AUTO"))
2720 t->row_labels = t->col_labels = CTLP_NORMAL;
2721 else if (lex_match_id (lexer, "ROWLABELS"))
2723 lex_match (lexer, T_EQUALS);
2724 if (lex_match_id (lexer, "OPPOSITE"))
2725 t->row_labels = CTLP_OPPOSITE;
2726 else if (lex_match_id (lexer, "LAYER"))
2727 t->row_labels = CTLP_LAYER;
2730 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2734 else if (lex_match_id (lexer, "COLLABELS"))
2736 lex_match (lexer, T_EQUALS);
2737 if (lex_match_id (lexer, "OPPOSITE"))
2738 t->col_labels = CTLP_OPPOSITE;
2739 else if (lex_match_id (lexer, "LAYER"))
2740 t->col_labels = CTLP_LAYER;
2743 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2749 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2755 else if (lex_match_id (lexer, "CRITERIA"))
2757 if (!lex_force_match_id (lexer, "CILEVEL"))
2759 lex_match (lexer, T_EQUALS);
2761 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2763 t->cilevel = lex_number (lexer);
2766 else if (lex_match_id (lexer, "CATEGORIES"))
2768 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2771 else if (lex_match_id (lexer, "TITLES"))
2776 if (lex_match_id (lexer, "CAPTION"))
2777 textp = &t->caption;
2778 else if (lex_match_id (lexer, "CORNER"))
2780 else if (lex_match_id (lexer, "TITLE"))
2784 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2787 lex_match (lexer, T_EQUALS);
2789 struct string s = DS_EMPTY_INITIALIZER;
2790 while (lex_is_string (lexer))
2792 if (!ds_is_empty (&s))
2793 ds_put_byte (&s, ' ');
2794 ds_put_substring (&s, lex_tokss (lexer));
2798 *textp = ds_steal_cstr (&s);
2800 while (lex_token (lexer) != T_SLASH
2801 && lex_token (lexer) != T_ENDCMD);
2803 else if (lex_match_id (lexer, "SIGTEST"))
2807 t->chisq = xmalloc (sizeof *t->chisq);
2808 *t->chisq = (struct ctables_chisq) {
2810 .include_mrsets = true,
2811 .all_visible = true,
2817 if (lex_match_id (lexer, "TYPE"))
2819 lex_match (lexer, T_EQUALS);
2820 if (!lex_force_match_id (lexer, "CHISQUARE"))
2823 else if (lex_match_id (lexer, "ALPHA"))
2825 lex_match (lexer, T_EQUALS);
2826 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2828 t->chisq->alpha = lex_number (lexer);
2831 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2833 lex_match (lexer, T_EQUALS);
2834 if (parse_bool (lexer, &t->chisq->include_mrsets))
2837 else if (lex_match_id (lexer, "CATEGORIES"))
2839 lex_match (lexer, T_EQUALS);
2840 if (lex_match_id (lexer, "ALLVISIBLE"))
2841 t->chisq->all_visible = true;
2842 else if (lex_match_id (lexer, "SUBTOTALS"))
2843 t->chisq->all_visible = false;
2846 lex_error_expecting (lexer,
2847 "ALLVISIBLE", "SUBTOTALS");
2853 lex_error_expecting (lexer, "TYPE", "ALPHA",
2854 "INCLUDEMRSETS", "CATEGORIES");
2858 while (lex_token (lexer) != T_SLASH
2859 && lex_token (lexer) != T_ENDCMD);
2861 else if (lex_match_id (lexer, "COMPARETEST"))
2865 t->pairwise = xmalloc (sizeof *t->pairwise);
2866 *t->pairwise = (struct ctables_pairwise) {
2868 .alpha = { .05, .05 },
2869 .adjust = BONFERRONI,
2870 .include_mrsets = true,
2871 .meansvariance_allcats = true,
2872 .all_visible = true,
2881 if (lex_match_id (lexer, "TYPE"))
2883 lex_match (lexer, T_EQUALS);
2884 if (lex_match_id (lexer, "PROP"))
2885 t->pairwise->type = PROP;
2886 else if (lex_match_id (lexer, "MEAN"))
2887 t->pairwise->type = MEAN;
2890 lex_error_expecting (lexer, "PROP", "MEAN");
2894 else if (lex_match_id (lexer, "ALPHA"))
2896 lex_match (lexer, T_EQUALS);
2898 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2900 double a0 = lex_number (lexer);
2903 lex_match (lexer, T_COMMA);
2904 if (lex_is_number (lexer))
2906 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2908 double a1 = lex_number (lexer);
2911 t->pairwise->alpha[0] = MIN (a0, a1);
2912 t->pairwise->alpha[1] = MAX (a0, a1);
2915 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2917 else if (lex_match_id (lexer, "ADJUST"))
2919 lex_match (lexer, T_EQUALS);
2920 if (lex_match_id (lexer, "BONFERRONI"))
2921 t->pairwise->adjust = BONFERRONI;
2922 else if (lex_match_id (lexer, "BH"))
2923 t->pairwise->adjust = BH;
2924 else if (lex_match_id (lexer, "NONE"))
2925 t->pairwise->adjust = 0;
2928 lex_error_expecting (lexer, "BONFERRONI", "BH",
2933 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2935 lex_match (lexer, T_EQUALS);
2936 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2939 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2941 lex_match (lexer, T_EQUALS);
2942 if (lex_match_id (lexer, "ALLCATS"))
2943 t->pairwise->meansvariance_allcats = true;
2944 else if (lex_match_id (lexer, "TESTEDCATS"))
2945 t->pairwise->meansvariance_allcats = false;
2948 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2952 else if (lex_match_id (lexer, "CATEGORIES"))
2954 lex_match (lexer, T_EQUALS);
2955 if (lex_match_id (lexer, "ALLVISIBLE"))
2956 t->pairwise->all_visible = true;
2957 else if (lex_match_id (lexer, "SUBTOTALS"))
2958 t->pairwise->all_visible = false;
2961 lex_error_expecting (lexer, "ALLVISIBLE",
2966 else if (lex_match_id (lexer, "MERGE"))
2968 lex_match (lexer, T_EQUALS);
2969 if (!parse_bool (lexer, &t->pairwise->merge))
2972 else if (lex_match_id (lexer, "STYLE"))
2974 lex_match (lexer, T_EQUALS);
2975 if (lex_match_id (lexer, "APA"))
2976 t->pairwise->apa_style = true;
2977 else if (lex_match_id (lexer, "SIMPLE"))
2978 t->pairwise->apa_style = false;
2981 lex_error_expecting (lexer, "APA", "SIMPLE");
2985 else if (lex_match_id (lexer, "SHOWSIG"))
2987 lex_match (lexer, T_EQUALS);
2988 if (!parse_bool (lexer, &t->pairwise->show_sig))
2993 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2994 "INCLUDEMRSETS", "MEANSVARIANCE",
2995 "CATEGORIES", "MERGE", "STYLE",
3000 while (lex_token (lexer) != T_SLASH
3001 && lex_token (lexer) != T_ENDCMD);
3005 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3006 "CRITERIA", "CATEGORIES", "TITLES",
3007 "SIGTEST", "COMPARETEST");
3012 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3014 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3019 while (lex_token (lexer) != T_ENDCMD);
3021 bool ok = ctables_execute (ds, ct);
3022 ctables_destroy (ct);
3023 return ok ? CMD_SUCCESS : CMD_FAILURE;
3026 ctables_destroy (ct);