349fcc1e3c7c995d71934cbf2ffd02c0ac0b9e0c
[pspp-builds.git] / src / language / stats / npar-summary.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include <output/table.h>
21 #include <libpspp/hash.h>
22 #include <data/variable.h>
23 #include "npar-summary.h"
24 #include <math/moments.h>
25 #include <data/casefile.h>
26 #include <data/casefilter.h>
27 #include <data/case.h>
28 #include <data/dictionary.h>
29 #include <math.h>
30 #include <minmax.h>
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35
36 void
37 npar_summary_calc_descriptives (struct descriptives *desc,
38                                 const struct casefile *cf,
39                                 struct casefilter *filter, 
40                                 const struct dictionary *dict,
41                                 const struct variable *const *vv, 
42                                 int n_vars UNUSED)
43 {
44   int i = 0;
45   while (*vv)
46     {
47       bool warn = true;
48       double minimum = DBL_MAX;
49       double maximum = -DBL_MAX;
50       double var;
51       struct moments1 *moments = moments1_create (MOMENT_VARIANCE);
52       struct casereader *r = casefile_get_reader (cf, filter);
53       struct ccase c;
54       const struct variable *v = *vv++;
55
56       while (casereader_read(r, &c))
57         {
58           const union value *val = case_data (&c, v);
59           double w = dict_get_case_weight (dict, &c, &warn);
60
61           if ( ! casefilter_variable_missing (filter, &c, v ))
62             {
63               minimum = MIN (minimum, val->f);
64               maximum = MAX (maximum, val->f);
65               moments1_add (moments, val->f, w); 
66             }
67           case_destroy (&c);
68         }
69       casereader_destroy (r);
70
71       moments1_calculate (moments, 
72                           &desc[i].n, 
73                           &desc[i].mean, 
74                           &var,
75                           NULL, NULL);
76
77       desc[i].std_dev = sqrt (var);
78
79       moments1_destroy (moments);
80       
81       desc[i].min = minimum;
82       desc[i].max = maximum;
83       
84       i++;
85     }
86 }
87
88
89
90 void
91 do_summary_box (const struct descriptives *desc, 
92                 const struct variable *const *vv,
93                 int n_vars)
94 {
95   int v;
96   bool quartiles = false;
97
98   int col;
99   int columns = 1 ;
100   struct tab_table *table ;
101
102
103   if ( desc ) columns += 5;
104   if ( quartiles ) columns += 3;
105
106   table = tab_create (columns, 2 + n_vars, 0);
107
108   tab_dim (table, tab_natural_dimensions);
109
110   tab_title (table, _("Descriptive Statistics"));
111         
112   tab_headers (table, 1, 0, 1, 0);
113
114   tab_box (table, TAL_1, TAL_1, -1, TAL_1, 
115            0, 0, table->nc - 1, tab_nr(table) - 1 );
116
117   tab_hline (table, TAL_2, 0, tab_nc (table) -1, 2);
118   tab_vline (table, TAL_2, 1, 0, tab_nr (table) - 1);
119
120   col = 1;
121   if ( desc ) 
122     {
123       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER, 
124                       _("N"));
125       col++;
126       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER, 
127                       _("Mean"));
128       col++;
129       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER, 
130                       _("Std. Deviation"));
131       col++;
132       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER, 
133                       _("Minimum"));
134       col++;
135       tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER, 
136                       _("Maximum"));
137       col++;
138     }
139
140   if ( quartiles ) 
141     {
142       tab_joint_text (table, col, 0, col + 2, 0, TAT_TITLE | TAB_CENTER,
143                       _("Percentiles"));
144       tab_hline (table, TAL_1, col, col + 2, 1);
145
146       tab_text (table, col, 1, TAT_TITLE | TAB_CENTER, 
147                 _("25th"));
148       col++;
149       tab_text (table, col, 1, TAT_TITLE | TAB_CENTER, 
150                 _("50th (Median)"));
151       col++;
152       tab_text (table, col, 1, TAT_TITLE | TAB_CENTER, 
153                 _("75th"));
154       col++;
155     }
156
157   for ( v = 0 ; v < n_vars ; ++v ) 
158     {
159       tab_text (table, 0, 2 + v, TAT_NONE, var_to_string (vv[v]));
160
161       tab_float (table, 1, 2 + v, TAT_NONE, desc[v].n, 8, 0);
162       tab_float (table, 2, 2 + v, TAT_NONE, desc[v].mean, 8, 3);
163       tab_float (table, 3, 2 + v, TAT_NONE, desc[v].std_dev, 8, 3);
164       tab_float (table, 4, 2 + v, TAT_NONE, desc[v].min, 8, 3);
165       tab_float (table, 5, 2 + v, TAT_NONE, desc[v].max, 8, 3);
166     }
167
168
169   tab_submit (table);
170 }