Rewrite PSPP output engine.
[pspp-builds.git] / src / language / stats / sign.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include "sign.h"
19
20 #include <data/variable.h>
21 #include <libpspp/str.h>
22 #include <output/tab.h>
23 #include <gsl/gsl_cdf.h>
24 #include <gsl/gsl_randist.h>
25 #include "npar.h"
26 #include <data/procedure.h>
27 #include <data/missing-values.h>
28 #include <data/dictionary.h>
29 #include <data/casereader.h>
30 #include <data/format.h>
31
32 #include "minmax.h"
33 #include "xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 struct sign_test_params
39 {
40   double pos;
41   double ties;
42   double neg;
43
44   double one_tailed_sig;
45   double point_prob;
46 };
47
48
49 static void
50 output_frequency_table (const struct two_sample_test *t2s,
51                         const struct sign_test_params *param,
52                         const struct dictionary *dict)
53 {
54   int i;
55   struct tab_table *table = tab_create (3, 1 + 4 * t2s->n_pairs);
56
57   const struct variable *wv = dict_get_weight (dict);
58   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
59
60   tab_title (table, _("Frequencies"));
61
62   tab_headers (table, 2, 0, 1, 0);
63
64   /* Vertical lines inside the box */
65   tab_box (table, 0, 0, -1, TAL_1,
66            1, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
67
68   /* Box around entire table */
69   tab_box (table, TAL_2, TAL_2, -1, -1,
70            0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
71
72   tab_text (table,  2, 0,  TAB_CENTER, _("N"));
73
74   for (i = 0 ; i < t2s->n_pairs; ++i)
75     {
76       variable_pair *vp = &t2s->pairs[i];
77
78       struct string pair_name;
79       ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
80       ds_put_cstr (&pair_name, " - ");
81       ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
82
83       tab_text (table, 0, 1 + i * 4, TAB_LEFT, ds_cstr (&pair_name));
84
85       ds_destroy (&pair_name);
86
87       tab_hline (table, TAL_1, 0, tab_nc (table) - 1, 1 + i * 4);
88
89       tab_text (table,  1, 1 + i * 4,  TAB_LEFT, _("Negative Differences"));
90       tab_text (table,  1, 2 + i * 4,  TAB_LEFT, _("Positive Differences"));
91       tab_text (table,  1, 3 + i * 4,  TAB_LEFT, _("Ties"));
92       tab_text (table,  1, 4 + i * 4,  TAB_LEFT, _("Total"));
93
94       tab_double (table, 2, 1 + i * 4, TAB_RIGHT, param[i].neg, wfmt);
95       tab_double (table, 2, 2 + i * 4, TAB_RIGHT, param[i].pos, wfmt);
96       tab_double (table, 2, 3 + i * 4, TAB_RIGHT, param[i].ties, wfmt);
97       tab_double (table, 2, 4 + i * 4, TAB_RIGHT,
98                  param[i].ties + param[i].neg + param[i].pos, wfmt);
99     }
100
101   tab_submit (table);
102 }
103
104 static void
105 output_statistics_table (const struct two_sample_test *t2s,
106                          const struct sign_test_params *param)
107 {
108   int i;
109   struct tab_table *table = tab_create (1 + t2s->n_pairs, 4);
110
111   tab_title (table, _("Test Statistics"));
112
113   tab_headers (table, 0, 1,  0, 1);
114
115   tab_hline (table, TAL_2, 0, tab_nc (table) - 1, 1);
116   tab_vline (table, TAL_2, 1, 0, tab_nr (table) - 1);
117
118
119   /* Vertical lines inside the box */
120   tab_box (table, -1, -1, -1, TAL_1,
121            0, 0,
122            tab_nc (table) - 1, tab_nr (table) - 1);
123
124   /* Box around entire table */
125   tab_box (table, TAL_2, TAL_2, -1, -1,
126            0, 0, tab_nc (table) - 1,
127            tab_nr (table) - 1);
128
129   tab_text (table,  0, 1, TAT_TITLE | TAB_LEFT,
130             _("Exact Sig. (2-tailed)"));
131
132   tab_text (table,  0, 2, TAT_TITLE | TAB_LEFT,
133             _("Exact Sig. (1-tailed)"));
134
135   tab_text (table,  0, 3, TAT_TITLE | TAB_LEFT,
136             _("Point Probability"));
137
138   for (i = 0 ; i < t2s->n_pairs; ++i)
139     {
140       variable_pair *vp = &t2s->pairs[i];
141
142       struct string pair_name;
143       ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
144       ds_put_cstr (&pair_name, " - ");
145       ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
146
147       tab_text (table,  1 + i, 0, TAB_LEFT, ds_cstr (&pair_name));
148       ds_destroy (&pair_name);
149
150       tab_double (table, 1 + i, 1, TAB_RIGHT,
151                   param[i].one_tailed_sig * 2, NULL);
152
153       tab_double (table, 1 + i, 2, TAB_RIGHT, param[i].one_tailed_sig, NULL);
154       tab_double (table, 1 + i, 3, TAB_RIGHT, param[i].point_prob, NULL);
155     }
156
157   tab_submit (table);
158 }
159
160 void
161 sign_execute (const struct dataset *ds,
162                   struct casereader *input,
163                   enum mv_class exclude,
164                   const struct npar_test *test,
165                   bool exact UNUSED,
166                   double timer UNUSED)
167 {
168   int i;
169   bool warn = true;
170   const struct dictionary *dict = dataset_dict (ds);
171   const struct two_sample_test *t2s = (const struct two_sample_test *) test;
172   struct ccase *c;
173
174   struct sign_test_params *stp = xcalloc (sizeof *stp, t2s->n_pairs);
175
176   struct casereader *r = input;
177
178   for (; (c = casereader_read (r)) != NULL; case_unref (c))
179     {
180       const double weight = dict_get_case_weight (dict, c, &warn);
181
182       for (i = 0 ; i < t2s->n_pairs; ++i )
183         {
184           variable_pair *vp = &t2s->pairs[i];
185           const union value *value0 = case_data (c, (*vp)[0]);
186           const union value *value1 = case_data (c, (*vp)[1]);
187           const double diff = value0->f - value1->f;
188
189           if (var_is_value_missing ((*vp)[0], value0, exclude))
190             continue;
191
192           if (var_is_value_missing ((*vp)[1], value1, exclude))
193             continue;
194
195           if ( diff > 0)
196             stp[i].pos += weight;
197           else if (diff < 0)
198             stp[i].neg += weight;
199           else
200             stp[i].ties += weight;
201         }
202     }
203
204   casereader_destroy (r);
205
206   for (i = 0 ; i < t2s->n_pairs; ++i )
207     {
208       int r = MIN (stp[i].pos, stp[i].neg);
209       stp[i].one_tailed_sig = gsl_cdf_binomial_P (r,
210                                                   0.5,
211                                                   stp[i].pos + stp[i].neg);
212
213       stp[i].point_prob = gsl_ran_binomial_pdf (r, 0.5,
214                                                 stp[i].pos + stp[i].neg);
215     }
216
217   output_frequency_table (t2s, stp, dict);
218
219   output_statistics_table (t2s, stp);
220
221   free (stp);
222 }