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