1 /* Pspp - a program for statistical analysis.
2 Copyright (C) 2008, 2009, 2010 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/>. */
23 #include <gsl/gsl_cdf.h>
28 #include <data/casereader.h>
29 #include <data/casewriter.h>
30 #include <data/dictionary.h>
31 #include <data/format.h>
32 #include <data/procedure.h>
33 #include <data/subcase.h>
34 #include <data/variable.h>
35 #include <gsl/gsl_cdf.h>
36 #include <libpspp/assertion.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <math/sort.h>
40 #include <math/wilcoxon-sig.h>
41 #include <output/tab.h>
49 append_difference (const struct ccase *c, casenumber n UNUSED, void *aux)
51 const variable_pair *vp = aux;
53 return case_data (c, (*vp)[0])->f - case_data (c, (*vp)[1])->f;
56 static void show_ranks_box (const struct wilcoxon_state *,
57 const struct two_sample_test *,
58 const struct dictionary *);
60 static void show_tests_box (const struct wilcoxon_state *,
61 const struct two_sample_test *,
62 bool exact, double timer);
67 distinct_callback (double v UNUSED, casenumber n, double w UNUSED, void *aux)
69 struct wilcoxon_state *ws = aux;
71 ws->tiebreaker += pow3 (n) - n;
77 wilcoxon_execute (const struct dataset *ds,
78 struct casereader *input,
79 enum mv_class exclude,
80 const struct npar_test *test,
86 const struct dictionary *dict = dataset_dict (ds);
87 const struct two_sample_test *t2s = (struct two_sample_test *) test;
89 struct wilcoxon_state *ws = xcalloc (sizeof (*ws), t2s->n_pairs);
90 const struct variable *weight = dict_get_weight (dict);
91 struct variable *weightx = dict_create_internal_var (WEIGHT_IDX, 0);
92 struct caseproto *proto;
95 casereader_create_filter_weight (input, dict, &warn, NULL);
97 proto = caseproto_create ();
98 proto = caseproto_add_width (proto, 0);
99 proto = caseproto_add_width (proto, 0);
101 proto = caseproto_add_width (proto, 0);
103 for (i = 0 ; i < t2s->n_pairs; ++i )
105 struct casereader *r = casereader_clone (input);
106 struct casewriter *writer;
108 struct subcase ordering;
109 variable_pair *vp = &t2s->pairs[i];
111 ws[i].sign = dict_create_internal_var (0, 0);
112 ws[i].absdiff = dict_create_internal_var (1, 0);
114 r = casereader_create_filter_missing (r, *vp, 2,
118 subcase_init_var (&ordering, ws[i].absdiff, SC_ASCEND);
119 writer = sort_create_writer (&ordering, proto);
120 subcase_destroy (&ordering);
122 for (; (c = casereader_read (r)) != NULL; case_unref (c))
124 struct ccase *output = case_create (proto);
125 double d = append_difference (c, 0, vp);
129 case_data_rw (output, ws[i].sign)->f = 1.0;
134 case_data_rw (output, ws[i].sign)->f = -1.0;
140 w = case_data (c, weight)->f;
142 /* Central point values should be dropped */
148 case_data_rw (output, ws[i].absdiff)->f = fabs (d);
151 case_data_rw (output, weightx)->f = case_data (c, weight)->f;
153 casewriter_write (writer, output);
155 casereader_destroy (r);
156 ws[i].reader = casewriter_make_reader (writer);
158 caseproto_unref (proto);
160 for (i = 0 ; i < t2s->n_pairs; ++i )
162 struct casereader *rr ;
164 enum rank_error err = 0;
166 rr = casereader_create_append_rank (ws[i].reader, ws[i].absdiff,
167 weight ? weightx : NULL, &err,
168 distinct_callback, &ws[i]
171 for (; (c = casereader_read (rr)) != NULL; case_unref (c))
173 double sign = case_data (c, ws[i].sign)->f;
174 double rank = case_data_idx (c, weight ? 3 : 2)->f;
177 w = case_data (c, weightx)->f;
181 ws[i].positives.sum += rank * w;
182 ws[i].positives.n += w;
186 ws[i].negatives.sum += rank * w;
187 ws[i].negatives.n += w;
193 casereader_destroy (rr);
196 casereader_destroy (input);
198 dict_destroy_internal_var (weightx);
200 show_ranks_box (ws, t2s, dict);
201 show_tests_box (ws, t2s, exact, timer);
203 for (i = 0 ; i < t2s->n_pairs; ++i )
205 dict_destroy_internal_var (ws[i].sign);
206 dict_destroy_internal_var (ws[i].absdiff);
216 #define _(msgid) gettext (msgid)
219 show_ranks_box (const struct wilcoxon_state *ws,
220 const struct two_sample_test *t2s,
221 const struct dictionary *dict)
225 const struct variable *wv = dict_get_weight (dict);
226 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
228 struct tab_table *table = tab_create (5, 1 + 4 * t2s->n_pairs);
230 tab_title (table, _("Ranks"));
232 tab_headers (table, 2, 0, 1, 0);
234 /* Vertical lines inside the box */
235 tab_box (table, 0, 0, -1, TAL_1,
236 1, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
238 /* Box around entire table */
239 tab_box (table, TAL_2, TAL_2, -1, -1,
240 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
243 tab_text (table, 2, 0, TAB_CENTER, _("N"));
244 tab_text (table, 3, 0, TAB_CENTER, _("Mean Rank"));
245 tab_text (table, 4, 0, TAB_CENTER, _("Sum of Ranks"));
248 for (i = 0 ; i < t2s->n_pairs; ++i)
250 variable_pair *vp = &t2s->pairs[i];
252 struct string pair_name;
253 ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
254 ds_put_cstr (&pair_name, " - ");
255 ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
257 tab_text (table, 1, 1 + i * 4, TAB_LEFT, _("Negative Ranks"));
258 tab_text (table, 1, 2 + i * 4, TAB_LEFT, _("Positive Ranks"));
259 tab_text (table, 1, 3 + i * 4, TAB_LEFT, _("Ties"));
260 tab_text (table, 1, 4 + i * 4, TAB_LEFT, _("Total"));
262 tab_hline (table, TAL_1, 0, tab_nc (table) - 1, 1 + i * 4);
265 tab_text (table, 0, 1 + i * 4, TAB_LEFT, ds_cstr (&pair_name));
266 ds_destroy (&pair_name);
270 tab_double (table, 2, 1 + i * 4, TAB_RIGHT, ws[i].negatives.n, wfmt);
271 tab_double (table, 2, 2 + i * 4, TAB_RIGHT, ws[i].positives.n, wfmt);
272 tab_double (table, 2, 3 + i * 4, TAB_RIGHT, ws[i].n_zeros, wfmt);
274 tab_double (table, 2, 4 + i * 4, TAB_RIGHT,
275 ws[i].n_zeros + ws[i].positives.n + ws[i].negatives.n, wfmt);
278 tab_double (table, 4, 1 + i * 4, TAB_RIGHT, ws[i].negatives.sum, NULL);
279 tab_double (table, 4, 2 + i * 4, TAB_RIGHT, ws[i].positives.sum, NULL);
283 tab_double (table, 3, 1 + i * 4, TAB_RIGHT,
284 ws[i].negatives.sum / (double) ws[i].negatives.n, NULL);
286 tab_double (table, 3, 2 + i * 4, TAB_RIGHT,
287 ws[i].positives.sum / (double) ws[i].positives.n, NULL);
291 tab_hline (table, TAL_2, 0, tab_nc (table) - 1, 1);
292 tab_vline (table, TAL_2, 2, 0, tab_nr (table) - 1);
300 show_tests_box (const struct wilcoxon_state *ws,
301 const struct two_sample_test *t2s,
307 struct tab_table *table = tab_create (1 + t2s->n_pairs, exact ? 5 : 3);
309 tab_title (table, _("Test Statistics"));
311 tab_headers (table, 1, 0, 1, 0);
313 /* Vertical lines inside the box */
314 tab_box (table, 0, 0, -1, TAL_1,
315 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
317 /* Box around entire table */
318 tab_box (table, TAL_2, TAL_2, -1, -1,
319 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
322 tab_text (table, 0, 1, TAB_LEFT, _("Z"));
323 tab_text (table, 0, 2, TAB_LEFT, _("Asymp. Sig. (2-tailed)"));
327 tab_text (table, 0, 3, TAB_LEFT, _("Exact Sig. (2-tailed)"));
328 tab_text (table, 0, 4, TAB_LEFT, _("Exact Sig. (1-tailed)"));
331 tab_text (table, 0, 5, TAB_LEFT, _("Point Probability"));
335 for (i = 0 ; i < t2s->n_pairs; ++i)
338 double n = ws[i].positives.n + ws[i].negatives.n;
339 variable_pair *vp = &t2s->pairs[i];
341 struct string pair_name;
342 ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
343 ds_put_cstr (&pair_name, " - ");
344 ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
347 tab_text (table, 1 + i, 0, TAB_CENTER, ds_cstr (&pair_name));
348 ds_destroy (&pair_name);
350 z = MIN (ws[i].positives.sum, ws[i].negatives.sum);
351 z -= n * (n + 1)/ 4.0;
353 z /= sqrt (n * (n + 1) * (2*n + 1)/24.0 - ws[i].tiebreaker / 48.0);
355 tab_double (table, 1 + i, 1, TAB_RIGHT, z, NULL);
357 tab_double (table, 1 + i, 2, TAB_RIGHT,
358 2.0 * gsl_cdf_ugaussian_P (z),
363 double p = LevelOfSignificanceWXMPSR (ws[i].positives.sum, n);
366 msg (MW, ("Too many pairs to calculate exact significance."));
370 tab_double (table, 1 + i, 3, TAB_RIGHT, p, NULL);
371 tab_double (table, 1 + i, 4, TAB_RIGHT, p / 2.0, NULL);
376 tab_hline (table, TAL_2, 0, tab_nc (table) - 1, 1);
377 tab_vline (table, TAL_2, 1, 0, tab_nr (table) - 1);