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