1 /* Pspp - a program for statistical analysis.
2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <data/variable.h>
22 #include <data/casereader.h>
23 #include <data/casewriter.h>
24 #include <data/subcase.h>
25 #include <math/sort.h>
26 #include <libpspp/message.h>
28 #include <output/table.h>
29 #include <data/procedure.h>
30 #include <data/dictionary.h>
31 #include <math/wilcoxon-sig.h>
32 #include <gsl/gsl_cdf.h>
35 #include <libpspp/assertion.h>
38 append_difference (const struct ccase *c, casenumber n UNUSED, void *aux)
40 const variable_pair *vp = aux;
42 return case_data (c, (*vp)[0])->f - case_data (c, (*vp)[1])->f;
45 static void show_ranks_box (const struct wilcoxon_state *,
46 const struct two_sample_test *);
48 static void show_tests_box (const struct wilcoxon_state *,
49 const struct two_sample_test *,
50 bool exact, double timer);
55 distinct_callback (double v UNUSED, casenumber n, double w UNUSED, void *aux)
57 struct wilcoxon_state *ws = aux;
59 ws->tiebreaker += pow3 (n) - n;
65 wilcoxon_execute (const struct dataset *ds,
66 struct casereader *input,
67 enum mv_class exclude,
68 const struct npar_test *test,
74 const struct dictionary *dict = dataset_dict (ds);
75 const struct two_sample_test *t2s = (struct two_sample_test *) test;
77 struct wilcoxon_state *ws = xcalloc (sizeof (*ws), t2s->n_pairs);
78 const struct variable *weight = dict_get_weight (dict);
79 struct variable *weightx = var_create_internal (WEIGHT_IDX);
82 casereader_create_filter_weight (input, dict, &warn, NULL);
84 for (i = 0 ; i < t2s->n_pairs; ++i )
86 struct casereader *r = casereader_clone (input);
87 struct casewriter *writer;
89 struct subcase ordering;
90 variable_pair *vp = &t2s->pairs[i];
92 const int reader_width = weight ? 3 : 2;
94 ws[i].sign = var_create_internal (0);
95 ws[i].absdiff = var_create_internal (1);
97 r = casereader_create_filter_missing (r, *vp, 2,
101 subcase_init_var (&ordering, ws[i].absdiff, SC_ASCEND);
102 writer = sort_create_writer (&ordering, reader_width);
103 subcase_destroy (&ordering);
105 for (; (c = casereader_read (r)) != NULL; case_unref (c))
107 struct ccase *output = case_create (reader_width);
108 double d = append_difference (c, 0, vp);
112 case_data_rw (output, ws[i].sign)->f = 1.0;
117 case_data_rw (output, ws[i].sign)->f = -1.0;
123 w = case_data (c, weight)->f;
125 /* Central point values should be dropped */
131 case_data_rw (output, ws[i].absdiff)->f = fabs (d);
134 case_data_rw (output, weightx)->f = case_data (c, weight)->f;
136 casewriter_write (writer, output);
138 casereader_destroy (r);
139 ws[i].reader = casewriter_make_reader (writer);
142 for (i = 0 ; i < t2s->n_pairs; ++i )
144 struct casereader *rr ;
146 enum rank_error err = 0;
148 rr = casereader_create_append_rank (ws[i].reader, ws[i].absdiff,
149 weight ? weightx : NULL, &err,
150 distinct_callback, &ws[i]
153 for (; (c = casereader_read (rr)) != NULL; case_unref (c))
155 double sign = case_data (c, ws[i].sign)->f;
156 double rank = case_data_idx (c, weight ? 3 : 2)->f;
159 w = case_data (c, weightx)->f;
163 ws[i].positives.sum += rank * w;
164 ws[i].positives.n += w;
168 ws[i].negatives.sum += rank * w;
169 ws[i].negatives.n += w;
175 casereader_destroy (rr);
178 casereader_destroy (input);
180 var_destroy (weightx);
182 show_ranks_box (ws, t2s);
183 show_tests_box (ws, t2s, exact, timer);
185 for (i = 0 ; i < t2s->n_pairs; ++i )
187 var_destroy (ws[i].sign);
188 var_destroy (ws[i].absdiff);
198 #define _(msgid) gettext (msgid)
201 show_ranks_box (const struct wilcoxon_state *ws, const struct two_sample_test *t2s)
204 struct tab_table *table = tab_create (5, 1 + 4 * t2s->n_pairs, 0);
206 tab_dim (table, tab_natural_dimensions);
208 tab_title (table, _("Ranks"));
210 tab_headers (table, 2, 0, 1, 0);
212 /* Vertical lines inside the box */
213 tab_box (table, 0, 0, -1, TAL_1,
214 1, 0, table->nc - 1, tab_nr (table) - 1 );
216 /* Box around entire table */
217 tab_box (table, TAL_2, TAL_2, -1, -1,
218 0, 0, table->nc - 1, tab_nr (table) - 1 );
221 tab_text (table, 2, 0, TAB_CENTER, _("N"));
222 tab_text (table, 3, 0, TAB_CENTER, _("Mean Rank"));
223 tab_text (table, 4, 0, TAB_CENTER, _("Sum of Ranks"));
226 for (i = 0 ; i < t2s->n_pairs; ++i)
228 variable_pair *vp = &t2s->pairs[i];
230 struct string pair_name;
231 ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
232 ds_put_cstr (&pair_name, " - ");
233 ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
235 tab_text (table, 1, 1 + i * 4, TAB_LEFT, _("Negative Ranks"));
236 tab_text (table, 1, 2 + i * 4, TAB_LEFT, _("Positive Ranks"));
237 tab_text (table, 1, 3 + i * 4, TAB_LEFT, _("Ties"));
238 tab_text (table, 1, 4 + i * 4, TAB_LEFT, _("Total"));
240 tab_hline (table, TAL_1, 0, table->nc - 1, 1 + i * 4);
243 tab_text (table, 0, 1 + i * 4, TAB_LEFT, ds_cstr (&pair_name));
244 ds_destroy (&pair_name);
248 tab_float (table, 2, 1 + i * 4, TAB_RIGHT, ws[i].negatives.n, 8, 0);
249 tab_float (table, 2, 2 + i * 4, TAB_RIGHT, ws[i].positives.n, 8, 0);
250 tab_float (table, 2, 3 + i * 4, TAB_RIGHT, ws[i].n_zeros, 8, 0);
252 tab_float (table, 2, 4 + i * 4, TAB_RIGHT,
253 ws[i].n_zeros + ws[i].positives.n + ws[i].negatives.n, 8, 0);
256 tab_float (table, 4, 1 + i * 4, TAB_RIGHT, ws[i].negatives.sum, 8, 2);
257 tab_float (table, 4, 2 + i * 4, TAB_RIGHT, ws[i].positives.sum, 8, 2);
261 tab_float (table, 3, 1 + i * 4, TAB_RIGHT,
262 ws[i].negatives.sum / (double) ws[i].negatives.n, 8, 2);
264 tab_float (table, 3, 2 + i * 4, TAB_RIGHT,
265 ws[i].positives.sum / (double) ws[i].positives.n, 8, 2);
269 tab_hline (table, TAL_2, 0, table->nc - 1, 1);
270 tab_vline (table, TAL_2, 2, 0, table->nr - 1);
278 show_tests_box (const struct wilcoxon_state *ws,
279 const struct two_sample_test *t2s,
285 struct tab_table *table = tab_create (1 + t2s->n_pairs, exact ? 5 : 3, 0);
287 tab_dim (table, tab_natural_dimensions);
289 tab_title (table, _("Test Statistics"));
291 tab_headers (table, 1, 0, 1, 0);
293 /* Vertical lines inside the box */
294 tab_box (table, 0, 0, -1, TAL_1,
295 0, 0, table->nc - 1, tab_nr (table) - 1 );
297 /* Box around entire table */
298 tab_box (table, TAL_2, TAL_2, -1, -1,
299 0, 0, table->nc - 1, tab_nr (table) - 1 );
302 tab_text (table, 0, 1, TAB_LEFT, _("Z"));
303 tab_text (table, 0, 2, TAB_LEFT, _("Asymp. Sig (2-tailed)"));
307 tab_text (table, 0, 3, TAB_LEFT, _("Exact Sig (2-tailed)"));
308 tab_text (table, 0, 4, TAB_LEFT, _("Exact Sig (1-tailed)"));
311 tab_text (table, 0, 5, TAB_LEFT, _("Point Probability"));
315 for (i = 0 ; i < t2s->n_pairs; ++i)
318 double n = ws[i].positives.n + ws[i].negatives.n;
319 variable_pair *vp = &t2s->pairs[i];
321 struct string pair_name;
322 ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
323 ds_put_cstr (&pair_name, " - ");
324 ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
327 tab_text (table, 1 + i, 0, TAB_CENTER, ds_cstr (&pair_name));
328 ds_destroy (&pair_name);
330 z = MIN (ws[i].positives.sum, ws[i].negatives.sum);
331 z -= n * (n + 1)/ 4.0;
333 z /= sqrt (n * (n + 1) * (2*n + 1)/24.0 - ws[i].tiebreaker / 48.0);
335 tab_float (table, 1 + i, 1, TAB_RIGHT, z, 8, 3);
337 tab_float (table, 1 + i, 2, TAB_RIGHT,
338 2.0 * gsl_cdf_ugaussian_P (z),
343 double p = LevelOfSignificanceWXMPSR (ws[i].positives.sum, n);
346 msg (MW, ("Too many pairs to calculate exact significance."));
350 tab_float (table, 1 + i, 3, TAB_RIGHT, p, 8, 3);
351 tab_float (table, 1 + i, 4, TAB_RIGHT, p / 2.0, 8, 3);
356 tab_hline (table, TAL_2, 0, table->nc - 1, 1);
357 tab_vline (table, TAL_2, 1, 0, table->nr - 1);