output: Introduce pivot tables.
[pspp] / src / language / stats / npar-summary.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "language/stats/npar-summary.h"
20
21 #include <math.h>
22
23 #include "data/case.h"
24 #include "data/casereader.h"
25 #include "data/dictionary.h"
26 #include "data/format.h"
27 #include "data/variable.h"
28 #include "math/moments.h"
29 #include "output/pivot-table.h"
30
31 #include "gl/minmax.h"
32
33 #include "gettext.h"
34 #define N_(msgid) msgid
35 #define _(msgid) gettext (msgid)
36
37
38 void
39 npar_summary_calc_descriptives (struct descriptives *desc,
40                                 struct casereader *input,
41                                 const struct dictionary *dict,
42                                 const struct variable *const *vv,
43                                 int n_vars,
44                                 enum mv_class filter)
45 {
46   int i = 0;
47   for (i = 0 ; i < n_vars; ++i)
48     {
49       double minimum = DBL_MAX;
50       double maximum = -DBL_MAX;
51       double var;
52       struct moments1 *moments = moments1_create (MOMENT_VARIANCE);
53       struct ccase *c;
54       const struct variable *v = vv[i];
55       struct casereader *pass;
56
57       pass = casereader_clone (input);
58       pass = casereader_create_filter_missing (pass,
59                                                &v, 1,
60                                                filter, NULL, NULL);
61       pass = casereader_create_filter_weight (pass, dict, NULL, NULL);
62       while ((c = casereader_read (pass)) != NULL)
63         {
64           double val = case_num (c, v);
65           double w = dict_get_case_weight (dict, c, NULL);
66           minimum = MIN (minimum, val);
67           maximum = MAX (maximum, val);
68           moments1_add (moments, val, w);
69           case_unref (c);
70         }
71       casereader_destroy (pass);
72
73       moments1_calculate (moments,
74                           &desc[i].n,
75                           &desc[i].mean,
76                           &var,
77                           NULL, NULL);
78
79       desc[i].std_dev = sqrt (var);
80
81       moments1_destroy (moments);
82
83       desc[i].min = minimum;
84       desc[i].max = maximum;
85     }
86
87   casereader_destroy (input);
88 }
89
90
91
92 void
93 do_summary_box (const struct descriptives *desc,
94                 const struct variable *const *vv,
95                 int n_vars,
96                 const struct fmt_spec *wfmt)
97 {
98   if (!desc)
99     return;
100
101   struct pivot_table *table = pivot_table_create (
102     N_("Descriptive Statistics"));
103   pivot_table_set_weight_format (table, wfmt);
104
105   pivot_dimension_create (
106     table, PIVOT_AXIS_COLUMN, N_("Statistics"),
107     N_("N"), PIVOT_RC_COUNT,
108     N_("Mean"), PIVOT_RC_OTHER,
109     N_("Std. Deviation"), PIVOT_RC_OTHER,
110     N_("Minimum"), N_("Maximum"));
111
112   struct pivot_dimension *variables = pivot_dimension_create (
113     table, PIVOT_AXIS_ROW, N_("Variable"));
114
115   for (int v = 0; v < n_vars; ++v)
116     {
117       const struct variable *var = vv[v];
118
119       int row = pivot_category_create_leaf (variables->root,
120                                             pivot_value_new_variable (var));
121
122       double entries[] = { desc[v].n, desc[v].mean, desc[v].std_dev };
123       for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
124         pivot_table_put2 (table, j, row, pivot_value_new_number (entries[j]));
125
126       union value extrema[2] = { { .f = desc[v].min }, { .f = desc[v].max } };
127       for (size_t j = 0; j < 2; j++)
128         pivot_table_put2 (table, 3 + j, row,
129                           pivot_value_new_var_value (var, &extrema[j]));
130     }
131
132   pivot_table_submit (table);
133 }