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 const struct ctables_axis *summary;
228 struct variable **vars;
234 struct var_array *vas;
240 struct ctables_axis *axes[PIVOT_N_AXES];
241 struct var_array2 vaas[PIVOT_N_AXES];
244 enum pivot_axis_type slabels_position;
245 bool slabels_visible;
247 enum ctables_label_position row_labels;
248 enum ctables_label_position col_labels;
250 /* Indexed by variable dictionary index. */
251 struct ctables_categories **categories;
260 struct ctables_chisq *chisq;
261 struct ctables_pairwise *pairwise;
270 struct variable *var;
271 const struct mrset *mrset;
275 static const struct fmt_spec *
276 ctables_var_get_print_format (const struct ctables_var *var)
278 return (var->is_mrset
279 ? var_get_print_format (var->mrset->vars[0])
280 : var_get_print_format (var->var));
284 ctables_var_name (const struct ctables_var *var)
286 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
289 struct ctables_categories
293 /* Explicit categories. */
294 struct ctables_cat_value *values;
297 /* Implicit categories. */
299 bool include_missing;
300 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
301 enum ctables_summary_function sort_func;
302 struct variable *sort_func_var;
310 /* Empty categories. */
314 struct ctables_cat_value
316 enum ctables_cat_value_type
330 double number; /* CCVT_NUMBER. */
331 char *string; /* CCVT_STRING. */
332 double range[2]; /* CCVT_RANGE. */
333 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
338 ctables_cat_value_uninit (struct ctables_cat_value *cv)
357 free (cv->subtotal_label);
362 ctables_categories_unref (struct ctables_categories *c)
367 assert (c->n_refs > 0);
371 for (size_t i = 0; i < c->n_values; i++)
372 ctables_cat_value_uninit (&c->values[i]);
374 free (c->total_label);
378 /* Chi-square test (SIGTEST). */
386 /* Pairwise comparison test (COMPARETEST). */
387 struct ctables_pairwise
389 enum { PROP, MEAN } type;
392 bool meansvariance_allcats;
394 enum { BONFERRONI = 1, BH } adjust;
418 struct ctables_var var;
420 struct ctables_summary_spec *summaries;
422 size_t allocated_summaries;
426 struct ctables_axis *subs[2];
429 struct msg_location *loc;
432 static void ctables_axis_destroy (struct ctables_axis *);
441 enum ctables_function_availability
443 CTFA_ALL, /* Any variables. */
444 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
445 CTFA_MRSETS, /* Only multiple-response sets */
448 struct ctables_summary_spec
450 enum ctables_summary_function function;
451 double percentile; /* CTSF_PTILE only. */
453 struct fmt_spec format; /* XXX extra CTABLES formats */
457 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
464 parse_col_width (struct lexer *lexer, const char *name, double *width)
466 lex_match (lexer, T_EQUALS);
467 if (lex_match_id (lexer, "DEFAULT"))
469 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
471 *width = lex_number (lexer);
481 parse_bool (struct lexer *lexer, bool *b)
483 if (lex_match_id (lexer, "NO"))
485 else if (lex_match_id (lexer, "YES"))
489 lex_error_expecting (lexer, "YES", "NO");
495 static enum ctables_function_availability
496 ctables_function_availability (enum ctables_summary_function f)
498 static enum ctables_function_availability availability[] = {
499 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
504 return availability[f];
508 parse_ctables_summary_function (struct lexer *lexer,
509 enum ctables_summary_function *f)
513 enum ctables_summary_function function;
514 struct substring name;
516 static struct pair names[] = {
517 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
518 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
521 /* The .COUNT suffix may be omitted. */
522 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
523 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
524 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
525 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
526 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
527 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
528 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
532 if (!lex_force_id (lexer))
535 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
536 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
538 *f = names[i].function;
543 lex_error (lexer, _("Expecting summary function name."));
548 ctables_axis_destroy (struct ctables_axis *axis)
556 for (size_t i = 0; i < axis->n_summaries; i++)
557 ctables_summary_spec_uninit (&axis->summaries[i]);
558 free (axis->summaries);
563 ctables_axis_destroy (axis->subs[0]);
564 ctables_axis_destroy (axis->subs[1]);
567 msg_location_destroy (axis->loc);
571 static struct ctables_axis *
572 ctables_axis_new_nonterminal (enum ctables_axis_op op,
573 struct ctables_axis *sub0,
574 struct ctables_axis *sub1,
575 struct lexer *lexer, int start_ofs)
577 struct ctables_axis *axis = xmalloc (sizeof *axis);
578 *axis = (struct ctables_axis) {
580 .subs = { sub0, sub1 },
581 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
586 struct ctables_axis_parse_ctx
589 struct dictionary *dict;
591 struct ctables_table *t;
594 static struct fmt_spec
595 ctables_summary_default_format (enum ctables_summary_function function,
596 const struct ctables_var *var)
598 static const enum ctables_format default_formats[] = {
599 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
603 switch (default_formats[function])
606 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
609 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
612 return *ctables_var_get_print_format (var);
620 ctables_summary_function_name (enum ctables_summary_function function)
622 static const char *names[] = {
623 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
627 return names[function];
631 add_summary_spec (struct ctables_axis *axis,
632 enum ctables_summary_function function, double percentile,
633 const char *label, const struct fmt_spec *format,
634 const struct msg_location *loc)
636 if (axis->op == CTAO_VAR)
638 if (axis->n_summaries >= axis->allocated_summaries)
639 axis->summaries = x2nrealloc (axis->summaries,
640 &axis->allocated_summaries,
641 sizeof *axis->summaries);
643 const char *function_name = ctables_summary_function_name (function);
644 const char *var_name = ctables_var_name (&axis->var);
645 switch (ctables_function_availability (function))
648 if (!axis->var.is_mrset)
650 msg_at (SE, loc, _("Summary function %s applies only to multiple "
651 "response sets."), function_name);
652 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
662 _("Summary function %s applies only to scale variables."),
664 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
674 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
675 *dst = (struct ctables_summary_spec) {
676 .function = function,
677 .percentile = percentile,
678 .label = xstrdup (label),
679 .format = (format ? *format
680 : ctables_summary_default_format (function, &axis->var)),
686 for (size_t i = 0; i < 2; i++)
687 if (!add_summary_spec (axis->subs[i], function, percentile, label,
694 static struct ctables_axis *ctables_axis_parse_stack (
695 struct ctables_axis_parse_ctx *);
698 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
699 struct ctables_var *var)
701 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
703 *var = (struct ctables_var) {
705 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
709 lex_error (lexer, _("'%s' does not name a multiple-response set "
710 "in the active file dictionary."),
711 lex_tokcstr (lexer));
719 *var = (struct ctables_var) {
721 .var = parse_variable (lexer, dict),
723 return var->var != NULL;
727 static struct ctables_axis *
728 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
730 if (lex_match (ctx->lexer, T_LPAREN))
732 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
733 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
735 ctables_axis_destroy (sub);
741 if (!lex_force_id (ctx->lexer))
744 int start_ofs = lex_ofs (ctx->lexer);
745 struct ctables_var var;
746 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
749 struct ctables_axis *axis = xmalloc (sizeof *axis);
750 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
752 /* XXX should figure out default measures by reading data */
753 axis->scale = (var.is_mrset ? false
754 : lex_match_phrase (ctx->lexer, "[S]") ? true
755 : lex_match_phrase (ctx->lexer, "[C]") ? false
756 : var_get_measure (var.var) == MEASURE_SCALE);
757 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
758 lex_ofs (ctx->lexer) - 1);
762 static struct ctables_axis *
763 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
765 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
766 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
771 int start_ofs = lex_ofs (ctx->lexer);
773 /* Parse function. */
774 enum ctables_summary_function function;
775 if (!parse_ctables_summary_function (ctx->lexer, &function))
778 /* Parse percentile. */
779 double percentile = 0;
780 if (function == CTSF_PTILE)
782 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
784 percentile = lex_number (ctx->lexer);
785 lex_get (ctx->lexer);
790 if (lex_is_string (ctx->lexer))
792 label = ss_xstrdup (lex_tokss (ctx->lexer));
793 lex_get (ctx->lexer);
795 else if (function == CTSF_PTILE)
796 label = xasprintf (_("Percentile %.2f"), percentile);
799 static const char *default_labels[] = {
800 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
804 label = xstrdup (gettext (default_labels[function]));
808 struct fmt_spec format;
809 const struct fmt_spec *formatp;
810 if (lex_token (ctx->lexer) == T_ID)
812 if (!parse_format_specifier (ctx->lexer, &format)
813 || !fmt_check_output (&format)
814 || !fmt_check_type_compat (&format, VAL_NUMERIC))
824 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
825 lex_ofs (ctx->lexer) - 1);
826 add_summary_spec (sub, function, percentile, label, formatp, loc);
828 msg_location_destroy (loc);
830 lex_match (ctx->lexer, T_COMMA);
832 while (!lex_match (ctx->lexer, T_RBRACK));
837 ctables_axis_destroy (sub);
841 static const struct ctables_axis *
842 find_scale (const struct ctables_axis *axis)
846 else if (axis->op == CTAO_VAR)
850 assert (!axis->var.is_mrset);
858 for (size_t i = 0; i < 2; i++)
860 const struct ctables_axis *scale = find_scale (axis->subs[i]);
868 static const struct ctables_axis *
869 find_categorical_summary_spec (const struct ctables_axis *axis)
873 else if (axis->op == CTAO_VAR)
874 return !axis->scale && axis->n_summaries ? axis : NULL;
877 for (size_t i = 0; i < 2; i++)
879 const struct ctables_axis *sum
880 = find_categorical_summary_spec (axis->subs[i]);
888 static struct ctables_axis *
889 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
891 int start_ofs = lex_ofs (ctx->lexer);
892 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
896 while (lex_match (ctx->lexer, T_GT))
898 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
902 struct ctables_axis *nest = ctables_axis_new_nonterminal (
903 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
905 const struct ctables_axis *outer_scale = find_scale (lhs);
906 const struct ctables_axis *inner_scale = find_scale (rhs);
907 if (outer_scale && inner_scale)
909 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
910 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
911 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
912 ctables_axis_destroy (nest);
916 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
919 msg_at (SE, nest->loc,
920 _("Summaries may only be requested for categorical variables "
921 "at the innermost nesting level."));
922 msg_at (SN, outer_sum->loc,
923 _("This outer categorical variable has a summary."));
924 ctables_axis_destroy (nest);
934 static struct ctables_axis *
935 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
937 int start_ofs = lex_ofs (ctx->lexer);
938 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
942 while (lex_match (ctx->lexer, T_PLUS))
944 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
948 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
949 ctx->lexer, start_ofs);
956 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
957 struct ctables *ct, struct ctables_table *t,
958 enum pivot_axis_type a)
960 if (lex_token (lexer) == T_BY
961 || lex_token (lexer) == T_SLASH
962 || lex_token (lexer) == T_ENDCMD)
965 struct ctables_axis_parse_ctx ctx = {
971 t->axes[a] = ctables_axis_parse_stack (&ctx);
972 return t->axes[a] != NULL;
976 ctables_chisq_destroy (struct ctables_chisq *chisq)
982 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
988 ctables_table_destroy (struct ctables_table *t)
993 for (size_t i = 0; i < t->n_categories; i++)
994 ctables_categories_unref (t->categories[i]);
995 free (t->categories);
997 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
998 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
999 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1003 ctables_chisq_destroy (t->chisq);
1004 ctables_pairwise_destroy (t->pairwise);
1009 ctables_destroy (struct ctables *ct)
1014 pivot_table_look_unref (ct->look);
1018 for (size_t i = 0; i < ct->n_tables; i++)
1019 ctables_table_destroy (ct->tables[i]);
1024 static struct ctables_cat_value
1025 ccvt_range (double low, double high)
1027 return (struct ctables_cat_value) {
1029 .range = { low, high }
1034 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1035 struct ctables_table *t)
1037 if (!lex_match_id (lexer, "VARIABLES"))
1039 lex_match (lexer, T_EQUALS);
1041 struct variable **vars;
1043 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1046 struct ctables_categories *c = xmalloc (sizeof *c);
1047 *c = (struct ctables_categories) { .n_refs = n_vars };
1048 for (size_t i = 0; i < n_vars; i++)
1050 struct ctables_categories **cp
1051 = &t->categories[var_get_dict_index (vars[i])];
1052 ctables_categories_unref (*cp);
1057 if (lex_match (lexer, T_LBRACK))
1059 size_t allocated_values = 0;
1062 if (c->n_values >= allocated_values)
1063 c->values = x2nrealloc (c->values, &allocated_values,
1066 struct ctables_cat_value *v = &c->values[c->n_values];
1067 if (lex_match_id (lexer, "OTHERNM"))
1068 v->type = CCVT_OTHERNM;
1069 else if (lex_match_id (lexer, "MISSING"))
1070 v->type = CCVT_MISSING;
1071 else if (lex_match_id (lexer, "SUBTOTAL"))
1072 *v = (struct ctables_cat_value)
1073 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1074 else if (lex_match_id (lexer, "HSUBTOTAL"))
1075 *v = (struct ctables_cat_value)
1076 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1077 else if (lex_match_id (lexer, "LO"))
1079 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1081 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1084 else if (lex_is_number (lexer))
1086 double number = lex_number (lexer);
1088 if (lex_match_id (lexer, "THRU"))
1090 v->type = CCVT_RANGE;
1091 v->range[0] = number;
1092 if (lex_match_id (lexer, "HI"))
1093 *v = ccvt_range (number, DBL_MAX);
1096 if (!lex_force_num (lexer))
1098 *v = ccvt_range (number, lex_number (lexer));
1103 *v = (struct ctables_cat_value) {
1104 .type = CCVT_NUMBER,
1108 else if (lex_is_string (lexer))
1110 *v = (struct ctables_cat_value) {
1111 .type = CCVT_STRING,
1112 .string = ss_xstrdup (lex_tokss (lexer)),
1118 lex_error (lexer, NULL);
1122 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1123 && lex_match (lexer, T_EQUALS))
1125 if (!lex_force_string (lexer))
1128 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1133 lex_match (lexer, T_COMMA);
1135 while (!lex_match (lexer, T_RBRACK));
1138 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1140 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1142 lex_match (lexer, T_EQUALS);
1143 if (lex_match_id (lexer, "A"))
1144 c->sort_ascending = true;
1145 else if (lex_match_id (lexer, "D"))
1146 c->sort_ascending = false;
1149 lex_error_expecting (lexer, "A", "D");
1153 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1155 lex_match (lexer, T_EQUALS);
1156 if (lex_match_id (lexer, "VALUE"))
1157 c->key = CTCS_VALUE;
1158 else if (lex_match_id (lexer, "LABEL"))
1159 c->key = CTCS_LABEL;
1162 c->key = CTCS_FUNCTION;
1163 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1166 if (lex_match (lexer, T_LPAREN))
1168 c->sort_func_var = parse_variable (lexer, dict);
1169 if (!c->sort_func_var)
1172 if (c->sort_func == CTSF_PTILE)
1174 lex_match (lexer, T_COMMA);
1175 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1177 c->percentile = lex_number (lexer);
1181 if (!lex_force_match (lexer, T_RPAREN))
1184 else if (ctables_function_availability (c->sort_func)
1187 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1192 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1194 lex_match (lexer, T_EQUALS);
1195 if (lex_match_id (lexer, "INCLUDE"))
1196 c->include_missing = true;
1197 else if (lex_match_id (lexer, "EXCLUDE"))
1198 c->include_missing = false;
1201 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1205 else if (lex_match_id (lexer, "TOTAL"))
1207 lex_match (lexer, T_EQUALS);
1208 if (!parse_bool (lexer, &c->show_totals))
1211 else if (lex_match_id (lexer, "LABEL"))
1213 lex_match (lexer, T_EQUALS);
1214 if (!lex_force_string (lexer))
1216 free (c->total_label);
1217 c->total_label = ss_xstrdup (lex_tokss (lexer));
1220 else if (lex_match_id (lexer, "POSITION"))
1222 lex_match (lexer, T_EQUALS);
1223 if (lex_match_id (lexer, "BEFORE"))
1224 c->totals_before = true;
1225 else if (lex_match_id (lexer, "AFTER"))
1226 c->totals_before = false;
1229 lex_error_expecting (lexer, "BEFORE", "AFTER");
1233 else if (lex_match_id (lexer, "EMPTY"))
1235 lex_match (lexer, T_EQUALS);
1236 if (lex_match_id (lexer, "INCLUDE"))
1237 c->show_empty = true;
1238 else if (lex_match_id (lexer, "EXCLUDE"))
1239 c->show_empty = false;
1242 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1249 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1250 "TOTAL", "LABEL", "POSITION", "EMPTY");
1252 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1260 var_array_uninit (struct var_array *va)
1267 var_array2_uninit (struct var_array2 *vaa)
1271 for (size_t i = 0; i < vaa->n; i++)
1272 var_array_uninit (&vaa->vas[i]);
1277 static struct var_array2
1278 nest_fts (struct var_array2 va0, struct var_array2 va1)
1285 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1286 for (size_t i = 0; i < va0.n; i++)
1287 for (size_t j = 0; j < va1.n; j++)
1289 const struct var_array *a = &va0.vas[i];
1290 const struct var_array *b = &va1.vas[j];
1292 size_t allocate = a->n + b->n;
1293 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1294 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1296 for (size_t k = 0; k < a->n; k++)
1297 vars[n++] = a->vars[k];
1298 for (size_t k = 0; k < b->n; k++)
1299 vars[n++] = b->vars[k];
1300 assert (n == allocate);
1302 assert (!(a->summary && b->summary));
1303 vaa.vas[vaa.n++] = (struct var_array) {
1304 .summary = a->summary ? a->summary : b->summary,
1309 var_array2_uninit (&va0);
1310 var_array2_uninit (&va1);
1314 static struct var_array2
1315 stack_fts (struct var_array2 va0, struct var_array2 va1)
1317 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1318 for (size_t i = 0; i < va0.n; i++)
1319 vaa.vas[vaa.n++] = va0.vas[i];
1320 for (size_t i = 0; i < va1.n; i++)
1321 vaa.vas[vaa.n++] = va1.vas[i];
1322 assert (vaa.n == va0.n + va1.n);
1328 static struct var_array2
1329 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1332 return (struct var_array2) { .n = 0 };
1337 assert (!a->var.is_mrset);
1338 struct var_array *va = xmalloc (sizeof *va);
1340 *va = (struct var_array) { .n = 0 };
1343 struct variable **vars = xmalloc (sizeof *vars);
1345 enum pivot_axis_type *axes = xmalloc (sizeof *axes);
1347 *va = (struct var_array) { .vars = vars, .n = 1 };
1349 va->summary = a->scale || a->n_summaries ? a : NULL;
1350 return (struct var_array2) { .vas = va, .n = 1 };
1353 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1354 enumerate_fts (axis_type, a->subs[1]));
1357 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1358 enumerate_fts (axis_type, a->subs[1]));
1364 union ctables_summary
1366 /* COUNT, VALIDN, TOTALN. */
1373 /* MINIMUM, MAXIMUM, RANGE. */
1380 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1381 struct moments1 *moments;
1383 /* XXX percentiles, median, mode, multiple response */
1388 ctables_summary_init (union ctables_summary *s,
1389 const struct ctables_summary_spec *ss)
1391 switch (ss->function)
1395 case CTSF_ROWPCT_COUNT:
1396 case CTSF_COLPCT_COUNT:
1397 case CTSF_TABLEPCT_COUNT:
1398 case CTSF_SUBTABLEPCT_COUNT:
1399 case CTSF_LAYERPCT_COUNT:
1400 case CTSF_LAYERROWPCT_COUNT:
1401 case CTSF_LAYERCOLPCT_COUNT:
1402 case CTSF_ROWPCT_VALIDN:
1403 case CTSF_COLPCT_VALIDN:
1404 case CTSF_TABLEPCT_VALIDN:
1405 case CTSF_SUBTABLEPCT_VALIDN:
1406 case CTSF_LAYERPCT_VALIDN:
1407 case CTSF_LAYERROWPCT_VALIDN:
1408 case CTSF_LAYERCOLPCT_VALIDN:
1409 case CTSF_ROWPCT_TOTALN:
1410 case CTSF_COLPCT_TOTALN:
1411 case CTSF_TABLEPCT_TOTALN:
1412 case CTSF_SUBTABLEPCT_TOTALN:
1413 case CTSF_LAYERPCT_TOTALN:
1414 case CTSF_LAYERROWPCT_TOTALN:
1415 case CTSF_LAYERCOLPCT_TOTALN:
1420 s->missing = s->valid = 0;
1426 s->min = s->max = SYSMIS;
1434 case CTSF_ROWPCT_SUM:
1435 case CTSF_COLPCT_SUM:
1436 case CTSF_TABLEPCT_SUM:
1437 case CTSF_SUBTABLEPCT_SUM:
1438 case CTSF_LAYERPCT_SUM:
1439 case CTSF_LAYERROWPCT_SUM:
1440 case CTSF_LAYERCOLPCT_SUM:
1441 s->moments = moments1_create (MOMENT_VARIANCE);
1450 case CTSF_RESPONSES:
1451 case CTSF_ROWPCT_RESPONSES:
1452 case CTSF_COLPCT_RESPONSES:
1453 case CTSF_TABLEPCT_RESPONSES:
1454 case CTSF_SUBTABLEPCT_RESPONSES:
1455 case CTSF_LAYERPCT_RESPONSES:
1456 case CTSF_LAYERROWPCT_RESPONSES:
1457 case CTSF_LAYERCOLPCT_RESPONSES:
1458 case CTSF_ROWPCT_RESPONSES_COUNT:
1459 case CTSF_COLPCT_RESPONSES_COUNT:
1460 case CTSF_TABLEPCT_RESPONSES_COUNT:
1461 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1462 case CTSF_LAYERPCT_RESPONSES_COUNT:
1463 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1464 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1465 case CTSF_ROWPCT_COUNT_RESPONSES:
1466 case CTSF_COLPCT_COUNT_RESPONSES:
1467 case CTSF_TABLEPCT_COUNT_RESPONSES:
1468 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1469 case CTSF_LAYERPCT_COUNT_RESPONSES:
1470 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1471 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1477 ctables_summary_uninit (union ctables_summary *s,
1478 const struct ctables_summary_spec *ss)
1480 switch (ss->function)
1484 case CTSF_ROWPCT_COUNT:
1485 case CTSF_COLPCT_COUNT:
1486 case CTSF_TABLEPCT_COUNT:
1487 case CTSF_SUBTABLEPCT_COUNT:
1488 case CTSF_LAYERPCT_COUNT:
1489 case CTSF_LAYERROWPCT_COUNT:
1490 case CTSF_LAYERCOLPCT_COUNT:
1491 case CTSF_ROWPCT_VALIDN:
1492 case CTSF_COLPCT_VALIDN:
1493 case CTSF_TABLEPCT_VALIDN:
1494 case CTSF_SUBTABLEPCT_VALIDN:
1495 case CTSF_LAYERPCT_VALIDN:
1496 case CTSF_LAYERROWPCT_VALIDN:
1497 case CTSF_LAYERCOLPCT_VALIDN:
1498 case CTSF_ROWPCT_TOTALN:
1499 case CTSF_COLPCT_TOTALN:
1500 case CTSF_TABLEPCT_TOTALN:
1501 case CTSF_SUBTABLEPCT_TOTALN:
1502 case CTSF_LAYERPCT_TOTALN:
1503 case CTSF_LAYERROWPCT_TOTALN:
1504 case CTSF_LAYERCOLPCT_TOTALN:
1521 case CTSF_ROWPCT_SUM:
1522 case CTSF_COLPCT_SUM:
1523 case CTSF_TABLEPCT_SUM:
1524 case CTSF_SUBTABLEPCT_SUM:
1525 case CTSF_LAYERPCT_SUM:
1526 case CTSF_LAYERROWPCT_SUM:
1527 case CTSF_LAYERCOLPCT_SUM:
1528 moments1_destroy (s->moments);
1537 case CTSF_RESPONSES:
1538 case CTSF_ROWPCT_RESPONSES:
1539 case CTSF_COLPCT_RESPONSES:
1540 case CTSF_TABLEPCT_RESPONSES:
1541 case CTSF_SUBTABLEPCT_RESPONSES:
1542 case CTSF_LAYERPCT_RESPONSES:
1543 case CTSF_LAYERROWPCT_RESPONSES:
1544 case CTSF_LAYERCOLPCT_RESPONSES:
1545 case CTSF_ROWPCT_RESPONSES_COUNT:
1546 case CTSF_COLPCT_RESPONSES_COUNT:
1547 case CTSF_TABLEPCT_RESPONSES_COUNT:
1548 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1549 case CTSF_LAYERPCT_RESPONSES_COUNT:
1550 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1551 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1552 case CTSF_ROWPCT_COUNT_RESPONSES:
1553 case CTSF_COLPCT_COUNT_RESPONSES:
1554 case CTSF_TABLEPCT_COUNT_RESPONSES:
1555 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1556 case CTSF_LAYERPCT_COUNT_RESPONSES:
1557 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1558 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1564 ctables_summary_add (union ctables_summary *s,
1565 const struct ctables_summary_spec *ss,
1566 const struct variable *var, const union value *value,
1569 switch (ss->function)
1573 case CTSF_ROWPCT_COUNT:
1574 case CTSF_COLPCT_COUNT:
1575 case CTSF_TABLEPCT_COUNT:
1576 case CTSF_SUBTABLEPCT_COUNT:
1577 case CTSF_LAYERPCT_COUNT:
1578 case CTSF_LAYERROWPCT_COUNT:
1579 case CTSF_LAYERCOLPCT_COUNT:
1580 case CTSF_ROWPCT_VALIDN:
1581 case CTSF_COLPCT_VALIDN:
1582 case CTSF_TABLEPCT_VALIDN:
1583 case CTSF_SUBTABLEPCT_VALIDN:
1584 case CTSF_LAYERPCT_VALIDN:
1585 case CTSF_LAYERROWPCT_VALIDN:
1586 case CTSF_LAYERCOLPCT_VALIDN:
1587 case CTSF_ROWPCT_TOTALN:
1588 case CTSF_COLPCT_TOTALN:
1589 case CTSF_TABLEPCT_TOTALN:
1590 case CTSF_SUBTABLEPCT_TOTALN:
1591 case CTSF_LAYERPCT_TOTALN:
1592 case CTSF_LAYERROWPCT_TOTALN:
1593 case CTSF_LAYERCOLPCT_TOTALN:
1598 if (var_is_value_missing (var, value))
1599 s->missing += weight;
1607 if (!var_is_value_missing (var, value))
1609 assert (!var_is_alpha (var)); /* XXX? */
1610 if (s->min == SYSMIS || value->f < s->min)
1612 if (s->max == SYSMIS || value->f > s->max)
1622 case CTSF_ROWPCT_SUM:
1623 case CTSF_COLPCT_SUM:
1624 case CTSF_TABLEPCT_SUM:
1625 case CTSF_SUBTABLEPCT_SUM:
1626 case CTSF_LAYERPCT_SUM:
1627 case CTSF_LAYERROWPCT_SUM:
1628 case CTSF_LAYERCOLPCT_SUM:
1629 moments1_add (s->moments, value->f, weight);
1638 case CTSF_RESPONSES:
1639 case CTSF_ROWPCT_RESPONSES:
1640 case CTSF_COLPCT_RESPONSES:
1641 case CTSF_TABLEPCT_RESPONSES:
1642 case CTSF_SUBTABLEPCT_RESPONSES:
1643 case CTSF_LAYERPCT_RESPONSES:
1644 case CTSF_LAYERROWPCT_RESPONSES:
1645 case CTSF_LAYERCOLPCT_RESPONSES:
1646 case CTSF_ROWPCT_RESPONSES_COUNT:
1647 case CTSF_COLPCT_RESPONSES_COUNT:
1648 case CTSF_TABLEPCT_RESPONSES_COUNT:
1649 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1650 case CTSF_LAYERPCT_RESPONSES_COUNT:
1651 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1652 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1653 case CTSF_ROWPCT_COUNT_RESPONSES:
1654 case CTSF_COLPCT_COUNT_RESPONSES:
1655 case CTSF_TABLEPCT_COUNT_RESPONSES:
1656 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1657 case CTSF_LAYERPCT_COUNT_RESPONSES:
1658 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1659 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1665 ctables_summary_value (union ctables_summary *s,
1666 const struct ctables_summary_spec *ss)
1668 switch (ss->function)
1674 case CTSF_ROWPCT_COUNT:
1675 case CTSF_COLPCT_COUNT:
1676 case CTSF_TABLEPCT_COUNT:
1677 case CTSF_SUBTABLEPCT_COUNT:
1678 case CTSF_LAYERPCT_COUNT:
1679 case CTSF_LAYERROWPCT_COUNT:
1680 case CTSF_LAYERCOLPCT_COUNT:
1681 case CTSF_ROWPCT_VALIDN:
1682 case CTSF_COLPCT_VALIDN:
1683 case CTSF_TABLEPCT_VALIDN:
1684 case CTSF_SUBTABLEPCT_VALIDN:
1685 case CTSF_LAYERPCT_VALIDN:
1686 case CTSF_LAYERROWPCT_VALIDN:
1687 case CTSF_LAYERCOLPCT_VALIDN:
1688 case CTSF_ROWPCT_TOTALN:
1689 case CTSF_COLPCT_TOTALN:
1690 case CTSF_TABLEPCT_TOTALN:
1691 case CTSF_SUBTABLEPCT_TOTALN:
1692 case CTSF_LAYERPCT_TOTALN:
1693 case CTSF_LAYERROWPCT_TOTALN:
1694 case CTSF_LAYERCOLPCT_TOTALN:
1699 return s->valid + s->missing;
1712 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1717 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1723 double weight, variance;
1724 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1725 return calc_semean (variance, weight);
1731 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1732 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1737 double weight, mean;
1738 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1739 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1745 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1749 case CTSF_ROWPCT_SUM:
1750 case CTSF_COLPCT_SUM:
1751 case CTSF_TABLEPCT_SUM:
1752 case CTSF_SUBTABLEPCT_SUM:
1753 case CTSF_LAYERPCT_SUM:
1754 case CTSF_LAYERROWPCT_SUM:
1755 case CTSF_LAYERCOLPCT_SUM:
1764 case CTSF_RESPONSES:
1765 case CTSF_ROWPCT_RESPONSES:
1766 case CTSF_COLPCT_RESPONSES:
1767 case CTSF_TABLEPCT_RESPONSES:
1768 case CTSF_SUBTABLEPCT_RESPONSES:
1769 case CTSF_LAYERPCT_RESPONSES:
1770 case CTSF_LAYERROWPCT_RESPONSES:
1771 case CTSF_LAYERCOLPCT_RESPONSES:
1772 case CTSF_ROWPCT_RESPONSES_COUNT:
1773 case CTSF_COLPCT_RESPONSES_COUNT:
1774 case CTSF_TABLEPCT_RESPONSES_COUNT:
1775 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1776 case CTSF_LAYERPCT_RESPONSES_COUNT:
1777 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1778 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1779 case CTSF_ROWPCT_COUNT_RESPONSES:
1780 case CTSF_COLPCT_COUNT_RESPONSES:
1781 case CTSF_TABLEPCT_COUNT_RESPONSES:
1782 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1783 case CTSF_LAYERPCT_COUNT_RESPONSES:
1784 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1785 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1795 struct hmap_node node; /* Element in hash table. */
1800 union value *values;
1804 //union ctables_summary *summaries;
1809 static struct ctables_freq *
1810 ctables_freq_create (struct ctables_freqtab *ft)
1812 struct ctables_freq *f = xmalloc (sizeof *f + ft->vars.n * sizeof *f->values);
1813 f->summaries = xmalloc (ft->n_summaries * sizeof *f->summaries);
1814 for (size_t i = 0; i < ft->n_summaries; i++)
1815 ctables_summary_init (&f->summaries[i], &ft->summaries[i]);
1820 ctables_freq_add (struct ctables_freqtab *ft, struct ctables_freq *f,
1821 const struct variable *var, const union value *value,
1824 for (size_t i = 0; i < ft->n_summaries; i++)
1825 ctables_summary_add (&f->summaries[i], &ft->summaries[i],
1826 var, value, weight);
1830 struct ctables_freq_sort_aux
1832 const struct ctables_table *t;
1833 enum pivot_axis_type a;
1837 ctables_freq_compare_3way (const void *a_, const void *b_, const void *aux_)
1839 const struct ctables_freq_sort_aux *aux = aux_;
1840 struct ctables_freq *const *ap = a_;
1841 struct ctables_freq *const *bp = b_;
1842 const struct ctables_freq *a = *ap;
1843 const struct ctables_freq *b = *bp;
1845 size_t a_idx = a->axes[aux->a].vaa_idx;
1846 size_t b_idx = b->axes[aux->a].vaa_idx;
1848 return a_idx < b_idx ? -1 : 1;
1850 const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1851 for (size_t i = 0; i < va->n; i++)
1853 int cmp = value_compare_3way (&a->axes[aux->a].values[i],
1854 &b->axes[aux->a].values[i],
1855 var_get_width (va->vars[i]));
1865 For each ctables_table:
1866 For each combination of row vars:
1867 For each combination of column vars:
1868 For each combination of layer vars:
1870 Make a table of row values:
1871 Sort entries by row values
1872 Assign a 0-based index to each actual value
1873 Construct a dimension
1874 Make a table of column values
1875 Make a table of layer values
1877 Fill the table entry using the indexes from before.
1881 ctables_freqtab_insert (struct ctables_table *t,
1882 const struct ccase *c,
1883 size_t ir, size_t ic, size_t il,
1886 size_t ix[PIVOT_N_AXES] = {
1887 [PIVOT_AXIS_ROW] = ir,
1888 [PIVOT_AXIS_COLUMN] = ic,
1889 [PIVOT_AXIS_LAYER] = il,
1893 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1895 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1896 hash = hash_int (ix[a], hash);
1897 for (size_t i = 0; i < va->n; i++)
1898 hash = value_hash (case_data (c, va->vars[i]),
1899 var_get_width (va->vars[i]), hash);
1902 struct ctables_freq *f;
1903 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &t->ft)
1905 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1907 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1908 if (f->axes[a].vaa_idx != ix[a])
1910 for (size_t i = 0; i < va->n; i++)
1911 if (!value_equal (case_data (c, va->vars[i]),
1912 &f->axes[a].values[i],
1913 var_get_width (va->vars[i])))
1923 f = xmalloc (sizeof *f);
1924 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1926 const struct var_array *va = &t->vaas[a].vas[ix[a]];
1927 f->axes[a].vaa_idx = ix[a];
1928 f->axes[a].values = (va->n
1929 ? xnmalloc (va->n, sizeof *f->axes[a].values)
1931 for (size_t i = 0; i < va->n; i++)
1932 value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
1933 var_get_width (va->vars[i]));
1936 hmap_insert (&t->ft, &f->node, hash);
1940 ctables_execute (struct dataset *ds, struct ctables *ct)
1942 for (size_t i = 0; i < ct->n_tables; i++)
1944 struct ctables_table *t = ct->tables[i];
1945 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1947 t->vaas[a] = enumerate_fts (a, t->axes[a]);
1950 struct var_array *va = xmalloc (sizeof *va);
1951 *va = (struct var_array) { .n = 0 };
1952 t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
1956 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
1959 bool warn_on_invalid = true;
1960 for (struct ccase *c = casereader_read (input); c;
1961 case_unref (c), c = casereader_read (input))
1963 double weight = dict_get_case_weight (dataset_dict (ds), c,
1966 for (size_t i = 0; i < ct->n_tables; i++)
1968 struct ctables_table *t = ct->tables[i];
1970 for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
1971 for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
1972 for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
1973 ctables_freqtab_insert (t, c, ir, ic, il, weight);
1976 casereader_destroy (input);
1978 for (size_t i = 0; i < ct->n_tables; i++)
1980 struct ctables_table *t = ct->tables[i];
1982 struct pivot_table *pt = pivot_table_create (N_("Custom Tables"));
1983 pivot_table_set_look (pt, ct->look);
1984 struct pivot_dimension *d[PIVOT_N_AXES];
1985 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1987 static const char *names[] = {
1988 [PIVOT_AXIS_ROW] = N_("Rows"),
1989 [PIVOT_AXIS_COLUMN] = N_("Columns"),
1990 [PIVOT_AXIS_LAYER] = N_("Layers"),
1993 ? pivot_dimension_create (pt, a, names[a])
1998 struct ctables_freq **sorted = xnmalloc (t->ft.count, sizeof *sorted);
2000 struct ctables_freq *f;
2002 HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2004 assert (n == t->ft.count);
2006 struct ctables_freq_sort_aux aux = { .t = t, .a = a };
2007 n = sort_unique (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
2009 size_t max_depth = 0;
2010 for (size_t j = 0; j < t->vaas[a].n; j++)
2011 if (t->vaas[a].vas[j].n > max_depth)
2012 max_depth = t->vaas[a].vas[j].n;
2014 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2015 struct pivot_category *top = NULL;
2016 for (size_t j = 0; j < n; j++)
2018 struct ctables_freq *f = sorted[j];
2019 const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2021 size_t n_common = 0;
2022 bool new_subtable = false;
2025 struct ctables_freq *prev = sorted[j - 1];
2026 if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2028 for (; n_common < va->n; n_common++)
2029 if (!value_equal (&prev->axes[a].values[n_common],
2030 &f->axes[a].values[n_common],
2031 var_get_type (va->vars[n_common])))
2035 new_subtable = true;
2038 new_subtable = true;
2040 top = pivot_category_create_group__ (
2041 d[a]->root, pivot_value_new_variable (va->vars[0]));
2042 printf ("n_common=%zu\n", n_common);
2044 for (size_t k = n_common; k < va->n; k++)
2046 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2050 pivot_category_create_leaf (
2052 pivot_value_new_var_value (va->vars[va->n - 1],
2053 &f->axes[a].values[va->n - 1]));
2057 parent = pivot_category_create_group__ (
2059 pivot_value_new_var_value (va->vars[k], &f->axes[a].values[k]));
2061 parent = pivot_category_create_group__ (
2062 parent, pivot_value_new_variable (va->vars[k]));
2066 for (size_t p = 0; p < ft->n_summaries; p++)
2068 if (a == t->slabels_position)
2069 pivot_category_create_leaf (
2070 c, pivot_value_new_text (ft->summaries[p].label));
2071 //pivot_table_put1 (pt, leaf, pivot_value_new_number (value));
2078 pivot_table_submit (pt);
2082 for (size_t i = 0; i < ct->n_tables; i++)
2084 struct ctables_table *t = ct->tables[i];
2086 for (size_t j = 0; j < t->n_fts; j++)
2088 struct ctables_freqtab *ft = t->fts[j];
2089 struct ctables_freq *f, *next;
2090 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2092 hmap_delete (&ft->data, &f->node);
2093 for (size_t k = 0; k < ft->n_summaries; k++)
2094 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2095 free (f->summaries);
2096 for (size_t k = 0; k < ft->vars.n; k++)
2098 const struct variable *var = ft->vars.vars[k];
2099 value_destroy (&f->values[k], var_get_width (var));
2103 hmap_destroy (&ft->data);
2104 var_array_uninit (&ft->vars);
2111 return proc_commit (ds);
2115 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2117 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2118 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2119 enum settings_value_show tvars = settings_get_show_variables ();
2120 for (size_t i = 0; i < n_vars; i++)
2121 vlabels[i] = (enum ctables_vlabel) tvars;
2123 struct ctables *ct = xmalloc (sizeof *ct);
2124 *ct = (struct ctables) {
2125 .look = pivot_table_look_unshare (pivot_table_look_ref (
2126 pivot_table_look_get_default ())),
2128 .hide_threshold = 5,
2130 ct->look->omit_empty = false;
2132 if (!lex_force_match (lexer, T_SLASH))
2135 while (!lex_match_id (lexer, "TABLE"))
2137 if (lex_match_id (lexer, "FORMAT"))
2139 double widths[2] = { SYSMIS, SYSMIS };
2140 double units_per_inch = 72.0;
2142 while (lex_token (lexer) != T_SLASH)
2144 if (lex_match_id (lexer, "MINCOLWIDTH"))
2146 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2149 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2151 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2154 else if (lex_match_id (lexer, "UNITS"))
2156 lex_match (lexer, T_EQUALS);
2157 if (lex_match_id (lexer, "POINTS"))
2158 units_per_inch = 72.0;
2159 else if (lex_match_id (lexer, "INCHES"))
2160 units_per_inch = 1.0;
2161 else if (lex_match_id (lexer, "CM"))
2162 units_per_inch = 2.54;
2165 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2169 else if (lex_match_id (lexer, "EMPTY"))
2174 lex_match (lexer, T_EQUALS);
2175 if (lex_match_id (lexer, "ZERO"))
2177 /* Nothing to do. */
2179 else if (lex_match_id (lexer, "BLANK"))
2180 ct->zero = xstrdup ("");
2181 else if (lex_force_string (lexer))
2183 ct->zero = ss_xstrdup (lex_tokss (lexer));
2189 else if (lex_match_id (lexer, "MISSING"))
2191 lex_match (lexer, T_EQUALS);
2192 if (!lex_force_string (lexer))
2196 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2197 ? ss_xstrdup (lex_tokss (lexer))
2203 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2204 "UNITS", "EMPTY", "MISSING");
2209 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2210 && widths[0] > widths[1])
2212 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2216 for (size_t i = 0; i < 2; i++)
2217 if (widths[i] != SYSMIS)
2219 int *wr = ct->look->width_ranges[TABLE_HORZ];
2220 wr[i] = widths[i] / units_per_inch * 96.0;
2225 else if (lex_match_id (lexer, "VLABELS"))
2227 if (!lex_force_match_id (lexer, "VARIABLES"))
2229 lex_match (lexer, T_EQUALS);
2231 struct variable **vars;
2233 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2237 if (!lex_force_match_id (lexer, "DISPLAY"))
2242 lex_match (lexer, T_EQUALS);
2244 enum ctables_vlabel vlabel;
2245 if (lex_match_id (lexer, "DEFAULT"))
2246 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2247 else if (lex_match_id (lexer, "NAME"))
2249 else if (lex_match_id (lexer, "LABEL"))
2250 vlabel = CTVL_LABEL;
2251 else if (lex_match_id (lexer, "BOTH"))
2253 else if (lex_match_id (lexer, "NONE"))
2257 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2263 for (size_t i = 0; i < n_vars; i++)
2264 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2267 else if (lex_match_id (lexer, "MRSETS"))
2269 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2271 lex_match (lexer, T_EQUALS);
2272 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2275 else if (lex_match_id (lexer, "SMISSING"))
2277 if (lex_match_id (lexer, "VARIABLE"))
2278 ct->smissing_listwise = false;
2279 else if (lex_match_id (lexer, "LISTWISE"))
2280 ct->smissing_listwise = true;
2283 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2288 else if (lex_match_id (lexer, "WEIGHT"))
2290 if (!lex_force_match_id (lexer, "VARIABLE"))
2292 lex_match (lexer, T_EQUALS);
2293 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2294 if (!ct->base_weight)
2297 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2299 if (!lex_force_match_id (lexer, "COUNT"))
2301 lex_match (lexer, T_EQUALS);
2302 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2304 ct->hide_threshold = lex_integer (lexer);
2309 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2310 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2311 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2315 if (!lex_force_match (lexer, T_SLASH))
2319 size_t allocated_tables = 0;
2322 if (ct->n_tables >= allocated_tables)
2323 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2324 sizeof *ct->tables);
2326 struct ctables_table *t = xmalloc (sizeof *t);
2327 *t = (struct ctables_table) {
2328 .ft = HMAP_INITIALIZER (t->ft),
2329 .slabels_position = PIVOT_AXIS_COLUMN,
2330 .slabels_visible = true,
2331 .row_labels = CTLP_NORMAL,
2332 .col_labels = CTLP_NORMAL,
2333 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2334 sizeof *t->categories),
2335 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2338 ct->tables[ct->n_tables++] = t;
2340 lex_match (lexer, T_EQUALS);
2341 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2343 if (lex_match (lexer, T_BY))
2345 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2346 ct, t, PIVOT_AXIS_COLUMN))
2349 if (lex_match (lexer, T_BY))
2351 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2352 ct, t, PIVOT_AXIS_LAYER))
2357 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2358 && !t->axes[PIVOT_AXIS_LAYER])
2360 lex_error (lexer, _("At least one variable must be specified."));
2364 const struct ctables_axis *scales[PIVOT_N_AXES];
2365 size_t n_scales = 0;
2366 for (size_t i = 0; i < 3; i++)
2368 scales[i] = find_scale (t->axes[i]);
2374 msg (SE, _("Scale variables may appear only on one dimension."));
2375 if (scales[PIVOT_AXIS_ROW])
2376 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2377 _("This scale variable appears in the rows dimension."));
2378 if (scales[PIVOT_AXIS_COLUMN])
2379 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2380 _("This scale variable appears in the columns dimension."));
2381 if (scales[PIVOT_AXIS_LAYER])
2382 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2383 _("This scale variable appears in the layer dimension."));
2387 if (lex_token (lexer) == T_ENDCMD)
2389 if (!lex_force_match (lexer, T_SLASH))
2392 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2394 if (lex_match_id (lexer, "SLABELS"))
2396 while (lex_token (lexer) != T_SLASH)
2398 if (lex_match_id (lexer, "POSITION"))
2400 lex_match (lexer, T_EQUALS);
2401 if (lex_match_id (lexer, "COLUMN"))
2402 t->slabels_position = PIVOT_AXIS_COLUMN;
2403 else if (lex_match_id (lexer, "ROW"))
2404 t->slabels_position = PIVOT_AXIS_ROW;
2405 else if (lex_match_id (lexer, "LAYER"))
2406 t->slabels_position = PIVOT_AXIS_LAYER;
2409 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2413 else if (lex_match_id (lexer, "VISIBLE"))
2415 lex_match (lexer, T_EQUALS);
2416 if (!parse_bool (lexer, &t->slabels_visible))
2421 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2426 else if (lex_match_id (lexer, "CLABELS"))
2428 while (lex_token (lexer) != T_SLASH)
2430 if (lex_match_id (lexer, "AUTO"))
2431 t->row_labels = t->col_labels = CTLP_NORMAL;
2432 else if (lex_match_id (lexer, "ROWLABELS"))
2434 lex_match (lexer, T_EQUALS);
2435 if (lex_match_id (lexer, "OPPOSITE"))
2436 t->row_labels = CTLP_OPPOSITE;
2437 else if (lex_match_id (lexer, "LAYER"))
2438 t->row_labels = CTLP_LAYER;
2441 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2445 else if (lex_match_id (lexer, "COLLABELS"))
2447 lex_match (lexer, T_EQUALS);
2448 if (lex_match_id (lexer, "OPPOSITE"))
2449 t->col_labels = CTLP_OPPOSITE;
2450 else if (lex_match_id (lexer, "LAYER"))
2451 t->col_labels = CTLP_LAYER;
2454 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2460 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2466 else if (lex_match_id (lexer, "CRITERIA"))
2468 if (!lex_force_match_id (lexer, "CILEVEL"))
2470 lex_match (lexer, T_EQUALS);
2472 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2474 t->cilevel = lex_number (lexer);
2477 else if (lex_match_id (lexer, "CATEGORIES"))
2479 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2482 else if (lex_match_id (lexer, "TITLES"))
2487 if (lex_match_id (lexer, "CAPTION"))
2488 textp = &t->caption;
2489 else if (lex_match_id (lexer, "CORNER"))
2491 else if (lex_match_id (lexer, "TITLE"))
2495 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2498 lex_match (lexer, T_EQUALS);
2500 struct string s = DS_EMPTY_INITIALIZER;
2501 while (lex_is_string (lexer))
2503 if (!ds_is_empty (&s))
2504 ds_put_byte (&s, ' ');
2505 ds_put_substring (&s, lex_tokss (lexer));
2509 *textp = ds_steal_cstr (&s);
2511 while (lex_token (lexer) != T_SLASH
2512 && lex_token (lexer) != T_ENDCMD);
2514 else if (lex_match_id (lexer, "SIGTEST"))
2518 t->chisq = xmalloc (sizeof *t->chisq);
2519 *t->chisq = (struct ctables_chisq) {
2521 .include_mrsets = true,
2522 .all_visible = true,
2528 if (lex_match_id (lexer, "TYPE"))
2530 lex_match (lexer, T_EQUALS);
2531 if (!lex_force_match_id (lexer, "CHISQUARE"))
2534 else if (lex_match_id (lexer, "ALPHA"))
2536 lex_match (lexer, T_EQUALS);
2537 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2539 t->chisq->alpha = lex_number (lexer);
2542 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2544 lex_match (lexer, T_EQUALS);
2545 if (parse_bool (lexer, &t->chisq->include_mrsets))
2548 else if (lex_match_id (lexer, "CATEGORIES"))
2550 lex_match (lexer, T_EQUALS);
2551 if (lex_match_id (lexer, "ALLVISIBLE"))
2552 t->chisq->all_visible = true;
2553 else if (lex_match_id (lexer, "SUBTOTALS"))
2554 t->chisq->all_visible = false;
2557 lex_error_expecting (lexer,
2558 "ALLVISIBLE", "SUBTOTALS");
2564 lex_error_expecting (lexer, "TYPE", "ALPHA",
2565 "INCLUDEMRSETS", "CATEGORIES");
2569 while (lex_token (lexer) != T_SLASH
2570 && lex_token (lexer) != T_ENDCMD);
2572 else if (lex_match_id (lexer, "COMPARETEST"))
2576 t->pairwise = xmalloc (sizeof *t->pairwise);
2577 *t->pairwise = (struct ctables_pairwise) {
2579 .alpha = { .05, .05 },
2580 .adjust = BONFERRONI,
2581 .include_mrsets = true,
2582 .meansvariance_allcats = true,
2583 .all_visible = true,
2592 if (lex_match_id (lexer, "TYPE"))
2594 lex_match (lexer, T_EQUALS);
2595 if (lex_match_id (lexer, "PROP"))
2596 t->pairwise->type = PROP;
2597 else if (lex_match_id (lexer, "MEAN"))
2598 t->pairwise->type = MEAN;
2601 lex_error_expecting (lexer, "PROP", "MEAN");
2605 else if (lex_match_id (lexer, "ALPHA"))
2607 lex_match (lexer, T_EQUALS);
2609 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2611 double a0 = lex_number (lexer);
2614 lex_match (lexer, T_COMMA);
2615 if (lex_is_number (lexer))
2617 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2619 double a1 = lex_number (lexer);
2622 t->pairwise->alpha[0] = MIN (a0, a1);
2623 t->pairwise->alpha[1] = MAX (a0, a1);
2626 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2628 else if (lex_match_id (lexer, "ADJUST"))
2630 lex_match (lexer, T_EQUALS);
2631 if (lex_match_id (lexer, "BONFERRONI"))
2632 t->pairwise->adjust = BONFERRONI;
2633 else if (lex_match_id (lexer, "BH"))
2634 t->pairwise->adjust = BH;
2635 else if (lex_match_id (lexer, "NONE"))
2636 t->pairwise->adjust = 0;
2639 lex_error_expecting (lexer, "BONFERRONI", "BH",
2644 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2646 lex_match (lexer, T_EQUALS);
2647 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2650 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2652 lex_match (lexer, T_EQUALS);
2653 if (lex_match_id (lexer, "ALLCATS"))
2654 t->pairwise->meansvariance_allcats = true;
2655 else if (lex_match_id (lexer, "TESTEDCATS"))
2656 t->pairwise->meansvariance_allcats = false;
2659 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2663 else if (lex_match_id (lexer, "CATEGORIES"))
2665 lex_match (lexer, T_EQUALS);
2666 if (lex_match_id (lexer, "ALLVISIBLE"))
2667 t->pairwise->all_visible = true;
2668 else if (lex_match_id (lexer, "SUBTOTALS"))
2669 t->pairwise->all_visible = false;
2672 lex_error_expecting (lexer, "ALLVISIBLE",
2677 else if (lex_match_id (lexer, "MERGE"))
2679 lex_match (lexer, T_EQUALS);
2680 if (!parse_bool (lexer, &t->pairwise->merge))
2683 else if (lex_match_id (lexer, "STYLE"))
2685 lex_match (lexer, T_EQUALS);
2686 if (lex_match_id (lexer, "APA"))
2687 t->pairwise->apa_style = true;
2688 else if (lex_match_id (lexer, "SIMPLE"))
2689 t->pairwise->apa_style = false;
2692 lex_error_expecting (lexer, "APA", "SIMPLE");
2696 else if (lex_match_id (lexer, "SHOWSIG"))
2698 lex_match (lexer, T_EQUALS);
2699 if (!parse_bool (lexer, &t->pairwise->show_sig))
2704 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2705 "INCLUDEMRSETS", "MEANSVARIANCE",
2706 "CATEGORIES", "MERGE", "STYLE",
2711 while (lex_token (lexer) != T_SLASH
2712 && lex_token (lexer) != T_ENDCMD);
2716 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2717 "CRITERIA", "CATEGORIES", "TITLES",
2718 "SIGTEST", "COMPARETEST");
2723 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2725 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2730 while (lex_token (lexer) != T_ENDCMD);
2732 bool ok = ctables_execute (ds, ct);
2733 ctables_destroy (ct);
2734 return ok ? CMD_SUCCESS : CMD_FAILURE;
2737 ctables_destroy (ct);