1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "language/stats/npar-summary.h"
23 #include "data/case.h"
24 #include "data/casereader.h"
25 #include "data/dictionary.h"
26 #include "data/format.h"
27 #include "data/variable.h"
28 #include "math/moments.h"
29 #include "output/pivot-table.h"
31 #include "gl/minmax.h"
34 #define N_(msgid) msgid
35 #define _(msgid) gettext (msgid)
39 npar_summary_calc_descriptives (struct descriptives *desc,
40 struct casereader *input,
41 const struct dictionary *dict,
42 const struct variable *const *vv,
47 for (i = 0 ; i < n_vars; ++i)
49 double minimum = DBL_MAX;
50 double maximum = -DBL_MAX;
52 struct moments1 *moments = moments1_create (MOMENT_VARIANCE);
54 const struct variable *v = vv[i];
55 struct casereader *pass;
57 pass = casereader_clone (input);
58 pass = casereader_create_filter_missing (pass,
61 pass = casereader_create_filter_weight (pass, dict, NULL, NULL);
62 while ((c = casereader_read (pass)) != NULL)
64 double val = case_num (c, v);
65 double w = dict_get_case_weight (dict, c, NULL);
66 minimum = MIN (minimum, val);
67 maximum = MAX (maximum, val);
68 moments1_add (moments, val, w);
71 casereader_destroy (pass);
73 moments1_calculate (moments,
79 desc[i].std_dev = sqrt (var);
81 moments1_destroy (moments);
83 desc[i].min = minimum;
84 desc[i].max = maximum;
87 casereader_destroy (input);
93 do_summary_box (const struct descriptives *desc,
94 const struct variable *const *vv,
96 const struct fmt_spec *wfmt)
101 struct pivot_table *table = pivot_table_create (
102 N_("Descriptive Statistics"));
103 pivot_table_set_weight_format (table, wfmt);
105 pivot_dimension_create (
106 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
107 N_("N"), PIVOT_RC_COUNT,
108 N_("Mean"), PIVOT_RC_OTHER,
109 N_("Std. Deviation"), PIVOT_RC_OTHER,
110 N_("Minimum"), N_("Maximum"));
112 struct pivot_dimension *variables = pivot_dimension_create (
113 table, PIVOT_AXIS_ROW, N_("Variable"));
115 for (int v = 0; v < n_vars; ++v)
117 const struct variable *var = vv[v];
119 int row = pivot_category_create_leaf (variables->root,
120 pivot_value_new_variable (var));
122 double entries[] = { desc[v].n, desc[v].mean, desc[v].std_dev };
123 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
124 pivot_table_put2 (table, j, row, pivot_value_new_number (entries[j]));
126 union value extrema[2] = { { .f = desc[v].min }, { .f = desc[v].max } };
127 for (size_t j = 0; j < 2; j++)
128 pivot_table_put2 (table, 3 + j, row,
129 pivot_value_new_var_value (var, &extrema[j]));
132 pivot_table_submit (table);