38c1eff6e4077ccb1c484a4ec4e6e8e2b0292dea
[pspp] / src / language / stats / t-test-one-sample.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.
16 */
17
18 #include <config.h>
19
20
21 #include "t-test.h"
22
23 #include <math.h>
24 #include <gsl/gsl_cdf.h>
25
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"
33
34 #include "output/tab.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39
40 struct per_var_stats
41 {
42   const struct variable *var;
43
44   /* The position for reporting purposes */
45   int posn;
46
47   /* N, Mean, Variance */
48   struct moments *mom;
49
50   /* Sum of the differences */
51   double sum_diff;
52 };
53
54
55 struct one_samp
56 {
57   struct hmapx hmap;
58   double testval;
59 };
60
61
62 static void
63 one_sample_test (const struct tt *tt, const struct one_samp *os)
64 {
65   struct hmapx_node *node;
66   struct per_var_stats *per_var_stats;
67
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;
72
73   struct tab_table *t = tab_create (cols, rows);
74
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);
78
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);
82
83   tab_joint_text_format (t, 1, 0, cols - 1, 0, TAB_CENTER,
84                          _("Test Value = %f"), os->testval);
85
86   tab_box (t, -1, -1, -1, TAL_1, 1, 1, cols - 1, rows - 1);
87
88   tab_joint_text_format (t, 5, 1, 6, 1, TAB_CENTER,
89                          _("%g%% Confidence Interval of the Difference"),
90                          tt->confidence * 100.0);
91
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"));
100
101   HMAPX_FOR_EACH (per_var_stats, node, &os->hmap)
102     {
103       const struct moments *m = per_var_stats->mom;
104       double cc, mean, sigma;
105       double tval, df;
106       double p, q;
107       double mean_diff;
108       double se_mean ;
109       const int v = per_var_stats->posn;
110
111       moments_calculate (m, &cc, &mean, &sigma, NULL, NULL);
112
113       tval = (mean - os->testval) * sqrt (cc / sigma);
114
115       mean_diff = per_var_stats->sum_diff / cc;
116       se_mean = sqrt (sigma / cc);
117       df = cc - 1.0;
118       p = gsl_cdf_tdist_P (tval, df);
119       q = gsl_cdf_tdist_Q (tval, df);
120
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);
124
125       /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
126          the correct tail*/
127       tab_double (t, 3, v + heading_rows, TAB_RIGHT, 2.0 * (tval > 0 ? q : p), NULL);
128
129       tab_double (t, 4, v + heading_rows, TAB_RIGHT, mean_diff,  NULL);
130
131       tval = gsl_cdf_tdist_Qinv ( (1.0 - tt->confidence) / 2.0, df);
132
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);
135     }
136
137   tab_submit (t);
138 }
139
140 static void
141 one_sample_summary (const struct tt *tt, const struct one_samp *os)
142 {
143   struct hmapx_node *node;
144   struct per_var_stats *per_var_stats;
145
146   const int cols = 5;
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;
151
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);
155
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"));
162
163   HMAPX_FOR_EACH (per_var_stats, node, &os->hmap)
164     {
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);
169
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);
175     }
176
177   tab_submit (t);
178 }
179
180 void
181 one_sample_run (const struct tt *tt, double testval, struct casereader *reader)
182 {
183   int i;
184   struct ccase *c;
185   struct one_samp os;
186   struct casereader *r;
187   struct hmapx_node *node;
188   struct per_var_stats *per_var_stats;
189
190   os.testval = testval;
191   hmapx_init (&os.hmap);
192
193   /* Insert all the variables into the map */
194   for (i = 0; i < tt->n_vars; ++i)
195     {
196       struct per_var_stats *per_var_stats = xzalloc (sizeof (*per_var_stats));
197
198       per_var_stats->posn = i;
199       per_var_stats->var = tt->vars[i];
200       per_var_stats->mom = moments_create (MOMENT_VARIANCE);
201
202       hmapx_insert (&os.hmap, per_var_stats, hash_pointer (per_var_stats->var, 0));
203     }
204
205   r = casereader_clone (reader);
206   for ( ; (c = casereader_read (r) ); case_unref (c))
207     {
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)
212         {
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))
216             continue;
217
218           moments_pass_one (per_var_stats->mom, val->f, w);
219         }
220     }
221   casereader_destroy (r);
222
223   r = reader;
224   for ( ; (c = casereader_read (r) ); case_unref (c))
225     {
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)
230         {
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))
234             continue;
235
236           moments_pass_two (per_var_stats->mom, val->f, w);
237           per_var_stats->sum_diff += w * (val->f - os.testval);
238         }
239     }
240   casereader_destroy (r);
241
242   one_sample_summary (tt, &os);
243   one_sample_test (tt, &os);
244
245   HMAPX_FOR_EACH (per_var_stats, node, &os.hmap)
246     {
247       moments_destroy (per_var_stats->mom);
248       free (per_var_stats);
249     }
250
251   hmapx_destroy (&os.hmap);
252 }
253