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/>. */
19 #include "data/dataset.h"
20 #include "data/dictionary.h"
21 #include "data/mrset.h"
22 #include "language/command.h"
23 #include "language/lexer/format-parser.h"
24 #include "language/lexer/lexer.h"
25 #include "language/lexer/variable-parser.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/hmap.h"
28 #include "libpspp/message.h"
29 #include "output/pivot-table.h"
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) (msgid)
40 CTVL_DEFAULT = SETTINGS_VALUE_SHOW_DEFAULT,
41 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
42 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
43 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
47 ctables_vlabel_unique (enum ctables_vlabel vlabel)
49 /* This ensures that all of the values are unique. */
62 - unweighted summaries (U*)
63 - lower confidence limits (*.LCL)
64 - upper confidence limits (*.UCL)
65 - standard error (*.SE)
68 /* All variables. */ \
69 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
70 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
71 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
90 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
91 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
93 /* Scale variables, totals, and subtotals. */ \
94 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
103 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
104 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
105 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
106 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
107 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
108 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
109 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
110 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
112 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
113 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
114 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
115 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
118 /* Multiple response sets. */ \
119 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
120 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
139 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
140 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
142 enum ctables_summary_function
144 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
150 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
151 N_CTSF_FUNCTIONS = SUMMARIES
157 struct pivot_table_look *look;
159 /* If this is NULL, zeros are displayed using the normal print format.
160 Otherwise, this string is displayed. */
163 /* If this is NULL, missing values are displayed using the normal print
164 format. Otherwise, this string is displayed. */
167 /* Indexed by variable dictionary index. */
168 enum ctables_vlabel *vlabels;
170 bool mrsets_count_duplicates; /* MRSETS. */
171 bool smissing_listwise; /* SMISSING. */
172 struct variable *base_weight; /* WEIGHT. */
173 int hide_threshold; /* HIDESMALLCOUNTS. */
175 struct ctables_table *tables;
179 struct ctables_postcompute
181 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
182 const char *name; /* Name, without leading &. */
184 struct ctables_postcompute_expr *expr;
187 bool hide_source_cats;
190 struct ctables_postcompute_expr
192 enum ctables_postcompute_op
200 /* XXX SUBTOTAL and HSUBTOTAL */
213 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
218 XXX what about string ranges? */
221 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
222 struct ctables_postcompute_expr *subs[2];
226 enum ctables_label_position
235 struct ctables_axis *axes[PIVOT_N_AXES];
237 enum pivot_axis_type slabels_position;
238 bool slabels_visible;
240 enum ctables_label_position row_labels;
241 enum ctables_label_position col_labels;
243 /* Indexed by variable dictionary index. */
244 struct ctables_categories **categories;
253 struct ctables_chisq *chisq;
254 struct ctables_pairwise *pairwise;
262 struct variable *var;
263 const struct mrset *mrset;
267 static const struct fmt_spec *
268 ctables_var_get_print_format (const struct ctables_var *var)
270 return (var->is_mrset
271 ? var_get_print_format (var->mrset->vars[0])
272 : var_get_print_format (var->var));
276 ctables_var_name (const struct ctables_var *var)
278 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
281 struct ctables_categories
285 /* Explicit categories. */
286 struct ctables_cat_value *values;
289 /* Implicit categories. */
291 bool include_missing;
292 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
293 enum ctables_summary_function sort_func;
294 struct variable *sort_func_var;
302 /* Empty categories. */
306 struct ctables_cat_value
308 enum ctables_cat_value_type
322 double number; /* CCVT_NUMBER. */
323 char *string; /* CCVT_STRING. */
324 double range[2]; /* CCVT_RANGE. */
325 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
330 ctables_cat_value_uninit (struct ctables_cat_value *cv)
349 free (cv->subtotal_label);
354 ctables_categories_unref (struct ctables_categories *c)
359 assert (c->n_refs > 0);
363 for (size_t i = 0; i < c->n_values; i++)
364 ctables_cat_value_uninit (&c->values[i]);
366 free (c->total_label);
370 /* Chi-square test (SIGTEST). */
378 /* Pairwise comparison test (COMPARETEST). */
379 struct ctables_pairwise
381 enum { PROP, MEAN } type;
384 bool meansvariance_allcats;
386 enum { BONFERRONI = 1, BH } adjust;
410 struct ctables_var var;
412 struct ctables_summary *summaries;
414 size_t allocated_summaries;
418 struct ctables_axis *subs[2];
421 struct msg_location *loc;
424 static void ctables_axis_destroy (struct ctables_axis *);
433 enum ctables_function_availability
435 CTFA_ALL, /* Any variables. */
436 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
437 CTFA_MRSETS, /* Only multiple-response sets */
440 struct ctables_summary
442 enum ctables_summary_function function;
443 double percentile; /* CTSF_PTILE only. */
445 struct fmt_spec format; /* XXX extra CTABLES formats */
449 ctables_summary_uninit (struct ctables_summary *s)
456 parse_col_width (struct lexer *lexer, const char *name, double *width)
458 lex_match (lexer, T_EQUALS);
459 if (lex_match_id (lexer, "DEFAULT"))
461 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
463 *width = lex_number (lexer);
473 parse_bool (struct lexer *lexer, bool *b)
475 if (lex_match_id (lexer, "NO"))
477 else if (lex_match_id (lexer, "YES"))
481 lex_error_expecting (lexer, "YES", "NO");
487 static enum ctables_function_availability
488 ctables_function_availability (enum ctables_summary_function f)
490 static enum ctables_function_availability availability[] = {
491 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
496 return availability[f];
500 parse_ctables_summary_function (struct lexer *lexer,
501 enum ctables_summary_function *f)
505 enum ctables_summary_function function;
506 struct substring name;
508 static struct pair names[] = {
509 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
510 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
513 /* The .COUNT suffix may be omitted. */
514 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
515 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
516 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
517 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
518 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
519 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
520 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
524 if (!lex_force_id (lexer))
527 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
528 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
530 *f = names[i].function;
535 lex_error (lexer, _("Expecting summary function name."));
540 ctables_axis_destroy (struct ctables_axis *axis)
548 for (size_t i = 0; i < axis->n_summaries; i++)
549 ctables_summary_uninit (&axis->summaries[i]);
550 free (axis->summaries);
555 ctables_axis_destroy (axis->subs[0]);
556 ctables_axis_destroy (axis->subs[1]);
559 msg_location_destroy (axis->loc);
563 static struct ctables_axis *
564 ctables_axis_new_nonterminal (enum ctables_axis_op op,
565 struct ctables_axis *sub0,
566 struct ctables_axis *sub1,
567 struct lexer *lexer, int start_ofs)
569 struct ctables_axis *axis = xmalloc (sizeof *axis);
570 *axis = (struct ctables_axis) {
572 .subs = { sub0, sub1 },
573 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
578 struct ctables_axis_parse_ctx
581 struct dictionary *dict;
583 struct ctables_table *t;
586 static struct fmt_spec
587 ctables_summary_default_format (enum ctables_summary_function function,
588 const struct ctables_var *var)
590 static const enum ctables_format default_formats[] = {
591 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
595 switch (default_formats[function])
598 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
601 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
604 return *ctables_var_get_print_format (var);
612 ctables_summary_function_name (enum ctables_summary_function function)
614 static const char *names[] = {
615 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
619 return names[function];
623 add_summary (struct ctables_axis *axis,
624 enum ctables_summary_function function, double percentile,
625 const char *label, const struct fmt_spec *format,
626 const struct msg_location *loc)
628 if (axis->op == CTAO_VAR)
630 if (axis->n_summaries >= axis->allocated_summaries)
631 axis->summaries = x2nrealloc (axis->summaries,
632 &axis->allocated_summaries,
633 sizeof *axis->summaries);
635 const char *function_name = ctables_summary_function_name (function);
636 const char *var_name = ctables_var_name (&axis->var);
637 switch (ctables_function_availability (function))
640 if (!axis->var.is_mrset)
642 msg_at (SE, loc, _("Summary function %s applies only to multiple "
643 "response sets."), function_name);
644 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
654 _("Summary function %s applies only to scale variables."),
656 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
666 struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
667 *dst = (struct ctables_summary) {
668 .function = function,
669 .percentile = percentile,
670 .label = xstrdup (label),
671 .format = (format ? *format
672 : ctables_summary_default_format (function, &axis->var)),
678 for (size_t i = 0; i < 2; i++)
679 if (!add_summary (axis->subs[i], function, percentile, label, format,
686 static struct ctables_axis *ctables_axis_parse_stack (
687 struct ctables_axis_parse_ctx *);
690 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
691 struct ctables_var *var)
693 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
695 *var = (struct ctables_var) {
697 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
701 lex_error (lexer, _("'%s' does not name a multiple-response set "
702 "in the active file dictionary."),
703 lex_tokcstr (lexer));
711 *var = (struct ctables_var) {
713 .var = parse_variable (lexer, dict),
715 return var->var != NULL;
719 static struct ctables_axis *
720 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
722 if (lex_match (ctx->lexer, T_LPAREN))
724 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
725 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
727 ctables_axis_destroy (sub);
733 if (!lex_force_id (ctx->lexer))
736 int start_ofs = lex_ofs (ctx->lexer);
737 struct ctables_var var;
738 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
741 struct ctables_axis *axis = xmalloc (sizeof *axis);
742 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
744 /* XXX should figure out default measures by reading data */
745 axis->scale = (var.is_mrset ? false
746 : lex_match_phrase (ctx->lexer, "[S]") ? true
747 : lex_match_phrase (ctx->lexer, "[C]") ? false
748 : var_get_measure (var.var) == MEASURE_SCALE);
749 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
750 lex_ofs (ctx->lexer) - 1);
754 static struct ctables_axis *
755 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
757 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
758 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
763 int start_ofs = lex_ofs (ctx->lexer);
765 /* Parse function. */
766 enum ctables_summary_function function;
767 if (!parse_ctables_summary_function (ctx->lexer, &function))
770 /* Parse percentile. */
771 double percentile = 0;
772 if (function == CTSF_PTILE)
774 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
776 percentile = lex_number (ctx->lexer);
777 lex_get (ctx->lexer);
782 if (lex_is_string (ctx->lexer))
784 label = ss_xstrdup (lex_tokss (ctx->lexer));
785 lex_get (ctx->lexer);
787 else if (function == CTSF_PTILE)
788 label = xasprintf (_("Percentile %.2f"), percentile);
791 static const char *default_labels[] = {
792 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
796 label = xstrdup (gettext (default_labels[function]));
800 struct fmt_spec format;
801 const struct fmt_spec *formatp;
802 if (lex_token (ctx->lexer) == T_ID)
804 if (!parse_format_specifier (ctx->lexer, &format)
805 || !fmt_check_output (&format)
806 || !fmt_check_type_compat (&format, VAL_NUMERIC))
816 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
817 lex_ofs (ctx->lexer) - 1);
818 add_summary (sub, function, percentile, label, formatp, loc);
820 msg_location_destroy (loc);
822 lex_match (ctx->lexer, T_COMMA);
824 while (!lex_match (ctx->lexer, T_RBRACK));
829 ctables_axis_destroy (sub);
833 static const struct ctables_axis *
834 find_scale (const struct ctables_axis *axis)
838 else if (axis->op == CTAO_VAR)
842 assert (!axis->var.is_mrset);
850 for (size_t i = 0; i < 2; i++)
852 const struct ctables_axis *scale = find_scale (axis->subs[i]);
860 static const struct ctables_axis *
861 find_categorical_summary (const struct ctables_axis *axis)
865 else if (axis->op == CTAO_VAR)
866 return !axis->scale && axis->n_summaries ? axis : NULL;
869 for (size_t i = 0; i < 2; i++)
871 const struct ctables_axis *sum
872 = find_categorical_summary (axis->subs[i]);
880 static struct ctables_axis *
881 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
883 int start_ofs = lex_ofs (ctx->lexer);
884 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
888 while (lex_match (ctx->lexer, T_GT))
890 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
894 struct ctables_axis *nest = ctables_axis_new_nonterminal (
895 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
897 const struct ctables_axis *outer_scale = find_scale (lhs);
898 const struct ctables_axis *inner_scale = find_scale (rhs);
899 if (outer_scale && inner_scale)
901 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
902 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
903 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
904 ctables_axis_destroy (nest);
908 const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
911 msg_at (SE, nest->loc,
912 _("Summaries may only be requested for categorical variables "
913 "at the innermost nesting level."));
914 msg_at (SN, outer_sum->loc,
915 _("This outer categorical variable has a summary."));
916 ctables_axis_destroy (nest);
926 static struct ctables_axis *
927 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
929 int start_ofs = lex_ofs (ctx->lexer);
930 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
934 while (lex_match (ctx->lexer, T_PLUS))
936 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
940 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
941 ctx->lexer, start_ofs);
948 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
949 struct ctables *ct, struct ctables_table *t,
950 enum pivot_axis_type a)
952 if (lex_token (lexer) == T_BY
953 || lex_token (lexer) == T_SLASH
954 || lex_token (lexer) == T_ENDCMD)
957 struct ctables_axis_parse_ctx ctx = {
963 t->axes[a] = ctables_axis_parse_stack (&ctx);
964 return t->axes[a] != NULL;
968 ctables_chisq_destroy (struct ctables_chisq *chisq)
974 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
980 ctables_table_uninit (struct ctables_table *t)
985 for (size_t i = 0; i < t->n_categories; i++)
986 ctables_categories_unref (t->categories[i]);
987 free (t->categories);
989 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
990 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
991 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
995 ctables_chisq_destroy (t->chisq);
996 ctables_pairwise_destroy (t->pairwise);
1000 ctables_destroy (struct ctables *ct)
1005 pivot_table_look_unref (ct->look);
1009 for (size_t i = 0; i < ct->n_tables; i++)
1010 ctables_table_uninit (&ct->tables[i]);
1015 static struct ctables_cat_value
1016 ccvt_range (double low, double high)
1018 return (struct ctables_cat_value) {
1020 .range = { low, high }
1025 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1026 struct ctables_table *t)
1028 if (!lex_match_id (lexer, "VARIABLES"))
1030 lex_match (lexer, T_EQUALS);
1032 struct variable **vars;
1034 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1037 struct ctables_categories *c = xmalloc (sizeof *c);
1038 *c = (struct ctables_categories) { .n_refs = n_vars };
1039 for (size_t i = 0; i < n_vars; i++)
1041 struct ctables_categories **cp
1042 = &t->categories[var_get_dict_index (vars[i])];
1043 ctables_categories_unref (*cp);
1048 if (lex_match (lexer, T_LBRACK))
1050 size_t allocated_values = 0;
1053 if (c->n_values >= allocated_values)
1054 c->values = x2nrealloc (c->values, &allocated_values,
1057 struct ctables_cat_value *v = &c->values[c->n_values];
1058 if (lex_match_id (lexer, "OTHERNM"))
1059 v->type = CCVT_OTHERNM;
1060 else if (lex_match_id (lexer, "MISSING"))
1061 v->type = CCVT_MISSING;
1062 else if (lex_match_id (lexer, "SUBTOTAL"))
1063 *v = (struct ctables_cat_value)
1064 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1065 else if (lex_match_id (lexer, "HSUBTOTAL"))
1066 *v = (struct ctables_cat_value)
1067 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1068 else if (lex_match_id (lexer, "LO"))
1070 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1072 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1075 else if (lex_is_number (lexer))
1077 double number = lex_number (lexer);
1079 if (lex_match_id (lexer, "THRU"))
1081 v->type = CCVT_RANGE;
1082 v->range[0] = number;
1083 if (lex_match_id (lexer, "HI"))
1084 *v = ccvt_range (number, DBL_MAX);
1087 if (!lex_force_num (lexer))
1089 *v = ccvt_range (number, lex_number (lexer));
1094 *v = (struct ctables_cat_value) {
1095 .type = CCVT_NUMBER,
1099 else if (lex_is_string (lexer))
1101 *v = (struct ctables_cat_value) {
1102 .type = CCVT_STRING,
1103 .string = ss_xstrdup (lex_tokss (lexer)),
1109 lex_error (lexer, NULL);
1113 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1114 && lex_match (lexer, T_EQUALS))
1116 if (!lex_force_string (lexer))
1119 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1124 lex_match (lexer, T_COMMA);
1126 while (!lex_match (lexer, T_RBRACK));
1129 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1131 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1133 lex_match (lexer, T_EQUALS);
1134 if (lex_match_id (lexer, "A"))
1135 c->sort_ascending = true;
1136 else if (lex_match_id (lexer, "D"))
1137 c->sort_ascending = false;
1140 lex_error_expecting (lexer, "A", "D");
1144 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1146 lex_match (lexer, T_EQUALS);
1147 if (lex_match_id (lexer, "VALUE"))
1148 c->key = CTCS_VALUE;
1149 else if (lex_match_id (lexer, "LABEL"))
1150 c->key = CTCS_LABEL;
1153 c->key = CTCS_FUNCTION;
1154 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1157 if (lex_match (lexer, T_LPAREN))
1159 c->sort_func_var = parse_variable (lexer, dict);
1160 if (!c->sort_func_var)
1163 if (c->sort_func == CTSF_PTILE)
1165 lex_match (lexer, T_COMMA);
1166 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1168 c->percentile = lex_number (lexer);
1172 if (!lex_force_match (lexer, T_RPAREN))
1175 else if (ctables_function_availability (c->sort_func)
1178 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1183 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1185 lex_match (lexer, T_EQUALS);
1186 if (lex_match_id (lexer, "INCLUDE"))
1187 c->include_missing = true;
1188 else if (lex_match_id (lexer, "EXCLUDE"))
1189 c->include_missing = false;
1192 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1196 else if (lex_match_id (lexer, "TOTAL"))
1198 lex_match (lexer, T_EQUALS);
1199 if (!parse_bool (lexer, &c->show_totals))
1202 else if (lex_match_id (lexer, "LABEL"))
1204 lex_match (lexer, T_EQUALS);
1205 if (!lex_force_string (lexer))
1207 free (c->total_label);
1208 c->total_label = ss_xstrdup (lex_tokss (lexer));
1211 else if (lex_match_id (lexer, "POSITION"))
1213 lex_match (lexer, T_EQUALS);
1214 if (lex_match_id (lexer, "BEFORE"))
1215 c->totals_before = true;
1216 else if (lex_match_id (lexer, "AFTER"))
1217 c->totals_before = false;
1220 lex_error_expecting (lexer, "BEFORE", "AFTER");
1224 else if (lex_match_id (lexer, "EMPTY"))
1226 lex_match (lexer, T_EQUALS);
1227 if (lex_match_id (lexer, "INCLUDE"))
1228 c->show_empty = true;
1229 else if (lex_match_id (lexer, "EXCLUDE"))
1230 c->show_empty = false;
1233 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1240 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1241 "TOTAL", "LABEL", "POSITION", "EMPTY");
1243 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1251 cmd_ctables (struct lexer *lexer, struct dataset *ds)
1253 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
1254 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
1255 for (size_t i = 0; i < n_vars; i++)
1256 vlabels[i] = CTVL_DEFAULT;
1258 struct ctables *ct = xmalloc (sizeof *ct);
1259 *ct = (struct ctables) {
1260 .look = pivot_table_look_unshare (pivot_table_look_ref (
1261 pivot_table_look_get_default ())),
1263 .hide_threshold = 5,
1266 if (!lex_force_match (lexer, T_SLASH))
1269 while (!lex_match_id (lexer, "TABLE"))
1271 if (lex_match_id (lexer, "FORMAT"))
1273 double widths[2] = { SYSMIS, SYSMIS };
1274 double units_per_inch = 72.0;
1276 while (lex_token (lexer) != T_SLASH)
1278 if (lex_match_id (lexer, "MINCOLWIDTH"))
1280 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1283 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1285 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1288 else if (lex_match_id (lexer, "UNITS"))
1290 lex_match (lexer, T_EQUALS);
1291 if (lex_match_id (lexer, "POINTS"))
1292 units_per_inch = 72.0;
1293 else if (lex_match_id (lexer, "INCHES"))
1294 units_per_inch = 1.0;
1295 else if (lex_match_id (lexer, "CM"))
1296 units_per_inch = 2.54;
1299 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1303 else if (lex_match_id (lexer, "EMPTY"))
1308 lex_match (lexer, T_EQUALS);
1309 if (lex_match_id (lexer, "ZERO"))
1311 /* Nothing to do. */
1313 else if (lex_match_id (lexer, "BLANK"))
1314 ct->zero = xstrdup ("");
1315 else if (lex_force_string (lexer))
1317 ct->zero = ss_xstrdup (lex_tokss (lexer));
1323 else if (lex_match_id (lexer, "MISSING"))
1325 lex_match (lexer, T_EQUALS);
1326 if (!lex_force_string (lexer))
1330 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1331 ? ss_xstrdup (lex_tokss (lexer))
1337 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1338 "UNITS", "EMPTY", "MISSING");
1343 if (widths[0] != SYSMIS && widths[1] != SYSMIS
1344 && widths[0] > widths[1])
1346 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1350 for (size_t i = 0; i < 2; i++)
1351 if (widths[i] != SYSMIS)
1353 int *wr = ct->look->width_ranges[TABLE_HORZ];
1354 wr[i] = widths[i] / units_per_inch * 96.0;
1359 else if (lex_match_id (lexer, "VLABELS"))
1361 if (!lex_force_match_id (lexer, "VARIABLES"))
1363 lex_match (lexer, T_EQUALS);
1365 struct variable **vars;
1367 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1371 if (!lex_force_match_id (lexer, "DISPLAY"))
1376 lex_match (lexer, T_EQUALS);
1378 enum ctables_vlabel vlabel;
1379 if (lex_match_id (lexer, "DEFAULT"))
1380 vlabel = CTVL_DEFAULT;
1381 else if (lex_match_id (lexer, "NAME"))
1383 else if (lex_match_id (lexer, "LABEL"))
1384 vlabel = CTVL_LABEL;
1385 else if (lex_match_id (lexer, "BOTH"))
1387 else if (lex_match_id (lexer, "NONE"))
1391 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1397 for (size_t i = 0; i < n_vars; i++)
1398 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1401 else if (lex_match_id (lexer, "MRSETS"))
1403 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1405 lex_match (lexer, T_EQUALS);
1406 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1409 else if (lex_match_id (lexer, "SMISSING"))
1411 if (lex_match_id (lexer, "VARIABLE"))
1412 ct->smissing_listwise = false;
1413 else if (lex_match_id (lexer, "LISTWISE"))
1414 ct->smissing_listwise = true;
1417 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1422 else if (lex_match_id (lexer, "WEIGHT"))
1424 if (!lex_force_match_id (lexer, "VARIABLE"))
1426 lex_match (lexer, T_EQUALS);
1427 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1428 if (!ct->base_weight)
1431 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1433 if (!lex_force_match_id (lexer, "COUNT"))
1435 lex_match (lexer, T_EQUALS);
1436 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1438 ct->hide_threshold = lex_integer (lexer);
1443 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1444 "SMISSING", "PCOMPUTE", "PPROPERTIES",
1445 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1449 if (!lex_force_match (lexer, T_SLASH))
1453 size_t allocated_tables = 0;
1456 if (ct->n_tables >= allocated_tables)
1457 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1458 sizeof *ct->tables);
1460 struct ctables_table *t = &ct->tables[ct->n_tables++];
1461 *t = (struct ctables_table) {
1462 .slabels_position = PIVOT_AXIS_COLUMN,
1463 .slabels_visible = true,
1464 .row_labels = CTLP_NORMAL,
1465 .col_labels = CTLP_NORMAL,
1466 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
1467 sizeof *t->categories),
1468 .n_categories = dict_get_n_vars (dataset_dict (ds)),
1472 lex_match (lexer, T_EQUALS);
1473 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1475 if (lex_match (lexer, T_BY))
1477 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1478 ct, t, PIVOT_AXIS_COLUMN))
1481 if (lex_match (lexer, T_BY))
1483 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1484 ct, t, PIVOT_AXIS_LAYER))
1489 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1490 && !t->axes[PIVOT_AXIS_LAYER])
1492 lex_error (lexer, _("At least one variable must be specified."));
1496 const struct ctables_axis *scales[PIVOT_N_AXES];
1497 size_t n_scales = 0;
1498 for (size_t i = 0; i < 3; i++)
1500 scales[i] = find_scale (t->axes[i]);
1506 msg (SE, _("Scale variables may appear only on one dimension."));
1507 if (scales[PIVOT_AXIS_ROW])
1508 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
1509 _("This scale variable appears in the rows dimension."));
1510 if (scales[PIVOT_AXIS_COLUMN])
1511 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
1512 _("This scale variable appears in the columns dimension."));
1513 if (scales[PIVOT_AXIS_LAYER])
1514 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
1515 _("This scale variable appears in the layer dimension."));
1519 if (lex_token (lexer) == T_ENDCMD)
1521 if (!lex_force_match (lexer, T_SLASH))
1524 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1526 if (lex_match_id (lexer, "SLABELS"))
1528 while (lex_token (lexer) != T_SLASH)
1530 if (lex_match_id (lexer, "POSITION"))
1532 lex_match (lexer, T_EQUALS);
1533 if (lex_match_id (lexer, "COLUMN"))
1534 t->slabels_position = PIVOT_AXIS_COLUMN;
1535 else if (lex_match_id (lexer, "ROW"))
1536 t->slabels_position = PIVOT_AXIS_ROW;
1537 else if (lex_match_id (lexer, "LAYER"))
1538 t->slabels_position = PIVOT_AXIS_LAYER;
1541 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1545 else if (lex_match_id (lexer, "VISIBLE"))
1547 lex_match (lexer, T_EQUALS);
1548 if (!parse_bool (lexer, &t->slabels_visible))
1553 lex_error_expecting (lexer, "POSITION", "VISIBLE");
1558 else if (lex_match_id (lexer, "CLABELS"))
1560 while (lex_token (lexer) != T_SLASH)
1562 if (lex_match_id (lexer, "AUTO"))
1563 t->row_labels = t->col_labels = CTLP_NORMAL;
1564 else if (lex_match_id (lexer, "ROWLABELS"))
1566 lex_match (lexer, T_EQUALS);
1567 if (lex_match_id (lexer, "OPPOSITE"))
1568 t->row_labels = CTLP_OPPOSITE;
1569 else if (lex_match_id (lexer, "LAYER"))
1570 t->row_labels = CTLP_LAYER;
1573 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1577 else if (lex_match_id (lexer, "COLLABELS"))
1579 lex_match (lexer, T_EQUALS);
1580 if (lex_match_id (lexer, "OPPOSITE"))
1581 t->col_labels = CTLP_OPPOSITE;
1582 else if (lex_match_id (lexer, "LAYER"))
1583 t->col_labels = CTLP_LAYER;
1586 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1592 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1598 else if (lex_match_id (lexer, "CRITERIA"))
1600 if (!lex_force_match_id (lexer, "CILEVEL"))
1602 lex_match (lexer, T_EQUALS);
1604 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1606 t->cilevel = lex_number (lexer);
1609 else if (lex_match_id (lexer, "CATEGORIES"))
1611 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1614 else if (lex_match_id (lexer, "TITLES"))
1619 if (lex_match_id (lexer, "CAPTION"))
1620 textp = &t->caption;
1621 else if (lex_match_id (lexer, "CORNER"))
1623 else if (lex_match_id (lexer, "TITLE"))
1627 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1630 lex_match (lexer, T_EQUALS);
1632 struct string s = DS_EMPTY_INITIALIZER;
1633 while (lex_is_string (lexer))
1635 if (!ds_is_empty (&s))
1636 ds_put_byte (&s, ' ');
1637 ds_put_substring (&s, lex_tokss (lexer));
1641 *textp = ds_steal_cstr (&s);
1643 while (lex_token (lexer) != T_SLASH
1644 && lex_token (lexer) != T_ENDCMD);
1646 else if (lex_match_id (lexer, "SIGTEST"))
1650 t->chisq = xmalloc (sizeof *t->chisq);
1651 *t->chisq = (struct ctables_chisq) {
1653 .include_mrsets = true,
1654 .all_visible = true,
1660 if (lex_match_id (lexer, "TYPE"))
1662 lex_match (lexer, T_EQUALS);
1663 if (!lex_force_match_id (lexer, "CHISQUARE"))
1666 else if (lex_match_id (lexer, "ALPHA"))
1668 lex_match (lexer, T_EQUALS);
1669 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
1671 t->chisq->alpha = lex_number (lexer);
1674 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1676 lex_match (lexer, T_EQUALS);
1677 if (parse_bool (lexer, &t->chisq->include_mrsets))
1680 else if (lex_match_id (lexer, "CATEGORIES"))
1682 lex_match (lexer, T_EQUALS);
1683 if (lex_match_id (lexer, "ALLVISIBLE"))
1684 t->chisq->all_visible = true;
1685 else if (lex_match_id (lexer, "SUBTOTALS"))
1686 t->chisq->all_visible = false;
1689 lex_error_expecting (lexer,
1690 "ALLVISIBLE", "SUBTOTALS");
1696 lex_error_expecting (lexer, "TYPE", "ALPHA",
1697 "INCLUDEMRSETS", "CATEGORIES");
1701 while (lex_token (lexer) != T_SLASH
1702 && lex_token (lexer) != T_ENDCMD);
1704 else if (lex_match_id (lexer, "COMPARETEST"))
1708 t->pairwise = xmalloc (sizeof *t->pairwise);
1709 *t->pairwise = (struct ctables_pairwise) {
1711 .alpha = { .05, .05 },
1712 .adjust = BONFERRONI,
1713 .include_mrsets = true,
1714 .meansvariance_allcats = true,
1715 .all_visible = true,
1724 if (lex_match_id (lexer, "TYPE"))
1726 lex_match (lexer, T_EQUALS);
1727 if (lex_match_id (lexer, "PROP"))
1728 t->pairwise->type = PROP;
1729 else if (lex_match_id (lexer, "MEAN"))
1730 t->pairwise->type = MEAN;
1733 lex_error_expecting (lexer, "PROP", "MEAN");
1737 else if (lex_match_id (lexer, "ALPHA"))
1739 lex_match (lexer, T_EQUALS);
1741 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1743 double a0 = lex_number (lexer);
1746 lex_match (lexer, T_COMMA);
1747 if (lex_is_number (lexer))
1749 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1751 double a1 = lex_number (lexer);
1754 t->pairwise->alpha[0] = MIN (a0, a1);
1755 t->pairwise->alpha[1] = MAX (a0, a1);
1758 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
1760 else if (lex_match_id (lexer, "ADJUST"))
1762 lex_match (lexer, T_EQUALS);
1763 if (lex_match_id (lexer, "BONFERRONI"))
1764 t->pairwise->adjust = BONFERRONI;
1765 else if (lex_match_id (lexer, "BH"))
1766 t->pairwise->adjust = BH;
1767 else if (lex_match_id (lexer, "NONE"))
1768 t->pairwise->adjust = 0;
1771 lex_error_expecting (lexer, "BONFERRONI", "BH",
1776 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1778 lex_match (lexer, T_EQUALS);
1779 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
1782 else if (lex_match_id (lexer, "MEANSVARIANCE"))
1784 lex_match (lexer, T_EQUALS);
1785 if (lex_match_id (lexer, "ALLCATS"))
1786 t->pairwise->meansvariance_allcats = true;
1787 else if (lex_match_id (lexer, "TESTEDCATS"))
1788 t->pairwise->meansvariance_allcats = false;
1791 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
1795 else if (lex_match_id (lexer, "CATEGORIES"))
1797 lex_match (lexer, T_EQUALS);
1798 if (lex_match_id (lexer, "ALLVISIBLE"))
1799 t->pairwise->all_visible = true;
1800 else if (lex_match_id (lexer, "SUBTOTALS"))
1801 t->pairwise->all_visible = false;
1804 lex_error_expecting (lexer, "ALLVISIBLE",
1809 else if (lex_match_id (lexer, "MERGE"))
1811 lex_match (lexer, T_EQUALS);
1812 if (!parse_bool (lexer, &t->pairwise->merge))
1815 else if (lex_match_id (lexer, "STYLE"))
1817 lex_match (lexer, T_EQUALS);
1818 if (lex_match_id (lexer, "APA"))
1819 t->pairwise->apa_style = true;
1820 else if (lex_match_id (lexer, "SIMPLE"))
1821 t->pairwise->apa_style = false;
1824 lex_error_expecting (lexer, "APA", "SIMPLE");
1828 else if (lex_match_id (lexer, "SHOWSIG"))
1830 lex_match (lexer, T_EQUALS);
1831 if (!parse_bool (lexer, &t->pairwise->show_sig))
1836 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
1837 "INCLUDEMRSETS", "MEANSVARIANCE",
1838 "CATEGORIES", "MERGE", "STYLE",
1843 while (lex_token (lexer) != T_SLASH
1844 && lex_token (lexer) != T_ENDCMD);
1848 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
1849 "CRITERIA", "CATEGORIES", "TITLES",
1850 "SIGTEST", "COMPARETEST");
1855 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
1857 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
1862 while (lex_token (lexer) != T_ENDCMD);
1863 ctables_destroy (ct);
1867 ctables_destroy (ct);