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 */
130 case_data_rw (output, ws[i].absdiff)->f = fabs (d);
133 case_data_rw (output, weightx)->f = case_data (c, weight)->f;
135 casewriter_write (writer, output);
137 casereader_destroy (r);
138 ws[i].reader = casewriter_make_reader (writer);
141 for (i = 0 ; i < t2s->n_pairs; ++i )
143 struct casereader *rr ;
145 enum rank_error err = 0;
147 rr = casereader_create_append_rank (ws[i].reader, ws[i].absdiff,
148 weight ? weightx : NULL, &err,
149 distinct_callback, &ws[i]
152 for (; (c = casereader_read (rr)) != NULL; case_unref (c))
154 double sign = case_data (c, ws[i].sign)->f;
155 double rank = case_data_idx (c, weight ? 3 : 2)->f;
158 w = case_data (c, weightx)->f;
162 ws[i].positives.sum += rank * w;
163 ws[i].positives.n += w;
167 ws[i].negatives.sum += rank * w;
168 ws[i].negatives.n += w;
174 casereader_destroy (rr);
177 casereader_destroy (input);
179 var_destroy (weightx);
181 show_ranks_box (ws, t2s);
182 show_tests_box (ws, t2s, exact, timer);
184 for (i = 0 ; i < t2s->n_pairs; ++i )
186 var_destroy (ws[i].sign);
187 var_destroy (ws[i].absdiff);
197 #define _(msgid) gettext (msgid)
200 show_ranks_box (const struct wilcoxon_state *ws, const struct two_sample_test *t2s)
203 struct tab_table *table = tab_create (5, 1 + 4 * t2s->n_pairs, 0);
205 tab_dim (table, tab_natural_dimensions);
207 tab_title (table, _("Ranks"));
209 tab_headers (table, 2, 0, 1, 0);
211 /* Vertical lines inside the box */
212 tab_box (table, 0, 0, -1, TAL_1,
213 1, 0, table->nc - 1, tab_nr (table) - 1 );
215 /* Box around entire table */
216 tab_box (table, TAL_2, TAL_2, -1, -1,
217 0, 0, table->nc - 1, tab_nr (table) - 1 );
220 tab_text (table, 2, 0, TAB_CENTER, _("N"));
221 tab_text (table, 3, 0, TAB_CENTER, _("Mean Rank"));
222 tab_text (table, 4, 0, TAB_CENTER, _("Sum of Ranks"));
225 for (i = 0 ; i < t2s->n_pairs; ++i)
227 variable_pair *vp = &t2s->pairs[i];
229 struct string pair_name;
230 ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
231 ds_put_cstr (&pair_name, " - ");
232 ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
234 tab_text (table, 1, 1 + i * 4, TAB_LEFT, _("Negative Ranks"));
235 tab_text (table, 1, 2 + i * 4, TAB_LEFT, _("Positive Ranks"));
236 tab_text (table, 1, 3 + i * 4, TAB_LEFT, _("Ties"));
237 tab_text (table, 1, 4 + i * 4, TAB_LEFT, _("Total"));
239 tab_hline (table, TAL_1, 0, table->nc - 1, 1 + i * 4);
242 tab_text (table, 0, 1 + i * 4, TAB_LEFT, ds_cstr (&pair_name));
243 ds_destroy (&pair_name);
247 tab_float (table, 2, 1 + i * 4, TAB_RIGHT, ws[i].negatives.n, 8, 0);
248 tab_float (table, 2, 2 + i * 4, TAB_RIGHT, ws[i].positives.n, 8, 0);
249 tab_float (table, 2, 3 + i * 4, TAB_RIGHT, ws[i].n_zeros, 8, 0);
251 tab_float (table, 2, 4 + i * 4, TAB_RIGHT,
252 ws[i].n_zeros + ws[i].positives.n + ws[i].negatives.n, 8, 0);
255 tab_float (table, 4, 1 + i * 4, TAB_RIGHT, ws[i].negatives.sum, 8, 2);
256 tab_float (table, 4, 2 + i * 4, TAB_RIGHT, ws[i].positives.sum, 8, 2);
260 tab_float (table, 3, 1 + i * 4, TAB_RIGHT,
261 ws[i].negatives.sum / (double) ws[i].negatives.n, 8, 2);
263 tab_float (table, 3, 2 + i * 4, TAB_RIGHT,
264 ws[i].positives.sum / (double) ws[i].positives.n, 8, 2);
268 tab_hline (table, TAL_2, 0, table->nc - 1, 1);
269 tab_vline (table, TAL_2, 2, 0, table->nr - 1);
277 show_tests_box (const struct wilcoxon_state *ws,
278 const struct two_sample_test *t2s,
284 struct tab_table *table = tab_create (1 + t2s->n_pairs, exact ? 5 : 3, 0);
286 tab_dim (table, tab_natural_dimensions);
288 tab_title (table, _("Test Statistics"));
290 tab_headers (table, 1, 0, 1, 0);
292 /* Vertical lines inside the box */
293 tab_box (table, 0, 0, -1, TAL_1,
294 0, 0, table->nc - 1, tab_nr (table) - 1 );
296 /* Box around entire table */
297 tab_box (table, TAL_2, TAL_2, -1, -1,
298 0, 0, table->nc - 1, tab_nr (table) - 1 );
301 tab_text (table, 0, 1, TAB_LEFT, _("Z"));
302 tab_text (table, 0, 2, TAB_LEFT, _("Asymp. Sig (2-tailed)"));
306 tab_text (table, 0, 3, TAB_LEFT, _("Exact Sig (2-tailed)"));
307 tab_text (table, 0, 4, TAB_LEFT, _("Exact Sig (1-tailed)"));
310 tab_text (table, 0, 5, TAB_LEFT, _("Point Probability"));
314 for (i = 0 ; i < t2s->n_pairs; ++i)
317 double n = ws[i].positives.n + ws[i].negatives.n;
318 variable_pair *vp = &t2s->pairs[i];
320 struct string pair_name;
321 ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
322 ds_put_cstr (&pair_name, " - ");
323 ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
326 tab_text (table, 1 + i, 0, TAB_CENTER, ds_cstr (&pair_name));
327 ds_destroy (&pair_name);
329 z = MIN (ws[i].positives.sum, ws[i].negatives.sum);
330 z -= n * (n + 1)/ 4.0;
332 z /= sqrt (n * (n + 1) * (2*n + 1)/24.0 - ws[i].tiebreaker / 48.0);
334 tab_float (table, 1 + i, 1, TAB_RIGHT, z, 8, 3);
336 tab_float (table, 1 + i, 2, TAB_RIGHT,
337 2.0 * gsl_cdf_ugaussian_P (z),
342 double p = LevelOfSignificanceWXMPSR (ws[i].positives.sum, n);
345 msg (MW, ("Too many pairs to calculate exact significance."));
349 tab_float (table, 1 + i, 3, TAB_RIGHT, p, 8, 3);
350 tab_float (table, 1 + i, 4, TAB_RIGHT, p / 2.0, 8, 3);
355 tab_hline (table, TAL_2, 0, table->nc - 1, 1);
356 tab_vline (table, TAL_2, 1, 0, table->nr - 1);