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/hmap.h"
32 #include "libpspp/message.h"
33 #include "libpspp/string-array.h"
34 #include "math/moments.h"
35 #include "output/pivot-table.h"
37 #include "gl/minmax.h"
38 #include "gl/xalloc.h"
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) (msgid)
46 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
47 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
48 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
49 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
53 - unweighted summaries (U*)
54 - lower confidence limits (*.LCL)
55 - upper confidence limits (*.UCL)
56 - standard error (*.SE)
59 /* All variables. */ \
60 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
61 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
62 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
63 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
64 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
84 /* Scale variables, totals, and subtotals. */ \
85 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
86 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
87 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
96 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
97 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
98 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
99 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
100 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
102 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
103 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
105 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
106 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
107 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
109 /* Multiple response sets. */ \
110 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
111 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
112 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
133 enum ctables_summary_function
135 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
141 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
142 N_CTSF_FUNCTIONS = SUMMARIES
148 struct pivot_table_look *look;
150 /* If this is NULL, zeros are displayed using the normal print format.
151 Otherwise, this string is displayed. */
154 /* If this is NULL, missing values are displayed using the normal print
155 format. Otherwise, this string is displayed. */
158 /* Indexed by variable dictionary index. */
159 enum ctables_vlabel *vlabels;
161 bool mrsets_count_duplicates; /* MRSETS. */
162 bool smissing_listwise; /* SMISSING. */
163 struct variable *base_weight; /* WEIGHT. */
164 int hide_threshold; /* HIDESMALLCOUNTS. */
166 struct ctables_table *tables;
170 struct ctables_postcompute
172 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
173 const char *name; /* Name, without leading &. */
175 struct ctables_postcompute_expr *expr;
178 bool hide_source_cats;
181 struct ctables_postcompute_expr
183 enum ctables_postcompute_op
191 /* XXX SUBTOTAL and HSUBTOTAL */
204 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
209 XXX what about string ranges? */
212 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
213 struct ctables_postcompute_expr *subs[2];
217 enum ctables_label_position
226 struct ctables_axis *axes[PIVOT_N_AXES];
228 enum pivot_axis_type slabels_position;
229 bool slabels_visible;
231 enum ctables_label_position row_labels;
232 enum ctables_label_position col_labels;
234 /* Indexed by variable dictionary index. */
235 struct ctables_categories **categories;
244 struct ctables_chisq *chisq;
245 struct ctables_pairwise *pairwise;
247 struct ctables_freqtab **fts;
256 struct variable *var;
257 const struct mrset *mrset;
261 static const struct fmt_spec *
262 ctables_var_get_print_format (const struct ctables_var *var)
264 return (var->is_mrset
265 ? var_get_print_format (var->mrset->vars[0])
266 : var_get_print_format (var->var));
270 ctables_var_name (const struct ctables_var *var)
272 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
275 struct ctables_categories
279 /* Explicit categories. */
280 struct ctables_cat_value *values;
283 /* Implicit categories. */
285 bool include_missing;
286 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
287 enum ctables_summary_function sort_func;
288 struct variable *sort_func_var;
296 /* Empty categories. */
300 struct ctables_cat_value
302 enum ctables_cat_value_type
316 double number; /* CCVT_NUMBER. */
317 char *string; /* CCVT_STRING. */
318 double range[2]; /* CCVT_RANGE. */
319 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
324 ctables_cat_value_uninit (struct ctables_cat_value *cv)
343 free (cv->subtotal_label);
348 ctables_categories_unref (struct ctables_categories *c)
353 assert (c->n_refs > 0);
357 for (size_t i = 0; i < c->n_values; i++)
358 ctables_cat_value_uninit (&c->values[i]);
360 free (c->total_label);
364 /* Chi-square test (SIGTEST). */
372 /* Pairwise comparison test (COMPARETEST). */
373 struct ctables_pairwise
375 enum { PROP, MEAN } type;
378 bool meansvariance_allcats;
380 enum { BONFERRONI = 1, BH } adjust;
404 struct ctables_var var;
406 struct ctables_summary_spec *summaries;
408 size_t allocated_summaries;
412 struct ctables_axis *subs[2];
415 struct msg_location *loc;
418 static void ctables_axis_destroy (struct ctables_axis *);
427 enum ctables_function_availability
429 CTFA_ALL, /* Any variables. */
430 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
431 CTFA_MRSETS, /* Only multiple-response sets */
434 struct ctables_summary_spec
436 enum ctables_summary_function function;
437 double percentile; /* CTSF_PTILE only. */
439 struct fmt_spec format; /* XXX extra CTABLES formats */
443 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
450 parse_col_width (struct lexer *lexer, const char *name, double *width)
452 lex_match (lexer, T_EQUALS);
453 if (lex_match_id (lexer, "DEFAULT"))
455 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
457 *width = lex_number (lexer);
467 parse_bool (struct lexer *lexer, bool *b)
469 if (lex_match_id (lexer, "NO"))
471 else if (lex_match_id (lexer, "YES"))
475 lex_error_expecting (lexer, "YES", "NO");
481 static enum ctables_function_availability
482 ctables_function_availability (enum ctables_summary_function f)
484 static enum ctables_function_availability availability[] = {
485 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
490 return availability[f];
494 parse_ctables_summary_function (struct lexer *lexer,
495 enum ctables_summary_function *f)
499 enum ctables_summary_function function;
500 struct substring name;
502 static struct pair names[] = {
503 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
504 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
507 /* The .COUNT suffix may be omitted. */
508 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
509 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
510 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
511 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
512 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
513 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
514 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
518 if (!lex_force_id (lexer))
521 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
522 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
524 *f = names[i].function;
529 lex_error (lexer, _("Expecting summary function name."));
534 ctables_axis_destroy (struct ctables_axis *axis)
542 for (size_t i = 0; i < axis->n_summaries; i++)
543 ctables_summary_spec_uninit (&axis->summaries[i]);
544 free (axis->summaries);
549 ctables_axis_destroy (axis->subs[0]);
550 ctables_axis_destroy (axis->subs[1]);
553 msg_location_destroy (axis->loc);
557 static struct ctables_axis *
558 ctables_axis_new_nonterminal (enum ctables_axis_op op,
559 struct ctables_axis *sub0,
560 struct ctables_axis *sub1,
561 struct lexer *lexer, int start_ofs)
563 struct ctables_axis *axis = xmalloc (sizeof *axis);
564 *axis = (struct ctables_axis) {
566 .subs = { sub0, sub1 },
567 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
572 struct ctables_axis_parse_ctx
575 struct dictionary *dict;
577 struct ctables_table *t;
580 static struct fmt_spec
581 ctables_summary_default_format (enum ctables_summary_function function,
582 const struct ctables_var *var)
584 static const enum ctables_format default_formats[] = {
585 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
589 switch (default_formats[function])
592 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
595 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
598 return *ctables_var_get_print_format (var);
606 ctables_summary_function_name (enum ctables_summary_function function)
608 static const char *names[] = {
609 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
613 return names[function];
617 add_summary_spec (struct ctables_axis *axis,
618 enum ctables_summary_function function, double percentile,
619 const char *label, const struct fmt_spec *format,
620 const struct msg_location *loc)
622 if (axis->op == CTAO_VAR)
624 if (axis->n_summaries >= axis->allocated_summaries)
625 axis->summaries = x2nrealloc (axis->summaries,
626 &axis->allocated_summaries,
627 sizeof *axis->summaries);
629 const char *function_name = ctables_summary_function_name (function);
630 const char *var_name = ctables_var_name (&axis->var);
631 switch (ctables_function_availability (function))
634 if (!axis->var.is_mrset)
636 msg_at (SE, loc, _("Summary function %s applies only to multiple "
637 "response sets."), function_name);
638 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
648 _("Summary function %s applies only to scale variables."),
650 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
660 struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
661 *dst = (struct ctables_summary_spec) {
662 .function = function,
663 .percentile = percentile,
664 .label = xstrdup (label),
665 .format = (format ? *format
666 : ctables_summary_default_format (function, &axis->var)),
672 for (size_t i = 0; i < 2; i++)
673 if (!add_summary_spec (axis->subs[i], function, percentile, label,
680 static struct ctables_axis *ctables_axis_parse_stack (
681 struct ctables_axis_parse_ctx *);
684 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
685 struct ctables_var *var)
687 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
689 *var = (struct ctables_var) {
691 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
695 lex_error (lexer, _("'%s' does not name a multiple-response set "
696 "in the active file dictionary."),
697 lex_tokcstr (lexer));
705 *var = (struct ctables_var) {
707 .var = parse_variable (lexer, dict),
709 return var->var != NULL;
713 static struct ctables_axis *
714 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
716 if (lex_match (ctx->lexer, T_LPAREN))
718 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
719 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
721 ctables_axis_destroy (sub);
727 if (!lex_force_id (ctx->lexer))
730 int start_ofs = lex_ofs (ctx->lexer);
731 struct ctables_var var;
732 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
735 struct ctables_axis *axis = xmalloc (sizeof *axis);
736 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
738 /* XXX should figure out default measures by reading data */
739 axis->scale = (var.is_mrset ? false
740 : lex_match_phrase (ctx->lexer, "[S]") ? true
741 : lex_match_phrase (ctx->lexer, "[C]") ? false
742 : var_get_measure (var.var) == MEASURE_SCALE);
743 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
744 lex_ofs (ctx->lexer) - 1);
748 static struct ctables_axis *
749 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
751 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
752 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
757 int start_ofs = lex_ofs (ctx->lexer);
759 /* Parse function. */
760 enum ctables_summary_function function;
761 if (!parse_ctables_summary_function (ctx->lexer, &function))
764 /* Parse percentile. */
765 double percentile = 0;
766 if (function == CTSF_PTILE)
768 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
770 percentile = lex_number (ctx->lexer);
771 lex_get (ctx->lexer);
776 if (lex_is_string (ctx->lexer))
778 label = ss_xstrdup (lex_tokss (ctx->lexer));
779 lex_get (ctx->lexer);
781 else if (function == CTSF_PTILE)
782 label = xasprintf (_("Percentile %.2f"), percentile);
785 static const char *default_labels[] = {
786 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
790 label = xstrdup (gettext (default_labels[function]));
794 struct fmt_spec format;
795 const struct fmt_spec *formatp;
796 if (lex_token (ctx->lexer) == T_ID)
798 if (!parse_format_specifier (ctx->lexer, &format)
799 || !fmt_check_output (&format)
800 || !fmt_check_type_compat (&format, VAL_NUMERIC))
810 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
811 lex_ofs (ctx->lexer) - 1);
812 add_summary_spec (sub, function, percentile, label, formatp, loc);
814 msg_location_destroy (loc);
816 lex_match (ctx->lexer, T_COMMA);
818 while (!lex_match (ctx->lexer, T_RBRACK));
823 ctables_axis_destroy (sub);
827 static const struct ctables_axis *
828 find_scale (const struct ctables_axis *axis)
832 else if (axis->op == CTAO_VAR)
836 assert (!axis->var.is_mrset);
844 for (size_t i = 0; i < 2; i++)
846 const struct ctables_axis *scale = find_scale (axis->subs[i]);
854 static const struct ctables_axis *
855 find_categorical_summary_spec (const struct ctables_axis *axis)
859 else if (axis->op == CTAO_VAR)
860 return !axis->scale && axis->n_summaries ? axis : NULL;
863 for (size_t i = 0; i < 2; i++)
865 const struct ctables_axis *sum
866 = find_categorical_summary_spec (axis->subs[i]);
874 static struct ctables_axis *
875 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
877 int start_ofs = lex_ofs (ctx->lexer);
878 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
882 while (lex_match (ctx->lexer, T_GT))
884 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
888 struct ctables_axis *nest = ctables_axis_new_nonterminal (
889 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
891 const struct ctables_axis *outer_scale = find_scale (lhs);
892 const struct ctables_axis *inner_scale = find_scale (rhs);
893 if (outer_scale && inner_scale)
895 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
896 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
897 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
898 ctables_axis_destroy (nest);
902 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
905 msg_at (SE, nest->loc,
906 _("Summaries may only be requested for categorical variables "
907 "at the innermost nesting level."));
908 msg_at (SN, outer_sum->loc,
909 _("This outer categorical variable has a summary."));
910 ctables_axis_destroy (nest);
920 static struct ctables_axis *
921 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
923 int start_ofs = lex_ofs (ctx->lexer);
924 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
928 while (lex_match (ctx->lexer, T_PLUS))
930 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
934 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
935 ctx->lexer, start_ofs);
942 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
943 struct ctables *ct, struct ctables_table *t,
944 enum pivot_axis_type a)
946 if (lex_token (lexer) == T_BY
947 || lex_token (lexer) == T_SLASH
948 || lex_token (lexer) == T_ENDCMD)
951 struct ctables_axis_parse_ctx ctx = {
957 t->axes[a] = ctables_axis_parse_stack (&ctx);
958 return t->axes[a] != NULL;
962 ctables_chisq_destroy (struct ctables_chisq *chisq)
968 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
974 ctables_table_uninit (struct ctables_table *t)
979 for (size_t i = 0; i < t->n_categories; i++)
980 ctables_categories_unref (t->categories[i]);
981 free (t->categories);
983 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
984 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
985 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
989 ctables_chisq_destroy (t->chisq);
990 ctables_pairwise_destroy (t->pairwise);
994 ctables_destroy (struct ctables *ct)
999 pivot_table_look_unref (ct->look);
1003 for (size_t i = 0; i < ct->n_tables; i++)
1004 ctables_table_uninit (&ct->tables[i]);
1009 static struct ctables_cat_value
1010 ccvt_range (double low, double high)
1012 return (struct ctables_cat_value) {
1014 .range = { low, high }
1019 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1020 struct ctables_table *t)
1022 if (!lex_match_id (lexer, "VARIABLES"))
1024 lex_match (lexer, T_EQUALS);
1026 struct variable **vars;
1028 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1031 struct ctables_categories *c = xmalloc (sizeof *c);
1032 *c = (struct ctables_categories) { .n_refs = n_vars };
1033 for (size_t i = 0; i < n_vars; i++)
1035 struct ctables_categories **cp
1036 = &t->categories[var_get_dict_index (vars[i])];
1037 ctables_categories_unref (*cp);
1042 if (lex_match (lexer, T_LBRACK))
1044 size_t allocated_values = 0;
1047 if (c->n_values >= allocated_values)
1048 c->values = x2nrealloc (c->values, &allocated_values,
1051 struct ctables_cat_value *v = &c->values[c->n_values];
1052 if (lex_match_id (lexer, "OTHERNM"))
1053 v->type = CCVT_OTHERNM;
1054 else if (lex_match_id (lexer, "MISSING"))
1055 v->type = CCVT_MISSING;
1056 else if (lex_match_id (lexer, "SUBTOTAL"))
1057 *v = (struct ctables_cat_value)
1058 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1059 else if (lex_match_id (lexer, "HSUBTOTAL"))
1060 *v = (struct ctables_cat_value)
1061 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1062 else if (lex_match_id (lexer, "LO"))
1064 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1066 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1069 else if (lex_is_number (lexer))
1071 double number = lex_number (lexer);
1073 if (lex_match_id (lexer, "THRU"))
1075 v->type = CCVT_RANGE;
1076 v->range[0] = number;
1077 if (lex_match_id (lexer, "HI"))
1078 *v = ccvt_range (number, DBL_MAX);
1081 if (!lex_force_num (lexer))
1083 *v = ccvt_range (number, lex_number (lexer));
1088 *v = (struct ctables_cat_value) {
1089 .type = CCVT_NUMBER,
1093 else if (lex_is_string (lexer))
1095 *v = (struct ctables_cat_value) {
1096 .type = CCVT_STRING,
1097 .string = ss_xstrdup (lex_tokss (lexer)),
1103 lex_error (lexer, NULL);
1107 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1108 && lex_match (lexer, T_EQUALS))
1110 if (!lex_force_string (lexer))
1113 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1118 lex_match (lexer, T_COMMA);
1120 while (!lex_match (lexer, T_RBRACK));
1123 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1125 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1127 lex_match (lexer, T_EQUALS);
1128 if (lex_match_id (lexer, "A"))
1129 c->sort_ascending = true;
1130 else if (lex_match_id (lexer, "D"))
1131 c->sort_ascending = false;
1134 lex_error_expecting (lexer, "A", "D");
1138 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1140 lex_match (lexer, T_EQUALS);
1141 if (lex_match_id (lexer, "VALUE"))
1142 c->key = CTCS_VALUE;
1143 else if (lex_match_id (lexer, "LABEL"))
1144 c->key = CTCS_LABEL;
1147 c->key = CTCS_FUNCTION;
1148 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1151 if (lex_match (lexer, T_LPAREN))
1153 c->sort_func_var = parse_variable (lexer, dict);
1154 if (!c->sort_func_var)
1157 if (c->sort_func == CTSF_PTILE)
1159 lex_match (lexer, T_COMMA);
1160 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1162 c->percentile = lex_number (lexer);
1166 if (!lex_force_match (lexer, T_RPAREN))
1169 else if (ctables_function_availability (c->sort_func)
1172 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1177 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1179 lex_match (lexer, T_EQUALS);
1180 if (lex_match_id (lexer, "INCLUDE"))
1181 c->include_missing = true;
1182 else if (lex_match_id (lexer, "EXCLUDE"))
1183 c->include_missing = false;
1186 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1190 else if (lex_match_id (lexer, "TOTAL"))
1192 lex_match (lexer, T_EQUALS);
1193 if (!parse_bool (lexer, &c->show_totals))
1196 else if (lex_match_id (lexer, "LABEL"))
1198 lex_match (lexer, T_EQUALS);
1199 if (!lex_force_string (lexer))
1201 free (c->total_label);
1202 c->total_label = ss_xstrdup (lex_tokss (lexer));
1205 else if (lex_match_id (lexer, "POSITION"))
1207 lex_match (lexer, T_EQUALS);
1208 if (lex_match_id (lexer, "BEFORE"))
1209 c->totals_before = true;
1210 else if (lex_match_id (lexer, "AFTER"))
1211 c->totals_before = false;
1214 lex_error_expecting (lexer, "BEFORE", "AFTER");
1218 else if (lex_match_id (lexer, "EMPTY"))
1220 lex_match (lexer, T_EQUALS);
1221 if (lex_match_id (lexer, "INCLUDE"))
1222 c->show_empty = true;
1223 else if (lex_match_id (lexer, "EXCLUDE"))
1224 c->show_empty = false;
1227 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1234 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1235 "TOTAL", "LABEL", "POSITION", "EMPTY");
1237 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1246 const struct ctables_axis *summary;
1247 struct variable **vars;
1252 var_array_uninit (struct var_array *va)
1260 struct var_array *vas;
1265 var_array2_uninit (struct var_array2 *vaa)
1269 for (size_t i = 0; i < vaa->n; i++)
1270 var_array_uninit (&vaa->vas[i]);
1275 static struct var_array2
1276 nest_fts (struct var_array2 va0, struct var_array2 va1)
1283 struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1284 for (size_t i = 0; i < va0.n; i++)
1285 for (size_t j = 0; j < va1.n; j++)
1287 const struct var_array *a = &va0.vas[i];
1288 const struct var_array *b = &va1.vas[j];
1290 size_t allocate = a->n + b->n;
1291 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1293 for (size_t k = 0; k < a->n; k++)
1294 vars[n++] = a->vars[k];
1295 for (size_t k = 0; k < b->n; k++)
1296 vars[n++] = b->vars[k];
1297 assert (n == allocate);
1299 assert (!(a->summary && b->summary));
1300 vaa.vas[vaa.n++] = (struct var_array) {
1301 .summary = a->summary ? a->summary : b->summary,
1306 var_array2_uninit (&va0);
1307 var_array2_uninit (&va1);
1311 static struct var_array2
1312 stack_fts (struct var_array2 va0, struct var_array2 va1)
1314 struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1315 for (size_t i = 0; i < va0.n; i++)
1316 vaa.vas[vaa.n++] = va0.vas[i];
1317 for (size_t i = 0; i < va1.n; i++)
1318 vaa.vas[vaa.n++] = va1.vas[i];
1319 assert (vaa.n == va0.n + va1.n);
1325 static struct var_array2
1326 enumerate_fts (const struct ctables_axis *a)
1329 return (struct var_array2) { .n = 0 };
1334 assert (!a->var.is_mrset);
1335 struct var_array *va = xmalloc (sizeof *va);
1337 *va = (struct var_array) { .n = 0 };
1340 struct variable **v = xmalloc (sizeof *v);
1342 *va = (struct var_array) { .vars = v, .n = 1 };
1344 va->summary = a->scale || a->n_summaries ? a : NULL;
1345 return (struct var_array2) { .vas = va, .n = 1 };
1348 return stack_fts (enumerate_fts (a->subs[0]),
1349 enumerate_fts (a->subs[1]));
1352 return nest_fts (enumerate_fts (a->subs[0]),
1353 enumerate_fts (a->subs[1]));
1359 union ctables_summary
1361 /* COUNT, VALIDN, TOTALN. */
1368 /* MINIMUM, MAXIMUM, RANGE. */
1375 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1376 struct moments1 *moments;
1378 /* XXX percentiles, median, mode, multiple response */
1382 ctables_summary_init (union ctables_summary *s,
1383 const struct ctables_summary_spec *ss)
1385 switch (ss->function)
1389 case CTSF_ROWPCT_COUNT:
1390 case CTSF_COLPCT_COUNT:
1391 case CTSF_TABLEPCT_COUNT:
1392 case CTSF_SUBTABLEPCT_COUNT:
1393 case CTSF_LAYERPCT_COUNT:
1394 case CTSF_LAYERROWPCT_COUNT:
1395 case CTSF_LAYERCOLPCT_COUNT:
1396 case CTSF_ROWPCT_VALIDN:
1397 case CTSF_COLPCT_VALIDN:
1398 case CTSF_TABLEPCT_VALIDN:
1399 case CTSF_SUBTABLEPCT_VALIDN:
1400 case CTSF_LAYERPCT_VALIDN:
1401 case CTSF_LAYERROWPCT_VALIDN:
1402 case CTSF_LAYERCOLPCT_VALIDN:
1403 case CTSF_ROWPCT_TOTALN:
1404 case CTSF_COLPCT_TOTALN:
1405 case CTSF_TABLEPCT_TOTALN:
1406 case CTSF_SUBTABLEPCT_TOTALN:
1407 case CTSF_LAYERPCT_TOTALN:
1408 case CTSF_LAYERROWPCT_TOTALN:
1409 case CTSF_LAYERCOLPCT_TOTALN:
1414 s->missing = s->valid = 0;
1420 s->min = s->max = SYSMIS;
1428 case CTSF_ROWPCT_SUM:
1429 case CTSF_COLPCT_SUM:
1430 case CTSF_TABLEPCT_SUM:
1431 case CTSF_SUBTABLEPCT_SUM:
1432 case CTSF_LAYERPCT_SUM:
1433 case CTSF_LAYERROWPCT_SUM:
1434 case CTSF_LAYERCOLPCT_SUM:
1435 s->moments = moments1_create (MOMENT_VARIANCE);
1444 case CTSF_RESPONSES:
1445 case CTSF_ROWPCT_RESPONSES:
1446 case CTSF_COLPCT_RESPONSES:
1447 case CTSF_TABLEPCT_RESPONSES:
1448 case CTSF_SUBTABLEPCT_RESPONSES:
1449 case CTSF_LAYERPCT_RESPONSES:
1450 case CTSF_LAYERROWPCT_RESPONSES:
1451 case CTSF_LAYERCOLPCT_RESPONSES:
1452 case CTSF_ROWPCT_RESPONSES_COUNT:
1453 case CTSF_COLPCT_RESPONSES_COUNT:
1454 case CTSF_TABLEPCT_RESPONSES_COUNT:
1455 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1456 case CTSF_LAYERPCT_RESPONSES_COUNT:
1457 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1458 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1459 case CTSF_ROWPCT_COUNT_RESPONSES:
1460 case CTSF_COLPCT_COUNT_RESPONSES:
1461 case CTSF_TABLEPCT_COUNT_RESPONSES:
1462 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1463 case CTSF_LAYERPCT_COUNT_RESPONSES:
1464 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1465 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1471 ctables_summary_uninit (union ctables_summary *s,
1472 const struct ctables_summary_spec *ss)
1474 switch (ss->function)
1478 case CTSF_ROWPCT_COUNT:
1479 case CTSF_COLPCT_COUNT:
1480 case CTSF_TABLEPCT_COUNT:
1481 case CTSF_SUBTABLEPCT_COUNT:
1482 case CTSF_LAYERPCT_COUNT:
1483 case CTSF_LAYERROWPCT_COUNT:
1484 case CTSF_LAYERCOLPCT_COUNT:
1485 case CTSF_ROWPCT_VALIDN:
1486 case CTSF_COLPCT_VALIDN:
1487 case CTSF_TABLEPCT_VALIDN:
1488 case CTSF_SUBTABLEPCT_VALIDN:
1489 case CTSF_LAYERPCT_VALIDN:
1490 case CTSF_LAYERROWPCT_VALIDN:
1491 case CTSF_LAYERCOLPCT_VALIDN:
1492 case CTSF_ROWPCT_TOTALN:
1493 case CTSF_COLPCT_TOTALN:
1494 case CTSF_TABLEPCT_TOTALN:
1495 case CTSF_SUBTABLEPCT_TOTALN:
1496 case CTSF_LAYERPCT_TOTALN:
1497 case CTSF_LAYERROWPCT_TOTALN:
1498 case CTSF_LAYERCOLPCT_TOTALN:
1515 case CTSF_ROWPCT_SUM:
1516 case CTSF_COLPCT_SUM:
1517 case CTSF_TABLEPCT_SUM:
1518 case CTSF_SUBTABLEPCT_SUM:
1519 case CTSF_LAYERPCT_SUM:
1520 case CTSF_LAYERROWPCT_SUM:
1521 case CTSF_LAYERCOLPCT_SUM:
1522 moments1_destroy (s->moments);
1531 case CTSF_RESPONSES:
1532 case CTSF_ROWPCT_RESPONSES:
1533 case CTSF_COLPCT_RESPONSES:
1534 case CTSF_TABLEPCT_RESPONSES:
1535 case CTSF_SUBTABLEPCT_RESPONSES:
1536 case CTSF_LAYERPCT_RESPONSES:
1537 case CTSF_LAYERROWPCT_RESPONSES:
1538 case CTSF_LAYERCOLPCT_RESPONSES:
1539 case CTSF_ROWPCT_RESPONSES_COUNT:
1540 case CTSF_COLPCT_RESPONSES_COUNT:
1541 case CTSF_TABLEPCT_RESPONSES_COUNT:
1542 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1543 case CTSF_LAYERPCT_RESPONSES_COUNT:
1544 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1545 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1546 case CTSF_ROWPCT_COUNT_RESPONSES:
1547 case CTSF_COLPCT_COUNT_RESPONSES:
1548 case CTSF_TABLEPCT_COUNT_RESPONSES:
1549 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1550 case CTSF_LAYERPCT_COUNT_RESPONSES:
1551 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1552 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1558 ctables_summary_add (union ctables_summary *s,
1559 const struct ctables_summary_spec *ss,
1560 const struct variable *var, const union value *value,
1563 switch (ss->function)
1567 case CTSF_ROWPCT_COUNT:
1568 case CTSF_COLPCT_COUNT:
1569 case CTSF_TABLEPCT_COUNT:
1570 case CTSF_SUBTABLEPCT_COUNT:
1571 case CTSF_LAYERPCT_COUNT:
1572 case CTSF_LAYERROWPCT_COUNT:
1573 case CTSF_LAYERCOLPCT_COUNT:
1574 case CTSF_ROWPCT_VALIDN:
1575 case CTSF_COLPCT_VALIDN:
1576 case CTSF_TABLEPCT_VALIDN:
1577 case CTSF_SUBTABLEPCT_VALIDN:
1578 case CTSF_LAYERPCT_VALIDN:
1579 case CTSF_LAYERROWPCT_VALIDN:
1580 case CTSF_LAYERCOLPCT_VALIDN:
1581 case CTSF_ROWPCT_TOTALN:
1582 case CTSF_COLPCT_TOTALN:
1583 case CTSF_TABLEPCT_TOTALN:
1584 case CTSF_SUBTABLEPCT_TOTALN:
1585 case CTSF_LAYERPCT_TOTALN:
1586 case CTSF_LAYERROWPCT_TOTALN:
1587 case CTSF_LAYERCOLPCT_TOTALN:
1592 if (var_is_value_missing (var, value))
1593 s->missing += weight;
1601 if (!var_is_value_missing (var, value))
1603 assert (!var_is_alpha (var)); /* XXX? */
1604 if (s->min == SYSMIS || value->f < s->min)
1606 if (s->max == SYSMIS || value->f > s->max)
1616 case CTSF_ROWPCT_SUM:
1617 case CTSF_COLPCT_SUM:
1618 case CTSF_TABLEPCT_SUM:
1619 case CTSF_SUBTABLEPCT_SUM:
1620 case CTSF_LAYERPCT_SUM:
1621 case CTSF_LAYERROWPCT_SUM:
1622 case CTSF_LAYERCOLPCT_SUM:
1623 moments1_add (s->moments, value->f, weight);
1632 case CTSF_RESPONSES:
1633 case CTSF_ROWPCT_RESPONSES:
1634 case CTSF_COLPCT_RESPONSES:
1635 case CTSF_TABLEPCT_RESPONSES:
1636 case CTSF_SUBTABLEPCT_RESPONSES:
1637 case CTSF_LAYERPCT_RESPONSES:
1638 case CTSF_LAYERROWPCT_RESPONSES:
1639 case CTSF_LAYERCOLPCT_RESPONSES:
1640 case CTSF_ROWPCT_RESPONSES_COUNT:
1641 case CTSF_COLPCT_RESPONSES_COUNT:
1642 case CTSF_TABLEPCT_RESPONSES_COUNT:
1643 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1644 case CTSF_LAYERPCT_RESPONSES_COUNT:
1645 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1646 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1647 case CTSF_ROWPCT_COUNT_RESPONSES:
1648 case CTSF_COLPCT_COUNT_RESPONSES:
1649 case CTSF_TABLEPCT_COUNT_RESPONSES:
1650 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1651 case CTSF_LAYERPCT_COUNT_RESPONSES:
1652 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1653 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1660 ctables_summary_value (union ctables_summary *s,
1661 const struct ctables_summary_spec *ss)
1663 switch (ss->function)
1669 case CTSF_ROWPCT_COUNT:
1670 case CTSF_COLPCT_COUNT:
1671 case CTSF_TABLEPCT_COUNT:
1672 case CTSF_SUBTABLEPCT_COUNT:
1673 case CTSF_LAYERPCT_COUNT:
1674 case CTSF_LAYERROWPCT_COUNT:
1675 case CTSF_LAYERCOLPCT_COUNT:
1676 case CTSF_ROWPCT_VALIDN:
1677 case CTSF_COLPCT_VALIDN:
1678 case CTSF_TABLEPCT_VALIDN:
1679 case CTSF_SUBTABLEPCT_VALIDN:
1680 case CTSF_LAYERPCT_VALIDN:
1681 case CTSF_LAYERROWPCT_VALIDN:
1682 case CTSF_LAYERCOLPCT_VALIDN:
1683 case CTSF_ROWPCT_TOTALN:
1684 case CTSF_COLPCT_TOTALN:
1685 case CTSF_TABLEPCT_TOTALN:
1686 case CTSF_SUBTABLEPCT_TOTALN:
1687 case CTSF_LAYERPCT_TOTALN:
1688 case CTSF_LAYERROWPCT_TOTALN:
1689 case CTSF_LAYERCOLPCT_TOTALN:
1694 return s->valid + s->missing;
1707 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1712 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1718 double weight, variance;
1719 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1720 return calc_semean (variance, weight);
1726 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1727 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1732 double weight, mean;
1733 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1734 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1740 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1744 case CTSF_ROWPCT_SUM:
1745 case CTSF_COLPCT_SUM:
1746 case CTSF_TABLEPCT_SUM:
1747 case CTSF_SUBTABLEPCT_SUM:
1748 case CTSF_LAYERPCT_SUM:
1749 case CTSF_LAYERROWPCT_SUM:
1750 case CTSF_LAYERCOLPCT_SUM:
1759 case CTSF_RESPONSES:
1760 case CTSF_ROWPCT_RESPONSES:
1761 case CTSF_COLPCT_RESPONSES:
1762 case CTSF_TABLEPCT_RESPONSES:
1763 case CTSF_SUBTABLEPCT_RESPONSES:
1764 case CTSF_LAYERPCT_RESPONSES:
1765 case CTSF_LAYERROWPCT_RESPONSES:
1766 case CTSF_LAYERCOLPCT_RESPONSES:
1767 case CTSF_ROWPCT_RESPONSES_COUNT:
1768 case CTSF_COLPCT_RESPONSES_COUNT:
1769 case CTSF_TABLEPCT_RESPONSES_COUNT:
1770 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1771 case CTSF_LAYERPCT_RESPONSES_COUNT:
1772 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1773 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1774 case CTSF_ROWPCT_COUNT_RESPONSES:
1775 case CTSF_COLPCT_COUNT_RESPONSES:
1776 case CTSF_TABLEPCT_COUNT_RESPONSES:
1777 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1778 case CTSF_LAYERPCT_COUNT_RESPONSES:
1779 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1780 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1789 struct hmap_node node; /* Element in hash table. */
1790 union ctables_summary *summaries;
1791 union value values[]; /* The value. */
1794 struct ctables_freqtab
1796 struct var_array vars;
1797 struct hmap data; /* Contains "struct ctables_freq"s. */
1798 const struct ctables_summary_spec *summaries;
1800 const struct variable *summary_var;
1801 struct ctables_freq **sorted;
1804 static struct ctables_freq *
1805 ctables_freq_create (struct ctables_freqtab *ft)
1807 struct ctables_freq *f = xmalloc (sizeof *f + ft->vars.n * sizeof *f->values);
1808 f->summaries = xmalloc (ft->n_summaries * sizeof *f->summaries);
1809 for (size_t i = 0; i < ft->n_summaries; i++)
1810 ctables_summary_init (&f->summaries[i], &ft->summaries[i]);
1815 ctables_freq_add (struct ctables_freqtab *ft, struct ctables_freq *f,
1816 const struct variable *var, const union value *value,
1819 for (size_t i = 0; i < ft->n_summaries; i++)
1820 ctables_summary_add (&f->summaries[i], &ft->summaries[i],
1821 var, value, weight);
1825 ctables_freq_compare_3way (const void *a_, const void *b_, const void *vars_)
1827 const struct var_array *vars = vars_;
1828 struct ctables_freq *const *a = a_;
1829 struct ctables_freq *const *b = b_;
1831 for (size_t i = 0; i < vars->n; i++)
1833 int cmp = value_compare_3way (&(*a)->values[i], &(*b)->values[i],
1834 var_get_width (vars->vars[i]));
1842 ctables_execute (struct dataset *ds, struct ctables *ct)
1844 for (size_t i = 0; i < ct->n_tables; i++)
1846 size_t allocated_fts = 0;
1848 struct ctables_table *t = &ct->tables[i];
1849 struct var_array2 vaa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
1850 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
1851 vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
1852 for (size_t i = 0; i < vaa.n; i++)
1854 for (size_t j = 0; j < vaa.vas[i].n; j++)
1857 fputs (", ", stdout);
1858 fputs (var_get_name (vaa.vas[i].vars[j]), stdout);
1863 for (size_t j = 0; j < vaa.n; j++)
1865 const struct var_array *va = &vaa.vas[j];
1866 const struct ctables_summary_spec *summaries;
1868 const struct variable *summary_var;
1871 static const struct ctables_summary_spec count = {
1872 .function = CTSF_COUNT,
1873 .label = (char *) N_("Count"),
1874 .format = { .type = FMT_F, .w = 40 },
1878 summary_var = va->vars[0];
1880 else if (va->summary->n_summaries)
1882 summaries = va->summary->summaries;
1883 n_summaries = va->summary->n_summaries;
1884 summary_var = va->summary->var.var;
1888 static const struct ctables_summary_spec mean = {
1889 .function = CTSF_MEAN,
1890 .label = (char *) N_("Mean"),
1891 .format = { .type = FMT_F, .w = 40, .d = 2}, /* XXX */
1895 summary_var = va->summary->var.var;
1898 struct ctables_freqtab *ft = xmalloc (sizeof *ft);
1899 *ft = (struct ctables_freqtab) {
1901 .summaries = summaries,
1902 .n_summaries = n_summaries,
1903 .summary_var = summary_var,
1904 .data = HMAP_INITIALIZER (ft->data),
1907 if (t->n_fts >= allocated_fts)
1908 t->fts = x2nrealloc (t->fts, &allocated_fts, sizeof *t->fts);
1909 t->fts[t->n_fts++] = ft;
1915 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
1918 bool warn_on_invalid = true;
1919 for (struct ccase *c = casereader_read (input); c;
1920 case_unref (c), c = casereader_read (input))
1922 double weight = dict_get_case_weight (dataset_dict (ds), c,
1925 for (size_t i = 0; i < ct->n_tables; i++)
1927 struct ctables_table *t = &ct->tables[i];
1929 for (size_t j = 0; j < t->n_fts; j++)
1931 struct ctables_freqtab *ft = t->fts[j];
1933 for (size_t k = 0; k < ft->vars.n; k++)
1935 const struct variable *var = ft->vars.vars[k];
1936 switch (var_is_value_missing (var, case_data (c, var)))
1942 if (!t->categories[var_get_dict_index (var)]
1943 || !t->categories[var_get_dict_index (var)]->include_missing)
1949 for (size_t k = 0; k < ft->vars.n; k++)
1951 const struct variable *var = ft->vars.vars[k];
1952 hash = value_hash (case_data (c, var), var_get_width (var), hash);
1955 struct ctables_freq *f;
1956 HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &ft->data)
1958 for (size_t k = 0; k < ft->vars.n; k++)
1960 const struct variable *var = ft->vars.vars[k];
1961 if (!value_equal (case_data (c, var), &f->values[k],
1962 var_get_width (var)))
1963 goto next_hash_node;
1970 f = ctables_freq_create (ft);
1971 for (size_t k = 0; k < ft->vars.n; k++)
1973 const struct variable *var = ft->vars.vars[k];
1974 value_clone (&f->values[k], case_data (c, var),
1975 var_get_width (var));
1977 hmap_insert (&ft->data, &f->node, hash);
1980 ctables_freq_add (ft, f, ft->summary_var,
1981 case_data (c, ft->summary_var), weight);
1987 casereader_destroy (input);
1989 for (size_t i = 0; i < ct->n_tables; i++)
1991 struct ctables_table *t = &ct->tables[i];
1993 struct pivot_table *pt = pivot_table_create (N_("Custom Tables"));
1994 struct pivot_dimension *d = pivot_dimension_create (
1995 pt, PIVOT_AXIS_ROW, N_("Rows"));
1996 for (size_t j = 0; j < t->n_fts; j++)
1998 struct ctables_freqtab *ft = t->fts[j];
1999 ft->sorted = xnmalloc (ft->data.count, sizeof *ft->sorted);
2001 struct ctables_freq *f;
2003 HMAP_FOR_EACH (f, struct ctables_freq, node, &ft->data)
2004 ft->sorted[n++] = f;
2005 assert (n == ft->data.count);
2006 sort (ft->sorted, n, sizeof *ft->sorted,
2007 ctables_freq_compare_3way, &ft->vars);
2009 struct pivot_category **groups = xnmalloc (ft->vars.n,
2011 for (size_t k = 0; k < n; k++)
2013 struct ctables_freq *prev = k > 0 ? ft->sorted[k - 1] : NULL;
2014 struct ctables_freq *f = ft->sorted[k];
2016 size_t n_common = 0;
2018 for (; n_common + 1 < ft->vars.n; n_common++)
2019 if (!value_equal (&prev->values[n_common],
2020 &f->values[n_common],
2021 var_get_type (ft->vars.vars[n_common])))
2024 for (size_t m = n_common; m < ft->vars.n; m++)
2026 struct pivot_category *parent = m > 0 ? groups[m - 1] : d->root;
2027 const struct variable *var = ft->vars.vars[m];
2028 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (var)];
2030 if (vlabel != CTVL_NONE)
2031 parent = pivot_category_create_group__ (
2032 parent, pivot_value_new_variable (ft->vars.vars[m]));
2034 if (m + 1 < ft->vars.n)
2035 parent = pivot_category_create_group__ (
2037 pivot_value_new_var_value (ft->vars.vars[m], &f->values[m]));
2040 if (m == ft->vars.n - 1)
2042 struct pivot_category *c = pivot_category_create_group__ (
2044 pivot_value_new_var_value (ft->vars.vars[ft->vars.n - 1],
2045 &f->values[ft->vars.n - 1]));
2046 for (size_t p = 0; p < ft->n_summaries; p++)
2048 double value = ctables_summary_value (
2049 &f->summaries[p], &ft->summaries[p]);
2050 int leaf = pivot_category_create_leaf (
2051 c, pivot_value_new_text (ft->summaries[p].label));
2052 pivot_table_put1 (pt, leaf, pivot_value_new_number (value));
2059 pivot_table_submit (pt);
2062 for (size_t i = 0; i < ct->n_tables; i++)
2064 struct ctables_table *t = &ct->tables[i];
2066 for (size_t j = 0; j < t->n_fts; j++)
2068 struct ctables_freqtab *ft = t->fts[j];
2069 struct ctables_freq *f, *next;
2070 HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2072 hmap_delete (&ft->data, &f->node);
2073 for (size_t k = 0; k < ft->n_summaries; k++)
2074 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2075 free (f->summaries);
2076 for (size_t k = 0; k < ft->vars.n; k++)
2078 const struct variable *var = ft->vars.vars[k];
2079 value_destroy (&f->values[k], var_get_width (var));
2083 hmap_destroy (&ft->data);
2085 var_array_uninit (&ft->vars);
2091 return proc_commit (ds);
2095 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2097 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2098 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2099 enum settings_value_show tvars = settings_get_show_variables ();
2100 for (size_t i = 0; i < n_vars; i++)
2101 vlabels[i] = (enum ctables_vlabel) tvars;
2103 struct ctables *ct = xmalloc (sizeof *ct);
2104 *ct = (struct ctables) {
2105 .look = pivot_table_look_unshare (pivot_table_look_ref (
2106 pivot_table_look_get_default ())),
2108 .hide_threshold = 5,
2111 if (!lex_force_match (lexer, T_SLASH))
2114 while (!lex_match_id (lexer, "TABLE"))
2116 if (lex_match_id (lexer, "FORMAT"))
2118 double widths[2] = { SYSMIS, SYSMIS };
2119 double units_per_inch = 72.0;
2121 while (lex_token (lexer) != T_SLASH)
2123 if (lex_match_id (lexer, "MINCOLWIDTH"))
2125 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2128 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2130 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2133 else if (lex_match_id (lexer, "UNITS"))
2135 lex_match (lexer, T_EQUALS);
2136 if (lex_match_id (lexer, "POINTS"))
2137 units_per_inch = 72.0;
2138 else if (lex_match_id (lexer, "INCHES"))
2139 units_per_inch = 1.0;
2140 else if (lex_match_id (lexer, "CM"))
2141 units_per_inch = 2.54;
2144 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2148 else if (lex_match_id (lexer, "EMPTY"))
2153 lex_match (lexer, T_EQUALS);
2154 if (lex_match_id (lexer, "ZERO"))
2156 /* Nothing to do. */
2158 else if (lex_match_id (lexer, "BLANK"))
2159 ct->zero = xstrdup ("");
2160 else if (lex_force_string (lexer))
2162 ct->zero = ss_xstrdup (lex_tokss (lexer));
2168 else if (lex_match_id (lexer, "MISSING"))
2170 lex_match (lexer, T_EQUALS);
2171 if (!lex_force_string (lexer))
2175 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2176 ? ss_xstrdup (lex_tokss (lexer))
2182 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2183 "UNITS", "EMPTY", "MISSING");
2188 if (widths[0] != SYSMIS && widths[1] != SYSMIS
2189 && widths[0] > widths[1])
2191 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2195 for (size_t i = 0; i < 2; i++)
2196 if (widths[i] != SYSMIS)
2198 int *wr = ct->look->width_ranges[TABLE_HORZ];
2199 wr[i] = widths[i] / units_per_inch * 96.0;
2204 else if (lex_match_id (lexer, "VLABELS"))
2206 if (!lex_force_match_id (lexer, "VARIABLES"))
2208 lex_match (lexer, T_EQUALS);
2210 struct variable **vars;
2212 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2216 if (!lex_force_match_id (lexer, "DISPLAY"))
2221 lex_match (lexer, T_EQUALS);
2223 enum ctables_vlabel vlabel;
2224 if (lex_match_id (lexer, "DEFAULT"))
2225 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2226 else if (lex_match_id (lexer, "NAME"))
2228 else if (lex_match_id (lexer, "LABEL"))
2229 vlabel = CTVL_LABEL;
2230 else if (lex_match_id (lexer, "BOTH"))
2232 else if (lex_match_id (lexer, "NONE"))
2236 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2242 for (size_t i = 0; i < n_vars; i++)
2243 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2246 else if (lex_match_id (lexer, "MRSETS"))
2248 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2250 lex_match (lexer, T_EQUALS);
2251 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2254 else if (lex_match_id (lexer, "SMISSING"))
2256 if (lex_match_id (lexer, "VARIABLE"))
2257 ct->smissing_listwise = false;
2258 else if (lex_match_id (lexer, "LISTWISE"))
2259 ct->smissing_listwise = true;
2262 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2267 else if (lex_match_id (lexer, "WEIGHT"))
2269 if (!lex_force_match_id (lexer, "VARIABLE"))
2271 lex_match (lexer, T_EQUALS);
2272 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2273 if (!ct->base_weight)
2276 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2278 if (!lex_force_match_id (lexer, "COUNT"))
2280 lex_match (lexer, T_EQUALS);
2281 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2283 ct->hide_threshold = lex_integer (lexer);
2288 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2289 "SMISSING", "PCOMPUTE", "PPROPERTIES",
2290 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2294 if (!lex_force_match (lexer, T_SLASH))
2298 size_t allocated_tables = 0;
2301 if (ct->n_tables >= allocated_tables)
2302 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2303 sizeof *ct->tables);
2305 struct ctables_table *t = &ct->tables[ct->n_tables++];
2306 *t = (struct ctables_table) {
2307 .slabels_position = PIVOT_AXIS_COLUMN,
2308 .slabels_visible = true,
2309 .row_labels = CTLP_NORMAL,
2310 .col_labels = CTLP_NORMAL,
2311 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2312 sizeof *t->categories),
2313 .n_categories = dict_get_n_vars (dataset_dict (ds)),
2317 lex_match (lexer, T_EQUALS);
2318 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2320 if (lex_match (lexer, T_BY))
2322 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2323 ct, t, PIVOT_AXIS_COLUMN))
2326 if (lex_match (lexer, T_BY))
2328 if (!ctables_axis_parse (lexer, dataset_dict (ds),
2329 ct, t, PIVOT_AXIS_LAYER))
2334 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2335 && !t->axes[PIVOT_AXIS_LAYER])
2337 lex_error (lexer, _("At least one variable must be specified."));
2341 const struct ctables_axis *scales[PIVOT_N_AXES];
2342 size_t n_scales = 0;
2343 for (size_t i = 0; i < 3; i++)
2345 scales[i] = find_scale (t->axes[i]);
2351 msg (SE, _("Scale variables may appear only on one dimension."));
2352 if (scales[PIVOT_AXIS_ROW])
2353 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2354 _("This scale variable appears in the rows dimension."));
2355 if (scales[PIVOT_AXIS_COLUMN])
2356 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2357 _("This scale variable appears in the columns dimension."));
2358 if (scales[PIVOT_AXIS_LAYER])
2359 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2360 _("This scale variable appears in the layer dimension."));
2364 if (lex_token (lexer) == T_ENDCMD)
2366 if (!lex_force_match (lexer, T_SLASH))
2369 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2371 if (lex_match_id (lexer, "SLABELS"))
2373 while (lex_token (lexer) != T_SLASH)
2375 if (lex_match_id (lexer, "POSITION"))
2377 lex_match (lexer, T_EQUALS);
2378 if (lex_match_id (lexer, "COLUMN"))
2379 t->slabels_position = PIVOT_AXIS_COLUMN;
2380 else if (lex_match_id (lexer, "ROW"))
2381 t->slabels_position = PIVOT_AXIS_ROW;
2382 else if (lex_match_id (lexer, "LAYER"))
2383 t->slabels_position = PIVOT_AXIS_LAYER;
2386 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2390 else if (lex_match_id (lexer, "VISIBLE"))
2392 lex_match (lexer, T_EQUALS);
2393 if (!parse_bool (lexer, &t->slabels_visible))
2398 lex_error_expecting (lexer, "POSITION", "VISIBLE");
2403 else if (lex_match_id (lexer, "CLABELS"))
2405 while (lex_token (lexer) != T_SLASH)
2407 if (lex_match_id (lexer, "AUTO"))
2408 t->row_labels = t->col_labels = CTLP_NORMAL;
2409 else if (lex_match_id (lexer, "ROWLABELS"))
2411 lex_match (lexer, T_EQUALS);
2412 if (lex_match_id (lexer, "OPPOSITE"))
2413 t->row_labels = CTLP_OPPOSITE;
2414 else if (lex_match_id (lexer, "LAYER"))
2415 t->row_labels = CTLP_LAYER;
2418 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2422 else if (lex_match_id (lexer, "COLLABELS"))
2424 lex_match (lexer, T_EQUALS);
2425 if (lex_match_id (lexer, "OPPOSITE"))
2426 t->col_labels = CTLP_OPPOSITE;
2427 else if (lex_match_id (lexer, "LAYER"))
2428 t->col_labels = CTLP_LAYER;
2431 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2437 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2443 else if (lex_match_id (lexer, "CRITERIA"))
2445 if (!lex_force_match_id (lexer, "CILEVEL"))
2447 lex_match (lexer, T_EQUALS);
2449 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2451 t->cilevel = lex_number (lexer);
2454 else if (lex_match_id (lexer, "CATEGORIES"))
2456 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2459 else if (lex_match_id (lexer, "TITLES"))
2464 if (lex_match_id (lexer, "CAPTION"))
2465 textp = &t->caption;
2466 else if (lex_match_id (lexer, "CORNER"))
2468 else if (lex_match_id (lexer, "TITLE"))
2472 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2475 lex_match (lexer, T_EQUALS);
2477 struct string s = DS_EMPTY_INITIALIZER;
2478 while (lex_is_string (lexer))
2480 if (!ds_is_empty (&s))
2481 ds_put_byte (&s, ' ');
2482 ds_put_substring (&s, lex_tokss (lexer));
2486 *textp = ds_steal_cstr (&s);
2488 while (lex_token (lexer) != T_SLASH
2489 && lex_token (lexer) != T_ENDCMD);
2491 else if (lex_match_id (lexer, "SIGTEST"))
2495 t->chisq = xmalloc (sizeof *t->chisq);
2496 *t->chisq = (struct ctables_chisq) {
2498 .include_mrsets = true,
2499 .all_visible = true,
2505 if (lex_match_id (lexer, "TYPE"))
2507 lex_match (lexer, T_EQUALS);
2508 if (!lex_force_match_id (lexer, "CHISQUARE"))
2511 else if (lex_match_id (lexer, "ALPHA"))
2513 lex_match (lexer, T_EQUALS);
2514 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2516 t->chisq->alpha = lex_number (lexer);
2519 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2521 lex_match (lexer, T_EQUALS);
2522 if (parse_bool (lexer, &t->chisq->include_mrsets))
2525 else if (lex_match_id (lexer, "CATEGORIES"))
2527 lex_match (lexer, T_EQUALS);
2528 if (lex_match_id (lexer, "ALLVISIBLE"))
2529 t->chisq->all_visible = true;
2530 else if (lex_match_id (lexer, "SUBTOTALS"))
2531 t->chisq->all_visible = false;
2534 lex_error_expecting (lexer,
2535 "ALLVISIBLE", "SUBTOTALS");
2541 lex_error_expecting (lexer, "TYPE", "ALPHA",
2542 "INCLUDEMRSETS", "CATEGORIES");
2546 while (lex_token (lexer) != T_SLASH
2547 && lex_token (lexer) != T_ENDCMD);
2549 else if (lex_match_id (lexer, "COMPARETEST"))
2553 t->pairwise = xmalloc (sizeof *t->pairwise);
2554 *t->pairwise = (struct ctables_pairwise) {
2556 .alpha = { .05, .05 },
2557 .adjust = BONFERRONI,
2558 .include_mrsets = true,
2559 .meansvariance_allcats = true,
2560 .all_visible = true,
2569 if (lex_match_id (lexer, "TYPE"))
2571 lex_match (lexer, T_EQUALS);
2572 if (lex_match_id (lexer, "PROP"))
2573 t->pairwise->type = PROP;
2574 else if (lex_match_id (lexer, "MEAN"))
2575 t->pairwise->type = MEAN;
2578 lex_error_expecting (lexer, "PROP", "MEAN");
2582 else if (lex_match_id (lexer, "ALPHA"))
2584 lex_match (lexer, T_EQUALS);
2586 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2588 double a0 = lex_number (lexer);
2591 lex_match (lexer, T_COMMA);
2592 if (lex_is_number (lexer))
2594 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2596 double a1 = lex_number (lexer);
2599 t->pairwise->alpha[0] = MIN (a0, a1);
2600 t->pairwise->alpha[1] = MAX (a0, a1);
2603 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2605 else if (lex_match_id (lexer, "ADJUST"))
2607 lex_match (lexer, T_EQUALS);
2608 if (lex_match_id (lexer, "BONFERRONI"))
2609 t->pairwise->adjust = BONFERRONI;
2610 else if (lex_match_id (lexer, "BH"))
2611 t->pairwise->adjust = BH;
2612 else if (lex_match_id (lexer, "NONE"))
2613 t->pairwise->adjust = 0;
2616 lex_error_expecting (lexer, "BONFERRONI", "BH",
2621 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2623 lex_match (lexer, T_EQUALS);
2624 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2627 else if (lex_match_id (lexer, "MEANSVARIANCE"))
2629 lex_match (lexer, T_EQUALS);
2630 if (lex_match_id (lexer, "ALLCATS"))
2631 t->pairwise->meansvariance_allcats = true;
2632 else if (lex_match_id (lexer, "TESTEDCATS"))
2633 t->pairwise->meansvariance_allcats = false;
2636 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
2640 else if (lex_match_id (lexer, "CATEGORIES"))
2642 lex_match (lexer, T_EQUALS);
2643 if (lex_match_id (lexer, "ALLVISIBLE"))
2644 t->pairwise->all_visible = true;
2645 else if (lex_match_id (lexer, "SUBTOTALS"))
2646 t->pairwise->all_visible = false;
2649 lex_error_expecting (lexer, "ALLVISIBLE",
2654 else if (lex_match_id (lexer, "MERGE"))
2656 lex_match (lexer, T_EQUALS);
2657 if (!parse_bool (lexer, &t->pairwise->merge))
2660 else if (lex_match_id (lexer, "STYLE"))
2662 lex_match (lexer, T_EQUALS);
2663 if (lex_match_id (lexer, "APA"))
2664 t->pairwise->apa_style = true;
2665 else if (lex_match_id (lexer, "SIMPLE"))
2666 t->pairwise->apa_style = false;
2669 lex_error_expecting (lexer, "APA", "SIMPLE");
2673 else if (lex_match_id (lexer, "SHOWSIG"))
2675 lex_match (lexer, T_EQUALS);
2676 if (!parse_bool (lexer, &t->pairwise->show_sig))
2681 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
2682 "INCLUDEMRSETS", "MEANSVARIANCE",
2683 "CATEGORIES", "MERGE", "STYLE",
2688 while (lex_token (lexer) != T_SLASH
2689 && lex_token (lexer) != T_ENDCMD);
2693 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
2694 "CRITERIA", "CATEGORIES", "TITLES",
2695 "SIGTEST", "COMPARETEST");
2700 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
2702 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
2707 while (lex_token (lexer) != T_ENDCMD);
2709 bool ok = ctables_execute (ds, ct);
2710 ctables_destroy (ct);
2711 return ok ? CMD_SUCCESS : CMD_FAILURE;
2714 ctables_destroy (ct);