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
149 struct pivot_table_look *look;
151 /* If this is NULL, zeros are displayed using the normal print format.
152 Otherwise, this string is displayed. */
155 /* If this is NULL, missing values are displayed using the normal print
156 format. Otherwise, this string is displayed. */
159 /* Indexed by variable dictionary index. */
160 enum ctables_vlabel *vlabels;
162 bool mrsets_count_duplicates; /* MRSETS. */
163 bool smissing_listwise; /* SMISSING. */
164 struct variable *base_weight; /* WEIGHT. */
165 int hide_threshold; /* HIDESMALLCOUNTS. */
167 struct ctables_table **tables;
171 struct ctables_postcompute
173 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
174 const char *name; /* Name, without leading &. */
176 struct ctables_postcompute_expr *expr;
179 bool hide_source_cats;
182 struct ctables_postcompute_expr
184 enum ctables_postcompute_op
192 /* XXX SUBTOTAL and HSUBTOTAL */
205 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
210 XXX what about string ranges? */
213 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
214 struct ctables_postcompute_expr *subs[2];
218 enum ctables_label_position
227 struct variable **vars;
230 struct ctables_summary_spec *summaries;
232 struct variable *summary_var;
237 struct var_array *vas;
243 struct ctables_axis *axes[PIVOT_N_AXES];
244 struct var_array2 vaas[PIVOT_N_AXES];
245 enum pivot_axis_type summary_axis;
248 enum pivot_axis_type slabels_position;
249 bool slabels_visible;
251 enum ctables_label_position row_labels;
252 enum ctables_label_position col_labels;
254 /* Indexed by variable dictionary index. */
255 struct ctables_categories **categories;
264 struct ctables_chisq *chisq;
265 struct ctables_pairwise *pairwise;
274 struct variable *var;
275 const struct mrset *mrset;
279 static const struct fmt_spec *
280 ctables_var_get_print_format (const struct ctables_var *var)
282 return (var->is_mrset
283 ? var_get_print_format (var->mrset->vars[0])
284 : var_get_print_format (var->var));
288 ctables_var_name (const struct ctables_var *var)
290 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
293 struct ctables_categories
297 /* Explicit categories. */
298 struct ctables_cat_value *values;
301 /* Implicit categories. */
303 bool include_missing;
304 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
305 enum ctables_summary_function sort_func;
306 struct variable *sort_func_var;
314 /* Empty categories. */
318 struct ctables_cat_value
320 enum ctables_cat_value_type
334 double number; /* CCVT_NUMBER. */
335 char *string; /* CCVT_STRING. */
336 double range[2]; /* CCVT_RANGE. */
337 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
342 ctables_cat_value_uninit (struct ctables_cat_value *cv)
361 free (cv->subtotal_label);
366 ctables_categories_unref (struct ctables_categories *c)
371 assert (c->n_refs > 0);
375 for (size_t i = 0; i < c->n_values; i++)
376 ctables_cat_value_uninit (&c->values[i]);
378 free (c->total_label);
382 /* Chi-square test (SIGTEST). */
390 /* Pairwise comparison test (COMPARETEST). */
391 struct ctables_pairwise
393 enum { PROP, MEAN } type;
396 bool meansvariance_allcats;
398 enum { BONFERRONI = 1, BH } adjust;
422 struct ctables_var var;
424 struct ctables_summary_spec *summaries;
426 size_t allocated_summaries;
430 struct ctables_axis *subs[2];
433 struct msg_location *loc;
436 static void ctables_axis_destroy (struct ctables_axis *);
445 enum ctables_function_availability
447 CTFA_ALL, /* Any variables. */
448 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
449 CTFA_MRSETS, /* Only multiple-response sets */
452 struct ctables_summary_spec
454 enum ctables_summary_function function;
455 double percentile; /* CTSF_PTILE only. */
457 struct fmt_spec format; /* XXX extra CTABLES formats */
461 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
468 parse_col_width (struct lexer *lexer, const char *name, double *width)
470 lex_match (lexer, T_EQUALS);
471 if (lex_match_id (lexer, "DEFAULT"))
473 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
475 *width = lex_number (lexer);
485 parse_bool (struct lexer *lexer, bool *b)
487 if (lex_match_id (lexer, "NO"))
489 else if (lex_match_id (lexer, "YES"))
493 lex_error_expecting (lexer, "YES", "NO");
499 static enum ctables_function_availability
500 ctables_function_availability (enum ctables_summary_function f)
502 static enum ctables_function_availability availability[] = {
503 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
508 return availability[f];
512 parse_ctables_summary_function (struct lexer *lexer,
513 enum ctables_summary_function *f)
517 enum ctables_summary_function function;
518 struct substring name;
520 static struct pair names[] = {
521 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
522 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
525 /* The .COUNT suffix may be omitted. */
526 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
527 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
528 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
529 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
530 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
531 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
532 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
536 if (!lex_force_id (lexer))
539 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
540 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
542 *f = names[i].function;
547 lex_error (lexer, _("Expecting summary function name."));
552 ctables_axis_destroy (struct ctables_axis *axis)
560 for (size_t i = 0; i < axis->n_summaries; i++)
561 ctables_summary_spec_uninit (&axis->summaries[i]);
562 free (axis->summaries);
567 ctables_axis_destroy (axis->subs[0]);
568 ctables_axis_destroy (axis->subs[1]);
571 msg_location_destroy (axis->loc);
575 static struct ctables_axis *
576 ctables_axis_new_nonterminal (enum ctables_axis_op op,
577 struct ctables_axis *sub0,
578 struct ctables_axis *sub1,
579 struct lexer *lexer, int start_ofs)
581 struct ctables_axis *axis = xmalloc (sizeof *axis);
582 *axis = (struct ctables_axis) {
584 .subs = { sub0, sub1 },
585 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
590 struct ctables_axis_parse_ctx
593 struct dictionary *dict;
595 struct ctables_table *t;
598 static struct fmt_spec
599 ctables_summary_default_format (enum ctables_summary_function function,
600 const struct ctables_var *var)
602 static const enum ctables_format default_formats[] = {
603 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
607 switch (default_formats[function])
610 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
613 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
616 return *ctables_var_get_print_format (var);
624 ctables_summary_default_label (enum ctables_summary_function function,
627 static const char *default_labels[] = {
628 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
633 return (function == CTSF_PTILE
634 ? xasprintf (_("Percentile %.2f"), percentile)
635 : xstrdup (gettext (default_labels[function])));
639 ctables_summary_function_name (enum ctables_summary_function function)
641 static const char *names[] = {
642 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
646 return names[function];
650 add_summary_spec (struct ctables_axis *axis,
651 enum ctables_summary_function function, double percentile,
652 const char *label, const struct fmt_spec *format,
653 const struct msg_location *loc)
655 if (axis->op == CTAO_VAR)
657 if (axis->n_summaries >= axis->allocated_summaries)
658 axis->summaries = x2nrealloc (axis->summaries,
659 &axis->allocated_summaries,
660 sizeof *axis->summaries);
662 const char *function_name = ctables_summary_function_name (function);
663 const char *var_name = ctables_var_name (&axis->var);
664 switch (ctables_function_availability (function))
667 if (!axis->var.is_mrset)
669 msg_at (SE, loc, _("Summary function %s applies only to multiple "
670 "response sets."), function_name);
671 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
681 _("Summary function %s applies only to scale variables."),
683 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
693 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
694 *dst = (struct ctables_summary_spec) {
695 .function = function,
696 .percentile = percentile,
697 .label = xstrdup (label),
698 .format = (format ? *format
699 : ctables_summary_default_format (function, &axis->var)),
705 for (size_t i = 0; i < 2; i++)
706 if (!add_summary_spec (axis->subs[i], function, percentile, label,
713 static struct ctables_axis *ctables_axis_parse_stack (
714 struct ctables_axis_parse_ctx *);
717 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
718 struct ctables_var *var)
720 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
722 *var = (struct ctables_var) {
724 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
728 lex_error (lexer, _("'%s' does not name a multiple-response set "
729 "in the active file dictionary."),
730 lex_tokcstr (lexer));
738 *var = (struct ctables_var) {
740 .var = parse_variable (lexer, dict),
742 return var->var != NULL;
746 static struct ctables_axis *
747 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
749 if (lex_match (ctx->lexer, T_LPAREN))
751 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
752 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
754 ctables_axis_destroy (sub);
760 if (!lex_force_id (ctx->lexer))
763 int start_ofs = lex_ofs (ctx->lexer);
764 struct ctables_var var;
765 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
768 struct ctables_axis *axis = xmalloc (sizeof *axis);
769 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
771 /* XXX should figure out default measures by reading data */
772 axis->scale = (var.is_mrset ? false
773 : lex_match_phrase (ctx->lexer, "[S]") ? true
774 : lex_match_phrase (ctx->lexer, "[C]") ? false
775 : var_get_measure (var.var) == MEASURE_SCALE);
776 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
777 lex_ofs (ctx->lexer) - 1);
781 static struct ctables_axis *
782 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
784 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
785 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
790 int start_ofs = lex_ofs (ctx->lexer);
792 /* Parse function. */
793 enum ctables_summary_function function;
794 if (!parse_ctables_summary_function (ctx->lexer, &function))
797 /* Parse percentile. */
798 double percentile = 0;
799 if (function == CTSF_PTILE)
801 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
803 percentile = lex_number (ctx->lexer);
804 lex_get (ctx->lexer);
809 if (lex_is_string (ctx->lexer))
811 label = ss_xstrdup (lex_tokss (ctx->lexer));
812 lex_get (ctx->lexer);
815 label = ctables_summary_default_label (function, percentile);
818 struct fmt_spec format;
819 const struct fmt_spec *formatp;
820 if (lex_token (ctx->lexer) == T_ID)
822 if (!parse_format_specifier (ctx->lexer, &format)
823 || !fmt_check_output (&format)
824 || !fmt_check_type_compat (&format, VAL_NUMERIC))
834 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
835 lex_ofs (ctx->lexer) - 1);
836 add_summary_spec (sub, function, percentile, label, formatp, loc);
838 msg_location_destroy (loc);
840 lex_match (ctx->lexer, T_COMMA);
842 while (!lex_match (ctx->lexer, T_RBRACK));
847 ctables_axis_destroy (sub);
851 static const struct ctables_axis *
852 find_scale (const struct ctables_axis *axis)
856 else if (axis->op == CTAO_VAR)
860 assert (!axis->var.is_mrset);
868 for (size_t i = 0; i < 2; i++)
870 const struct ctables_axis *scale = find_scale (axis->subs[i]);
878 static const struct ctables_axis *
879 find_categorical_summary_spec (const struct ctables_axis *axis)
883 else if (axis->op == CTAO_VAR)
884 return !axis->scale && axis->n_summaries ? axis : NULL;
887 for (size_t i = 0; i < 2; i++)
889 const struct ctables_axis *sum
890 = find_categorical_summary_spec (axis->subs[i]);
898 static struct ctables_axis *
899 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
901 int start_ofs = lex_ofs (ctx->lexer);
902 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
906 while (lex_match (ctx->lexer, T_GT))
908 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
912 struct ctables_axis *nest = ctables_axis_new_nonterminal (
913 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
915 const struct ctables_axis *outer_scale = find_scale (lhs);
916 const struct ctables_axis *inner_scale = find_scale (rhs);
917 if (outer_scale && inner_scale)
919 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
920 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
921 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
922 ctables_axis_destroy (nest);
926 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
929 msg_at (SE, nest->loc,
930 _("Summaries may only be requested for categorical variables "
931 "at the innermost nesting level."));
932 msg_at (SN, outer_sum->loc,
933 _("This outer categorical variable has a summary."));
934 ctables_axis_destroy (nest);
944 static struct ctables_axis *
945 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
947 int start_ofs = lex_ofs (ctx->lexer);
948 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
952 while (lex_match (ctx->lexer, T_PLUS))
954 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
958 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
959 ctx->lexer, start_ofs);
966 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
967 struct ctables *ct, struct ctables_table *t,
968 enum pivot_axis_type a)
970 if (lex_token (lexer) == T_BY
971 || lex_token (lexer) == T_SLASH
972 || lex_token (lexer) == T_ENDCMD)
975 struct ctables_axis_parse_ctx ctx = {
981 t->axes[a] = ctables_axis_parse_stack (&ctx);
982 return t->axes[a] != NULL;
986 ctables_chisq_destroy (struct ctables_chisq *chisq)
992 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
998 ctables_table_destroy (struct ctables_table *t)
1003 for (size_t i = 0; i < t->n_categories; i++)
1004 ctables_categories_unref (t->categories[i]);
1005 free (t->categories);
1007 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1008 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1009 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1013 ctables_chisq_destroy (t->chisq);
1014 ctables_pairwise_destroy (t->pairwise);
1019 ctables_destroy (struct ctables *ct)
1024 pivot_table_look_unref (ct->look);
1028 for (size_t i = 0; i < ct->n_tables; i++)
1029 ctables_table_destroy (ct->tables[i]);
1034 static struct ctables_cat_value
1035 ccvt_range (double low, double high)
1037 return (struct ctables_cat_value) {
1039 .range = { low, high }
1044 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1045 struct ctables_table *t)
1047 if (!lex_match_id (lexer, "VARIABLES"))
1049 lex_match (lexer, T_EQUALS);
1051 struct variable **vars;
1053 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1056 struct ctables_categories *c = xmalloc (sizeof *c);
1057 *c = (struct ctables_categories) { .n_refs = n_vars };
1058 for (size_t i = 0; i < n_vars; i++)
1060 struct ctables_categories **cp
1061 = &t->categories[var_get_dict_index (vars[i])];
1062 ctables_categories_unref (*cp);
1067 if (lex_match (lexer, T_LBRACK))
1069 size_t allocated_values = 0;
1072 if (c->n_values >= allocated_values)
1073 c->values = x2nrealloc (c->values, &allocated_values,
1076 struct ctables_cat_value *v = &c->values[c->n_values];
1077 if (lex_match_id (lexer, "OTHERNM"))
1078 v->type = CCVT_OTHERNM;
1079 else if (lex_match_id (lexer, "MISSING"))
1080 v->type = CCVT_MISSING;
1081 else if (lex_match_id (lexer, "SUBTOTAL"))
1082 *v = (struct ctables_cat_value)
1083 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1084 else if (lex_match_id (lexer, "HSUBTOTAL"))
1085 *v = (struct ctables_cat_value)
1086 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1087 else if (lex_match_id (lexer, "LO"))
1089 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1091 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1094 else if (lex_is_number (lexer))
1096 double number = lex_number (lexer);
1098 if (lex_match_id (lexer, "THRU"))
1100 v->type = CCVT_RANGE;
1101 v->range[0] = number;
1102 if (lex_match_id (lexer, "HI"))
1103 *v = ccvt_range (number, DBL_MAX);
1106 if (!lex_force_num (lexer))
1108 *v = ccvt_range (number, lex_number (lexer));
1113 *v = (struct ctables_cat_value) {
1114 .type = CCVT_NUMBER,
1118 else if (lex_is_string (lexer))
1120 *v = (struct ctables_cat_value) {
1121 .type = CCVT_STRING,
1122 .string = ss_xstrdup (lex_tokss (lexer)),
1128 lex_error (lexer, NULL);
1132 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1133 && lex_match (lexer, T_EQUALS))
1135 if (!lex_force_string (lexer))
1138 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1143 lex_match (lexer, T_COMMA);
1145 while (!lex_match (lexer, T_RBRACK));
1148 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1150 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1152 lex_match (lexer, T_EQUALS);
1153 if (lex_match_id (lexer, "A"))
1154 c->sort_ascending = true;
1155 else if (lex_match_id (lexer, "D"))
1156 c->sort_ascending = false;
1159 lex_error_expecting (lexer, "A", "D");
1163 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1165 lex_match (lexer, T_EQUALS);
1166 if (lex_match_id (lexer, "VALUE"))
1167 c->key = CTCS_VALUE;
1168 else if (lex_match_id (lexer, "LABEL"))
1169 c->key = CTCS_LABEL;
1172 c->key = CTCS_FUNCTION;
1173 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1176 if (lex_match (lexer, T_LPAREN))
1178 c->sort_func_var = parse_variable (lexer, dict);
1179 if (!c->sort_func_var)
1182 if (c->sort_func == CTSF_PTILE)
1184 lex_match (lexer, T_COMMA);
1185 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1187 c->percentile = lex_number (lexer);
1191 if (!lex_force_match (lexer, T_RPAREN))
1194 else if (ctables_function_availability (c->sort_func)
1197 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1202 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1204 lex_match (lexer, T_EQUALS);
1205 if (lex_match_id (lexer, "INCLUDE"))
1206 c->include_missing = true;
1207 else if (lex_match_id (lexer, "EXCLUDE"))
1208 c->include_missing = false;
1211 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1215 else if (lex_match_id (lexer, "TOTAL"))
1217 lex_match (lexer, T_EQUALS);
1218 if (!parse_bool (lexer, &c->show_totals))
1221 else if (lex_match_id (lexer, "LABEL"))
1223 lex_match (lexer, T_EQUALS);
1224 if (!lex_force_string (lexer))
1226 free (c->total_label);
1227 c->total_label = ss_xstrdup (lex_tokss (lexer));
1230 else if (lex_match_id (lexer, "POSITION"))
1232 lex_match (lexer, T_EQUALS);
1233 if (lex_match_id (lexer, "BEFORE"))
1234 c->totals_before = true;
1235 else if (lex_match_id (lexer, "AFTER"))
1236 c->totals_before = false;
1239 lex_error_expecting (lexer, "BEFORE", "AFTER");
1243 else if (lex_match_id (lexer, "EMPTY"))
1245 lex_match (lexer, T_EQUALS);
1246 if (lex_match_id (lexer, "INCLUDE"))
1247 c->show_empty = true;
1248 else if (lex_match_id (lexer, "EXCLUDE"))
1249 c->show_empty = false;
1252 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1259 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1260 "TOTAL", "LABEL", "POSITION", "EMPTY");
1262 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1270 var_array_uninit (struct var_array *va)
1277 var_array2_uninit (struct var_array2 *vaa)
1281 for (size_t i = 0; i < vaa->n; i++)
1282 var_array_uninit (&vaa->vas[i]);
1287 static struct var_array2
1288 nest_fts (struct var_array2 va0, struct var_array2 va1)
1295 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1296 for (size_t i = 0; i < va0.n; i++)
1297 for (size_t j = 0; j < va1.n; j++)
1299 const struct var_array *a = &va0.vas[i];
1300 const struct var_array *b = &va1.vas[j];
1302 size_t allocate = a->n + b->n;
1303 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1304 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1306 for (size_t k = 0; k < a->n; k++)
1307 vars[n++] = a->vars[k];
1308 for (size_t k = 0; k < b->n; k++)
1309 vars[n++] = b->vars[k];
1310 assert (n == allocate);
1312 const struct var_array *summary_src;
1313 if (!a->summary_var)
1315 else if (!b->summary_var)
1319 vaa.vas[vaa.n++] = (struct var_array) {
1322 .summaries = summary_src->summaries,
1323 .n_summaries = summary_src->n_summaries,
1324 .summary_var = summary_src->summary_var,
1327 var_array2_uninit (&va0);
1328 var_array2_uninit (&va1);
1332 static struct var_array2
1333 stack_fts (struct var_array2 va0, struct var_array2 va1)
1335 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1336 for (size_t i = 0; i < va0.n; i++)
1337 vaa.vas[vaa.n++] = va0.vas[i];
1338 for (size_t i = 0; i < va1.n; i++)
1339 vaa.vas[vaa.n++] = va1.vas[i];
1340 assert (vaa.n == va0.n + va1.n);
1346 static struct var_array2
1347 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1350 return (struct var_array2) { .n = 0 };
1355 assert (!a->var.is_mrset);
1356 struct var_array *va = xmalloc (sizeof *va);
1358 *va = (struct var_array) { .n = 0 };
1361 struct variable **vars = xmalloc (sizeof *vars);
1363 enum pivot_axis_type *axes = xmalloc (sizeof *axes);
1365 *va = (struct var_array) { .vars = vars, .n = 1 };
1367 if (a->n_summaries || a->scale)
1369 va->summaries = a->summaries;
1370 va->n_summaries = a->n_summaries;
1371 va->summary_var = a->var.var;
1373 return (struct var_array2) { .vas = va, .n = 1 };
1376 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1377 enumerate_fts (axis_type, a->subs[1]));
1380 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1381 enumerate_fts (axis_type, a->subs[1]));
1387 union ctables_summary
1389 /* COUNT, VALIDN, TOTALN. */
1396 /* MINIMUM, MAXIMUM, RANGE. */
1403 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1404 struct moments1 *moments;
1406 /* XXX percentiles, median, mode, multiple response */
1410 ctables_summary_init (union ctables_summary *s,
1411 const struct ctables_summary_spec *ss)
1413 switch (ss->function)
1417 case CTSF_ROWPCT_COUNT:
1418 case CTSF_COLPCT_COUNT:
1419 case CTSF_TABLEPCT_COUNT:
1420 case CTSF_SUBTABLEPCT_COUNT:
1421 case CTSF_LAYERPCT_COUNT:
1422 case CTSF_LAYERROWPCT_COUNT:
1423 case CTSF_LAYERCOLPCT_COUNT:
1424 case CTSF_ROWPCT_VALIDN:
1425 case CTSF_COLPCT_VALIDN:
1426 case CTSF_TABLEPCT_VALIDN:
1427 case CTSF_SUBTABLEPCT_VALIDN:
1428 case CTSF_LAYERPCT_VALIDN:
1429 case CTSF_LAYERROWPCT_VALIDN:
1430 case CTSF_LAYERCOLPCT_VALIDN:
1431 case CTSF_ROWPCT_TOTALN:
1432 case CTSF_COLPCT_TOTALN:
1433 case CTSF_TABLEPCT_TOTALN:
1434 case CTSF_SUBTABLEPCT_TOTALN:
1435 case CTSF_LAYERPCT_TOTALN:
1436 case CTSF_LAYERROWPCT_TOTALN:
1437 case CTSF_LAYERCOLPCT_TOTALN:
1442 s->missing = s->valid = 0;
1448 s->min = s->max = SYSMIS;
1456 case CTSF_ROWPCT_SUM:
1457 case CTSF_COLPCT_SUM:
1458 case CTSF_TABLEPCT_SUM:
1459 case CTSF_SUBTABLEPCT_SUM:
1460 case CTSF_LAYERPCT_SUM:
1461 case CTSF_LAYERROWPCT_SUM:
1462 case CTSF_LAYERCOLPCT_SUM:
1463 s->moments = moments1_create (MOMENT_VARIANCE);
1472 case CTSF_RESPONSES:
1473 case CTSF_ROWPCT_RESPONSES:
1474 case CTSF_COLPCT_RESPONSES:
1475 case CTSF_TABLEPCT_RESPONSES:
1476 case CTSF_SUBTABLEPCT_RESPONSES:
1477 case CTSF_LAYERPCT_RESPONSES:
1478 case CTSF_LAYERROWPCT_RESPONSES:
1479 case CTSF_LAYERCOLPCT_RESPONSES:
1480 case CTSF_ROWPCT_RESPONSES_COUNT:
1481 case CTSF_COLPCT_RESPONSES_COUNT:
1482 case CTSF_TABLEPCT_RESPONSES_COUNT:
1483 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1484 case CTSF_LAYERPCT_RESPONSES_COUNT:
1485 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1486 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1487 case CTSF_ROWPCT_COUNT_RESPONSES:
1488 case CTSF_COLPCT_COUNT_RESPONSES:
1489 case CTSF_TABLEPCT_COUNT_RESPONSES:
1490 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1491 case CTSF_LAYERPCT_COUNT_RESPONSES:
1492 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1493 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1499 ctables_summary_uninit (union ctables_summary *s,
1500 const struct ctables_summary_spec *ss)
1502 switch (ss->function)
1506 case CTSF_ROWPCT_COUNT:
1507 case CTSF_COLPCT_COUNT:
1508 case CTSF_TABLEPCT_COUNT:
1509 case CTSF_SUBTABLEPCT_COUNT:
1510 case CTSF_LAYERPCT_COUNT:
1511 case CTSF_LAYERROWPCT_COUNT:
1512 case CTSF_LAYERCOLPCT_COUNT:
1513 case CTSF_ROWPCT_VALIDN:
1514 case CTSF_COLPCT_VALIDN:
1515 case CTSF_TABLEPCT_VALIDN:
1516 case CTSF_SUBTABLEPCT_VALIDN:
1517 case CTSF_LAYERPCT_VALIDN:
1518 case CTSF_LAYERROWPCT_VALIDN:
1519 case CTSF_LAYERCOLPCT_VALIDN:
1520 case CTSF_ROWPCT_TOTALN:
1521 case CTSF_COLPCT_TOTALN:
1522 case CTSF_TABLEPCT_TOTALN:
1523 case CTSF_SUBTABLEPCT_TOTALN:
1524 case CTSF_LAYERPCT_TOTALN:
1525 case CTSF_LAYERROWPCT_TOTALN:
1526 case CTSF_LAYERCOLPCT_TOTALN:
1543 case CTSF_ROWPCT_SUM:
1544 case CTSF_COLPCT_SUM:
1545 case CTSF_TABLEPCT_SUM:
1546 case CTSF_SUBTABLEPCT_SUM:
1547 case CTSF_LAYERPCT_SUM:
1548 case CTSF_LAYERROWPCT_SUM:
1549 case CTSF_LAYERCOLPCT_SUM:
1550 moments1_destroy (s->moments);
1559 case CTSF_RESPONSES:
1560 case CTSF_ROWPCT_RESPONSES:
1561 case CTSF_COLPCT_RESPONSES:
1562 case CTSF_TABLEPCT_RESPONSES:
1563 case CTSF_SUBTABLEPCT_RESPONSES:
1564 case CTSF_LAYERPCT_RESPONSES:
1565 case CTSF_LAYERROWPCT_RESPONSES:
1566 case CTSF_LAYERCOLPCT_RESPONSES:
1567 case CTSF_ROWPCT_RESPONSES_COUNT:
1568 case CTSF_COLPCT_RESPONSES_COUNT:
1569 case CTSF_TABLEPCT_RESPONSES_COUNT:
1570 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1571 case CTSF_LAYERPCT_RESPONSES_COUNT:
1572 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1573 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1574 case CTSF_ROWPCT_COUNT_RESPONSES:
1575 case CTSF_COLPCT_COUNT_RESPONSES:
1576 case CTSF_TABLEPCT_COUNT_RESPONSES:
1577 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1578 case CTSF_LAYERPCT_COUNT_RESPONSES:
1579 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1580 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1586 ctables_summary_add (union ctables_summary *s,
1587 const struct ctables_summary_spec *ss,
1588 const struct variable *var, const union value *value,
1591 switch (ss->function)
1595 case CTSF_ROWPCT_COUNT:
1596 case CTSF_COLPCT_COUNT:
1597 case CTSF_TABLEPCT_COUNT:
1598 case CTSF_SUBTABLEPCT_COUNT:
1599 case CTSF_LAYERPCT_COUNT:
1600 case CTSF_LAYERROWPCT_COUNT:
1601 case CTSF_LAYERCOLPCT_COUNT:
1602 case CTSF_ROWPCT_VALIDN:
1603 case CTSF_COLPCT_VALIDN:
1604 case CTSF_TABLEPCT_VALIDN:
1605 case CTSF_SUBTABLEPCT_VALIDN:
1606 case CTSF_LAYERPCT_VALIDN:
1607 case CTSF_LAYERROWPCT_VALIDN:
1608 case CTSF_LAYERCOLPCT_VALIDN:
1609 case CTSF_ROWPCT_TOTALN:
1610 case CTSF_COLPCT_TOTALN:
1611 case CTSF_TABLEPCT_TOTALN:
1612 case CTSF_SUBTABLEPCT_TOTALN:
1613 case CTSF_LAYERPCT_TOTALN:
1614 case CTSF_LAYERROWPCT_TOTALN:
1615 case CTSF_LAYERCOLPCT_TOTALN:
1620 if (var_is_value_missing (var, value))
1621 s->missing += weight;
1629 if (!var_is_value_missing (var, value))
1631 assert (!var_is_alpha (var)); /* XXX? */
1632 if (s->min == SYSMIS || value->f < s->min)
1634 if (s->max == SYSMIS || value->f > s->max)
1644 case CTSF_ROWPCT_SUM:
1645 case CTSF_COLPCT_SUM:
1646 case CTSF_TABLEPCT_SUM:
1647 case CTSF_SUBTABLEPCT_SUM:
1648 case CTSF_LAYERPCT_SUM:
1649 case CTSF_LAYERROWPCT_SUM:
1650 case CTSF_LAYERCOLPCT_SUM:
1651 moments1_add (s->moments, value->f, weight);
1660 case CTSF_RESPONSES:
1661 case CTSF_ROWPCT_RESPONSES:
1662 case CTSF_COLPCT_RESPONSES:
1663 case CTSF_TABLEPCT_RESPONSES:
1664 case CTSF_SUBTABLEPCT_RESPONSES:
1665 case CTSF_LAYERPCT_RESPONSES:
1666 case CTSF_LAYERROWPCT_RESPONSES:
1667 case CTSF_LAYERCOLPCT_RESPONSES:
1668 case CTSF_ROWPCT_RESPONSES_COUNT:
1669 case CTSF_COLPCT_RESPONSES_COUNT:
1670 case CTSF_TABLEPCT_RESPONSES_COUNT:
1671 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1672 case CTSF_LAYERPCT_RESPONSES_COUNT:
1673 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1674 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1675 case CTSF_ROWPCT_COUNT_RESPONSES:
1676 case CTSF_COLPCT_COUNT_RESPONSES:
1677 case CTSF_TABLEPCT_COUNT_RESPONSES:
1678 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1679 case CTSF_LAYERPCT_COUNT_RESPONSES:
1680 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1681 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1687 ctables_summary_value (union ctables_summary *s,
1688 const struct ctables_summary_spec *ss)
1690 switch (ss->function)
1696 case CTSF_ROWPCT_COUNT:
1697 case CTSF_COLPCT_COUNT:
1698 case CTSF_TABLEPCT_COUNT:
1699 case CTSF_SUBTABLEPCT_COUNT:
1700 case CTSF_LAYERPCT_COUNT:
1701 case CTSF_LAYERROWPCT_COUNT:
1702 case CTSF_LAYERCOLPCT_COUNT:
1703 case CTSF_ROWPCT_VALIDN:
1704 case CTSF_COLPCT_VALIDN:
1705 case CTSF_TABLEPCT_VALIDN:
1706 case CTSF_SUBTABLEPCT_VALIDN:
1707 case CTSF_LAYERPCT_VALIDN:
1708 case CTSF_LAYERROWPCT_VALIDN:
1709 case CTSF_LAYERCOLPCT_VALIDN:
1710 case CTSF_ROWPCT_TOTALN:
1711 case CTSF_COLPCT_TOTALN:
1712 case CTSF_TABLEPCT_TOTALN:
1713 case CTSF_SUBTABLEPCT_TOTALN:
1714 case CTSF_LAYERPCT_TOTALN:
1715 case CTSF_LAYERROWPCT_TOTALN:
1716 case CTSF_LAYERCOLPCT_TOTALN:
1721 return s->valid + s->missing;
1734 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1739 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1745 double weight, variance;
1746 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1747 return calc_semean (variance, weight);
1753 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1754 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1759 double weight, mean;
1760 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1761 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1767 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1771 case CTSF_ROWPCT_SUM:
1772 case CTSF_COLPCT_SUM:
1773 case CTSF_TABLEPCT_SUM:
1774 case CTSF_SUBTABLEPCT_SUM:
1775 case CTSF_LAYERPCT_SUM:
1776 case CTSF_LAYERROWPCT_SUM:
1777 case CTSF_LAYERCOLPCT_SUM:
1786 case CTSF_RESPONSES:
1787 case CTSF_ROWPCT_RESPONSES:
1788 case CTSF_COLPCT_RESPONSES:
1789 case CTSF_TABLEPCT_RESPONSES:
1790 case CTSF_SUBTABLEPCT_RESPONSES:
1791 case CTSF_LAYERPCT_RESPONSES:
1792 case CTSF_LAYERROWPCT_RESPONSES:
1793 case CTSF_LAYERCOLPCT_RESPONSES:
1794 case CTSF_ROWPCT_RESPONSES_COUNT:
1795 case CTSF_COLPCT_RESPONSES_COUNT:
1796 case CTSF_TABLEPCT_RESPONSES_COUNT:
1797 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1798 case CTSF_LAYERPCT_RESPONSES_COUNT:
1799 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1800 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1801 case CTSF_ROWPCT_COUNT_RESPONSES:
1802 case CTSF_COLPCT_COUNT_RESPONSES:
1803 case CTSF_TABLEPCT_COUNT_RESPONSES:
1804 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1805 case CTSF_LAYERPCT_COUNT_RESPONSES:
1806 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1807 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1816 struct hmap_node node; /* Element in hash table. */
1821 union value *values;
1826 union ctables_summary *summaries;
1830 static struct ctables_freq *
1831 ctables_freq_create (struct ctables_freqtab *ft)
1833 struct ctables_freq *f = xmalloc (sizeof *f + ft->vars.n * sizeof *f->values);
1834 f->summaries = xmalloc (ft->n_summaries * sizeof *f->summaries);
1835 for (size_t i = 0; i < ft->n_summaries; i++)
1836 ctables_summary_init (&f->summaries[i], &ft->summaries[i]);
1841 ctables_freq_add (struct ctables_freqtab *ft, struct ctables_freq *f,
1842 const struct variable *var, const union value *value,
1845 for (size_t i = 0; i < ft->n_summaries; i++)
1846 ctables_summary_add (&f->summaries[i], &ft->summaries[i],
1847 var, value, weight);
1851 struct ctables_freq_sort_aux
1853 const struct ctables_table *t;
1854 enum pivot_axis_type a;
1858 ctables_freq_compare_3way (const void *a_, const void *b_, const void *aux_)
1860 const struct ctables_freq_sort_aux *aux = aux_;
1861 struct ctables_freq *const *ap = a_;
1862 struct ctables_freq *const *bp = b_;
1863 const struct ctables_freq *a = *ap;
1864 const struct ctables_freq *b = *bp;
1866 size_t a_idx = a->axes[aux->a].vaa_idx;
1867 size_t b_idx = b->axes[aux->a].vaa_idx;
1869 return a_idx < b_idx ? -1 : 1;
1871 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1872 for (size_t i = 0; i < va->n; i++)
1874 int cmp = value_compare_3way (&a->axes[aux->a].values[i],
1875 &b->axes[aux->a].values[i],
1876 var_get_width (va->vars[i]));
1886 For each ctables_table:
1887 For each combination of row vars:
1888 For each combination of column vars:
1889 For each combination of layer vars:
1891 Make a table of row values:
1892 Sort entries by row values
1893 Assign a 0-based index to each actual value
1894 Construct a dimension
1895 Make a table of column values
1896 Make a table of layer values
1898 Fill the table entry using the indexes from before.
1902 ctables_freqtab_insert (struct ctables_table *t,
1903 const struct ccase *c,
1904 size_t ir, size_t ic, size_t il,
1907 size_t ix[PIVOT_N_AXES] = {
1908 [PIVOT_AXIS_ROW] = ir,
1909 [PIVOT_AXIS_COLUMN] = ic,
1910 [PIVOT_AXIS_LAYER] = il,
1912 const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
1915 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1917 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1918 hash = hash_int (ix[a], hash);
1919 for (size_t i = 0; i < va->n; i++)
1920 hash = value_hash (case_data (c, va->vars[i]),
1921 var_get_width (va->vars[i]), hash);
1924 struct ctables_freq *f;
1925 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &t->ft)
1927 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1929 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1930 if (f->axes[a].vaa_idx != ix[a])
1932 for (size_t i = 0; i < va->n; i++)
1933 if (!value_equal (case_data (c, va->vars[i]),
1934 &f->axes[a].values[i],
1935 var_get_width (va->vars[i])))
1944 f = xmalloc (sizeof *f);
1945 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1947 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1948 f->axes[a].vaa_idx = ix[a];
1949 f->axes[a].values = (va->n
1950 ? xnmalloc (va->n, sizeof *f->axes[a].values)
1952 for (size_t i = 0; i < va->n; i++)
1953 value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
1954 var_get_width (va->vars[i]));
1956 f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
1957 for (size_t i = 0; i < ss->n_summaries; i++)
1958 ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
1959 hmap_insert (&t->ft, &f->node, hash);
1962 for (size_t i = 0; i < ss->n_summaries; i++)
1963 ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
1964 case_data (c, ss->summary_var), weight);
1968 ctables_execute (struct dataset *ds, struct ctables *ct)
1970 for (size_t i = 0; i < ct->n_tables; i++)
1972 struct ctables_table *t = ct->tables[i];
1973 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1975 t->vaas[a] = enumerate_fts (a, t->axes[a]);
1978 struct var_array *va = xmalloc (sizeof *va);
1979 *va = (struct var_array) { .n = 0 };
1980 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
1983 for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
1985 struct var_array *va = &t->vaas[t->summary_axis].vas[i];
1986 if (!va->n_summaries)
1988 va->summaries = xmalloc (sizeof *va->summaries);
1989 va->n_summaries = 1;
1991 enum ctables_summary_function function
1992 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
1993 struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
1995 *va->summaries = (struct ctables_summary_spec) {
1996 .function = function,
1997 .format = ctables_summary_default_format (function, &var),
1998 .label = ctables_summary_default_label (function, 0),
2000 if (!va->summary_var)
2001 va->summary_var = va->vars[0];
2006 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2009 bool warn_on_invalid = true;
2010 for (struct ccase *c = casereader_read (input); c;
2011 case_unref (c), c = casereader_read (input))
2013 double weight = dict_get_case_weight (dataset_dict (ds), c,
2016 for (size_t i = 0; i < ct->n_tables; i++)
2018 struct ctables_table *t = ct->tables[i];
2020 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2021 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2022 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2023 ctables_freqtab_insert (t, c, ir, ic, il, weight);
2026 casereader_destroy (input);
2028 for (size_t i = 0; i < ct->n_tables; i++)
2030 struct ctables_table *t = ct->tables[i];
2032 struct pivot_table *pt = pivot_table_create (N_("Custom Tables"));
2033 pivot_table_set_look (pt, ct->look);
2034 struct pivot_dimension *d[PIVOT_N_AXES];
2035 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2037 static const char *names[] = {
2038 [PIVOT_AXIS_ROW] = N_("Rows"),
2039 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2040 [PIVOT_AXIS_LAYER] = N_("Layers"),
2042 d[a] = (t->axes[a] || a == t->summary_axis
2043 ? pivot_dimension_create (pt, a, names[a])
2048 assert (t->axes[a]);
2050 struct ctables_freq **sorted = xnmalloc (t->ft.count, sizeof *sorted);
2052 struct ctables_freq *f;
2054 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2056 assert (n == t->ft.count);
2058 struct ctables_freq_sort_aux aux = { .t = t, .a = a };
2059 sort (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
2061 size_t max_depth = 0;
2062 for (size_t j = 0; j < t->vaas[a].n; j++)
2063 if (t->vaas[a].vas[j].n > max_depth)
2064 max_depth = t->vaas[a].vas[j].n;
2066 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2067 struct pivot_category *top = NULL;
2069 for (size_t j = 0; j < n; j++)
2071 struct ctables_freq *f = sorted[j];
2072 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2074 size_t n_common = 0;
2075 bool new_subtable = false;
2078 struct ctables_freq *prev = sorted[j - 1];
2079 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2081 for (; n_common < va->n; n_common++)
2082 if (!value_equal (&prev->axes[a].values[n_common],
2083 &f->axes[a].values[n_common],
2084 var_get_type (va->vars[n_common])))
2088 new_subtable = true;
2091 new_subtable = true;
2095 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2097 if (vlabel != CTVL_NONE)
2098 top = pivot_category_create_group__ (
2099 top, pivot_value_new_variable (va->vars[0]));
2101 if (n_common == va->n)
2103 f->axes[a].leaf = prev_leaf;
2107 for (size_t k = n_common; k < va->n; k++)
2109 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2111 struct pivot_value *label = pivot_value_new_var_value (
2112 va->vars[k], &f->axes[a].values[k]);
2116 if (a == t->summary_axis)
2118 parent = pivot_category_create_group__ (parent, label);
2119 for (size_t m = 0; m < va->n_summaries; m++)
2121 int leaf = pivot_category_create_leaf (
2122 parent, pivot_value_new_text (va->summaries[m].label));
2128 prev_leaf = pivot_category_create_leaf (parent, label);
2132 parent = pivot_category_create_group__ (parent, label);
2134 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2135 if (vlabel != CTVL_NONE)
2136 parent = pivot_category_create_group__ (
2137 parent, pivot_value_new_variable (va->vars[k + 1]));
2141 f->axes[a].leaf = prev_leaf;
2146 struct ctables_freq *f;
2147 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2149 const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2150 for (size_t j = 0; j < ss->n_summaries; j++)
2153 size_t n_dindexes = 0;
2155 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2158 int leaf = f->axes[a].leaf;
2159 if (a == t->summary_axis)
2161 dindexes[n_dindexes++] = leaf;
2164 double value = ctables_summary_value (&f->summaries[j], &ss->summaries[j]);
2165 pivot_table_put (pt, dindexes, n_dindexes,
2166 pivot_value_new_number (value));
2170 pivot_table_submit (pt);
2174 for (size_t i = 0; i < ct->n_tables; i++)
2176 struct ctables_table *t = ct->tables[i];
2178 for (size_t j = 0; j < t->n_fts; j++)
2180 struct ctables_freqtab *ft = t->fts[j];
2181 struct ctables_freq *f, *next;
2182 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2184 hmap_delete (&ft->data, &f->node);
2185 for (size_t k = 0; k < ft->n_summaries; k++)
2186 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2187 free (f->summaries);
2188 for (size_t k = 0; k < ft->vars.n; k++)
2190 const struct variable *var = ft->vars.vars[k];
2191 value_destroy (&f->values[k], var_get_width (var));
2195 hmap_destroy (&ft->data);
2196 var_array_uninit (&ft->vars);
2203 return proc_commit (ds);
2207 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2209 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2210 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2211 enum settings_value_show tvars = settings_get_show_variables ();
2212 for (size_t i = 0; i < n_vars; i++)
2213 vlabels[i] = (enum ctables_vlabel) tvars;
2215 struct ctables *ct = xmalloc (sizeof *ct);
2216 *ct = (struct ctables) {
2217 .look = pivot_table_look_unshare (pivot_table_look_ref (
2218 pivot_table_look_get_default ())),
2220 .hide_threshold = 5,
2222 ct->look->omit_empty = false;
2224 if (!lex_force_match (lexer, T_SLASH))
2227 while (!lex_match_id (lexer, "TABLE"))
2229 if (lex_match_id (lexer, "FORMAT"))
2231 double widths[2] = { SYSMIS, SYSMIS };
2232 double units_per_inch = 72.0;
2234 while (lex_token (lexer) != T_SLASH)
2236 if (lex_match_id (lexer, "MINCOLWIDTH"))
2238 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2241 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2243 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2246 else if (lex_match_id (lexer, "UNITS"))
2248 lex_match (lexer, T_EQUALS);
2249 if (lex_match_id (lexer, "POINTS"))
2250 units_per_inch = 72.0;
2251 else if (lex_match_id (lexer, "INCHES"))
2252 units_per_inch = 1.0;
2253 else if (lex_match_id (lexer, "CM"))
2254 units_per_inch = 2.54;
2257 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2261 else if (lex_match_id (lexer, "EMPTY"))
2266 lex_match (lexer, T_EQUALS);
2267 if (lex_match_id (lexer, "ZERO"))
2269 /* Nothing to do. */
2271 else if (lex_match_id (lexer, "BLANK"))
2272 ct->zero = xstrdup ("");
2273 else if (lex_force_string (lexer))
2275 ct->zero = ss_xstrdup (lex_tokss (lexer));
2281 else if (lex_match_id (lexer, "MISSING"))
2283 lex_match (lexer, T_EQUALS);
2284 if (!lex_force_string (lexer))
2288 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2289 ? ss_xstrdup (lex_tokss (lexer))
2295 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2296 "UNITS", "EMPTY", "MISSING");
2301 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2302 && widths[0] > widths[1])
2304 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2308 for (size_t i = 0; i < 2; i++)
2309 if (widths[i] != SYSMIS)
2311 int *wr = ct->look->width_ranges[TABLE_HORZ];
2312 wr[i] = widths[i] / units_per_inch * 96.0;
2317 else if (lex_match_id (lexer, "VLABELS"))
2319 if (!lex_force_match_id (lexer, "VARIABLES"))
2321 lex_match (lexer, T_EQUALS);
2323 struct variable **vars;
2325 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2329 if (!lex_force_match_id (lexer, "DISPLAY"))
2334 lex_match (lexer, T_EQUALS);
2336 enum ctables_vlabel vlabel;
2337 if (lex_match_id (lexer, "DEFAULT"))
2338 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2339 else if (lex_match_id (lexer, "NAME"))
2341 else if (lex_match_id (lexer, "LABEL"))
2342 vlabel = CTVL_LABEL;
2343 else if (lex_match_id (lexer, "BOTH"))
2345 else if (lex_match_id (lexer, "NONE"))
2349 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2355 for (size_t i = 0; i < n_vars; i++)
2356 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2359 else if (lex_match_id (lexer, "MRSETS"))
2361 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2363 lex_match (lexer, T_EQUALS);
2364 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2367 else if (lex_match_id (lexer, "SMISSING"))
2369 if (lex_match_id (lexer, "VARIABLE"))
2370 ct->smissing_listwise = false;
2371 else if (lex_match_id (lexer, "LISTWISE"))
2372 ct->smissing_listwise = true;
2375 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2380 else if (lex_match_id (lexer, "WEIGHT"))
2382 if (!lex_force_match_id (lexer, "VARIABLE"))
2384 lex_match (lexer, T_EQUALS);
2385 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2386 if (!ct->base_weight)
2389 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2391 if (!lex_force_match_id (lexer, "COUNT"))
2393 lex_match (lexer, T_EQUALS);
2394 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2396 ct->hide_threshold = lex_integer (lexer);
2401 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2402 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2403 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2407 if (!lex_force_match (lexer, T_SLASH))
2411 size_t allocated_tables = 0;
2414 if (ct->n_tables >= allocated_tables)
2415 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2416 sizeof *ct->tables);
2418 struct ctables_table *t = xmalloc (sizeof *t);
2419 *t = (struct ctables_table) {
2420 .ft = HMAP_INITIALIZER (t->ft),
2421 .slabels_position = PIVOT_AXIS_COLUMN,
2422 .slabels_visible = true,
2423 .row_labels = CTLP_NORMAL,
2424 .col_labels = CTLP_NORMAL,
2425 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2426 sizeof *t->categories),
2427 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2430 ct->tables[ct->n_tables++] = t;
2432 lex_match (lexer, T_EQUALS);
2433 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2435 if (lex_match (lexer, T_BY))
2437 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2438 ct, t, PIVOT_AXIS_COLUMN))
2441 if (lex_match (lexer, T_BY))
2443 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2444 ct, t, PIVOT_AXIS_LAYER))
2449 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2450 && !t->axes[PIVOT_AXIS_LAYER])
2452 lex_error (lexer, _("At least one variable must be specified."));
2456 const struct ctables_axis *scales[PIVOT_N_AXES];
2457 size_t n_scales = 0;
2458 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2460 scales[a] = find_scale (t->axes[a]);
2466 msg (SE, _("Scale variables may appear only on one axis."));
2467 if (scales[PIVOT_AXIS_ROW])
2468 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2469 _("This scale variable appears on the rows axis."));
2470 if (scales[PIVOT_AXIS_COLUMN])
2471 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2472 _("This scale variable appears on the columns axis."));
2473 if (scales[PIVOT_AXIS_LAYER])
2474 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2475 _("This scale variable appears on the layer axis."));
2479 const struct ctables_axis *summaries[PIVOT_N_AXES];
2480 size_t n_summaries = 0;
2481 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2483 summaries[a] = (scales[a]
2485 : find_categorical_summary_spec (t->axes[a]));
2489 if (n_summaries > 1)
2491 msg (SE, _("Summaries may appear only on one axis."));
2492 if (summaries[PIVOT_AXIS_ROW])
2493 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2494 _("This variable on the rows axis has a summary."));
2495 if (summaries[PIVOT_AXIS_COLUMN])
2496 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2497 _("This variable on the columns axis has a summary."));
2498 if (summaries[PIVOT_AXIS_LAYER])
2499 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2500 _("This variable on the layers axis has a summary."));
2503 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2504 if (n_summaries ? summaries[a] : t->axes[a])
2506 t->summary_axis = a;
2510 if (lex_token (lexer) == T_ENDCMD)
2512 if (!lex_force_match (lexer, T_SLASH))
2515 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2517 if (lex_match_id (lexer, "SLABELS"))
2519 while (lex_token (lexer) != T_SLASH)
2521 if (lex_match_id (lexer, "POSITION"))
2523 lex_match (lexer, T_EQUALS);
2524 if (lex_match_id (lexer, "COLUMN"))
2525 t->slabels_position = PIVOT_AXIS_COLUMN;
2526 else if (lex_match_id (lexer, "ROW"))
2527 t->slabels_position = PIVOT_AXIS_ROW;
2528 else if (lex_match_id (lexer, "LAYER"))
2529 t->slabels_position = PIVOT_AXIS_LAYER;
2532 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2536 else if (lex_match_id (lexer, "VISIBLE"))
2538 lex_match (lexer, T_EQUALS);
2539 if (!parse_bool (lexer, &t->slabels_visible))
2544 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2549 else if (lex_match_id (lexer, "CLABELS"))
2551 while (lex_token (lexer) != T_SLASH)
2553 if (lex_match_id (lexer, "AUTO"))
2554 t->row_labels = t->col_labels = CTLP_NORMAL;
2555 else if (lex_match_id (lexer, "ROWLABELS"))
2557 lex_match (lexer, T_EQUALS);
2558 if (lex_match_id (lexer, "OPPOSITE"))
2559 t->row_labels = CTLP_OPPOSITE;
2560 else if (lex_match_id (lexer, "LAYER"))
2561 t->row_labels = CTLP_LAYER;
2564 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2568 else if (lex_match_id (lexer, "COLLABELS"))
2570 lex_match (lexer, T_EQUALS);
2571 if (lex_match_id (lexer, "OPPOSITE"))
2572 t->col_labels = CTLP_OPPOSITE;
2573 else if (lex_match_id (lexer, "LAYER"))
2574 t->col_labels = CTLP_LAYER;
2577 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2583 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2589 else if (lex_match_id (lexer, "CRITERIA"))
2591 if (!lex_force_match_id (lexer, "CILEVEL"))
2593 lex_match (lexer, T_EQUALS);
2595 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2597 t->cilevel = lex_number (lexer);
2600 else if (lex_match_id (lexer, "CATEGORIES"))
2602 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2605 else if (lex_match_id (lexer, "TITLES"))
2610 if (lex_match_id (lexer, "CAPTION"))
2611 textp = &t->caption;
2612 else if (lex_match_id (lexer, "CORNER"))
2614 else if (lex_match_id (lexer, "TITLE"))
2618 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2621 lex_match (lexer, T_EQUALS);
2623 struct string s = DS_EMPTY_INITIALIZER;
2624 while (lex_is_string (lexer))
2626 if (!ds_is_empty (&s))
2627 ds_put_byte (&s, ' ');
2628 ds_put_substring (&s, lex_tokss (lexer));
2632 *textp = ds_steal_cstr (&s);
2634 while (lex_token (lexer) != T_SLASH
2635 && lex_token (lexer) != T_ENDCMD);
2637 else if (lex_match_id (lexer, "SIGTEST"))
2641 t->chisq = xmalloc (sizeof *t->chisq);
2642 *t->chisq = (struct ctables_chisq) {
2644 .include_mrsets = true,
2645 .all_visible = true,
2651 if (lex_match_id (lexer, "TYPE"))
2653 lex_match (lexer, T_EQUALS);
2654 if (!lex_force_match_id (lexer, "CHISQUARE"))
2657 else if (lex_match_id (lexer, "ALPHA"))
2659 lex_match (lexer, T_EQUALS);
2660 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2662 t->chisq->alpha = lex_number (lexer);
2665 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2667 lex_match (lexer, T_EQUALS);
2668 if (parse_bool (lexer, &t->chisq->include_mrsets))
2671 else if (lex_match_id (lexer, "CATEGORIES"))
2673 lex_match (lexer, T_EQUALS);
2674 if (lex_match_id (lexer, "ALLVISIBLE"))
2675 t->chisq->all_visible = true;
2676 else if (lex_match_id (lexer, "SUBTOTALS"))
2677 t->chisq->all_visible = false;
2680 lex_error_expecting (lexer,
2681 "ALLVISIBLE", "SUBTOTALS");
2687 lex_error_expecting (lexer, "TYPE", "ALPHA",
2688 "INCLUDEMRSETS", "CATEGORIES");
2692 while (lex_token (lexer) != T_SLASH
2693 && lex_token (lexer) != T_ENDCMD);
2695 else if (lex_match_id (lexer, "COMPARETEST"))
2699 t->pairwise = xmalloc (sizeof *t->pairwise);
2700 *t->pairwise = (struct ctables_pairwise) {
2702 .alpha = { .05, .05 },
2703 .adjust = BONFERRONI,
2704 .include_mrsets = true,
2705 .meansvariance_allcats = true,
2706 .all_visible = true,
2715 if (lex_match_id (lexer, "TYPE"))
2717 lex_match (lexer, T_EQUALS);
2718 if (lex_match_id (lexer, "PROP"))
2719 t->pairwise->type = PROP;
2720 else if (lex_match_id (lexer, "MEAN"))
2721 t->pairwise->type = MEAN;
2724 lex_error_expecting (lexer, "PROP", "MEAN");
2728 else if (lex_match_id (lexer, "ALPHA"))
2730 lex_match (lexer, T_EQUALS);
2732 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2734 double a0 = lex_number (lexer);
2737 lex_match (lexer, T_COMMA);
2738 if (lex_is_number (lexer))
2740 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2742 double a1 = lex_number (lexer);
2745 t->pairwise->alpha[0] = MIN (a0, a1);
2746 t->pairwise->alpha[1] = MAX (a0, a1);
2749 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2751 else if (lex_match_id (lexer, "ADJUST"))
2753 lex_match (lexer, T_EQUALS);
2754 if (lex_match_id (lexer, "BONFERRONI"))
2755 t->pairwise->adjust = BONFERRONI;
2756 else if (lex_match_id (lexer, "BH"))
2757 t->pairwise->adjust = BH;
2758 else if (lex_match_id (lexer, "NONE"))
2759 t->pairwise->adjust = 0;
2762 lex_error_expecting (lexer, "BONFERRONI", "BH",
2767 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2769 lex_match (lexer, T_EQUALS);
2770 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2773 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2775 lex_match (lexer, T_EQUALS);
2776 if (lex_match_id (lexer, "ALLCATS"))
2777 t->pairwise->meansvariance_allcats = true;
2778 else if (lex_match_id (lexer, "TESTEDCATS"))
2779 t->pairwise->meansvariance_allcats = false;
2782 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2786 else if (lex_match_id (lexer, "CATEGORIES"))
2788 lex_match (lexer, T_EQUALS);
2789 if (lex_match_id (lexer, "ALLVISIBLE"))
2790 t->pairwise->all_visible = true;
2791 else if (lex_match_id (lexer, "SUBTOTALS"))
2792 t->pairwise->all_visible = false;
2795 lex_error_expecting (lexer, "ALLVISIBLE",
2800 else if (lex_match_id (lexer, "MERGE"))
2802 lex_match (lexer, T_EQUALS);
2803 if (!parse_bool (lexer, &t->pairwise->merge))
2806 else if (lex_match_id (lexer, "STYLE"))
2808 lex_match (lexer, T_EQUALS);
2809 if (lex_match_id (lexer, "APA"))
2810 t->pairwise->apa_style = true;
2811 else if (lex_match_id (lexer, "SIMPLE"))
2812 t->pairwise->apa_style = false;
2815 lex_error_expecting (lexer, "APA", "SIMPLE");
2819 else if (lex_match_id (lexer, "SHOWSIG"))
2821 lex_match (lexer, T_EQUALS);
2822 if (!parse_bool (lexer, &t->pairwise->show_sig))
2827 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2828 "INCLUDEMRSETS", "MEANSVARIANCE",
2829 "CATEGORIES", "MERGE", "STYLE",
2834 while (lex_token (lexer) != T_SLASH
2835 && lex_token (lexer) != T_ENDCMD);
2839 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2840 "CRITERIA", "CATEGORIES", "TITLES",
2841 "SIGTEST", "COMPARETEST");
2846 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2848 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2853 while (lex_token (lexer) != T_ENDCMD);
2855 bool ok = ctables_execute (ds, ct);
2856 ctables_destroy (ct);
2857 return ok ? CMD_SUCCESS : CMD_FAILURE;
2860 ctables_destroy (ct);