1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 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/>.
24 #include <gsl/gsl_cdf.h>
26 #include "data/variable.h"
27 #include "data/format.h"
28 #include "data/casereader.h"
29 #include "data/dictionary.h"
30 #include "libpspp/hash-functions.h"
31 #include "libpspp/hmapx.h"
32 #include "math/moments.h"
34 #include "output/tab.h"
37 #define _(msgid) gettext (msgid)
42 const struct variable *var;
44 /* The position for reporting purposes */
47 /* N, Mean, Variance */
50 /* Sum of the differences */
63 one_sample_test (const struct tt *tt, const struct one_samp *os)
65 struct hmapx_node *node;
66 struct per_var_stats *per_var_stats;
68 const int heading_rows = 3;
69 const size_t rows = heading_rows + tt->n_vars;
70 const size_t cols = 7;
71 const struct fmt_spec *wfmt = tt->wv ? var_get_print_format (tt->wv) : & F_8_0;
73 struct tab_table *t = tab_create (cols, rows);
75 tab_headers (t, 0, 0, heading_rows, 0);
76 tab_box (t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols - 1, rows - 1);
77 tab_hline (t, TAL_2, 0, cols - 1, 3);
79 tab_title (t, _("One-Sample Test"));
80 tab_hline (t, TAL_1, 1, cols - 1, 1);
81 tab_vline (t, TAL_2, 1, 0, rows - 1);
83 tab_joint_text_format (t, 1, 0, cols - 1, 0, TAB_CENTER,
84 _("Test Value = %f"), os->testval);
86 tab_box (t, -1, -1, -1, TAL_1, 1, 1, cols - 1, rows - 1);
88 tab_joint_text_format (t, 5, 1, 6, 1, TAB_CENTER,
89 _("%g%% Confidence Interval of the Difference"),
90 tt->confidence * 100.0);
92 tab_vline (t, TAL_GAP, 6, 1, 1);
93 tab_hline (t, TAL_1, 5, 6, 2);
94 tab_text (t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
95 tab_text (t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
96 tab_text (t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
97 tab_text (t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
98 tab_text (t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
99 tab_text (t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
101 HMAPX_FOR_EACH (per_var_stats, node, &os->hmap)
103 const struct moments *m = per_var_stats->mom;
104 double cc, mean, sigma;
109 const int v = per_var_stats->posn;
111 moments_calculate (m, &cc, &mean, &sigma, NULL, NULL);
113 tval = (mean - os->testval) * sqrt (cc / sigma);
115 mean_diff = per_var_stats->sum_diff / cc;
116 se_mean = sqrt (sigma / cc);
118 p = gsl_cdf_tdist_P (tval, df);
119 q = gsl_cdf_tdist_Q (tval, df);
121 tab_text (t, 0, v + heading_rows, TAB_LEFT, var_to_string (per_var_stats->var));
122 tab_double (t, 1, v + heading_rows, TAB_RIGHT, tval, NULL);
123 tab_double (t, 2, v + heading_rows, TAB_RIGHT, df, wfmt);
125 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
127 tab_double (t, 3, v + heading_rows, TAB_RIGHT, 2.0 * (tval > 0 ? q : p), NULL);
129 tab_double (t, 4, v + heading_rows, TAB_RIGHT, mean_diff, NULL);
131 tval = gsl_cdf_tdist_Qinv ( (1.0 - tt->confidence) / 2.0, df);
133 tab_double (t, 5, v + heading_rows, TAB_RIGHT, mean_diff - tval * se_mean, NULL);
134 tab_double (t, 6, v + heading_rows, TAB_RIGHT, mean_diff + tval * se_mean, NULL);
141 one_sample_summary (const struct tt *tt, const struct one_samp *os)
143 struct hmapx_node *node;
144 struct per_var_stats *per_var_stats;
147 const int heading_rows = 1;
148 const int rows = tt->n_vars + heading_rows;
149 struct tab_table *t = tab_create (cols, rows);
150 const struct fmt_spec *wfmt = tt->wv ? var_get_print_format (tt->wv) : & F_8_0;
152 tab_headers (t, 0, 0, heading_rows, 0);
153 tab_box (t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols - 1, rows - 1);
154 tab_hline (t, TAL_2, 0, cols - 1, 1);
156 tab_title (t, _("One-Sample Statistics"));
157 tab_vline (t, TAL_2, 1, 0, rows - 1);
158 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
159 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
160 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
161 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("S.E. Mean"));
163 HMAPX_FOR_EACH (per_var_stats, node, &os->hmap)
165 const struct moments *m = per_var_stats->mom;
166 const int v = per_var_stats->posn;
167 double cc, mean, sigma;
168 moments_calculate (m, &cc, &mean, &sigma, NULL, NULL);
170 tab_text (t, 0, v + heading_rows, TAB_LEFT, var_to_string (per_var_stats->var));
171 tab_double (t, 1, v + heading_rows, TAB_RIGHT, cc, wfmt);
172 tab_double (t, 2, v + heading_rows, TAB_RIGHT, mean, NULL);
173 tab_double (t, 3, v + heading_rows, TAB_RIGHT, sqrt (sigma), NULL);
174 tab_double (t, 4, v + heading_rows, TAB_RIGHT, sqrt (sigma / cc), NULL);
181 one_sample_run (const struct tt *tt, double testval, struct casereader *reader)
186 struct casereader *r;
187 struct hmapx_node *node;
188 struct per_var_stats *per_var_stats;
190 os.testval = testval;
191 hmapx_init (&os.hmap);
193 /* Insert all the variables into the map */
194 for (i = 0; i < tt->n_vars; ++i)
196 struct per_var_stats *per_var_stats = xzalloc (sizeof (*per_var_stats));
198 per_var_stats->posn = i;
199 per_var_stats->var = tt->vars[i];
200 per_var_stats->mom = moments_create (MOMENT_VARIANCE);
202 hmapx_insert (&os.hmap, per_var_stats, hash_pointer (per_var_stats->var, 0));
205 r = casereader_clone (reader);
206 for ( ; (c = casereader_read (r) ); case_unref (c))
208 double w = dict_get_case_weight (tt->dict, c, NULL);
209 struct hmapx_node *node;
210 struct per_var_stats *per_var_stats;
211 HMAPX_FOR_EACH (per_var_stats, node, &os.hmap)
213 const struct variable *var = per_var_stats->var;
214 const union value *val = case_data (c, var);
215 if (var_is_value_missing (var, val, tt->exclude))
218 moments_pass_one (per_var_stats->mom, val->f, w);
221 casereader_destroy (r);
224 for ( ; (c = casereader_read (r) ); case_unref (c))
226 double w = dict_get_case_weight (tt->dict, c, NULL);
227 struct hmapx_node *node;
228 struct per_var_stats *per_var_stats;
229 HMAPX_FOR_EACH (per_var_stats, node, &os.hmap)
231 const struct variable *var = per_var_stats->var;
232 const union value *val = case_data (c, var);
233 if (var_is_value_missing (var, val, tt->exclude))
236 moments_pass_two (per_var_stats->mom, val->f, w);
237 per_var_stats->sum_diff += w * (val->f - os.testval);
240 casereader_destroy (r);
242 one_sample_summary (tt, &os);
243 one_sample_test (tt, &os);
245 HMAPX_FOR_EACH (per_var_stats, node, &os.hmap)
247 moments_destroy (per_var_stats->mom);
248 free (per_var_stats);
251 hmapx_destroy (&os.hmap);