1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <gsl/gsl_cdf.h>
22 #include <gsl/gsl_randist.h>
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/format.h"
28 #include "data/missing-values.h"
29 #include "data/variable.h"
30 #include "language/stats/npar.h"
31 #include "libpspp/str.h"
32 #include "output/pivot-table.h"
33 #include "libpspp/message.h"
35 #include "gl/minmax.h"
36 #include "gl/xalloc.h"
38 #include "data/value.h"
41 #define N_(msgid) msgid
42 #define _(msgid) gettext (msgid)
57 output_freq_table (variable_pair *vp,
58 const struct mcnemar *param,
59 const struct dictionary *dict);
63 output_statistics_table (const struct two_sample_test *t2s,
64 const struct mcnemar *param,
65 const struct dictionary *dict);
69 mcnemar_execute (const struct dataset *ds,
70 struct casereader *input,
71 enum mv_class exclude,
72 const struct npar_test *test,
79 const struct dictionary *dict = dataset_dict (ds);
81 const struct two_sample_test *t2s = UP_CAST (test, const struct two_sample_test, parent);
84 struct casereader *r = input;
86 struct mcnemar *mc = XCALLOC (t2s->n_pairs, struct mcnemar);
88 for (i = 0 ; i < t2s->n_pairs; ++i)
90 mc[i].val0.f = mc[i].val1.f = SYSMIS;
93 for (; (c = casereader_read (r)) != NULL; case_unref (c))
95 const double weight = dict_get_case_weight (dict, c, &warn);
97 for (i = 0 ; i < t2s->n_pairs; ++i)
99 variable_pair *vp = &t2s->pairs[i];
100 const union value *value0 = case_data (c, (*vp)[0]);
101 const union value *value1 = case_data (c, (*vp)[1]);
103 if (var_is_value_missing ((*vp)[0], value0) & exclude)
106 if (var_is_value_missing ((*vp)[1], value1) & exclude)
110 if (mc[i].val0.f == SYSMIS)
112 if (mc[i].val1.f != value0->f)
113 mc[i].val0.f = value0->f;
114 else if (mc[i].val1.f != value1->f)
115 mc[i].val0.f = value1->f;
118 if (mc[i].val1.f == SYSMIS)
120 if (mc[i].val0.f != value1->f)
121 mc[i].val1.f = value1->f;
122 else if (mc[i].val0.f != value0->f)
123 mc[i].val1.f = value0->f;
126 if (mc[i].val0.f == value0->f && mc[i].val0.f == value1->f)
130 else if (mc[i].val0.f == value0->f && mc[i].val1.f == value1->f)
134 else if (mc[i].val1.f == value0->f && mc[i].val0.f == value1->f)
138 else if (mc[i].val1.f == value0->f && mc[i].val1.f == value1->f)
144 msg (ME, _("The McNemar test is appropriate only for dichotomous variables"));
149 casereader_destroy (r);
151 for (i = 0 ; i < t2s->n_pairs ; ++i)
152 output_freq_table (&t2s->pairs[i], mc + i, dict);
154 output_statistics_table (t2s, mc, dict);
160 make_pair_name (const variable_pair *pair)
162 return xasprintf ("%s & %s", var_to_string ((*pair)[0]),
163 var_to_string ((*pair)[1]));
167 output_freq_table (variable_pair *vp,
168 const struct mcnemar *param,
169 const struct dictionary *dict)
171 struct pivot_table *table = pivot_table_create__ (
172 pivot_value_new_user_text_nocopy (make_pair_name (vp)), "Frequencies");
173 pivot_table_set_weight_var (table, dict_get_weight (dict));
175 struct pivot_dimension *vars[2];
176 for (int i = 0; i < 2; i++)
178 vars[i] = pivot_dimension_create__ (
179 table, i ? PIVOT_AXIS_COLUMN : PIVOT_AXIS_ROW,
180 pivot_value_new_variable ((*vp)[i]));
181 vars[i]->root->show_label = true;
183 for (int j = 0; j < 2; j++)
185 const union value *val = j ? ¶m->val1 : ¶m->val0;
186 pivot_category_create_leaf_rc (
187 vars[i]->root, pivot_value_new_var_value ((*vp)[0], val),
199 { 0, 0, param->n00 },
200 { 1, 0, param->n01 },
201 { 0, 1, param->n10 },
202 { 1, 1, param->n11 },
204 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
206 const struct entry *e = &entries[i];
207 pivot_table_put2 (table, e->idx0, e->idx1,
208 pivot_value_new_number (e->x));
211 pivot_table_submit (table);
215 output_statistics_table (const struct two_sample_test *t2s,
216 const struct mcnemar *mc,
217 const struct dictionary *dict)
219 struct pivot_table *table = pivot_table_create (N_("Test Statistics"));
220 pivot_table_set_weight_var (table, dict_get_weight (dict));
222 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
223 N_("N"), PIVOT_RC_COUNT,
224 N_("Exact Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE,
225 N_("Exact Sig. (1-tailed)"), PIVOT_RC_SIGNIFICANCE,
226 N_("Point Probability"), PIVOT_RC_OTHER);
228 struct pivot_dimension *pairs = pivot_dimension_create (
229 table, PIVOT_AXIS_ROW, N_("Pairs"));
231 for (size_t i = 0 ; i < t2s->n_pairs; ++i)
233 variable_pair *vp = &t2s->pairs[i];
234 int pair_idx = pivot_category_create_leaf (
235 pairs->root, pivot_value_new_user_text_nocopy (make_pair_name (vp)));
237 double n = mc[i].n00 + mc[i].n01 + mc[i].n10 + mc[i].n11;
238 double sig = gsl_cdf_binomial_P ((mc[i].n01 > mc[i].n10) ? mc[i].n10: mc[i].n01,
239 0.5, mc[i].n01 + mc[i].n10);
241 double point = gsl_ran_binomial_pdf (mc[i].n01, 0.5,
242 mc[i].n01 + mc[i].n10);
243 double entries[] = { n, 2.0 * sig, sig, point };
244 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
245 pivot_table_put2 (table, j, pair_idx,
246 pivot_value_new_number (entries[j]));
249 pivot_table_submit (table);