1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011 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/sign.h"
21 #include <gsl/gsl_cdf.h>
22 #include <gsl/gsl_randist.h>
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/pivot-table.h"
34 #include "gl/minmax.h"
35 #include "gl/xalloc.h"
38 #define N_(msgid) msgid
39 #define _(msgid) gettext (msgid)
41 struct sign_test_params
47 double one_tailed_sig;
52 add_pair_leaf (struct pivot_dimension *dimension, variable_pair *pair)
54 char *label = xasprintf ("%s - %s", var_to_string ((*pair)[0]),
55 var_to_string ((*pair)[1]));
56 return pivot_category_create_leaf (
58 pivot_value_new_user_text_nocopy (label));
62 output_frequency_table (const struct two_sample_test *t2s,
63 const struct sign_test_params *param,
64 const struct dictionary *dict)
66 struct pivot_table *table = pivot_table_create (N_("Frequencies"));
67 pivot_table_set_weight_var (table, dict_get_weight (dict));
69 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("N"),
70 N_("N"), PIVOT_RC_COUNT);
72 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Differences"),
73 N_("Negative Differences"),
74 N_("Positive Differences"),
75 N_("Ties"), N_("Total"));
77 struct pivot_dimension *pairs = pivot_dimension_create (
78 table, PIVOT_AXIS_ROW, N_("Pairs"));
80 for (size_t i = 0 ; i < t2s->n_pairs; ++i)
82 variable_pair *vp = &t2s->pairs[i];
84 int pair_idx = add_pair_leaf (pairs, vp);
86 const struct sign_test_params *p = ¶m[i];
87 double values[] = { p->neg, p->pos, p->ties, p->ties + p->neg + p->pos };
88 for (size_t j = 0; j < sizeof values / sizeof *values; j++)
89 pivot_table_put3 (table, 0, j, pair_idx,
90 pivot_value_new_number (values[j]));
93 pivot_table_submit (table);
97 output_statistics_table (const struct two_sample_test *t2s,
98 const struct sign_test_params *param)
100 struct pivot_table *table = pivot_table_create (N_("Test Statistics"));
102 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Statistics"),
103 N_("Exact Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE,
104 N_("Exact Sig. (1-tailed)"), PIVOT_RC_SIGNIFICANCE,
105 N_("Point Probability"), PIVOT_RC_SIGNIFICANCE);
107 struct pivot_dimension *pairs = pivot_dimension_create (
108 table, PIVOT_AXIS_COLUMN, N_("Pairs"));
110 for (size_t i = 0 ; i < t2s->n_pairs; ++i)
112 variable_pair *vp = &t2s->pairs[i];
113 int pair_idx = add_pair_leaf (pairs, vp);
115 const struct sign_test_params *p = ¶m[i];
116 double values[] = { p->one_tailed_sig * 2,
119 for (size_t j = 0; j < sizeof values / sizeof *values; j++)
120 pivot_table_put2 (table, j, pair_idx,
121 pivot_value_new_number (values[j]));
124 pivot_table_submit (table);
128 sign_execute (const struct dataset *ds,
129 struct casereader *input,
130 enum mv_class exclude,
131 const struct npar_test *test,
137 const struct dictionary *dict = dataset_dict (ds);
138 const struct two_sample_test *t2s = UP_CAST (test, const struct two_sample_test, parent);
141 struct sign_test_params *stp = XCALLOC (t2s->n_pairs, struct sign_test_params);
143 struct casereader *r = input;
145 for (; (c = casereader_read (r)) != NULL; case_unref (c))
147 const double weight = dict_get_case_weight (dict, c, &warn);
149 for (i = 0 ; i < t2s->n_pairs; ++i)
151 variable_pair *vp = &t2s->pairs[i];
152 const union value *value0 = case_data (c, (*vp)[0]);
153 const union value *value1 = case_data (c, (*vp)[1]);
154 const double diff = value0->f - value1->f;
156 if (var_is_value_missing ((*vp)[0], value0, exclude))
159 if (var_is_value_missing ((*vp)[1], value1, exclude))
163 stp[i].pos += weight;
165 stp[i].neg += weight;
167 stp[i].ties += weight;
171 casereader_destroy (r);
173 for (i = 0 ; i < t2s->n_pairs; ++i)
175 int r = MIN (stp[i].pos, stp[i].neg);
176 stp[i].one_tailed_sig = gsl_cdf_binomial_P (r,
178 stp[i].pos + stp[i].neg);
180 stp[i].point_prob = gsl_ran_binomial_pdf (r, 0.5,
181 stp[i].pos + stp[i].neg);
184 output_frequency_table (t2s, stp, dict);
186 output_statistics_table (t2s, stp);