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