1 /* PSPP - a program for statistical analysis. -*-c-*-
2 Copyright (C) 2010 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/>.
22 #include <gsl/gsl_cdf.h>
25 #include <data/format.h>
27 #include <libpspp/misc.h>
28 #include <libpspp/message.h>
29 #include <data/procedure.h>
30 #include <data/casereader.h>
31 #include <data/dictionary.h>
32 #include <data/variable.h>
36 #define _(msgid) gettext (msgid)
44 const struct dictionary *dict;
47 static void show_ranks_box (const struct one_sample_test *ost,
48 const struct friedman *fr);
50 static void show_sig_box (const struct one_sample_test *ost,
51 const struct friedman *fr);
61 cmp_x (const void *a_, const void *b_)
63 const struct datum *a = a_;
64 const struct datum *b = b_;
73 cmp_posn (const void *a_, const void *b_)
75 const struct datum *a = a_;
76 const struct datum *b = b_;
78 if (a->posn < b->posn)
81 return (a->posn > b->posn);
85 friedman_execute (const struct dataset *ds,
86 struct casereader *input,
87 enum mv_class exclude,
88 const struct npar_test *test,
92 double numerator = 0.0;
93 double denominator = 0.0;
96 const struct dictionary *dict = dataset_dict (ds);
97 const struct variable *weight = dict_get_weight (dict);
99 struct one_sample_test *ft = UP_CAST (test, struct one_sample_test, parent);
102 double sigma_t = 0.0;
103 struct datum *row = xcalloc (ft->n_vars, sizeof *row);
106 fr.rank_sum = xcalloc (ft->n_vars, sizeof *fr.rank_sum);
109 for (v = 0; v < ft->n_vars; ++v)
112 fr.rank_sum[v] = 0.0;
115 input = casereader_create_filter_weight (input, dict, &warn, NULL);
116 input = casereader_create_filter_missing (input,
117 ft->vars, ft->n_vars,
120 for (; (c = casereader_read (input)); case_unref (c))
122 double prev_x = SYSMIS;
125 const double w = weight ? case_data (c, weight)->f: 1.0;
129 for (v = 0; v < ft->n_vars; ++v)
131 const struct variable *var = ft->vars[v];
132 const union value *val = case_data (c, var);
136 qsort (row, ft->n_vars, sizeof *row, cmp_x);
137 for (v = 0; v < ft->n_vars; ++v)
140 /* Replace value by the Rank */
146 for (i = v - run_length; i < v; ++i)
148 row[i].x *= run_length ;
150 row[i].x /= run_length + 1;
152 row[v].x = row[v-1].x;
159 double t = run_length + 1;
160 sigma_t += pow3 (t) - t;
168 double t = run_length + 1;
169 sigma_t += pow3 (t) - t;
172 qsort (row, ft->n_vars, sizeof *row, cmp_posn);
174 for (v = 0; v < ft->n_vars; ++v)
175 fr.rank_sum[v] += row[v].x;
178 casereader_destroy (input);
182 for (v = 0; v < ft->n_vars; ++v)
184 numerator += pow2 (fr.rank_sum[v]);
187 numerator *= 12.0 / (fr.cc * ft->n_vars * ( ft->n_vars + 1));
188 numerator -= 3 * fr.cc * ( ft->n_vars + 1);
190 denominator = 1 - sigma_t / ( fr.cc * ft->n_vars * ( pow2 (ft->n_vars) - 1));
192 fr.chi_sq = numerator / denominator;
194 show_ranks_box (ft, &fr);
196 show_sig_box (ft, &fr);
203 #include <output/tab.h>
206 show_ranks_box (const struct one_sample_test *ost, const struct friedman *fr)
209 const int row_headers = 1;
210 const int column_headers = 1;
211 struct tab_table *table =
212 tab_create (row_headers + 1, column_headers + ost->n_vars);
214 tab_headers (table, row_headers, 0, column_headers, 0);
216 tab_title (table, _("Ranks"));
218 /* Vertical lines inside the box */
219 tab_box (table, 1, 0, -1, TAL_1,
220 row_headers, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
222 /* Box around the table */
223 tab_box (table, TAL_2, TAL_2, -1, -1,
224 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
227 tab_text (table, 1, 0, 0, _("Mean Rank"));
229 tab_hline (table, TAL_2, 0, tab_nc (table) - 1, column_headers);
230 tab_vline (table, TAL_2, row_headers, 0, tab_nr (table) - 1);
232 for (i = 0 ; i < ost->n_vars ; ++i)
234 tab_text (table, 0, row_headers + i,
235 TAB_LEFT, var_to_string (ost->vars[i]));
237 tab_double (table, 1, row_headers + i,
238 0, fr->rank_sum[i] / fr->cc, 0);
246 show_sig_box (const struct one_sample_test *ost, const struct friedman *fr)
248 const struct variable *weight = dict_get_weight (fr->dict);
249 const struct fmt_spec *wfmt = weight ? var_get_print_format (weight) : &F_8_0;
251 const int row_headers = 1;
252 const int column_headers = 0;
253 struct tab_table *table =
254 tab_create (row_headers + 1, column_headers + 4);
256 tab_headers (table, row_headers, 0, column_headers, 0);
258 tab_title (table, _("Test Statistics"));
260 tab_text (table, 0, column_headers,
261 TAT_TITLE | TAB_LEFT , _("N"));
263 tab_text (table, 0, 1 + column_headers,
264 TAT_TITLE | TAB_LEFT , _("Chi-Square"));
266 tab_text (table, 0, 2 + column_headers,
267 TAT_TITLE | TAB_LEFT, _("df"));
269 tab_text (table, 0, 3 + column_headers,
270 TAT_TITLE | TAB_LEFT, _("Asymp. Sig."));
272 /* Box around the table */
273 tab_box (table, TAL_2, TAL_2, -1, -1,
274 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
277 tab_hline (table, TAL_2, 0, tab_nc (table) -1, column_headers);
278 tab_vline (table, TAL_2, row_headers, 0, tab_nr (table) - 1);
280 tab_double (table, 1, column_headers,
283 tab_double (table, 1, column_headers + 1,
286 tab_double (table, 1, column_headers + 2,
287 0, ost->n_vars - 1, &F_8_0);
289 tab_double (table, 1, column_headers + 3,
290 0, gsl_cdf_chisq_Q (fr->chi_sq, ost->n_vars - 1),