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