1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2007 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/chisquare.h>
24 #include <data/format.h>
25 #include <data/case.h>
26 #include <data/casereader.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/stats/freq.h>
32 #include <language/stats/npar.h>
33 #include <libpspp/assertion.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/taint.h>
38 #include <output/table.h>
40 #include <gsl/gsl_cdf.h>
45 #define _(msgid) gettext (msgid)
47 /* Return a hash table containing the frequency counts of each
49 It is the caller's responsibility to free the hash table when
52 static struct hsh_table *
53 create_freq_hash_with_range (const struct dictionary *dict,
54 struct casereader *input,
55 const struct variable *var,
63 struct hsh_table *freq_hash =
64 hsh_create (4, compare_freq, hash_freq,
65 free_freq_mutable_hash,
68 /* Populate the hash with zero entries */
69 for (i_d = trunc (lo); i_d <= trunc (hi); i_d += 1.0 )
71 union value the_value;
72 struct freq_mutable *fr = xmalloc (sizeof (*fr));
76 fr->value = value_dup (&the_value, 0);
79 hsh_insert (freq_hash, fr);
82 while (casereader_read (input, &c))
84 union value obs_value;
85 struct freq **existing_fr;
86 struct freq *fr = xmalloc(sizeof (*fr));
87 fr->value = case_data (&c, var);
89 fr->count = dict_get_case_weight (dict, &c, &warn);
91 obs_value.f = trunc (fr->value->f);
93 if ( obs_value.f < lo || obs_value.f > hi)
100 fr->value = &obs_value;
102 existing_fr = (struct freq **) hsh_probe (freq_hash, fr);
104 /* This must exist in the hash, because we previously populated it
106 assert (*existing_fr);
108 (*existing_fr)->count += fr->count;
113 if (casereader_destroy (input))
117 hsh_destroy (freq_hash);
123 /* Return a hash table containing the frequency counts of each
124 value of VAR in INPUT .
125 It is the caller's responsibility to free the hash table when
128 static struct hsh_table *
129 create_freq_hash (const struct dictionary *dict,
130 struct casereader *input,
131 const struct variable *var)
136 struct hsh_table *freq_hash =
137 hsh_create (4, compare_freq, hash_freq,
138 free_freq_mutable_hash,
141 for (; casereader_read (input, &c); case_destroy (&c))
143 struct freq **existing_fr;
144 struct freq *fr = xmalloc(sizeof (*fr));
145 fr->value = case_data (&c, var);
147 fr->count = dict_get_case_weight (dict, &c, &warn);
149 existing_fr = (struct freq **) hsh_probe (freq_hash, fr);
152 (*existing_fr)->count += fr->count;
158 fr->value = value_dup (fr->value, var_get_width (var));
161 if (casereader_destroy (input))
165 hsh_destroy (freq_hash);
172 static struct tab_table *
173 create_variable_frequency_table (const struct dictionary *dict,
174 struct casereader *input,
175 const struct chisquare_test *test,
177 struct hsh_table **freq_hash)
181 const struct one_sample_test *ost = (const struct one_sample_test*)test;
183 struct tab_table *table ;
184 const struct variable *var = ost->vars[v];
186 *freq_hash = create_freq_hash (dict, input, var);
187 if (*freq_hash == NULL)
190 n_cells = hsh_count (*freq_hash);
192 if ( test->n_expected > 0 && n_cells != test->n_expected )
194 msg(ME, _("CHISQUARE test specified %d expected values, but"
195 " %d distinct values were encountered in variable %s."),
196 test->n_expected, n_cells,
199 hsh_destroy (*freq_hash);
204 table = tab_create(4, n_cells + 2, 0);
205 tab_dim (table, tab_natural_dimensions);
207 tab_title (table, var_to_string(var));
208 tab_text (table, 1, 0, TAB_LEFT, _("Observed N"));
209 tab_text (table, 2, 0, TAB_LEFT, _("Expected N"));
210 tab_text (table, 3, 0, TAB_LEFT, _("Residual"));
212 tab_headers (table, 1, 0, 1, 0);
214 tab_box (table, TAL_1, TAL_1, -1, -1,
215 0, 0, table->nc - 1, tab_nr(table) - 1 );
217 tab_hline (table, TAL_1, 0, tab_nc(table) - 1, 1);
219 tab_vline (table, TAL_2, 1, 0, tab_nr(table) - 1);
220 for ( i = 2 ; i < 4 ; ++i )
221 tab_vline (table, TAL_1, i, 0, tab_nr(table) - 1);
224 tab_text (table, 0, table->nr - 1, TAB_LEFT, _("Total"));
230 static struct tab_table *
231 create_combo_frequency_table (const struct chisquare_test *test)
234 const struct one_sample_test *ost = (const struct one_sample_test*)test;
236 struct tab_table *table ;
238 int n_cells = test->hi - test->lo + 1;
240 table = tab_create(1 + ost->n_vars * 4, n_cells + 3, 0);
241 tab_dim (table, tab_natural_dimensions);
243 tab_title (table, _("Frequencies"));
244 for ( i = 0 ; i < ost->n_vars ; ++i )
246 const struct variable *var = ost->vars[i];
247 tab_text (table, i * 4 + 1, 1, TAB_LEFT, _("Category"));
248 tab_text (table, i * 4 + 2, 1, TAB_LEFT, _("Observed N"));
249 tab_text (table, i * 4 + 3, 1, TAB_LEFT, _("Expected N"));
250 tab_text (table, i * 4 + 4, 1, TAB_LEFT, _("Residual"));
252 tab_vline (table, TAL_2, i * 4 + 1,
253 0, tab_nr (table) - 1);
255 tab_vline (table, TAL_1, i * 4 + 2,
256 0, tab_nr (table) - 1);
258 tab_vline (table, TAL_1, i * 4 + 3,
259 1, tab_nr (table) - 1);
261 tab_vline (table, TAL_1, i * 4 + 4,
262 1, tab_nr (table) - 1);
265 tab_joint_text (table,
269 var_to_string (var));
272 for ( i = test->lo ; i <= test->hi ; ++i )
273 tab_fixed (table, 0, 2 + i - test->lo,
274 TAB_LEFT, 1 + i - test->lo, 8, 0);
276 tab_headers (table, 1, 0, 2, 0);
278 tab_box (table, TAL_1, TAL_1, -1, -1,
279 0, 0, table->nc - 1, tab_nr(table) - 1 );
281 tab_hline (table, TAL_1, 1, tab_nc(table) - 1, 1);
282 tab_hline (table, TAL_1, 0, tab_nc(table) - 1, 2);
284 tab_text (table, 0, table->nr - 1, TAB_LEFT, _("Total"));
290 static struct tab_table *
291 create_stats_table (const struct chisquare_test *test)
293 const struct one_sample_test *ost = (const struct one_sample_test*) test;
295 struct tab_table *table;
296 table = tab_create (1 + ost->n_vars, 4, 0);
297 tab_dim (table, tab_natural_dimensions);
298 tab_title (table, _("Test Statistics"));
299 tab_headers (table, 1, 0, 1, 0);
301 tab_box (table, TAL_1, TAL_1, -1, -1,
302 0, 0, tab_nc(table) - 1, tab_nr(table) - 1 );
304 tab_box (table, -1, -1, -1, TAL_1,
305 1, 0, tab_nc(table) - 1, tab_nr(table) - 1 );
308 tab_vline (table, TAL_2, 1, 0, tab_nr (table) - 1);
309 tab_hline (table, TAL_1, 0, tab_nc (table) - 1, 1);
312 tab_text (table, 0, 1, TAB_LEFT, _("Chi-Square"));
313 tab_text (table, 0, 2, TAB_LEFT, _("df"));
314 tab_text (table, 0, 3, TAB_LEFT, _("Asymp. Sig."));
321 chisquare_execute (const struct dataset *ds,
322 struct casereader *input,
323 enum mv_class exclude,
324 const struct npar_test *test)
326 const struct dictionary *dict = dataset_dict (ds);
328 struct one_sample_test *ost = (struct one_sample_test *) test;
329 struct chisquare_test *cst = (struct chisquare_test *) test;
331 double total_expected = 0.0;
332 const struct variable *wvar = dict_get_weight (dict);
333 const struct fmt_spec *wfmt = wvar ?
334 var_get_print_format (wvar) : & F_8_0;
336 double *df = xzalloc (sizeof (*df) * ost->n_vars);
337 double *xsq = xzalloc (sizeof (*df) * ost->n_vars);
340 for ( i = 0 ; i < cst->n_expected ; ++i )
341 total_expected += cst->expected[i];
343 if ( cst->ranged == false )
345 for ( v = 0 ; v < ost->n_vars ; ++v )
347 double total_obs = 0.0;
348 struct hsh_table *freq_hash = NULL;
349 struct casereader *reader =
350 casereader_create_filter_missing (casereader_clone (input),
351 &ost->vars[v], 1, exclude, NULL);
352 struct tab_table *freq_table =
353 create_variable_frequency_table(dict, reader, cst, v, &freq_hash);
357 if ( NULL == freq_table )
359 ff = (struct freq **) hsh_sort (freq_hash);
361 n_cells = hsh_count (freq_hash);
363 for ( i = 0 ; i < n_cells ; ++i )
364 total_obs += ff[i]->count;
367 for ( i = 0 ; i < n_cells ; ++i )
371 const union value *observed_value = ff[i]->value;
373 ds_init_empty (&str);
374 var_append_value_name (ost->vars[v], observed_value, &str);
377 tab_text (freq_table, 0, i + 1, TAB_LEFT, ds_cstr (&str));
382 tab_double (freq_table, 1, i + 1, TAB_NONE,
385 if ( cst->n_expected > 0 )
386 exp = cst->expected[i] * total_obs / total_expected ;
388 exp = total_obs / (double) n_cells;
390 tab_double (freq_table, 2, i + 1, TAB_NONE,
394 tab_double (freq_table, 3, i + 1, TAB_NONE,
395 ff[i]->count - exp, NULL);
397 xsq[v] += (ff[i]->count - exp) * (ff[i]->count - exp) / exp;
400 df[v] = n_cells - 1.0;
402 tab_double (freq_table, 1, i + 1, TAB_NONE,
405 tab_submit (freq_table);
407 hsh_destroy (freq_hash);
410 else /* ranged == true */
412 struct tab_table *freq_table = create_combo_frequency_table (cst);
414 n_cells = cst->hi - cst->lo + 1;
416 for ( v = 0 ; v < ost->n_vars ; ++v )
418 double total_obs = 0.0;
419 struct casereader *reader =
420 casereader_create_filter_missing (casereader_clone (input),
421 &ost->vars[v], 1, exclude, NULL);
422 struct hsh_table *freq_hash =
423 create_freq_hash_with_range (dict, reader,
424 ost->vars[v], cst->lo, cst->hi);
428 if (freq_hash == NULL)
431 ff = (struct freq **) hsh_sort (freq_hash);
432 assert ( n_cells == hsh_count (freq_hash));
434 for ( i = 0 ; i < hsh_count (freq_hash) ; ++i )
435 total_obs += ff[i]->count;
438 for ( i = 0 ; i < hsh_count (freq_hash) ; ++i )
443 const union value *observed_value = ff[i]->value;
445 ds_init_empty (&str);
446 var_append_value_name (ost->vars[v], observed_value, &str);
448 tab_text (freq_table, v * 4 + 1, i + 2 , TAB_LEFT,
453 tab_double (freq_table, v * 4 + 2, i + 2 , TAB_NONE,
456 if ( cst->n_expected > 0 )
457 exp = cst->expected[i] * total_obs / total_expected ;
459 exp = total_obs / (double) hsh_count (freq_hash);
462 tab_double (freq_table, v * 4 + 3, i + 2 , TAB_NONE,
466 tab_double (freq_table, v * 4 + 4, i + 2 , TAB_NONE,
467 ff[i]->count - exp, NULL);
469 xsq[v] += (ff[i]->count - exp) * (ff[i]->count - exp) / exp;
473 tab_double (freq_table, v * 4 + 2, tab_nr (freq_table) - 1, TAB_NONE,
476 df[v] = n_cells - 1.0;
478 hsh_destroy (freq_hash);
481 tab_submit (freq_table);
483 ok = !taint_has_tainted_successor (casereader_get_taint (input));
484 casereader_destroy (input);
488 struct tab_table *stats_table = create_stats_table (cst);
490 /* Populate the summary statistics table */
491 for ( v = 0 ; v < ost->n_vars ; ++v )
493 const struct variable *var = ost->vars[v];
495 tab_text (stats_table, 1 + v, 0, TAB_CENTER, var_get_name (var));
497 tab_double (stats_table, 1 + v, 1, TAB_NONE, xsq[v], NULL);
498 tab_fixed (stats_table, 1 + v, 2, TAB_NONE, df[v], 8, 0);
500 tab_double (stats_table, 1 + v, 3, TAB_NONE,
501 gsl_cdf_chisq_Q (xsq[v], df[v]), NULL);
503 tab_submit (stats_table);