1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2009, 2010, 2011, 2014 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/>. */
19 #include "language/stats/binomial.h"
22 #include <gsl/gsl_cdf.h>
23 #include <gsl/gsl_randist.h>
25 #include "data/case.h"
26 #include "data/casereader.h"
27 #include "data/dataset.h"
28 #include "data/dictionary.h"
29 #include "data/format.h"
30 #include "data/value-labels.h"
31 #include "data/value.h"
32 #include "data/variable.h"
33 #include "language/stats/freq.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
38 #include "output/tab.h"
40 #include "gl/xalloc.h"
41 #include "gl/minmax.h"
44 #define _(msgid) gettext (msgid)
46 static double calculate_binomial_internal (double n1, double n2,
51 swap (double *i1, double *i2)
59 calculate_binomial (double n1, double n2, double p)
61 const double n = n1 + n2;
62 const bool test_reversed = (n1 / n > p ) ;
69 return calculate_binomial_internal (n1, n2, p);
73 calculate_binomial_internal (double n1, double n2, double p)
75 /* SPSS Statistical Algorithms has completely different and WRONG
78 double sig1tailed = gsl_cdf_binomial_P (n1, p, n1 + n2);
81 return sig1tailed > 0.5 ? 1.0 :sig1tailed * 2.0;
87 do_binomial (const struct dictionary *dict,
88 struct casereader *input,
89 const struct one_sample_test *ost,
95 const struct binomial_test *bst = UP_CAST (ost, const struct binomial_test, parent);
100 for (; (c = casereader_read (input)) != NULL; case_unref (c))
103 double w = dict_get_case_weight (dict, c, &warn);
105 for (v = 0 ; v < ost->n_vars ; ++v )
107 const struct variable *var = ost->vars[v];
108 double value = case_num (c, var);
110 if (var_is_num_missing (var, value, exclude))
113 if (bst->cutpoint != SYSMIS)
115 if ( cat1[v].values[0].f >= value )
122 if ( SYSMIS == cat1[v].values[0].f )
124 cat1[v].values[0].f = value;
127 else if ( cat1[v].values[0].f == value )
129 else if ( SYSMIS == cat2[v].values[0].f )
131 cat2[v].values[0].f = value;
134 else if ( cat2[v].values[0].f == value )
136 else if ( bst->category1 == SYSMIS)
137 msg (ME, _("Variable %s is not dichotomous"), var_get_name (var));
141 return casereader_destroy (input);
147 binomial_execute (const struct dataset *ds,
148 struct casereader *input,
149 enum mv_class exclude,
150 const struct npar_test *test,
155 const struct dictionary *dict = dataset_dict (ds);
156 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
157 const struct binomial_test *bst = UP_CAST (ost, const struct binomial_test, parent);
162 assert ((bst->category1 == SYSMIS) == (bst->category2 == SYSMIS) || bst->cutpoint != SYSMIS);
164 for (i = 0; i < 2; i++)
168 value = bst->cutpoint != SYSMIS ? bst->cutpoint : bst->category1;
170 value = bst->category2;
172 cat[i] = xnmalloc (ost->n_vars, sizeof *cat[i]);
173 for (v = 0; v < ost->n_vars; v++)
175 cat[i][v].values[0].f = value;
180 if (do_binomial (dataset_dict (ds), input, ost, cat[0], cat[1], exclude))
182 const struct variable *wvar = dict_get_weight (dict);
183 const struct fmt_spec *wfmt = wvar ?
184 var_get_print_format (wvar) : & F_8_0;
186 struct tab_table *table = tab_create (7, ost->n_vars * 3 + 1);
187 tab_set_format (table, RC_WEIGHT, wfmt);
189 tab_title (table, _("Binomial Test"));
191 tab_headers (table, 2, 0, 1, 0);
193 tab_box (table, TAL_1, TAL_1, -1, TAL_1,
194 0, 0, tab_nc (table) - 1, tab_nr(table) - 1 );
196 for (v = 0 ; v < ost->n_vars; ++v)
199 struct string catstr[2];
200 const struct variable *var = ost->vars[v];
202 ds_init_empty (&catstr[0]);
203 ds_init_empty (&catstr[1]);
205 if ( bst->cutpoint != SYSMIS)
207 ds_put_format (&catstr[0], "<= %.*g",
208 DBL_DIG + 1, bst->cutpoint);
212 var_append_value_name (var, cat[0][v].values, &catstr[0]);
213 var_append_value_name (var, cat[1][v].values, &catstr[1]);
216 tab_hline (table, TAL_1, 0, tab_nc (table) -1, 1 + v * 3);
219 tab_text (table, 0, 1 + v * 3, TAB_LEFT, var_to_string (var));
220 tab_text (table, 1, 1 + v * 3, TAB_LEFT, _("Group1"));
221 tab_text (table, 1, 2 + v * 3, TAB_LEFT, _("Group2"));
222 tab_text (table, 1, 3 + v * 3, TAB_LEFT, _("Total"));
225 tab_double (table, 5, 1 + v * 3, TAB_NONE, bst->p, NULL, RC_OTHER);
227 /* Category labels */
228 tab_text (table, 2, 1 + v * 3, TAB_NONE, ds_cstr (&catstr[0]));
229 tab_text (table, 2, 2 + v * 3, TAB_NONE, ds_cstr (&catstr[1]));
232 tab_double (table, 3, 1 + v * 3, TAB_NONE, cat[0][v].count, NULL, RC_WEIGHT);
233 tab_double (table, 3, 2 + v * 3, TAB_NONE, cat[1][v].count, NULL, RC_WEIGHT);
235 n_total = cat[0][v].count + cat[1][v].count;
236 tab_double (table, 3, 3 + v * 3, TAB_NONE, n_total, NULL, RC_WEIGHT);
238 /* Observed Proportions */
239 tab_double (table, 4, 1 + v * 3, TAB_NONE,
240 cat[0][v].count / n_total, NULL, RC_OTHER);
241 tab_double (table, 4, 2 + v * 3, TAB_NONE,
242 cat[1][v].count / n_total, NULL, RC_OTHER);
244 tab_double (table, 4, 3 + v * 3, TAB_NONE,
245 (cat[0][v].count + cat[1][v].count) / n_total, NULL, RC_OTHER);
248 sig = calculate_binomial (cat[0][v].count, cat[1][v].count, bst->p);
249 tab_double (table, 6, 1 + v * 3, TAB_NONE, sig, NULL, RC_PVALUE);
251 ds_destroy (&catstr[0]);
252 ds_destroy (&catstr[1]);
255 tab_text (table, 2, 0, TAB_CENTER, _("Category"));
256 tab_text (table, 3, 0, TAB_CENTER, _("N"));
257 tab_text (table, 4, 0, TAB_CENTER, _("Observed Prop."));
258 tab_text (table, 5, 0, TAB_CENTER, _("Test Prop."));
260 tab_text_format (table, 6, 0, TAB_CENTER,
261 _("Exact Sig. (%d-tailed)"),
262 bst->p == 0.5 ? 2 : 1);
264 tab_vline (table, TAL_2, 2, 0, tab_nr (table) -1);
268 for (i = 0; i < 2; i++)