1 /* PSPP - computes sample statistics.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include <libpspp/compiler.h>
21 #include <output/table.h>
22 #include <libpspp/alloc.h>
24 #include <data/case.h>
25 #include <data/casefile.h>
26 #include <data/dictionary.h>
27 #include <data/procedure.h>
28 #include <data/variable.h>
29 #include <data/value.h>
30 #include <data/value-labels.h>
31 #include <data/casefilter.h>
33 #include <libpspp/message.h>
34 #include <libpspp/assertion.h>
40 #define _(msgid) gettext (msgid)
42 #include <libpspp/misc.h>
44 #include <gsl/gsl_cdf.h>
45 #include <gsl/gsl_randist.h>
46 #include <gsl-extras/gsl-extras.h>
50 #include <libpspp/hash.h>
52 static double calculate_binomial_internal (double n1, double n2,
57 swap (double *i1, double *i2)
65 calculate_binomial (double n1, double n2, double p)
67 const double n = n1 + n2;
68 const bool test_reversed = (n1 / n > p ) ;
75 return calculate_binomial_internal (n1, n2, p);
79 calculate_binomial_internal (double n1, double n2, double p)
81 /* SPSS Statistical Algorithms has completely different and WRONG
84 double sig1tailed = gslextras_cdf_binomial_P (n1, n1 + n2, p);
87 return sig1tailed > 0.5 ? 1.0 :sig1tailed * 2.0;
93 do_binomial (const struct dictionary *dict,
94 const struct casefile *cf,
95 const struct binomial_test *bst,
98 const struct casefilter *filter
103 const struct one_sample_test *ost = (const struct one_sample_test *) bst;
105 struct casereader *r = casefile_get_reader (cf, NULL);
107 while (casereader_read(r, &c))
111 dict_get_case_weight (dict, &c, &warn);
113 for (v = 0 ; v < ost->n_vars ; ++v )
115 const struct variable *var = ost->vars[v];
116 const union value *value = case_data (&c, var);
118 if ( casefilter_variable_missing (filter, &c, var))
121 if ( NULL == cat1[v].value )
123 cat1[v].value = value_dup (value, var_get_width (var));
126 else if ( 0 == compare_values (cat1[v].value, value,
127 var_get_width (var)))
129 else if ( NULL == cat2[v].value )
131 cat2[v].value = value_dup (value, var_get_width (var));
134 else if ( 0 == compare_values (cat2[v].value, value,
135 var_get_width (var)))
137 else if ( bst->category1 == SYSMIS)
138 msg (ME, _("Variable %s is not dichotomous"), var_get_name (var));
143 casereader_destroy (r);
149 binomial_execute (const struct dataset *ds,
150 const struct casefile *cf,
151 struct casefilter *filter,
152 const struct npar_test *test)
155 const struct binomial_test *bst = (const struct binomial_test *) test;
156 const struct one_sample_test *ost = (const struct one_sample_test*) test;
158 struct freq *cat1 = xzalloc (sizeof (*cat1) * ost->n_vars);
159 struct freq *cat2 = xzalloc (sizeof (*cat1) * ost->n_vars);
160 struct tab_table *table ;
162 assert ((bst->category1 == SYSMIS) == (bst->category2 == SYSMIS) );
164 if ( bst->category1 != SYSMIS )
167 v.f = bst->category1;
168 cat1->value = value_dup (&v, 0);
171 if ( bst->category2 != SYSMIS )
174 v.f = bst->category2;
175 cat2->value = value_dup (&v, 0);
178 do_binomial (dataset_dict(ds), cf, bst, cat1, cat2, filter);
180 table = tab_create (7, ost->n_vars * 3 + 1, 0);
182 tab_dim (table, tab_natural_dimensions);
184 tab_title (table, _("Binomial Test"));
186 tab_headers (table, 2, 0, 1, 0);
188 tab_box (table, TAL_1, TAL_1, -1, TAL_1,
189 0, 0, table->nc - 1, tab_nr(table) - 1 );
191 for (v = 0 ; v < ost->n_vars; ++v)
194 const struct variable *var = ost->vars[v];
195 tab_hline (table, TAL_1, 0, tab_nc (table) -1, 1 + v * 3);
198 tab_text (table, 0, 1 + v * 3, TAB_LEFT,
199 var_to_string (var));
201 tab_text (table, 1, 1 + v * 3, TAB_LEFT,
204 tab_text (table, 1, 2 + v * 3, TAB_LEFT,
207 tab_text (table, 1, 3 + v * 3, TAB_LEFT,
211 tab_float (table, 5, 1 + v * 3, TAB_NONE, bst->p, 8, 3);
213 /* Category labels */
214 tab_text (table, 2, 1 + v * 3, TAB_NONE,
215 var_get_value_name (var, cat1[v].value));
217 tab_text (table, 2, 2 + v * 3, TAB_NONE,
218 var_get_value_name (var, cat2[v].value));
221 tab_float (table, 3, 1 + v * 3, TAB_NONE,
222 cat1[v].count, 8, 0);
224 tab_float (table, 3, 2 + v * 3, TAB_NONE,
225 cat2[v].count, 8, 0);
227 n_total = cat1[v].count + cat2[v].count;
230 tab_float (table, 3, 3 + v * 3, TAB_NONE,
233 /* Observed Proportions */
235 tab_float (table, 4, 1 + v * 3, TAB_NONE,
236 cat1[v].count / n_total, 8, 3);
238 tab_float (table, 4, 2 + v * 3, TAB_NONE,
239 cat2[v].count / n_total, 8, 3);
241 tab_float (table, 4, 3 + v * 3, TAB_NONE,
242 (cat1[v].count + cat2[v].count) / n_total, 8, 2);
246 sig = calculate_binomial (cat1[v].count, cat2[v].count,
249 tab_float (table, 6, 1 + v * 3, TAB_NONE,
253 tab_text (table, 2, 0, TAB_CENTER, _("Category"));
254 tab_text (table, 3, 0, TAB_CENTER, _("N"));
255 tab_text (table, 4, 0, TAB_CENTER, _("Observed Prop."));
256 tab_text (table, 5, 0, TAB_CENTER, _("Test Prop."));
258 tab_text (table, 6, 0, TAB_CENTER | TAT_PRINTF,
259 _("Exact Sig. (%d-tailed)"),
260 bst->p == 0.5 ? 2: 1);
262 tab_vline (table, TAL_2, 2, 0, tab_nr (table) -1);