1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014, 2016 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 - How to calculate significance of some symmetric and directional measures?
20 - How to calculate ASE for symmetric Somers ' d?
21 - How to calculate ASE for Goodman and Kruskal's tau?
22 - How to calculate approx. T of symmetric uncertainty coefficient?
30 #include <gsl/gsl_cdf.h>
34 #include "data/case.h"
35 #include "data/casegrouper.h"
36 #include "data/casereader.h"
37 #include "data/data-out.h"
38 #include "data/dataset.h"
39 #include "data/dictionary.h"
40 #include "data/format.h"
41 #include "data/value-labels.h"
42 #include "data/variable.h"
43 #include "language/command.h"
44 #include "language/stats/freq.h"
45 #include "language/dictionary/split-file.h"
46 #include "language/lexer/lexer.h"
47 #include "language/lexer/variable-parser.h"
48 #include "libpspp/array.h"
49 #include "libpspp/assertion.h"
50 #include "libpspp/compiler.h"
51 #include "libpspp/hash-functions.h"
52 #include "libpspp/hmap.h"
53 #include "libpspp/hmapx.h"
54 #include "libpspp/message.h"
55 #include "libpspp/misc.h"
56 #include "libpspp/pool.h"
57 #include "libpspp/str.h"
58 #include "output/pivot-table.h"
59 #include "output/charts/barchart.h"
61 #include "gl/minmax.h"
62 #include "gl/xalloc.h"
66 #define _(msgid) gettext (msgid)
67 #define N_(msgid) msgid
69 /* Kinds of cells in the crosstabulation. */
71 C(COUNT, N_("Count"), PIVOT_RC_COUNT) \
72 C(EXPECTED, N_("Expected"), PIVOT_RC_OTHER) \
73 C(ROW, N_("Row %"), PIVOT_RC_PERCENT) \
74 C(COLUMN, N_("Column %"), PIVOT_RC_PERCENT) \
75 C(TOTAL, N_("Total %"), PIVOT_RC_PERCENT) \
76 C(RESIDUAL, N_("Residual"), PIVOT_RC_RESIDUAL) \
77 C(SRESIDUAL, N_("Std. Residual"), PIVOT_RC_RESIDUAL) \
78 C(ASRESIDUAL, N_("Adjusted Residual"), PIVOT_RC_RESIDUAL)
81 #define C(KEYWORD, STRING, RC) CRS_CL_##KEYWORD,
86 #define C(KEYWORD, STRING, RC) + 1
87 CRS_N_CELLS = CRS_CELLS
90 #define CRS_ALL_CELLS ((1u << CRS_N_CELLS) - 1)
92 /* Kinds of statistics. */
93 #define CRS_STATISTICS \
107 enum crs_statistic_index {
108 #define S(KEYWORD) CRS_ST_##KEYWORD##_INDEX,
112 enum crs_statistic_bit {
113 #define S(KEYWORD) CRS_ST_##KEYWORD = 1u << CRS_ST_##KEYWORD##_INDEX,
118 #define S(KEYWORD) + 1
119 CRS_N_STATISTICS = CRS_STATISTICS
122 #define CRS_ALL_STATISTICS ((1u << CRS_N_STATISTICS) - 1)
124 /* Number of chi-square statistics. */
127 /* Number of symmetric statistics. */
128 #define N_SYMMETRIC 9
130 /* Number of directional statistics. */
131 #define N_DIRECTIONAL 13
133 /* Indexes into the 'vars' member of struct crosstabulation and
134 struct crosstab member. */
137 ROW_VAR = 0, /* Row variable. */
138 COL_VAR = 1 /* Column variable. */
139 /* Higher indexes cause multiple tables to be output. */
144 const struct variable *var;
149 /* A crosstabulation of 2 or more variables. */
150 struct crosstabulation
152 struct crosstabs_proc *proc;
153 struct fmt_spec weight_format; /* Format for weight variable. */
154 double missing; /* Weight of missing cases. */
156 /* Variables (2 or more). */
158 struct xtab_var *vars;
160 /* Constants (0 or more). */
162 struct xtab_var *const_vars;
163 size_t *const_indexes;
167 struct freq **entries;
170 /* Number of statistically interesting columns/rows
171 (columns/rows with data in them). */
172 int ns_cols, ns_rows;
174 /* Matrix contents. */
175 double *mat; /* Matrix proper. */
176 double *row_tot; /* Row totals. */
177 double *col_tot; /* Column totals. */
178 double total; /* Grand total. */
181 /* Integer mode variable info. */
184 struct hmap_node hmap_node; /* In struct crosstabs_proc var_ranges map. */
185 const struct variable *var; /* The variable. */
186 int min; /* Minimum value. */
187 int max; /* Maximum value + 1. */
188 int count; /* max - min. */
191 struct crosstabs_proc
193 const struct dictionary *dict;
194 enum { INTEGER, GENERAL } mode;
195 enum mv_class exclude;
198 struct fmt_spec weight_format;
200 /* Variables specifies on VARIABLES. */
201 const struct variable **variables;
203 struct hmap var_ranges;
206 struct crosstabulation *pivots;
210 int n_cells; /* Number of cells requested. */
211 unsigned int cells; /* Bit k is 1 if cell k is requested. */
212 int a_cells[CRS_N_CELLS]; /* 0...n_cells-1 are the requested cells. */
214 /* Rounding of cells. */
215 bool round_case_weights; /* Round case weights? */
216 bool round_cells; /* If !round_case_weights, round cells? */
217 bool round_down; /* Round down? (otherwise to nearest) */
220 unsigned int statistics; /* Bit k is 1 if statistic k is requested. */
222 bool descending; /* True if descending sort order is requested. */
225 static bool parse_crosstabs_tables (struct lexer *, struct dataset *,
226 struct crosstabs_proc *);
227 static bool parse_crosstabs_variables (struct lexer *, struct dataset *,
228 struct crosstabs_proc *);
230 static const struct var_range *get_var_range (const struct crosstabs_proc *,
231 const struct variable *);
233 static bool should_tabulate_case (const struct crosstabulation *,
234 const struct ccase *, enum mv_class exclude);
235 static void tabulate_general_case (struct crosstabulation *, const struct ccase *,
237 static void tabulate_integer_case (struct crosstabulation *, const struct ccase *,
239 static void postcalc (struct crosstabs_proc *);
242 round_weight (const struct crosstabs_proc *proc, double weight)
244 return proc->round_down ? floor (weight) : floor (weight + 0.5);
247 #define FOR_EACH_POPULATED_COLUMN(C, XT) \
248 for (int C = next_populated_column (0, XT); \
249 C < (XT)->vars[COL_VAR].n_values; \
250 C = next_populated_column (C + 1, XT))
252 next_populated_column (int c, const struct crosstabulation *xt)
254 int n_columns = xt->vars[COL_VAR].n_values;
255 for (; c < n_columns; c++)
261 #define FOR_EACH_POPULATED_ROW(R, XT) \
262 for (int R = next_populated_row (0, XT); R < (XT)->vars[ROW_VAR].n_values; \
263 R = next_populated_row (R + 1, XT))
265 next_populated_row (int r, const struct crosstabulation *xt)
267 int n_rows = xt->vars[ROW_VAR].n_values;
268 for (; r < n_rows; r++)
274 /* Parses and executes the CROSSTABS procedure. */
276 cmd_crosstabs (struct lexer *lexer, struct dataset *ds)
278 int result = CMD_FAILURE;
280 struct crosstabs_proc proc = {
281 .dict = dataset_dict (ds),
286 .weight_format = *dict_get_weight_format (dataset_dict (ds)),
290 .var_ranges = HMAP_INITIALIZER (proc.var_ranges),
295 .cells = 1u << CRS_CL_COUNT,
296 /* n_cells and a_cells will be filled in later. */
298 .round_case_weights = false,
299 .round_cells = false,
306 bool show_tables = true;
307 lex_match (lexer, T_SLASH);
310 if (lex_match_id (lexer, "VARIABLES"))
312 if (!parse_crosstabs_variables (lexer, ds, &proc))
315 else if (lex_match_id (lexer, "MISSING"))
317 lex_match (lexer, T_EQUALS);
318 if (lex_match_id (lexer, "TABLE"))
319 proc.exclude = MV_ANY;
320 else if (lex_match_id (lexer, "INCLUDE"))
321 proc.exclude = MV_SYSTEM;
322 else if (lex_match_id (lexer, "REPORT"))
323 proc.exclude = MV_NEVER;
326 lex_error (lexer, NULL);
330 else if (lex_match_id (lexer, "COUNT"))
332 lex_match (lexer, T_EQUALS);
334 /* Default is CELL. */
335 proc.round_case_weights = false;
336 proc.round_cells = true;
338 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
340 if (lex_match_id (lexer, "ASIS"))
342 proc.round_case_weights = false;
343 proc.round_cells = false;
345 else if (lex_match_id (lexer, "CASE"))
347 proc.round_case_weights = true;
348 proc.round_cells = false;
350 else if (lex_match_id (lexer, "CELL"))
352 proc.round_case_weights = false;
353 proc.round_cells = true;
355 else if (lex_match_id (lexer, "ROUND"))
356 proc.round_down = false;
357 else if (lex_match_id (lexer, "TRUNCATE"))
358 proc.round_down = true;
361 lex_error (lexer, NULL);
364 lex_match (lexer, T_COMMA);
367 else if (lex_match_id (lexer, "FORMAT"))
369 lex_match (lexer, T_EQUALS);
370 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
372 if (lex_match_id (lexer, "AVALUE"))
373 proc.descending = false;
374 else if (lex_match_id (lexer, "DVALUE"))
375 proc.descending = true;
376 else if (lex_match_id (lexer, "TABLES"))
378 else if (lex_match_id (lexer, "NOTABLES"))
382 lex_error (lexer, NULL);
385 lex_match (lexer, T_COMMA);
388 else if (lex_match_id (lexer, "BARCHART"))
389 proc.barchart = true;
390 else if (lex_match_id (lexer, "CELLS"))
392 lex_match (lexer, T_EQUALS);
394 if (lex_match_id (lexer, "NONE"))
396 else if (lex_match (lexer, T_ALL))
397 proc.cells = CRS_ALL_CELLS;
401 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
403 #define C(KEYWORD, STRING, RC) \
404 if (lex_match_id (lexer, #KEYWORD)) \
406 proc.cells |= 1u << CRS_CL_##KEYWORD; \
411 lex_error (lexer, NULL);
415 proc.cells = ((1u << CRS_CL_COUNT) | (1u << CRS_CL_ROW)
416 | (1u << CRS_CL_COLUMN) | (1u << CRS_CL_TOTAL));
419 else if (lex_match_id (lexer, "STATISTICS"))
421 lex_match (lexer, T_EQUALS);
423 if (lex_match_id (lexer, "NONE"))
425 else if (lex_match (lexer, T_ALL))
426 proc.statistics = CRS_ALL_STATISTICS;
430 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
433 if (lex_match_id (lexer, #KEYWORD)) \
435 proc.statistics |= CRS_ST_##KEYWORD; \
440 lex_error (lexer, NULL);
443 if (!proc.statistics)
444 proc.statistics = CRS_ST_CHISQ;
447 else if (!parse_crosstabs_tables (lexer, ds, &proc))
450 if (!lex_match (lexer, T_SLASH))
453 if (!lex_end_of_command (lexer))
458 msg (SE, _("At least one crosstabulation must be requested (using "
459 "the TABLES subcommand)."));
466 for (int i = 0; i < CRS_N_CELLS; i++)
467 if (proc.cells & (1u << i))
468 proc.a_cells[proc.n_cells++] = i;
469 assert (proc.n_cells < CRS_N_CELLS);
471 /* Missing values. */
472 if (proc.mode == GENERAL && proc.exclude == MV_NEVER)
474 msg (SE, _("Missing mode %s not allowed in general mode. "
475 "Assuming %s."), "REPORT", "MISSING=TABLE");
476 proc.exclude = MV_ANY;
479 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
482 struct casegrouper *grouper = casegrouper_create_splits (input, dataset_dict (ds));
483 struct casereader *group;
484 while (casegrouper_get_next_group (grouper, &group))
488 /* Output SPLIT FILE variables. */
489 c = casereader_peek (group, 0);
492 output_split_file_values (ds, c);
496 /* Initialize hash tables. */
497 for (struct crosstabulation *xt = &proc.pivots[0];
498 xt < &proc.pivots[proc.n_pivots]; xt++)
499 hmap_init (&xt->data);
502 for (; (c = casereader_read (group)) != NULL; case_unref (c))
503 for (struct crosstabulation *xt = &proc.pivots[0];
504 xt < &proc.pivots[proc.n_pivots]; xt++)
506 double weight = dict_get_case_weight (dataset_dict (ds), c,
508 if (proc.round_case_weights)
510 weight = round_weight (&proc, weight);
514 if (should_tabulate_case (xt, c, proc.exclude))
516 if (proc.mode == GENERAL)
517 tabulate_general_case (xt, c, weight);
519 tabulate_integer_case (xt, c, weight);
522 xt->missing += weight;
524 casereader_destroy (group);
529 bool ok = casegrouper_destroy (grouper);
530 ok = proc_commit (ds) && ok;
532 result = ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
535 free (proc.variables);
537 struct var_range *range, *next_range;
538 HMAP_FOR_EACH_SAFE (range, next_range, struct var_range, hmap_node,
541 hmap_delete (&proc.var_ranges, &range->hmap_node);
544 for (struct crosstabulation *xt = &proc.pivots[0];
545 xt < &proc.pivots[proc.n_pivots]; xt++)
548 free (xt->const_vars);
549 free (xt->const_indexes);
556 /* Parses the TABLES subcommand. */
558 parse_crosstabs_tables (struct lexer *lexer, struct dataset *ds,
559 struct crosstabs_proc *proc)
561 const struct variable ***by = NULL;
562 size_t *by_nvar = NULL;
565 /* Ensure that this is a TABLES subcommand. */
566 if (!lex_match_id (lexer, "TABLES")
567 && (lex_token (lexer) != T_ID ||
568 dict_lookup_var (dataset_dict (ds), lex_tokcstr (lexer)) == NULL)
569 && lex_token (lexer) != T_ALL)
571 lex_error (lexer, NULL);
574 lex_match (lexer, T_EQUALS);
576 struct const_var_set *var_set
578 ? const_var_set_create_from_array (proc->variables,
580 : const_var_set_create_from_dict (dataset_dict (ds)));
586 by = xnrealloc (by, n_by + 1, sizeof *by);
587 by_nvar = xnrealloc (by_nvar, n_by + 1, sizeof *by_nvar);
588 if (!parse_const_var_set_vars (lexer, var_set, &by[n_by], &by_nvar[n_by],
589 PV_NO_DUPLICATE | PV_NO_SCRATCH))
591 if (xalloc_oversized (nx, by_nvar[n_by]))
593 msg (SE, _("Too many cross-tabulation variables or dimensions."));
599 if (!lex_match (lexer, T_BY))
608 int *by_iter = xcalloc (n_by, sizeof *by_iter);
609 proc->pivots = xnrealloc (proc->pivots,
610 proc->n_pivots + nx, sizeof *proc->pivots);
611 for (int i = 0; i < nx; i++)
613 struct crosstabulation *xt = &proc->pivots[proc->n_pivots++];
615 *xt = (struct crosstabulation) {
617 .weight_format = proc->weight_format,
620 .vars = xcalloc (n_by, sizeof *xt->vars),
623 .const_indexes = NULL,
626 for (int j = 0; j < n_by; j++)
627 xt->vars[j].var = by[j][by_iter[j]];
629 for (int j = n_by - 1; j >= 0; j--)
631 if (++by_iter[j] < by_nvar[j])
640 /* All return paths lead here. */
641 for (int i = 0; i < n_by; i++)
646 const_var_set_destroy (var_set);
651 /* Parses the VARIABLES subcommand. */
653 parse_crosstabs_variables (struct lexer *lexer, struct dataset *ds,
654 struct crosstabs_proc *proc)
658 msg (SE, _("%s must be specified before %s."), "VARIABLES", "TABLES");
662 lex_match (lexer, T_EQUALS);
666 size_t orig_nv = proc->n_variables;
671 if (!parse_variables_const (lexer, dataset_dict (ds),
672 &proc->variables, &proc->n_variables,
673 (PV_APPEND | PV_NUMERIC
674 | PV_NO_DUPLICATE | PV_NO_SCRATCH)))
677 if (!lex_force_match (lexer, T_LPAREN))
680 if (!lex_force_int (lexer))
682 min = lex_integer (lexer);
685 lex_match (lexer, T_COMMA);
687 if (!lex_force_int (lexer))
689 max = lex_integer (lexer);
692 msg (SE, _("Maximum value (%ld) less than minimum value (%ld)."),
698 if (!lex_force_match (lexer, T_RPAREN))
701 for (i = orig_nv; i < proc->n_variables; i++)
703 const struct variable *var = proc->variables[i];
704 struct var_range *vr = xmalloc (sizeof *vr);
709 vr->count = max - min + 1;
710 hmap_insert (&proc->var_ranges, &vr->hmap_node,
711 hash_pointer (var, 0));
714 if (lex_token (lexer) == T_SLASH)
718 proc->mode = INTEGER;
722 free (proc->variables);
723 proc->variables = NULL;
724 proc->n_variables = 0;
728 /* Data file processing. */
730 static const struct var_range *
731 get_var_range (const struct crosstabs_proc *proc, const struct variable *var)
733 if (!hmap_is_empty (&proc->var_ranges))
735 const struct var_range *range;
737 HMAP_FOR_EACH_IN_BUCKET (range, struct var_range, hmap_node,
738 hash_pointer (var, 0), &proc->var_ranges)
739 if (range->var == var)
747 should_tabulate_case (const struct crosstabulation *xt, const struct ccase *c,
748 enum mv_class exclude)
751 for (j = 0; j < xt->n_vars; j++)
753 const struct variable *var = xt->vars[j].var;
754 const struct var_range *range = get_var_range (xt->proc, var);
756 if (var_is_value_missing (var, case_data (c, var), exclude))
761 double num = case_num (c, var);
762 if (num < range->min || num >= range->max + 1.)
770 tabulate_integer_case (struct crosstabulation *xt, const struct ccase *c,
778 for (j = 0; j < xt->n_vars; j++)
780 /* Throw away fractional parts of values. */
781 hash = hash_int (case_num (c, xt->vars[j].var), hash);
784 HMAP_FOR_EACH_WITH_HASH (te, struct freq, node, hash, &xt->data)
786 for (j = 0; j < xt->n_vars; j++)
787 if ((int) case_num (c, xt->vars[j].var) != (int) te->values[j].f)
790 /* Found an existing entry. */
797 /* No existing entry. Create a new one. */
798 te = xmalloc (table_entry_size (xt->n_vars));
800 for (j = 0; j < xt->n_vars; j++)
801 te->values[j].f = (int) case_num (c, xt->vars[j].var);
802 hmap_insert (&xt->data, &te->node, hash);
806 tabulate_general_case (struct crosstabulation *xt, const struct ccase *c,
814 for (j = 0; j < xt->n_vars; j++)
816 const struct variable *var = xt->vars[j].var;
817 hash = value_hash (case_data (c, var), var_get_width (var), hash);
820 HMAP_FOR_EACH_WITH_HASH (te, struct freq, node, hash, &xt->data)
822 for (j = 0; j < xt->n_vars; j++)
824 const struct variable *var = xt->vars[j].var;
825 if (!value_equal (case_data (c, var), &te->values[j],
826 var_get_width (var)))
830 /* Found an existing entry. */
837 /* No existing entry. Create a new one. */
838 te = xmalloc (table_entry_size (xt->n_vars));
840 for (j = 0; j < xt->n_vars; j++)
842 const struct variable *var = xt->vars[j].var;
843 value_clone (&te->values[j], case_data (c, var), var_get_width (var));
845 hmap_insert (&xt->data, &te->node, hash);
848 /* Post-data reading calculations. */
850 static int compare_table_entry_vars_3way (const struct freq *a,
851 const struct freq *b,
852 const struct crosstabulation *xt,
854 static int compare_table_entry_3way (const void *ap_, const void *bp_,
856 static int compare_table_entry_3way_inv (const void *ap_, const void *bp_,
859 static void enum_var_values (const struct crosstabulation *, int var_idx,
861 static void free_var_values (const struct crosstabulation *, int var_idx);
862 static void output_crosstabulation (struct crosstabs_proc *,
863 struct crosstabulation *);
864 static void make_crosstabulation_subset (struct crosstabulation *xt,
865 size_t row0, size_t row1,
866 struct crosstabulation *subset);
867 static void make_summary_table (struct crosstabs_proc *);
868 static bool find_crosstab (struct crosstabulation *, size_t *row0p,
872 postcalc (struct crosstabs_proc *proc)
874 /* Round hash table entries, if requested
876 If this causes any of the cell counts to fall to zero, delete those
878 if (proc->round_cells)
879 for (struct crosstabulation *xt = proc->pivots;
880 xt < &proc->pivots[proc->n_pivots]; xt++)
882 struct freq *e, *next;
883 HMAP_FOR_EACH_SAFE (e, next, struct freq, node, &xt->data)
885 e->count = round_weight (proc, e->count);
888 hmap_delete (&xt->data, &e->node);
894 /* Convert hash tables into sorted arrays of entries. */
895 for (struct crosstabulation *xt = proc->pivots;
896 xt < &proc->pivots[proc->n_pivots]; xt++)
900 xt->n_entries = hmap_count (&xt->data);
901 xt->entries = xnmalloc (xt->n_entries, sizeof *xt->entries);
903 HMAP_FOR_EACH (e, struct freq, node, &xt->data)
904 xt->entries[i++] = e;
905 hmap_destroy (&xt->data);
907 sort (xt->entries, xt->n_entries, sizeof *xt->entries,
908 proc->descending ? compare_table_entry_3way_inv : compare_table_entry_3way,
913 make_summary_table (proc);
915 /* Output each pivot table. */
916 for (struct crosstabulation *xt = proc->pivots;
917 xt < &proc->pivots[proc->n_pivots]; xt++)
919 output_crosstabulation (proc, xt);
922 int n_vars = (xt->n_vars > 2 ? 2 : xt->n_vars);
923 const struct variable **vars = xcalloc (n_vars, sizeof *vars);
924 for (size_t i = 0; i < n_vars; i++)
925 vars[i] = xt->vars[i].var;
926 chart_submit (barchart_create (vars, n_vars, _("Count"),
928 xt->entries, xt->n_entries));
933 /* Free output and prepare for next split file. */
934 for (struct crosstabulation *xt = proc->pivots;
935 xt < &proc->pivots[proc->n_pivots]; xt++)
939 /* Free the members that were allocated in this function(and the values
940 owned by the entries.
942 The other pointer members are either both allocated and destroyed at a
943 lower level (in output_crosstabulation), or both allocated and
944 destroyed at a higher level (in crs_custom_tables and free_proc,
946 for (size_t i = 0; i < xt->n_vars; i++)
948 int width = var_get_width (xt->vars[i].var);
949 if (value_needs_init (width))
953 for (j = 0; j < xt->n_entries; j++)
954 value_destroy (&xt->entries[j]->values[i], width);
958 for (size_t i = 0; i < xt->n_entries; i++)
959 free (xt->entries[i]);
965 make_crosstabulation_subset (struct crosstabulation *xt, size_t row0,
966 size_t row1, struct crosstabulation *subset)
971 assert (xt->n_consts == 0);
973 subset->vars = xt->vars;
975 subset->n_consts = xt->n_vars - 2;
976 subset->const_vars = xt->vars + 2;
977 subset->const_indexes = xcalloc (subset->n_consts,
978 sizeof *subset->const_indexes);
979 for (size_t i = 0; i < subset->n_consts; i++)
981 const union value *value = &xt->entries[row0]->values[2 + i];
983 for (size_t j = 0; j < xt->vars[2 + i].n_values; j++)
984 if (value_equal (&xt->vars[2 + i].values[j], value,
985 var_get_width (xt->vars[2 + i].var)))
987 subset->const_indexes[i] = j;
994 subset->entries = &xt->entries[row0];
995 subset->n_entries = row1 - row0;
999 compare_table_entry_var_3way (const struct freq *a,
1000 const struct freq *b,
1001 const struct crosstabulation *xt,
1004 return value_compare_3way (&a->values[idx], &b->values[idx],
1005 var_get_width (xt->vars[idx].var));
1009 compare_table_entry_vars_3way (const struct freq *a,
1010 const struct freq *b,
1011 const struct crosstabulation *xt,
1016 for (i = idx1 - 1; i >= idx0; i--)
1018 int cmp = compare_table_entry_var_3way (a, b, xt, i);
1025 /* Compare the struct freq at *AP to the one at *BP and
1026 return a strcmp()-type result. */
1028 compare_table_entry_3way (const void *ap_, const void *bp_, const void *xt_)
1030 const struct freq *const *ap = ap_;
1031 const struct freq *const *bp = bp_;
1032 const struct freq *a = *ap;
1033 const struct freq *b = *bp;
1034 const struct crosstabulation *xt = xt_;
1037 cmp = compare_table_entry_vars_3way (a, b, xt, 2, xt->n_vars);
1041 cmp = compare_table_entry_var_3way (a, b, xt, ROW_VAR);
1045 return compare_table_entry_var_3way (a, b, xt, COL_VAR);
1048 /* Inverted version of compare_table_entry_3way */
1050 compare_table_entry_3way_inv (const void *ap_, const void *bp_, const void *xt_)
1052 return -compare_table_entry_3way (ap_, bp_, xt_);
1055 /* Output a table summarizing the cases processed. */
1057 make_summary_table (struct crosstabs_proc *proc)
1059 struct pivot_table *table = pivot_table_create (N_("Summary"));
1060 pivot_table_set_weight_var (table, dict_get_weight (proc->dict));
1062 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1063 N_("N"), PIVOT_RC_COUNT,
1064 N_("Percent"), PIVOT_RC_PERCENT);
1066 struct pivot_dimension *cases = pivot_dimension_create (
1067 table, PIVOT_AXIS_COLUMN, N_("Cases"),
1068 N_("Valid"), N_("Missing"), N_("Total"));
1069 cases->root->show_label = true;
1071 struct pivot_dimension *tables = pivot_dimension_create (
1072 table, PIVOT_AXIS_ROW, N_("Crosstabulation"));
1073 for (struct crosstabulation *xt = &proc->pivots[0];
1074 xt < &proc->pivots[proc->n_pivots]; xt++)
1076 struct string name = DS_EMPTY_INITIALIZER;
1077 for (size_t i = 0; i < xt->n_vars; i++)
1080 ds_put_cstr (&name, " × ");
1081 ds_put_cstr (&name, var_to_string (xt->vars[i].var));
1084 int row = pivot_category_create_leaf (
1086 pivot_value_new_user_text_nocopy (ds_steal_cstr (&name)));
1089 for (size_t i = 0; i < xt->n_entries; i++)
1090 valid += xt->entries[i]->count;
1096 for (int i = 0; i < 3; i++)
1098 pivot_table_put3 (table, 0, i, row, pivot_value_new_number (n[i]));
1099 pivot_table_put3 (table, 1, i, row,
1100 pivot_value_new_number (n[i] / n[2] * 100.0));
1104 pivot_table_submit (table);
1109 static struct pivot_table *create_crosstab_table (
1110 struct crosstabs_proc *, struct crosstabulation *,
1111 size_t crs_leaves[CRS_N_CELLS]);
1112 static struct pivot_table *create_chisq_table (struct crosstabulation *);
1113 static struct pivot_table *create_sym_table (struct crosstabulation *);
1114 static struct pivot_table *create_risk_table (
1115 struct crosstabulation *, struct pivot_dimension **risk_statistics);
1116 static struct pivot_table *create_direct_table (struct crosstabulation *);
1117 static void display_crosstabulation (struct crosstabs_proc *,
1118 struct crosstabulation *,
1119 struct pivot_table *,
1120 size_t crs_leaves[CRS_N_CELLS]);
1121 static void display_chisq (struct crosstabulation *, struct pivot_table *);
1122 static void display_symmetric (struct crosstabs_proc *,
1123 struct crosstabulation *, struct pivot_table *);
1124 static void display_risk (struct crosstabulation *, struct pivot_table *,
1125 struct pivot_dimension *risk_statistics);
1126 static void display_directional (struct crosstabs_proc *,
1127 struct crosstabulation *,
1128 struct pivot_table *);
1129 static void delete_missing (struct crosstabulation *);
1130 static void build_matrix (struct crosstabulation *);
1132 /* Output pivot table XT in the context of PROC. */
1134 output_crosstabulation (struct crosstabs_proc *proc, struct crosstabulation *xt)
1136 for (size_t i = 0; i < xt->n_vars; i++)
1137 enum_var_values (xt, i, proc->descending);
1139 if (xt->vars[COL_VAR].n_values == 0)
1144 ds_init_cstr (&vars, var_to_string (xt->vars[0].var));
1145 for (i = 1; i < xt->n_vars; i++)
1146 ds_put_format (&vars, " × %s", var_to_string (xt->vars[i].var));
1148 /* TRANSLATORS: The %s here describes a crosstabulation. It takes the
1149 form "var1 * var2 * var3 * ...". */
1150 msg (SW, _("Crosstabulation %s contained no non-missing cases."),
1154 for (size_t i = 0; i < xt->n_vars; i++)
1155 free_var_values (xt, i);
1159 size_t crs_leaves[CRS_N_CELLS];
1160 struct pivot_table *table = (proc->cells
1161 ? create_crosstab_table (proc, xt, crs_leaves)
1163 struct pivot_table *chisq = (proc->statistics & CRS_ST_CHISQ
1164 ? create_chisq_table (xt)
1166 struct pivot_table *sym
1167 = (proc->statistics & (CRS_ST_PHI | CRS_ST_CC | CRS_ST_BTAU | CRS_ST_CTAU
1168 | CRS_ST_GAMMA | CRS_ST_CORR | CRS_ST_KAPPA)
1169 ? create_sym_table (xt)
1171 struct pivot_dimension *risk_statistics = NULL;
1172 struct pivot_table *risk = (proc->statistics & CRS_ST_RISK
1173 ? create_risk_table (xt, &risk_statistics)
1175 struct pivot_table *direct
1176 = (proc->statistics & (CRS_ST_LAMBDA | CRS_ST_UC | CRS_ST_D | CRS_ST_ETA)
1177 ? create_direct_table (xt)
1182 while (find_crosstab (xt, &row0, &row1))
1184 struct crosstabulation x;
1186 make_crosstabulation_subset (xt, row0, row1, &x);
1188 size_t n_rows = x.vars[ROW_VAR].n_values;
1189 size_t n_cols = x.vars[COL_VAR].n_values;
1190 if (size_overflow_p (xtimes (xtimes (n_rows, n_cols), sizeof (double))))
1192 x.row_tot = xmalloc (n_rows * sizeof *x.row_tot);
1193 x.col_tot = xmalloc (n_cols * sizeof *x.col_tot);
1194 x.mat = xmalloc (n_rows * n_cols * sizeof *x.mat);
1198 /* Find the first variable that differs from the last subtable. */
1200 display_crosstabulation (proc, &x, table, crs_leaves);
1202 if (proc->exclude == MV_NEVER)
1203 delete_missing (&x);
1206 display_chisq (&x, chisq);
1209 display_symmetric (proc, &x, sym);
1211 display_risk (&x, risk, risk_statistics);
1213 display_directional (proc, &x, direct);
1218 free (x.const_indexes);
1222 pivot_table_submit (table);
1225 pivot_table_submit (chisq);
1228 pivot_table_submit (sym);
1232 if (!pivot_table_is_empty (risk))
1233 pivot_table_submit (risk);
1235 pivot_table_unref (risk);
1239 pivot_table_submit (direct);
1241 for (size_t i = 0; i < xt->n_vars; i++)
1242 free_var_values (xt, i);
1246 build_matrix (struct crosstabulation *x)
1248 const int col_var_width = var_get_width (x->vars[COL_VAR].var);
1249 const int row_var_width = var_get_width (x->vars[ROW_VAR].var);
1250 size_t n_rows = x->vars[ROW_VAR].n_values;
1251 size_t n_cols = x->vars[COL_VAR].n_values;
1258 for (p = x->entries; p < &x->entries[x->n_entries]; p++)
1260 const struct freq *te = *p;
1262 while (!value_equal (&x->vars[ROW_VAR].values[row],
1263 &te->values[ROW_VAR], row_var_width))
1265 for (; col < n_cols; col++)
1271 while (!value_equal (&x->vars[COL_VAR].values[col],
1272 &te->values[COL_VAR], col_var_width))
1279 if (++col >= n_cols)
1285 while (mp < &x->mat[n_cols * n_rows])
1287 assert (mp == &x->mat[n_cols * n_rows]);
1289 /* Column totals, row totals, ns_rows. */
1291 for (col = 0; col < n_cols; col++)
1292 x->col_tot[col] = 0.0;
1293 for (row = 0; row < n_rows; row++)
1294 x->row_tot[row] = 0.0;
1296 for (row = 0; row < n_rows; row++)
1298 bool row_is_empty = true;
1299 for (col = 0; col < n_cols; col++)
1303 row_is_empty = false;
1304 x->col_tot[col] += *mp;
1305 x->row_tot[row] += *mp;
1312 assert (mp == &x->mat[n_cols * n_rows]);
1316 for (col = 0; col < n_cols; col++)
1317 for (row = 0; row < n_rows; row++)
1318 if (x->mat[col + row * n_cols] != 0.0)
1326 for (col = 0; col < n_cols; col++)
1327 x->total += x->col_tot[col];
1331 add_var_dimension (struct pivot_table *table, const struct xtab_var *var,
1332 enum pivot_axis_type axis_type, bool total)
1334 struct pivot_dimension *d = pivot_dimension_create__ (
1335 table, axis_type, pivot_value_new_variable (var->var));
1337 struct pivot_footnote *missing_footnote = pivot_table_create_footnote (
1338 table, pivot_value_new_text (N_("Missing value")));
1340 struct pivot_category *group = pivot_category_create_group__ (
1341 d->root, pivot_value_new_variable (var->var));
1342 for (size_t j = 0; j < var->n_values; j++)
1344 struct pivot_value *value = pivot_value_new_var_value (
1345 var->var, &var->values[j]);
1346 if (var_is_value_missing (var->var, &var->values[j], MV_ANY))
1347 pivot_value_add_footnote (value, missing_footnote);
1348 pivot_category_create_leaf (group, value);
1352 pivot_category_create_leaf (d->root, pivot_value_new_text (N_("Total")));
1355 static struct pivot_table *
1356 create_crosstab_table (struct crosstabs_proc *proc, struct crosstabulation *xt,
1357 size_t crs_leaves[CRS_N_CELLS])
1360 struct string title = DS_EMPTY_INITIALIZER;
1361 for (size_t i = 0; i < xt->n_vars; i++)
1364 ds_put_cstr (&title, " × ");
1365 ds_put_cstr (&title, var_to_string (xt->vars[i].var));
1367 for (size_t i = 0; i < xt->n_consts; i++)
1369 const struct variable *var = xt->const_vars[i].var;
1370 const union value *value = &xt->entries[0]->values[2 + i];
1373 ds_put_format (&title, ", %s=", var_to_string (var));
1375 /* Insert the formatted value of VAR without any leading spaces. */
1376 s = data_out (value, var_get_encoding (var), var_get_print_format (var),
1377 settings_get_fmt_settings ());
1378 ds_put_cstr (&title, s + strspn (s, " "));
1381 struct pivot_table *table = pivot_table_create__ (
1382 pivot_value_new_user_text_nocopy (ds_steal_cstr (&title)),
1384 pivot_table_set_weight_format (table, &proc->weight_format);
1386 struct pivot_dimension *statistics = pivot_dimension_create (
1387 table, PIVOT_AXIS_ROW, N_("Statistics"));
1394 static const struct statistic stats[CRS_N_CELLS] =
1396 #define C(KEYWORD, STRING, RC) { STRING, RC },
1400 for (size_t i = 0; i < CRS_N_CELLS; i++)
1401 if (proc->cells & (1u << i) && stats[i].label)
1402 crs_leaves[i] = pivot_category_create_leaf_rc (
1403 statistics->root, pivot_value_new_text (stats[i].label),
1406 for (size_t i = 0; i < xt->n_vars; i++)
1407 add_var_dimension (table, &xt->vars[i],
1408 i == COL_VAR ? PIVOT_AXIS_COLUMN : PIVOT_AXIS_ROW,
1414 static struct pivot_table *
1415 create_chisq_table (struct crosstabulation *xt)
1417 struct pivot_table *chisq = pivot_table_create (N_("Chi-Square Tests"));
1418 pivot_table_set_weight_format (chisq, &xt->weight_format);
1420 pivot_dimension_create (
1421 chisq, PIVOT_AXIS_ROW, N_("Statistics"),
1422 N_("Pearson Chi-Square"),
1423 N_("Likelihood Ratio"),
1424 N_("Fisher's Exact Test"),
1425 N_("Continuity Correction"),
1426 N_("Linear-by-Linear Association"),
1427 N_("N of Valid Cases"), PIVOT_RC_COUNT);
1429 pivot_dimension_create (
1430 chisq, PIVOT_AXIS_COLUMN, N_("Statistics"),
1431 N_("Value"), PIVOT_RC_OTHER,
1432 N_("df"), PIVOT_RC_COUNT,
1433 N_("Asymptotic Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE,
1434 N_("Exact Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE,
1435 N_("Exact Sig. (1-tailed)"), PIVOT_RC_SIGNIFICANCE);
1437 for (size_t i = 2; i < xt->n_vars; i++)
1438 add_var_dimension (chisq, &xt->vars[i], PIVOT_AXIS_ROW, false);
1443 /* Symmetric measures. */
1444 static struct pivot_table *
1445 create_sym_table (struct crosstabulation *xt)
1447 struct pivot_table *sym = pivot_table_create (N_("Symmetric Measures"));
1448 pivot_table_set_weight_format (sym, &xt->weight_format);
1450 pivot_dimension_create (
1451 sym, PIVOT_AXIS_COLUMN, N_("Values"),
1452 N_("Value"), PIVOT_RC_OTHER,
1453 N_("Asymp. Std. Error"), PIVOT_RC_OTHER,
1454 N_("Approx. T"), PIVOT_RC_OTHER,
1455 N_("Approx. Sig."), PIVOT_RC_SIGNIFICANCE);
1457 struct pivot_dimension *statistics = pivot_dimension_create (
1458 sym, PIVOT_AXIS_ROW, N_("Statistics"));
1459 pivot_category_create_group (
1460 statistics->root, N_("Nominal by Nominal"),
1461 N_("Phi"), N_("Cramer's V"), N_("Contingency Coefficient"));
1462 pivot_category_create_group (
1463 statistics->root, N_("Ordinal by Ordinal"),
1464 N_("Kendall's tau-b"), N_("Kendall's tau-c"),
1465 N_("Gamma"), N_("Spearman Correlation"));
1466 pivot_category_create_group (
1467 statistics->root, N_("Interval by Interval"),
1469 pivot_category_create_group (
1470 statistics->root, N_("Measure of Agreement"),
1472 pivot_category_create_leaves (statistics->root, N_("N of Valid Cases"),
1475 for (size_t i = 2; i < xt->n_vars; i++)
1476 add_var_dimension (sym, &xt->vars[i], PIVOT_AXIS_ROW, false);
1481 /* Risk estimate. */
1482 static struct pivot_table *
1483 create_risk_table (struct crosstabulation *xt,
1484 struct pivot_dimension **risk_statistics)
1486 struct pivot_table *risk = pivot_table_create (N_("Risk Estimate"));
1487 pivot_table_set_weight_format (risk, &xt->weight_format);
1489 struct pivot_dimension *values = pivot_dimension_create (
1490 risk, PIVOT_AXIS_COLUMN, N_("Values"),
1491 N_("Value"), PIVOT_RC_OTHER);
1492 pivot_category_create_group (
1493 /* xgettext:no-c-format */
1494 values->root, N_("95% Confidence Interval"),
1495 N_("Lower"), PIVOT_RC_OTHER,
1496 N_("Upper"), PIVOT_RC_OTHER);
1498 *risk_statistics = pivot_dimension_create (
1499 risk, PIVOT_AXIS_ROW, N_("Statistics"));
1501 for (size_t i = 2; i < xt->n_vars; i++)
1502 add_var_dimension (risk, &xt->vars[i], PIVOT_AXIS_ROW, false);
1508 create_direct_stat (struct pivot_category *parent,
1509 const struct crosstabulation *xt,
1510 const char *name, bool symmetric)
1512 struct pivot_category *group = pivot_category_create_group (
1515 pivot_category_create_leaf (group, pivot_value_new_text (N_("Symmetric")));
1517 char *row_label = xasprintf (_("%s Dependent"),
1518 var_to_string (xt->vars[ROW_VAR].var));
1519 pivot_category_create_leaf (group, pivot_value_new_user_text_nocopy (
1522 char *col_label = xasprintf (_("%s Dependent"),
1523 var_to_string (xt->vars[COL_VAR].var));
1524 pivot_category_create_leaf (group, pivot_value_new_user_text_nocopy (
1528 /* Directional measures. */
1529 static struct pivot_table *
1530 create_direct_table (struct crosstabulation *xt)
1532 struct pivot_table *direct = pivot_table_create (N_("Directional Measures"));
1533 pivot_table_set_weight_format (direct, &xt->weight_format);
1535 pivot_dimension_create (
1536 direct, PIVOT_AXIS_COLUMN, N_("Values"),
1537 N_("Value"), PIVOT_RC_OTHER,
1538 N_("Asymp. Std. Error"), PIVOT_RC_OTHER,
1539 N_("Approx. T"), PIVOT_RC_OTHER,
1540 N_("Approx. Sig."), PIVOT_RC_SIGNIFICANCE);
1542 struct pivot_dimension *statistics = pivot_dimension_create (
1543 direct, PIVOT_AXIS_ROW, N_("Statistics"));
1544 struct pivot_category *nn = pivot_category_create_group (
1545 statistics->root, N_("Nominal by Nominal"));
1546 create_direct_stat (nn, xt, N_("Lambda"), true);
1547 create_direct_stat (nn, xt, N_("Goodman and Kruskal tau"), false);
1548 create_direct_stat (nn, xt, N_("Uncertainty Coefficient"), true);
1549 struct pivot_category *oo = pivot_category_create_group (
1550 statistics->root, N_("Ordinal by Ordinal"));
1551 create_direct_stat (oo, xt, N_("Somers' d"), true);
1552 struct pivot_category *ni = pivot_category_create_group (
1553 statistics->root, N_("Nominal by Interval"));
1554 create_direct_stat (ni, xt, N_("Eta"), false);
1556 for (size_t i = 2; i < xt->n_vars; i++)
1557 add_var_dimension (direct, &xt->vars[i], PIVOT_AXIS_ROW, false);
1562 /* Delete missing rows and columns for statistical analysis when
1565 delete_missing (struct crosstabulation *xt)
1567 size_t n_rows = xt->vars[ROW_VAR].n_values;
1568 size_t n_cols = xt->vars[COL_VAR].n_values;
1571 for (r = 0; r < n_rows; r++)
1572 if (var_is_num_missing (xt->vars[ROW_VAR].var,
1573 xt->vars[ROW_VAR].values[r].f, MV_USER))
1575 for (c = 0; c < n_cols; c++)
1576 xt->mat[c + r * n_cols] = 0.;
1581 for (c = 0; c < n_cols; c++)
1582 if (var_is_num_missing (xt->vars[COL_VAR].var,
1583 xt->vars[COL_VAR].values[c].f, MV_USER))
1585 for (r = 0; r < n_rows; r++)
1586 xt->mat[c + r * n_cols] = 0.;
1592 find_crosstab (struct crosstabulation *xt, size_t *row0p, size_t *row1p)
1594 size_t row0 = *row1p;
1597 if (row0 >= xt->n_entries)
1600 for (row1 = row0 + 1; row1 < xt->n_entries; row1++)
1602 struct freq *a = xt->entries[row0];
1603 struct freq *b = xt->entries[row1];
1604 if (compare_table_entry_vars_3way (a, b, xt, 2, xt->n_vars) != 0)
1612 /* Compares `union value's A_ and B_ and returns a strcmp()-like
1613 result. WIDTH_ points to an int which is either 0 for a
1614 numeric value or a string width for a string value. */
1616 compare_value_3way (const void *a_, const void *b_, const void *width_)
1618 const union value *a = a_;
1619 const union value *b = b_;
1620 const int *width = width_;
1622 return value_compare_3way (a, b, *width);
1625 /* Inverted version of the above */
1627 compare_value_3way_inv (const void *a_, const void *b_, const void *width_)
1629 return -compare_value_3way (a_, b_, width_);
1633 /* Given an array of ENTRY_CNT table_entry structures starting at
1634 ENTRIES, creates a sorted list of the values that the variable
1635 with index VAR_IDX takes on. Stores the array of the values in
1636 XT->values and the number of values in XT->n_values. */
1638 enum_var_values (const struct crosstabulation *xt, int var_idx,
1641 struct xtab_var *xv = &xt->vars[var_idx];
1642 const struct var_range *range = get_var_range (xt->proc, xv->var);
1646 xv->values = xnmalloc (range->count, sizeof *xv->values);
1647 xv->n_values = range->count;
1648 for (size_t i = 0; i < range->count; i++)
1649 xv->values[i].f = range->min + i;
1653 int width = var_get_width (xv->var);
1654 struct hmapx_node *node;
1655 const union value *iter;
1659 for (size_t i = 0; i < xt->n_entries; i++)
1661 const struct freq *te = xt->entries[i];
1662 const union value *value = &te->values[var_idx];
1663 size_t hash = value_hash (value, width, 0);
1665 HMAPX_FOR_EACH_WITH_HASH (iter, node, hash, &set)
1666 if (value_equal (iter, value, width))
1669 hmapx_insert (&set, (union value *) value, hash);
1674 xv->n_values = hmapx_count (&set);
1675 xv->values = xnmalloc (xv->n_values, sizeof *xv->values);
1677 HMAPX_FOR_EACH (iter, node, &set)
1678 xv->values[i++] = *iter;
1679 hmapx_destroy (&set);
1681 sort (xv->values, xv->n_values, sizeof *xv->values,
1682 descending ? compare_value_3way_inv : compare_value_3way,
1688 free_var_values (const struct crosstabulation *xt, int var_idx)
1690 struct xtab_var *xv = &xt->vars[var_idx];
1696 /* Displays the crosstabulation table. */
1698 display_crosstabulation (struct crosstabs_proc *proc,
1699 struct crosstabulation *xt, struct pivot_table *table,
1700 size_t crs_leaves[CRS_N_CELLS])
1702 size_t n_rows = xt->vars[ROW_VAR].n_values;
1703 size_t n_cols = xt->vars[COL_VAR].n_values;
1705 size_t *indexes = xnmalloc (table->n_dimensions, sizeof *indexes);
1706 assert (xt->n_vars == 2);
1707 for (size_t i = 0; i < xt->n_consts; i++)
1708 indexes[i + 3] = xt->const_indexes[i];
1710 /* Put in the actual cells. */
1711 double *mp = xt->mat;
1712 for (size_t r = 0; r < n_rows; r++)
1714 if (!xt->row_tot[r] && proc->mode != INTEGER)
1717 indexes[ROW_VAR + 1] = r;
1718 for (size_t c = 0; c < n_cols; c++)
1720 if (!xt->col_tot[c] && proc->mode != INTEGER)
1723 indexes[COL_VAR + 1] = c;
1725 double expected_value = xt->row_tot[r] * xt->col_tot[c] / xt->total;
1726 double residual = *mp - expected_value;
1727 double sresidual = residual / sqrt (expected_value);
1728 double asresidual = (sresidual
1729 * (1. - xt->row_tot[r] / xt->total)
1730 * (1. - xt->col_tot[c] / xt->total));
1731 double entries[CRS_N_CELLS] = {
1732 [CRS_CL_COUNT] = *mp,
1733 [CRS_CL_ROW] = *mp / xt->row_tot[r] * 100.,
1734 [CRS_CL_COLUMN] = *mp / xt->col_tot[c] * 100.,
1735 [CRS_CL_TOTAL] = *mp / xt->total * 100.,
1736 [CRS_CL_EXPECTED] = expected_value,
1737 [CRS_CL_RESIDUAL] = residual,
1738 [CRS_CL_SRESIDUAL] = sresidual,
1739 [CRS_CL_ASRESIDUAL] = asresidual,
1741 for (size_t i = 0; i < proc->n_cells; i++)
1743 int cell = proc->a_cells[i];
1744 indexes[0] = crs_leaves[cell];
1745 pivot_table_put (table, indexes, table->n_dimensions,
1746 pivot_value_new_number (entries[cell]));
1754 for (size_t r = 0; r < n_rows; r++)
1756 if (!xt->row_tot[r] && proc->mode != INTEGER)
1759 double expected_value = xt->row_tot[r] / xt->total;
1760 double entries[CRS_N_CELLS] = {
1761 [CRS_CL_COUNT] = xt->row_tot[r],
1762 [CRS_CL_ROW] = 100.0,
1763 [CRS_CL_COLUMN] = expected_value * 100.,
1764 [CRS_CL_TOTAL] = expected_value * 100.,
1765 [CRS_CL_EXPECTED] = expected_value,
1766 [CRS_CL_RESIDUAL] = SYSMIS,
1767 [CRS_CL_SRESIDUAL] = SYSMIS,
1768 [CRS_CL_ASRESIDUAL] = SYSMIS,
1770 for (size_t i = 0; i < proc->n_cells; i++)
1772 int cell = proc->a_cells[i];
1773 double entry = entries[cell];
1774 if (entry != SYSMIS)
1776 indexes[ROW_VAR + 1] = r;
1777 indexes[COL_VAR + 1] = n_cols;
1778 indexes[0] = crs_leaves[cell];
1779 pivot_table_put (table, indexes, table->n_dimensions,
1780 pivot_value_new_number (entry));
1785 for (size_t c = 0; c <= n_cols; c++)
1787 if (c < n_cols && !xt->col_tot[c] && proc->mode != INTEGER)
1790 double ct = c < n_cols ? xt->col_tot[c] : xt->total;
1791 double expected_value = ct / xt->total;
1792 double entries[CRS_N_CELLS] = {
1793 [CRS_CL_COUNT] = ct,
1794 [CRS_CL_ROW] = expected_value * 100.0,
1795 [CRS_CL_COLUMN] = 100.0,
1796 [CRS_CL_TOTAL] = expected_value * 100.,
1797 [CRS_CL_EXPECTED] = expected_value,
1798 [CRS_CL_RESIDUAL] = SYSMIS,
1799 [CRS_CL_SRESIDUAL] = SYSMIS,
1800 [CRS_CL_ASRESIDUAL] = SYSMIS,
1802 for (size_t i = 0; i < proc->n_cells; i++)
1804 int cell = proc->a_cells[i];
1805 double entry = entries[cell];
1806 if (entry != SYSMIS)
1808 indexes[ROW_VAR + 1] = n_rows;
1809 indexes[COL_VAR + 1] = c;
1810 indexes[0] = crs_leaves[cell];
1811 pivot_table_put (table, indexes, table->n_dimensions,
1812 pivot_value_new_number (entry));
1820 static void calc_r (struct crosstabulation *,
1821 double *XT, double *Y, double *, double *, double *);
1822 static void calc_chisq (struct crosstabulation *,
1823 double[N_CHISQ], int[N_CHISQ], double *, double *);
1825 /* Display chi-square statistics. */
1827 display_chisq (struct crosstabulation *xt, struct pivot_table *chisq)
1829 double chisq_v[N_CHISQ];
1830 double fisher1, fisher2;
1832 calc_chisq (xt, chisq_v, df, &fisher1, &fisher2);
1834 size_t *indexes = xnmalloc (chisq->n_dimensions, sizeof *indexes);
1835 assert (xt->n_vars == 2);
1836 for (size_t i = 0; i < xt->n_consts; i++)
1837 indexes[i + 2] = xt->const_indexes[i];
1838 for (int i = 0; i < N_CHISQ; i++)
1842 double entries[5] = { SYSMIS, SYSMIS, SYSMIS, SYSMIS, SYSMIS };
1845 entries[3] = fisher2;
1846 entries[4] = fisher1;
1848 else if (chisq_v[i] != SYSMIS)
1850 entries[0] = chisq_v[i];
1852 entries[2] = gsl_cdf_chisq_Q (chisq_v[i], df[i]);
1855 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1856 if (entries[j] != SYSMIS)
1859 pivot_table_put (chisq, indexes, chisq->n_dimensions,
1860 pivot_value_new_number (entries[j]));
1866 pivot_table_put (chisq, indexes, chisq->n_dimensions,
1867 pivot_value_new_number (xt->total));
1872 static int calc_symmetric (struct crosstabs_proc *, struct crosstabulation *,
1873 double[N_SYMMETRIC], double[N_SYMMETRIC],
1874 double[N_SYMMETRIC],
1875 double[3], double[3], double[3]);
1877 /* Display symmetric measures. */
1879 display_symmetric (struct crosstabs_proc *proc, struct crosstabulation *xt,
1880 struct pivot_table *sym)
1882 double sym_v[N_SYMMETRIC], sym_ase[N_SYMMETRIC], sym_t[N_SYMMETRIC];
1883 double somers_d_v[3], somers_d_ase[3], somers_d_t[3];
1885 if (!calc_symmetric (proc, xt, sym_v, sym_ase, sym_t,
1886 somers_d_v, somers_d_ase, somers_d_t))
1889 size_t *indexes = xnmalloc (sym->n_dimensions, sizeof *indexes);
1890 assert (xt->n_vars == 2);
1891 for (size_t i = 0; i < xt->n_consts; i++)
1892 indexes[i + 2] = xt->const_indexes[i];
1894 for (int i = 0; i < N_SYMMETRIC; i++)
1896 if (sym_v[i] == SYSMIS)
1901 double entries[] = { sym_v[i], sym_ase[i], sym_t[i] };
1902 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1903 if (entries[j] != SYSMIS)
1906 pivot_table_put (sym, indexes, sym->n_dimensions,
1907 pivot_value_new_number (entries[j]));
1911 indexes[1] = N_SYMMETRIC;
1913 struct pivot_value *total = pivot_value_new_number (xt->total);
1914 pivot_value_set_rc (sym, total, PIVOT_RC_COUNT);
1915 pivot_table_put (sym, indexes, sym->n_dimensions, total);
1920 static bool calc_risk (struct crosstabulation *,
1921 double[], double[], double[], union value *,
1924 /* Display risk estimate. */
1926 display_risk (struct crosstabulation *xt, struct pivot_table *risk,
1927 struct pivot_dimension *risk_statistics)
1929 double risk_v[3], lower[3], upper[3], n_valid;
1931 if (!calc_risk (xt, risk_v, upper, lower, c, &n_valid))
1934 size_t *indexes = xnmalloc (risk->n_dimensions, sizeof *indexes);
1935 assert (xt->n_vars == 2);
1936 for (size_t i = 0; i < xt->n_consts; i++)
1937 indexes[i + 2] = xt->const_indexes[i];
1939 for (int i = 0; i < 3; i++)
1941 const struct variable *cv = xt->vars[COL_VAR].var;
1942 const struct variable *rv = xt->vars[ROW_VAR].var;
1944 if (risk_v[i] == SYSMIS)
1947 struct string label = DS_EMPTY_INITIALIZER;
1951 ds_put_format (&label, _("Odds Ratio for %s"), var_to_string (rv));
1952 ds_put_cstr (&label, " (");
1953 var_append_value_name (rv, &c[0], &label);
1954 ds_put_cstr (&label, " / ");
1955 var_append_value_name (rv, &c[1], &label);
1956 ds_put_cstr (&label, ")");
1960 ds_put_format (&label, _("For cohort %s = "), var_to_string (cv));
1961 var_append_value_name (cv, &xt->vars[ROW_VAR].values[i - 1], &label);
1965 indexes[1] = pivot_category_create_leaf (
1966 risk_statistics->root,
1967 pivot_value_new_user_text_nocopy (ds_steal_cstr (&label)));
1969 double entries[] = { risk_v[i], lower[i], upper[i] };
1970 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1973 pivot_table_put (risk, indexes, risk->n_dimensions,
1974 pivot_value_new_number (entries[i]));
1977 indexes[1] = pivot_category_create_leaf (
1978 risk_statistics->root,
1979 pivot_value_new_text (N_("N of Valid Cases")));
1981 pivot_table_put (risk, indexes, risk->n_dimensions,
1982 pivot_value_new_number (n_valid));
1986 static int calc_directional (struct crosstabs_proc *, struct crosstabulation *,
1987 double[N_DIRECTIONAL], double[N_DIRECTIONAL],
1988 double[N_DIRECTIONAL], double[N_DIRECTIONAL]);
1990 /* Display directional measures. */
1992 display_directional (struct crosstabs_proc *proc,
1993 struct crosstabulation *xt, struct pivot_table *direct)
1995 double direct_v[N_DIRECTIONAL];
1996 double direct_ase[N_DIRECTIONAL];
1997 double direct_t[N_DIRECTIONAL];
1998 double sig[N_DIRECTIONAL];
1999 if (!calc_directional (proc, xt, direct_v, direct_ase, direct_t, sig))
2002 size_t *indexes = xnmalloc (direct->n_dimensions, sizeof *indexes);
2003 assert (xt->n_vars == 2);
2004 for (size_t i = 0; i < xt->n_consts; i++)
2005 indexes[i + 2] = xt->const_indexes[i];
2007 for (int i = 0; i < N_DIRECTIONAL; i++)
2009 if (direct_v[i] == SYSMIS)
2014 double entries[] = {
2015 direct_v[i], direct_ase[i], direct_t[i], sig[i],
2017 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
2018 if (entries[j] != SYSMIS)
2021 pivot_table_put (direct, indexes, direct->n_dimensions,
2022 pivot_value_new_number (entries[j]));
2029 /* Statistical calculations. */
2031 /* Returns the value of the logarithm of gamma (factorial) function for an integer
2034 log_gamma_int (double xt)
2039 for (i = 2; i < xt; i++)
2045 /* Calculate P_r as specified in _SPSS Statistical Algorithms_,
2047 static inline double
2048 Pr (int a, int b, int c, int d)
2050 return exp (log_gamma_int (a + b + 1.) - log_gamma_int (a + 1.)
2051 + log_gamma_int (c + d + 1.) - log_gamma_int (b + 1.)
2052 + log_gamma_int (a + c + 1.) - log_gamma_int (c + 1.)
2053 + log_gamma_int (b + d + 1.) - log_gamma_int (d + 1.)
2054 - log_gamma_int (a + b + c + d + 1.));
2057 /* Swap the contents of A and B. */
2059 swap (int *a, int *b)
2066 /* Calculate significance for Fisher's exact test as specified in
2067 _SPSS Statistical Algorithms_, Appendix 5. */
2069 calc_fisher (int a, int b, int c, int d, double *fisher1, double *fisher2)
2074 if (MIN (c, d) < MIN (a, b))
2075 swap (&a, &c), swap (&b, &d);
2076 if (MIN (b, d) < MIN (a, c))
2077 swap (&a, &b), swap (&c, &d);
2081 swap (&a, &b), swap (&c, &d);
2083 swap (&a, &c), swap (&b, &d);
2086 pn1 = Pr (a, b, c, d);
2088 for (xt = 1; xt <= a; xt++)
2090 *fisher1 += Pr (a - xt, b + xt, c + xt, d - xt);
2093 *fisher2 = *fisher1;
2095 for (xt = 1; xt <= b; xt++)
2097 double p = Pr (a + xt, b - xt, c - xt, d + xt);
2103 /* Calculates chi-squares into CHISQ. MAT is a matrix with N_COLS
2104 columns with values COLS and N_ROWS rows with values ROWS. Values
2105 in the matrix sum to xt->total. */
2107 calc_chisq (struct crosstabulation *xt,
2108 double chisq[N_CHISQ], int df[N_CHISQ],
2109 double *fisher1, double *fisher2)
2111 chisq[0] = chisq[1] = 0.;
2112 chisq[2] = chisq[3] = chisq[4] = SYSMIS;
2113 *fisher1 = *fisher2 = SYSMIS;
2115 df[0] = df[1] = (xt->ns_cols - 1) * (xt->ns_rows - 1);
2117 if (xt->ns_rows <= 1 || xt->ns_cols <= 1)
2119 chisq[0] = chisq[1] = SYSMIS;
2123 size_t n_cols = xt->vars[COL_VAR].n_values;
2124 FOR_EACH_POPULATED_ROW (r, xt)
2125 FOR_EACH_POPULATED_COLUMN (c, xt)
2127 const double expected = xt->row_tot[r] * xt->col_tot[c] / xt->total;
2128 const double freq = xt->mat[n_cols * r + c];
2129 const double residual = freq - expected;
2131 chisq[0] += residual * residual / expected;
2133 chisq[1] += freq * log (expected / freq);
2144 /* Calculate Yates and Fisher exact test. */
2145 if (xt->ns_cols == 2 && xt->ns_rows == 2)
2147 double f11, f12, f21, f22;
2153 FOR_EACH_POPULATED_COLUMN (c, xt)
2161 f11 = xt->mat[nz_cols[0]];
2162 f12 = xt->mat[nz_cols[1]];
2163 f21 = xt->mat[nz_cols[0] + n_cols];
2164 f22 = xt->mat[nz_cols[1] + n_cols];
2169 const double xt_ = fabs (f11 * f22 - f12 * f21) - 0.5 * xt->total;
2172 chisq[3] = (xt->total * pow2 (xt_)
2173 / (f11 + f12) / (f21 + f22)
2174 / (f11 + f21) / (f12 + f22));
2182 calc_fisher (f11 + .5, f12 + .5, f21 + .5, f22 + .5, fisher1, fisher2);
2185 /* Calculate Mantel-Haenszel. */
2186 if (var_is_numeric (xt->vars[ROW_VAR].var)
2187 && var_is_numeric (xt->vars[COL_VAR].var))
2189 double r, ase_0, ase_1;
2190 calc_r (xt, (double *) xt->vars[ROW_VAR].values,
2191 (double *) xt->vars[COL_VAR].values,
2192 &r, &ase_0, &ase_1);
2194 chisq[4] = (xt->total - 1.) * r * r;
2199 /* Calculate the value of Pearson's r. r is stored into R, its T value into
2200 T, and standard error into ERROR. The row and column values must be
2201 passed in XT and Y. */
2203 calc_r (struct crosstabulation *xt,
2204 double *XT, double *Y, double *r, double *t, double *error)
2206 size_t n_rows = xt->vars[ROW_VAR].n_values;
2207 size_t n_cols = xt->vars[COL_VAR].n_values;
2208 double SX, SY, S, T;
2210 double sum_XYf, sum_X2Y2f;
2211 double sum_Xr, sum_X2r;
2212 double sum_Yc, sum_Y2c;
2215 for (sum_X2Y2f = sum_XYf = 0., i = 0; i < n_rows; i++)
2216 for (j = 0; j < n_cols; j++)
2218 double fij = xt->mat[j + i * n_cols];
2219 double product = XT[i] * Y[j];
2220 double temp = fij * product;
2222 sum_X2Y2f += temp * product;
2225 for (sum_Xr = sum_X2r = 0., i = 0; i < n_rows; i++)
2227 sum_Xr += XT[i] * xt->row_tot[i];
2228 sum_X2r += pow2 (XT[i]) * xt->row_tot[i];
2230 Xbar = sum_Xr / xt->total;
2232 for (sum_Yc = sum_Y2c = 0., i = 0; i < n_cols; i++)
2234 sum_Yc += Y[i] * xt->col_tot[i];
2235 sum_Y2c += Y[i] * Y[i] * xt->col_tot[i];
2237 Ybar = sum_Yc / xt->total;
2239 S = sum_XYf - sum_Xr * sum_Yc / xt->total;
2240 SX = sum_X2r - pow2 (sum_Xr) / xt->total;
2241 SY = sum_Y2c - pow2 (sum_Yc) / xt->total;
2244 *t = *r / sqrt (1 - pow2 (*r)) * sqrt (xt->total - 2);
2249 for (s = c = 0., i = 0; i < n_rows; i++)
2250 for (j = 0; j < n_cols; j++)
2252 double Xresid, Yresid;
2255 Xresid = XT[i] - Xbar;
2256 Yresid = Y[j] - Ybar;
2257 temp = (T * Xresid * Yresid
2259 * (Xresid * Xresid * SY + Yresid * Yresid * SX)));
2260 y = xt->mat[j + i * n_cols] * temp * temp - c;
2265 *error = sqrt (s) / (T * T);
2269 /* Calculate symmetric statistics and their asymptotic standard
2270 errors. Returns 0 if none could be calculated. */
2272 calc_symmetric (struct crosstabs_proc *proc, struct crosstabulation *xt,
2273 double v[N_SYMMETRIC], double ase[N_SYMMETRIC],
2274 double t[N_SYMMETRIC],
2275 double somers_d_v[3], double somers_d_ase[3],
2276 double somers_d_t[3])
2278 size_t n_rows = xt->vars[ROW_VAR].n_values;
2279 size_t n_cols = xt->vars[COL_VAR].n_values;
2282 q = MIN (xt->ns_rows, xt->ns_cols);
2286 for (i = 0; i < N_SYMMETRIC; i++)
2287 v[i] = ase[i] = t[i] = SYSMIS;
2289 /* Phi, Cramer's V, contingency coefficient. */
2290 if (proc->statistics & (CRS_ST_PHI | CRS_ST_CC))
2292 double Xp = 0.; /* Pearson chi-square. */
2294 FOR_EACH_POPULATED_ROW (r, xt)
2295 FOR_EACH_POPULATED_COLUMN (c, xt)
2297 double expected = xt->row_tot[r] * xt->col_tot[c] / xt->total;
2298 double freq = xt->mat[n_cols * r + c];
2299 double residual = freq - expected;
2301 Xp += residual * residual / expected;
2304 if (proc->statistics & CRS_ST_PHI)
2306 v[0] = sqrt (Xp / xt->total);
2307 v[1] = sqrt (Xp / (xt->total * (q - 1)));
2309 if (proc->statistics & CRS_ST_CC)
2310 v[2] = sqrt (Xp / (Xp + xt->total));
2313 if (proc->statistics & (CRS_ST_BTAU | CRS_ST_CTAU
2314 | CRS_ST_GAMMA | CRS_ST_D))
2319 double btau_cum, ctau_cum, gamma_cum, d_yx_cum, d_xy_cum;
2323 Dr = Dc = pow2 (xt->total);
2324 for (r = 0; r < n_rows; r++)
2325 Dr -= pow2 (xt->row_tot[r]);
2326 for (c = 0; c < n_cols; c++)
2327 Dc -= pow2 (xt->col_tot[c]);
2329 cum = xnmalloc (n_cols * n_rows, sizeof *cum);
2330 for (c = 0; c < n_cols; c++)
2334 for (r = 0; r < n_rows; r++)
2335 cum[c + r * n_cols] = ct += xt->mat[c + r * n_cols];
2344 for (i = 0; i < n_rows; i++)
2348 for (j = 1; j < n_cols; j++)
2349 Cij += xt->col_tot[j] - cum[j + i * n_cols];
2352 for (j = 1; j < n_cols; j++)
2353 Dij += cum[j + (i - 1) * n_cols];
2357 double fij = xt->mat[j + i * n_cols];
2363 assert (j < n_cols);
2365 Cij -= xt->col_tot[j] - cum[j + i * n_cols];
2366 Dij += xt->col_tot[j - 1] - cum[j - 1 + i * n_cols];
2370 Cij += cum[j - 1 + (i - 1) * n_cols];
2371 Dij -= cum[j + (i - 1) * n_cols];
2377 if (proc->statistics & CRS_ST_BTAU)
2378 v[3] = (P - Q) / sqrt (Dr * Dc);
2379 if (proc->statistics & CRS_ST_CTAU)
2380 v[4] = (q * (P - Q)) / (pow2 (xt->total) * (q - 1));
2381 if (proc->statistics & CRS_ST_GAMMA)
2382 v[5] = (P - Q) / (P + Q);
2384 /* ASE for tau-b, tau-c, gamma. Calculations could be
2385 eliminated here, at expense of memory. */
2390 btau_cum = ctau_cum = gamma_cum = d_yx_cum = d_xy_cum = 0.;
2391 for (i = 0; i < n_rows; i++)
2395 for (j = 1; j < n_cols; j++)
2396 Cij += xt->col_tot[j] - cum[j + i * n_cols];
2399 for (j = 1; j < n_cols; j++)
2400 Dij += cum[j + (i - 1) * n_cols];
2404 double fij = xt->mat[j + i * n_cols];
2406 if (proc->statistics & CRS_ST_BTAU)
2408 const double temp = (2. * sqrt (Dr * Dc) * (Cij - Dij)
2409 + v[3] * (xt->row_tot[i] * Dc
2410 + xt->col_tot[j] * Dr));
2411 btau_cum += fij * temp * temp;
2415 const double temp = Cij - Dij;
2416 ctau_cum += fij * temp * temp;
2419 if (proc->statistics & CRS_ST_GAMMA)
2421 const double temp = Q * Cij - P * Dij;
2422 gamma_cum += fij * temp * temp;
2425 if (proc->statistics & CRS_ST_D)
2427 d_yx_cum += fij * pow2 (Dr * (Cij - Dij)
2428 - (P - Q) * (xt->total - xt->row_tot[i]));
2429 d_xy_cum += fij * pow2 (Dc * (Dij - Cij)
2430 - (Q - P) * (xt->total - xt->col_tot[j]));
2435 assert (j < n_cols);
2437 Cij -= xt->col_tot[j] - cum[j + i * n_cols];
2438 Dij += xt->col_tot[j - 1] - cum[j - 1 + i * n_cols];
2442 Cij += cum[j - 1 + (i - 1) * n_cols];
2443 Dij -= cum[j + (i - 1) * n_cols];
2449 btau_var = ((btau_cum
2450 - (xt->total * pow2 (xt->total * (P - Q) / sqrt (Dr * Dc) * (Dr + Dc))))
2452 if (proc->statistics & CRS_ST_BTAU)
2454 ase[3] = sqrt (btau_var);
2455 t[3] = v[3] / (2 * sqrt ((ctau_cum - (P - Q) * (P - Q) / xt->total)
2458 if (proc->statistics & CRS_ST_CTAU)
2460 ase[4] = ((2 * q / ((q - 1) * pow2 (xt->total)))
2461 * sqrt (ctau_cum - (P - Q) * (P - Q) / xt->total));
2462 t[4] = v[4] / ase[4];
2464 if (proc->statistics & CRS_ST_GAMMA)
2466 ase[5] = ((4. / ((P + Q) * (P + Q))) * sqrt (gamma_cum));
2467 t[5] = v[5] / (2. / (P + Q)
2468 * sqrt (ctau_cum - (P - Q) * (P - Q) / xt->total));
2470 if (proc->statistics & CRS_ST_D)
2472 somers_d_v[0] = (P - Q) / (.5 * (Dc + Dr));
2473 somers_d_ase[0] = SYSMIS;
2474 somers_d_t[0] = (somers_d_v[0]
2476 * sqrt (ctau_cum - pow2 (P - Q) / xt->total)));
2477 somers_d_v[1] = (P - Q) / Dc;
2478 somers_d_ase[1] = 2. / pow2 (Dc) * sqrt (d_xy_cum);
2479 somers_d_t[1] = (somers_d_v[1]
2481 * sqrt (ctau_cum - pow2 (P - Q) / xt->total)));
2482 somers_d_v[2] = (P - Q) / Dr;
2483 somers_d_ase[2] = 2. / pow2 (Dr) * sqrt (d_yx_cum);
2484 somers_d_t[2] = (somers_d_v[2]
2486 * sqrt (ctau_cum - pow2 (P - Q) / xt->total)));
2492 /* Spearman correlation, Pearson's r. */
2493 if (proc->statistics & CRS_ST_CORR)
2495 double *R = xmalloc (sizeof *R * n_rows);
2496 double *C = xmalloc (sizeof *C * n_cols);
2499 double y, t, c = 0., s = 0.;
2504 R[i] = s + (xt->row_tot[i] + 1.) / 2.;
2505 y = xt->row_tot[i] - c;
2511 assert (i < n_rows);
2516 double y, t, c = 0., s = 0.;
2521 C[j] = s + (xt->col_tot[j] + 1.) / 2;
2522 y = xt->col_tot[j] - c;
2528 assert (j < n_cols);
2532 calc_r (xt, R, C, &v[6], &t[6], &ase[6]);
2537 calc_r (xt, (double *) xt->vars[ROW_VAR].values,
2538 (double *) xt->vars[COL_VAR].values,
2539 &v[7], &t[7], &ase[7]);
2542 /* Cohen's kappa. */
2543 if (proc->statistics & CRS_ST_KAPPA && xt->ns_rows == xt->ns_cols)
2545 double ase_under_h0;
2546 double sum_fii, sum_rici, sum_fiiri_ci, sum_fijri_ci2, sum_riciri_ci;
2549 for (sum_fii = sum_rici = sum_fiiri_ci = sum_riciri_ci = 0., i = j = 0;
2550 i < xt->ns_rows; i++, j++)
2554 while (xt->col_tot[j] == 0.)
2557 prod = xt->row_tot[i] * xt->col_tot[j];
2558 sum = xt->row_tot[i] + xt->col_tot[j];
2560 sum_fii += xt->mat[j + i * n_cols];
2562 sum_fiiri_ci += xt->mat[j + i * n_cols] * sum;
2563 sum_riciri_ci += prod * sum;
2565 for (sum_fijri_ci2 = 0., i = 0; i < xt->ns_rows; i++)
2566 for (j = 0; j < xt->ns_cols; j++)
2568 double sum = xt->row_tot[i] + xt->col_tot[j];
2569 sum_fijri_ci2 += xt->mat[j + i * n_cols] * sum * sum;
2572 v[8] = (xt->total * sum_fii - sum_rici) / (pow2 (xt->total) - sum_rici);
2574 ase_under_h0 = sqrt ((pow2 (xt->total) * sum_rici
2575 + sum_rici * sum_rici
2576 - xt->total * sum_riciri_ci)
2577 / (xt->total * (pow2 (xt->total) - sum_rici) * (pow2 (xt->total) - sum_rici)));
2579 ase[8] = sqrt (xt->total * (((sum_fii * (xt->total - sum_fii))
2580 / pow2 (pow2 (xt->total) - sum_rici))
2581 + ((2. * (xt->total - sum_fii)
2582 * (2. * sum_fii * sum_rici
2583 - xt->total * sum_fiiri_ci))
2584 / pow3 (pow2 (xt->total) - sum_rici))
2585 + (pow2 (xt->total - sum_fii)
2586 * (xt->total * sum_fijri_ci2 - 4.
2587 * sum_rici * sum_rici)
2588 / pow4 (pow2 (xt->total) - sum_rici))));
2590 t[8] = v[8] / ase_under_h0;
2596 /* Calculate risk estimate. */
2598 calc_risk (struct crosstabulation *xt,
2599 double *value, double *upper, double *lower, union value *c,
2602 size_t n_cols = xt->vars[COL_VAR].n_values;
2603 double f11, f12, f21, f22;
2606 for (int i = 0; i < 3; i++)
2607 value[i] = upper[i] = lower[i] = SYSMIS;
2609 if (xt->ns_rows != 2 || xt->ns_cols != 2)
2613 /* Find populated columns. */
2616 FOR_EACH_POPULATED_COLUMN (c, xt)
2620 /* Find populated rows. */
2623 FOR_EACH_POPULATED_ROW (r, xt)
2627 f11 = xt->mat[nz_cols[0] + n_cols * nz_rows[0]];
2628 f12 = xt->mat[nz_cols[1] + n_cols * nz_rows[0]];
2629 f21 = xt->mat[nz_cols[0] + n_cols * nz_rows[1]];
2630 f22 = xt->mat[nz_cols[1] + n_cols * nz_rows[1]];
2631 *n_valid = f11 + f12 + f21 + f22;
2633 c[0] = xt->vars[COL_VAR].values[nz_cols[0]];
2634 c[1] = xt->vars[COL_VAR].values[nz_cols[1]];
2637 value[0] = (f11 * f22) / (f12 * f21);
2638 v = sqrt (1. / f11 + 1. / f12 + 1. / f21 + 1. / f22);
2639 lower[0] = value[0] * exp (-1.960 * v);
2640 upper[0] = value[0] * exp (1.960 * v);
2642 value[1] = (f11 * (f21 + f22)) / (f21 * (f11 + f12));
2643 v = sqrt ((f12 / (f11 * (f11 + f12)))
2644 + (f22 / (f21 * (f21 + f22))));
2645 lower[1] = value[1] * exp (-1.960 * v);
2646 upper[1] = value[1] * exp (1.960 * v);
2648 value[2] = (f12 * (f21 + f22)) / (f22 * (f11 + f12));
2649 v = sqrt ((f11 / (f12 * (f11 + f12)))
2650 + (f21 / (f22 * (f21 + f22))));
2651 lower[2] = value[2] * exp (-1.960 * v);
2652 upper[2] = value[2] * exp (1.960 * v);
2657 /* Calculate directional measures. */
2659 calc_directional (struct crosstabs_proc *proc, struct crosstabulation *xt,
2660 double v[N_DIRECTIONAL], double ase[N_DIRECTIONAL],
2661 double t[N_DIRECTIONAL], double sig[N_DIRECTIONAL])
2663 size_t n_rows = xt->vars[ROW_VAR].n_values;
2664 size_t n_cols = xt->vars[COL_VAR].n_values;
2665 for (int i = 0; i < N_DIRECTIONAL; i++)
2666 v[i] = ase[i] = t[i] = sig[i] = SYSMIS;
2669 if (proc->statistics & CRS_ST_LAMBDA)
2671 /* Find maximum for each row and their sum. */
2672 double *fim = xnmalloc (n_rows, sizeof *fim);
2673 int *fim_index = xnmalloc (n_rows, sizeof *fim_index);
2674 double sum_fim = 0.0;
2675 for (int i = 0; i < n_rows; i++)
2677 double max = xt->mat[i * n_cols];
2680 for (int j = 1; j < n_cols; j++)
2681 if (xt->mat[j + i * n_cols] > max)
2683 max = xt->mat[j + i * n_cols];
2689 fim_index[i] = index;
2692 /* Find maximum for each column. */
2693 double *fmj = xnmalloc (n_cols, sizeof *fmj);
2694 int *fmj_index = xnmalloc (n_cols, sizeof *fmj_index);
2695 double sum_fmj = 0.0;
2696 for (int j = 0; j < n_cols; j++)
2698 double max = xt->mat[j];
2701 for (int i = 1; i < n_rows; i++)
2702 if (xt->mat[j + i * n_cols] > max)
2704 max = xt->mat[j + i * n_cols];
2710 fmj_index[j] = index;
2713 /* Find maximum row total. */
2714 double rm = xt->row_tot[0];
2716 for (int i = 1; i < n_rows; i++)
2717 if (xt->row_tot[i] > rm)
2719 rm = xt->row_tot[i];
2723 /* Find maximum column total. */
2724 double cm = xt->col_tot[0];
2726 for (int j = 1; j < n_cols; j++)
2727 if (xt->col_tot[j] > cm)
2729 cm = xt->col_tot[j];
2733 v[0] = (sum_fim + sum_fmj - cm - rm) / (2. * xt->total - rm - cm);
2734 v[1] = (sum_fmj - rm) / (xt->total - rm);
2735 v[2] = (sum_fim - cm) / (xt->total - cm);
2737 /* ASE1 for Y given XT. */
2740 for (int i = 0; i < n_rows; i++)
2741 if (cm_index == fim_index[i])
2743 ase[2] = sqrt ((xt->total - sum_fim) * (sum_fim + cm - 2. * accum)
2744 / pow3 (xt->total - cm));
2747 /* ASE0 for Y given XT. */
2750 for (int i = 0; i < n_rows; i++)
2751 if (cm_index != fim_index[i])
2752 accum += (xt->mat[i * n_cols + fim_index[i]]
2753 + xt->mat[i * n_cols + cm_index]);
2754 t[2] = v[2] / (sqrt (accum - pow2 (sum_fim - cm) / xt->total) / (xt->total - cm));
2757 /* ASE1 for XT given Y. */
2760 for (int j = 0; j < n_cols; j++)
2761 if (rm_index == fmj_index[j])
2763 ase[1] = sqrt ((xt->total - sum_fmj) * (sum_fmj + rm - 2. * accum)
2764 / pow3 (xt->total - rm));
2767 /* ASE0 for XT given Y. */
2770 for (int j = 0; j < n_cols; j++)
2771 if (rm_index != fmj_index[j])
2772 accum += (xt->mat[j + n_cols * fmj_index[j]]
2773 + xt->mat[j + n_cols * rm_index]);
2774 t[1] = v[1] / (sqrt (accum - pow2 (sum_fmj - rm) / xt->total) / (xt->total - rm));
2777 /* Symmetric ASE0 and ASE1. */
2779 double accum0 = 0.0;
2780 double accum1 = 0.0;
2781 for (int i = 0; i < n_rows; i++)
2782 for (int j = 0; j < n_cols; j++)
2784 int temp0 = (fmj_index[j] == i) + (fim_index[i] == j);
2785 int temp1 = (i == rm_index) + (j == cm_index);
2786 accum0 += xt->mat[j + i * n_cols] * pow2 (temp0 - temp1);
2787 accum1 += (xt->mat[j + i * n_cols]
2788 * pow2 (temp0 + (v[0] - 1.) * temp1));
2790 ase[0] = sqrt (accum1 - 4. * xt->total * v[0] * v[0]) / (2. * xt->total - rm - cm);
2791 t[0] = v[0] / (sqrt (accum0 - pow2 (sum_fim + sum_fmj - cm - rm) / xt->total)
2792 / (2. * xt->total - rm - cm));
2795 for (int i = 0; i < 3; i++)
2796 sig[i] = 2 * gsl_cdf_ugaussian_Q (t[i]);
2805 double sum_fij2_ri = 0.0;
2806 double sum_fij2_ci = 0.0;
2807 FOR_EACH_POPULATED_ROW (i, xt)
2808 FOR_EACH_POPULATED_COLUMN (j, xt)
2810 double temp = pow2 (xt->mat[j + i * n_cols]);
2811 sum_fij2_ri += temp / xt->row_tot[i];
2812 sum_fij2_ci += temp / xt->col_tot[j];
2815 double sum_ri2 = 0.0;
2816 for (int i = 0; i < n_rows; i++)
2817 sum_ri2 += pow2 (xt->row_tot[i]);
2819 double sum_cj2 = 0.0;
2820 for (int j = 0; j < n_cols; j++)
2821 sum_cj2 += pow2 (xt->col_tot[j]);
2823 v[3] = (xt->total * sum_fij2_ci - sum_ri2) / (pow2 (xt->total) - sum_ri2);
2824 v[4] = (xt->total * sum_fij2_ri - sum_cj2) / (pow2 (xt->total) - sum_cj2);
2828 if (proc->statistics & CRS_ST_UC)
2831 FOR_EACH_POPULATED_ROW (i, xt)
2832 UX -= xt->row_tot[i] / xt->total * log (xt->row_tot[i] / xt->total);
2835 FOR_EACH_POPULATED_COLUMN (j, xt)
2836 UY -= xt->col_tot[j] / xt->total * log (xt->col_tot[j] / xt->total);
2840 for (int i = 0; i < n_rows; i++)
2841 for (int j = 0; j < n_cols; j++)
2843 double entry = xt->mat[j + i * n_cols];
2848 P += entry * pow2 (log (xt->col_tot[j] * xt->row_tot[i] / (xt->total * entry)));
2849 UXY -= entry / xt->total * log (entry / xt->total);
2852 double ase1_yx = 0.0;
2853 double ase1_xy = 0.0;
2854 double ase1_sym = 0.0;
2855 for (int i = 0; i < n_rows; i++)
2856 for (int j = 0; j < n_cols; j++)
2858 double entry = xt->mat[j + i * n_cols];
2863 ase1_yx += entry * pow2 (UY * log (entry / xt->row_tot[i])
2864 + (UX - UXY) * log (xt->col_tot[j] / xt->total));
2865 ase1_xy += entry * pow2 (UX * log (entry / xt->col_tot[j])
2866 + (UY - UXY) * log (xt->row_tot[i] / xt->total));
2867 ase1_sym += entry * pow2 ((UXY
2868 * log (xt->row_tot[i] * xt->col_tot[j] / pow2 (xt->total)))
2869 - (UX + UY) * log (entry / xt->total));
2872 v[5] = 2. * ((UX + UY - UXY) / (UX + UY));
2873 ase[5] = (2. / (xt->total * pow2 (UX + UY))) * sqrt (ase1_sym);
2876 v[6] = (UX + UY - UXY) / UX;
2877 ase[6] = sqrt (ase1_xy) / (xt->total * UX * UX);
2878 t[6] = v[6] / (sqrt (P - xt->total * pow2 (UX + UY - UXY)) / (xt->total * UX));
2880 v[7] = (UX + UY - UXY) / UY;
2881 ase[7] = sqrt (ase1_yx) / (xt->total * UY * UY);
2882 t[7] = v[7] / (sqrt (P - xt->total * pow2 (UX + UY - UXY)) / (xt->total * UY));
2886 if (proc->statistics & CRS_ST_D)
2888 double v_dummy[N_SYMMETRIC];
2889 double ase_dummy[N_SYMMETRIC];
2890 double t_dummy[N_SYMMETRIC];
2891 double somers_d_v[3];
2892 double somers_d_ase[3];
2893 double somers_d_t[3];
2895 if (calc_symmetric (proc, xt, v_dummy, ase_dummy, t_dummy,
2896 somers_d_v, somers_d_ase, somers_d_t))
2898 for (int i = 0; i < 3; i++)
2900 v[8 + i] = somers_d_v[i];
2901 ase[8 + i] = somers_d_ase[i];
2902 t[8 + i] = somers_d_t[i];
2903 sig[8 + i] = 2 * gsl_cdf_ugaussian_Q (fabs (somers_d_t[i]));
2909 if (proc->statistics & CRS_ST_ETA)
2912 double sum_Xr = 0.0;
2913 double sum_X2r = 0.0;
2914 for (int i = 0; i < n_rows; i++)
2916 sum_Xr += xt->vars[ROW_VAR].values[i].f * xt->row_tot[i];
2917 sum_X2r += pow2 (xt->vars[ROW_VAR].values[i].f) * xt->row_tot[i];
2919 double SX = sum_X2r - pow2 (sum_Xr) / xt->total;
2922 FOR_EACH_POPULATED_COLUMN (j, xt)
2926 for (int i = 0; i < n_rows; i++)
2928 SXW += (pow2 (xt->vars[ROW_VAR].values[i].f)
2929 * xt->mat[j + i * n_cols]);
2930 cum += (xt->vars[ROW_VAR].values[i].f
2931 * xt->mat[j + i * n_cols]);
2934 SXW -= cum * cum / xt->col_tot[j];
2936 v[11] = sqrt (1. - SXW / SX);
2939 double sum_Yc = 0.0;
2940 double sum_Y2c = 0.0;
2941 for (int i = 0; i < n_cols; i++)
2943 sum_Yc += xt->vars[COL_VAR].values[i].f * xt->col_tot[i];
2944 sum_Y2c += pow2 (xt->vars[COL_VAR].values[i].f) * xt->col_tot[i];
2946 double SY = sum_Y2c - pow2 (sum_Yc) / xt->total;
2949 FOR_EACH_POPULATED_ROW (i, xt)
2952 for (int j = 0; j < n_cols; j++)
2954 SYW += (pow2 (xt->vars[COL_VAR].values[j].f)
2955 * xt->mat[j + i * n_cols]);
2956 cum += (xt->vars[COL_VAR].values[j].f
2957 * xt->mat[j + i * n_cols]);
2960 SYW -= cum * cum / xt->row_tot[i];
2962 v[12] = sqrt (1. - SYW / SY);