1 /* PSPP - computes sample statistics.
2 Copyright (C) 2006 Free Software Foundation, Inc.
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.
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.
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
20 #include <output/table.h>
21 #include <data/casereader.h>
22 #include <libpspp/hash.h>
23 #include <data/variable.h>
24 #include "npar-summary.h"
25 #include <math/moments.h>
26 #include <data/case.h>
27 #include <data/dictionary.h>
32 #define _(msgid) gettext (msgid)
36 npar_summary_calc_descriptives (struct descriptives *desc,
37 struct casereader *input,
38 const struct dictionary *dict,
39 const struct variable *const *vv,
46 double minimum = DBL_MAX;
47 double maximum = -DBL_MAX;
49 struct moments1 *moments = moments1_create (MOMENT_VARIANCE);
51 const struct variable *v = *vv++;
52 struct casereader *pass;
54 pass = casereader_clone (input);
55 pass = casereader_create_filter_missing (pass,
58 pass = casereader_create_filter_weight (pass, dict, NULL, NULL);
59 while (casereader_read(pass, &c))
61 double val = case_num (&c, v);
62 double w = dict_get_case_weight (dict, &c, NULL);
63 minimum = MIN (minimum, val);
64 maximum = MAX (maximum, val);
65 moments1_add (moments, val, w);
68 casereader_destroy (pass);
70 moments1_calculate (moments,
76 desc[i].std_dev = sqrt (var);
78 moments1_destroy (moments);
80 desc[i].min = minimum;
81 desc[i].max = maximum;
85 casereader_destroy (input);
91 do_summary_box (const struct descriptives *desc,
92 const struct variable *const *vv,
96 bool quartiles = false;
100 struct tab_table *table ;
103 if ( desc ) columns += 5;
104 if ( quartiles ) columns += 3;
106 table = tab_create (columns, 2 + n_vars, 0);
108 tab_dim (table, tab_natural_dimensions);
110 tab_title (table, _("Descriptive Statistics"));
112 tab_headers (table, 1, 0, 1, 0);
114 tab_box (table, TAL_1, TAL_1, -1, TAL_1,
115 0, 0, table->nc - 1, tab_nr(table) - 1 );
117 tab_hline (table, TAL_2, 0, tab_nc (table) -1, 2);
118 tab_vline (table, TAL_2, 1, 0, tab_nr (table) - 1);
123 tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
126 tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
129 tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
130 _("Std. Deviation"));
132 tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
135 tab_joint_text (table, col, 0, col, 1, TAT_TITLE | TAB_CENTER,
142 tab_joint_text (table, col, 0, col + 2, 0, TAT_TITLE | TAB_CENTER,
144 tab_hline (table, TAL_1, col, col + 2, 1);
146 tab_text (table, col, 1, TAT_TITLE | TAB_CENTER,
149 tab_text (table, col, 1, TAT_TITLE | TAB_CENTER,
152 tab_text (table, col, 1, TAT_TITLE | TAB_CENTER,
157 for ( v = 0 ; v < n_vars ; ++v )
159 tab_text (table, 0, 2 + v, TAT_NONE, var_to_string (vv[v]));
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);