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