Merge commit 'origin/stable'
[pspp-builds.git] / src / language / stats / npar-summary.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2009 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 <data/format.h>
20 #include <output/table.h>
21 #include <data/casereader.h>
22 #include <libpspp/hash.h>
23 #include <data/variable.h>
24 #include "npar-summary.h"
25 #include <math/moments.h>
26 #include <data/case.h>
27 #include <data/dictionary.h>
28 #include <math.h>
29 #include <minmax.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34
35 void
36 npar_summary_calc_descriptives (struct descriptives *desc,
37                                 struct casereader *input,
38                                 const struct dictionary *dict,
39                                 const struct variable *const *vv,
40                                 int n_vars UNUSED,
41                                 enum mv_class filter)
42 {
43   int i = 0;
44   while (*vv)
45     {
46       double minimum = DBL_MAX;
47       double maximum = -DBL_MAX;
48       double var;
49       struct moments1 *moments = moments1_create (MOMENT_VARIANCE);
50       struct ccase *c;
51       const struct variable *v = *vv++;
52       struct casereader *pass;
53
54       pass = casereader_clone (input);
55       pass = casereader_create_filter_missing (pass,
56                                                &v, 1,
57                                                filter, NULL, NULL);
58       pass = casereader_create_filter_weight (pass, dict, NULL, NULL);
59       while ((c = casereader_read (pass)) != NULL)
60         {
61           double val = case_num (c, v);
62           double w = dict_get_case_weight (dict, c, NULL);
63           minimum = MIN (minimum, val);
64           maximum = MAX (maximum, val);
65           moments1_add (moments, val, w);
66           case_unref (c);
67         }
68       casereader_destroy (pass);
69
70       moments1_calculate (moments,
71                           &desc[i].n,
72                           &desc[i].mean,
73                           &var,
74                           NULL, NULL);
75
76       desc[i].std_dev = sqrt (var);
77
78       moments1_destroy (moments);
79
80       desc[i].min = minimum;
81       desc[i].max = maximum;
82
83       i++;
84     }
85   casereader_destroy (input);
86 }
87
88
89 void
90 do_summary_box (const struct descriptives *desc,
91                 const struct variable *const *vv,
92                 int n_vars);
93
94
95 void
96 do_summary_box (const struct descriptives *desc,
97                 const struct variable *const *vv,
98                 int n_vars)
99 {
100   int v;
101   bool quartiles = false;
102
103   int col;
104   int columns = 1 ;
105   struct tab_table *table ;
106
107   if ( desc ) columns += 5;
108   if ( quartiles ) columns += 3;
109
110   table = tab_create (columns, 2 + n_vars, 0);
111
112   tab_dim (table, tab_natural_dimensions);
113
114   tab_title (table, _("Descriptive Statistics"));
115
116   tab_headers (table, 1, 0, 1, 0);
117
118   tab_box (table, TAL_1, TAL_1, -1, TAL_1,
119            0, 0, table->nc - 1, tab_nr(table) - 1 );
120
121   tab_hline (table, TAL_2, 0, tab_nc (table) -1, 2);
122   tab_vline (table, TAL_2, 1, 0, tab_nr (table) - 1);
123
124   col = 1;
125   if ( desc )
126     {
127       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
128                       _("N"));
129       col++;
130       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
131                       _("Mean"));
132       col++;
133       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
134                       _("Std. Deviation"));
135       col++;
136       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
137                       _("Minimum"));
138       col++;
139       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
140                       _("Maximum"));
141       col++;
142     }
143
144   if ( quartiles )
145     {
146       tab_joint_text (table, col, 0, col + 2, 0, TAT_TITLE | TAB_CENTER,
147                       _("Percentiles"));
148       tab_hline (table, TAL_1, col, col + 2, 1);
149
150       tab_text (table, col, 1, TAT_TITLE | TAB_CENTER,
151                 _("25th"));
152       col++;
153       tab_text (table, col, 1, TAT_TITLE | TAB_CENTER,
154                 _("50th (Median)"));
155       col++;
156       tab_text (table, col, 1, TAT_TITLE | TAB_CENTER,
157                 _("75th"));
158       col++;
159     }
160
161
162   for ( v = 0 ; v < n_vars ; ++v )
163     {
164       const struct variable *var = vv[v];
165       const struct fmt_spec *fmt = var_get_print_format (var);
166
167       tab_text (table, 0, 2 + v, TAT_NONE, var_to_string (var));
168
169       tab_double (table, 1, 2 + v, TAT_NONE, desc[v].n, fmt);
170       tab_double (table, 2, 2 + v, TAT_NONE, desc[v].mean, fmt);
171       tab_double (table, 3, 2 + v, TAT_NONE, desc[v].std_dev, fmt);
172       tab_double (table, 4, 2 + v, TAT_NONE, desc[v].min, fmt);
173       tab_double (table, 5, 2 + v, TAT_NONE, desc[v].max, fmt);
174     }
175
176
177   tab_submit (table);
178 }