Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / stats / t-test.q
1 /* PSPP - computes sample statistics. -*-c-*-
2
3    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
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
22 #include <gsl/gsl_cdf.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/value-labels.h>
33 #include <data/variable.h>
34 #include <language/command.h>
35 #include <language/dictionary/split-file.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/assertion.h>
39 #include <libpspp/compiler.h>
40 #include <libpspp/hash.h>
41 #include <libpspp/magic.h>
42 #include <libpspp/message.h>
43 #include <libpspp/misc.h>
44 #include <libpspp/str.h>
45 #include <libpspp/taint.h>
46 #include <math/group-proc.h>
47 #include <math/levene.h>
48 #include <output/manager.h>
49 #include <output/table.h>
50
51 #include "size_max.h"
52
53 #include "gettext.h"
54 #define _(msgid) gettext (msgid)
55
56 /* (headers) */
57
58 /* (specification)
59    "T-TEST" (tts_):
60      +groups=custom;
61      testval=double;
62      +variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
63      +pairs=custom;
64      missing=miss:!analysis/listwise,
65             incl:include/!exclude;
66      +format=fmt:!labels/nolabels;
67      criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
68 */
69 /* (declarations) */
70 /* (functions) */
71
72
73 /* Variable for the GROUPS subcommand, if given. */
74 static struct variable *indep_var;
75
76 enum comparison
77   {
78     CMP_LE = -2,
79     CMP_EQ = 0,
80   };
81
82 struct group_properties
83 {
84   /* The comparison criterion */
85   enum comparison criterion;
86
87   /* The width of the independent variable */
88   int indep_width ;  
89
90   union {
91     /* The value of the independent variable at which groups are determined to 
92        belong to one group or the other */
93     double critical_value;
94     
95
96     /* The values of the independent variable for each group */
97     union value g_value[2];
98   } v ;
99
100 };
101
102
103 static struct group_properties gp ;
104
105
106
107 /* PAIRS: Number of pairs to be compared ; each pair. */
108 static int n_pairs = 0 ;
109 struct pair 
110 {
111   /* The variables comprising the pair */
112   const struct variable *v[2];
113
114   /* The number of valid variable pairs */
115   double n;
116
117   /* The sum of the members */
118   double sum[2];
119
120   /* sum of squares of the members */
121   double ssq[2];
122
123   /* Std deviation of the members */
124   double std_dev[2];
125
126
127   /* Sample Std deviation of the members */
128   double s_std_dev[2];
129
130   /* The means of the members */
131   double mean[2];
132
133   /* The correlation coefficient between the variables */
134   double correlation;
135
136   /* The sum of the differences */
137   double sum_of_diffs;
138
139   /* The sum of the products */
140   double sum_of_prod;
141
142   /* The mean of the differences */
143   double mean_diff;
144
145   /* The sum of the squares of the differences */
146   double ssq_diffs;
147
148   /* The std deviation of the differences */
149   double std_dev_diff;
150 };
151
152 static struct pair *pairs=0;
153
154 static int parse_value (struct lexer *lexer, union value * v, enum var_type);
155
156 /* Structures and Functions for the Statistics Summary Box */
157 struct ssbox;
158 typedef void populate_ssbox_func(struct ssbox *ssb,
159                                             struct cmd_t_test *cmd);
160 typedef void finalize_ssbox_func(struct ssbox *ssb);
161
162 struct ssbox
163 {
164   struct tab_table *t;
165
166   populate_ssbox_func *populate;
167   finalize_ssbox_func *finalize;
168
169 };
170
171 /* Create a ssbox */
172 void ssbox_create(struct ssbox *ssb,   struct cmd_t_test *cmd, int mode);
173
174 /* Populate a ssbox according to cmd */
175 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
176
177 /* Submit and destroy a ssbox */
178 void ssbox_finalize(struct ssbox *ssb);
179
180 /* A function to create, populate and submit the Paired Samples Correlation 
181    box */
182 void pscbox(void);
183
184
185 /* Structures and Functions for the Test Results Box */
186 struct trbox;
187
188 typedef void populate_trbox_func(struct trbox *trb,
189                                  struct cmd_t_test *cmd);
190 typedef void finalize_trbox_func(struct trbox *trb);
191
192 struct trbox {
193   struct tab_table *t;
194   populate_trbox_func *populate;
195   finalize_trbox_func *finalize;
196 };
197
198 /* Create a trbox */
199 void trbox_create(struct trbox *trb,   struct cmd_t_test *cmd, int mode);
200
201 /* Populate a ssbox according to cmd */
202 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
203
204 /* Submit and destroy a ssbox */
205 void trbox_finalize(struct trbox *trb);
206
207 /* Which mode was T-TEST invoked */
208 enum {
209   T_1_SAMPLE = 0 ,
210   T_IND_SAMPLES, 
211   T_PAIRED
212 };
213
214
215 static int common_calc (const struct dictionary *dict, 
216                         const struct ccase *, void *, 
217                         enum mv_class);
218 static void common_precalc (struct cmd_t_test *);
219 static void common_postcalc (struct cmd_t_test *);
220
221 static int one_sample_calc (const struct dictionary *dict, const struct ccase *, void *, enum mv_class);
222 static void one_sample_precalc (struct cmd_t_test *);
223 static void one_sample_postcalc (struct cmd_t_test *);
224
225 static int  paired_calc (const struct dictionary *dict, const struct ccase *, 
226                          struct cmd_t_test*, enum mv_class);
227 static void paired_precalc (struct cmd_t_test *);
228 static void paired_postcalc (struct cmd_t_test *);
229
230 static void group_precalc (struct cmd_t_test *);
231 static int  group_calc (const struct dictionary *dict, const struct ccase *, 
232                         struct cmd_t_test *, enum mv_class);
233 static void group_postcalc (struct cmd_t_test *);
234
235
236 static void calculate(struct cmd_t_test *,
237                       struct casereader *,
238                       const struct dataset *);
239
240 static  int mode;
241
242 static struct cmd_t_test cmd;
243
244 static bool bad_weight_warn = false;
245
246
247 static int compare_group_binary(const struct group_statistics *a, 
248                                 const struct group_statistics *b, 
249                                 const struct group_properties *p);
250
251
252 static unsigned  hash_group_binary(const struct group_statistics *g, 
253                                    const struct group_properties *p);
254
255
256
257 int
258 cmd_t_test (struct lexer *lexer, struct dataset *ds)
259 {
260   struct casegrouper *grouper;
261   struct casereader *group;
262   bool ok;
263   
264   if ( !parse_t_test (lexer, ds, &cmd, NULL) )
265     return CMD_FAILURE;
266
267   if (! cmd.sbc_criteria)
268     cmd.criteria=0.95;
269
270   {
271     int m=0;
272     if (cmd.sbc_testval) ++m;
273     if (cmd.sbc_groups) ++m;
274     if (cmd.sbc_pairs) ++m;
275
276     if ( m != 1)
277       {
278         msg(SE, 
279             _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
280             );
281         free_t_test(&cmd);
282         return CMD_FAILURE;
283       }
284   }
285
286   if (cmd.sbc_testval) 
287     mode=T_1_SAMPLE;
288   else if (cmd.sbc_groups)
289     mode=T_IND_SAMPLES;
290   else
291     mode=T_PAIRED;
292
293   if ( mode == T_PAIRED) 
294     {
295       if (cmd.sbc_variables) 
296         {
297           msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
298           free_t_test(&cmd);
299           return CMD_FAILURE;
300         }
301       else
302         {
303           /* Iterate through the pairs and put each variable that is a 
304              member of a pair into cmd.v_variables */
305
306           int i;
307           struct hsh_iterator hi;
308           struct const_hsh_table *hash;
309           const struct variable *v;
310
311           hash = const_hsh_create (n_pairs, compare_vars_by_name, hash_var_by_name,
312           0, 0);
313
314           for (i=0; i < n_pairs; ++i)
315             {
316               const_hsh_insert (hash, pairs[i].v[0]);
317               const_hsh_insert (hash, pairs[i].v[1]);
318             }
319
320           assert(cmd.n_variables == 0);
321           cmd.n_variables = const_hsh_count (hash);
322
323           cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
324                                        sizeof *cmd.v_variables);
325           /* Iterate through the hash */
326           for (i=0,v = const_hsh_first (hash, &hi);
327                v != 0;
328                v = const_hsh_next (hash, &hi) ) 
329             cmd.v_variables[i++]=v;
330           const_hsh_destroy(hash);
331         }
332     }
333   else if ( !cmd.sbc_variables) 
334     {
335       msg(SE, _("One or more VARIABLES must be specified."));
336       free_t_test(&cmd);
337       return CMD_FAILURE;
338     }
339
340   bad_weight_warn = true;
341
342   /* Data pass. */
343   grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
344   while (casegrouper_get_next_group (grouper, &group)) 
345     calculate (&cmd, group, ds);
346   ok = casegrouper_destroy (grouper);
347   ok = proc_commit (ds) && ok;
348
349   n_pairs=0;
350   free(pairs);
351   pairs=0;
352
353   if ( mode == T_IND_SAMPLES) 
354     {
355       int v;
356       /* Destroy any group statistics we created */
357       for (v = 0 ; v < cmd.n_variables ; ++v ) 
358         {
359           struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
360           hsh_destroy (grpp->group_hash);
361         }
362     }
363     
364   free_t_test(&cmd);
365   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
366 }
367
368 static int
369 tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
370 {
371   int n_group_values=0;
372
373   lex_match (lexer, '=');
374
375   indep_var = parse_variable (lexer, dataset_dict (ds));
376   if (!indep_var)
377     {
378       lex_error (lexer, "expecting variable name in GROUPS subcommand");
379       return 0;
380     }
381
382   if (var_is_long_string (indep_var))
383     {
384       msg (SE, _("Long string variable %s is not valid here."),
385            var_get_name (indep_var));
386       return 0;
387     }
388
389   if (!lex_match (lexer, '('))
390     {
391       if (var_is_numeric (indep_var))
392         {
393           gp.v.g_value[0].f = 1;
394           gp.v.g_value[1].f = 2;
395
396           gp.criterion = CMP_EQ;
397           
398           n_group_values = 2;
399
400           return 1;
401         }
402       else
403         {
404           msg (SE, _("When applying GROUPS to a string variable, two "
405                      "values must be specified."));
406           return 0;
407         }
408     }
409
410   if (!parse_value (lexer, &gp.v.g_value[0], var_get_type (indep_var)))
411       return 0;
412
413   lex_match (lexer, ',');
414   if (lex_match (lexer, ')'))
415     {
416       if (var_is_alpha (indep_var))
417         {
418           msg (SE, _("When applying GROUPS to a string variable, two "
419                      "values must be specified."));
420           return 0;
421         }
422       gp.criterion = CMP_LE;
423       gp.v.critical_value = gp.v.g_value[0].f;
424
425       n_group_values = 1;
426       return 1;
427     }
428
429   if (!parse_value (lexer, &gp.v.g_value[1], var_get_type (indep_var)))
430     return 0;
431
432   n_group_values = 2;
433   if (!lex_force_match (lexer, ')'))
434     return 0;
435
436   if ( n_group_values == 2 ) 
437     gp.criterion = CMP_EQ ;
438   else
439     gp.criterion = CMP_LE ;
440
441
442   return 1;
443 }
444
445
446 static int
447 tts_custom_pairs (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
448 {
449   const struct variable **vars;
450   size_t n_vars;
451   size_t n_pairs_local;
452
453   size_t n_before_WITH;
454   size_t n_after_WITH = SIZE_MAX;
455   int paired ; /* Was the PAIRED keyword given ? */
456
457   lex_match (lexer, '=');
458
459   n_vars=0;
460   if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
461                         PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
462     {
463       free (vars);
464       return 0;
465     }
466   assert (n_vars);
467
468   n_before_WITH = 0;
469   if (lex_match (lexer, T_WITH))
470     {
471       n_before_WITH = n_vars;
472       if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
473                             PV_DUPLICATE | PV_APPEND
474                             | PV_NUMERIC | PV_NO_SCRATCH))
475         {
476           free (vars);
477           return 0;
478         }
479       n_after_WITH = n_vars - n_before_WITH;
480     }
481
482   paired = (lex_match (lexer, '(') && lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
483
484   /* Determine the number of pairs needed */
485   if (paired)
486     {
487       if (n_before_WITH != n_after_WITH)
488         {
489           free (vars);
490           msg (SE, _("PAIRED was specified but the number of variables "
491                      "preceding WITH (%d) did not match the number "
492                      "following (%d)."),
493                (int) n_before_WITH, (int) n_after_WITH );
494           return 0;
495         }
496       n_pairs_local = n_before_WITH;
497     }
498   else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
499     {
500       n_pairs_local = n_before_WITH * n_after_WITH ;
501     }
502   else /* Neither WITH nor PAIRED keyword given */
503     {
504       if (n_vars < 2)
505         {
506           free (vars);
507           msg (SE, _("At least two variables must be specified "
508                      "on PAIRS."));
509           return 0;
510         }
511
512       /* how many ways can you pick 2 from n_vars ? */
513       n_pairs_local = n_vars * (n_vars - 1) / 2;
514     }
515
516
517   /* Allocate storage for the pairs */
518   pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
519
520   /* Populate the pairs with the appropriate variables */
521   if ( paired ) 
522     {
523       int i;
524
525       assert(n_pairs_local == n_vars / 2);
526       for (i = 0; i < n_pairs_local; ++i)
527         {
528           pairs[i].v[n_pairs] = vars[i];
529           pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
530         }
531     }
532   else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
533     {
534       int i,j;
535       size_t p = n_pairs;
536
537       for(i=0 ; i < n_before_WITH ; ++i ) 
538         {
539           for(j=0 ; j < n_after_WITH ; ++j)
540             {
541               pairs[p].v[0] = vars[i];
542               pairs[p].v[1] = vars[j+n_before_WITH];
543               ++p;
544             }
545         }
546     }
547   else /* Neither WITH nor PAIRED given */
548     {
549       size_t i,j;
550       size_t p=n_pairs;
551       
552       for(i=0 ; i < n_vars ; ++i ) 
553         {
554           for(j=i+1 ; j < n_vars ; ++j)
555             {
556               pairs[p].v[0] = vars[i];
557               pairs[p].v[1] = vars[j];
558               ++p;
559             }
560         }
561     }
562
563   n_pairs+=n_pairs_local;
564
565   free (vars);
566   return 1;
567 }
568
569 /* Parses the current token (numeric or string, depending on type)
570     value v and returns success. */
571 static int
572 parse_value (struct lexer *lexer, union value * v, enum var_type type)
573 {
574   if (type == VAR_NUMERIC)
575     {
576       if (!lex_force_num (lexer))
577         return 0;
578       v->f = lex_tokval (lexer);
579     }
580   else
581     {
582       if (!lex_force_string (lexer))
583         return 0;
584       strncpy (v->s, ds_cstr (lex_tokstr (lexer)), ds_length (lex_tokstr (lexer)));
585     }
586
587   lex_get (lexer);
588
589   return 1;
590 }
591
592
593 /* Implementation of the SSBOX object */
594
595 void ssbox_base_init(struct ssbox *this, int cols,int rows);
596
597 void ssbox_base_finalize(struct ssbox *ssb);
598
599 void ssbox_one_sample_init(struct ssbox *this, 
600                            struct cmd_t_test *cmd );
601
602 void ssbox_independent_samples_init(struct ssbox *this,
603                                     struct cmd_t_test *cmd);
604
605 void ssbox_paired_init(struct ssbox *this,
606                            struct cmd_t_test *cmd);
607
608
609 /* Factory to create an ssbox */
610 void 
611 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
612 {
613     switch (mode) 
614       {
615       case T_1_SAMPLE:
616         ssbox_one_sample_init(ssb,cmd);
617         break;
618       case T_IND_SAMPLES:
619         ssbox_independent_samples_init(ssb,cmd);
620         break;
621       case T_PAIRED:
622         ssbox_paired_init(ssb,cmd);
623         break;
624       default:
625         NOT_REACHED ();
626       }
627 }
628
629
630
631 /* Despatcher for the populate method */
632 void
633 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
634 {
635   ssb->populate(ssb,cmd);
636 }
637
638
639 /* Despatcher for finalize */
640 void
641 ssbox_finalize(struct ssbox *ssb)
642 {
643   ssb->finalize(ssb);
644 }
645
646
647 /* Submit the box and clear up */
648 void 
649 ssbox_base_finalize(struct ssbox *ssb)
650 {
651   tab_submit(ssb->t);
652 }
653
654
655
656 /* Initialize a ssbox struct */
657 void 
658 ssbox_base_init(struct ssbox *this, int cols,int rows)
659 {
660   this->finalize = ssbox_base_finalize;
661   this->t = tab_create (cols, rows, 0);
662
663   tab_columns (this->t, SOM_COL_DOWN, 1);
664   tab_headers (this->t,0,0,1,0); 
665   tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
666   tab_hline(this->t, TAL_2,0,cols-1,1);
667   tab_dim (this->t, tab_natural_dimensions);
668 }
669
670 void  ssbox_one_sample_populate(struct ssbox *ssb,
671                               struct cmd_t_test *cmd);
672
673 /* Initialize the one_sample ssbox */
674 void 
675 ssbox_one_sample_init(struct ssbox *this, 
676                            struct cmd_t_test *cmd )
677 {
678   const int hsize=5;
679   const int vsize=cmd->n_variables+1;
680
681   this->populate = ssbox_one_sample_populate;
682
683   ssbox_base_init(this, hsize,vsize);
684   tab_title (this->t, _("One-Sample Statistics"));
685   tab_vline(this->t, TAL_2, 1,0,vsize - 1);
686   tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
687   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
688   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
689   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
690 }
691
692 void ssbox_independent_samples_populate(struct ssbox *ssb,
693                                         struct cmd_t_test *cmd);
694
695 /* Initialize the independent samples ssbox */
696 void 
697 ssbox_independent_samples_init(struct ssbox *this, 
698         struct cmd_t_test *cmd)
699 {
700   int hsize=6;
701   int vsize = cmd->n_variables*2 +1;
702
703   this->populate = ssbox_independent_samples_populate;
704
705   ssbox_base_init(this, hsize,vsize);
706   tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
707   tab_title (this->t, _("Group Statistics"));
708   tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, var_get_name (indep_var));
709   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
710   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
711   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
712   tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
713 }
714
715
716 /* Populate the ssbox for independent samples */
717 void 
718 ssbox_independent_samples_populate(struct ssbox *ssb,
719                               struct cmd_t_test *cmd)
720 {
721   int i;
722
723   const char *val_lab0;
724   const char *val_lab1;
725   double indep_value[2];
726
727   char prefix[2][3]={"",""};
728
729   if ( var_is_numeric (indep_var) ) 
730     {
731       val_lab0 = var_lookup_value_label (indep_var, &gp.v.g_value[0]); 
732       val_lab1 = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
733     }
734   else
735     {
736       val_lab0 = gp.v.g_value[0].s;
737       val_lab1 = gp.v.g_value[1].s;
738     }
739
740   if (gp.criterion == CMP_LE ) 
741     {
742       strcpy(prefix[0],"< ");
743       strcpy(prefix[1],">=");
744       indep_value[0] = gp.v.critical_value;
745       indep_value[1] = gp.v.critical_value;
746     }
747   else
748     {
749       indep_value[0] = gp.v.g_value[0].f;
750       indep_value[1] = gp.v.g_value[1].f;
751     }
752
753   assert(ssb->t);
754
755   for (i=0; i < cmd->n_variables; ++i)
756     {
757       const struct variable *var = cmd->v_variables[i];
758       struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
759       int count=0;
760
761       tab_text (ssb->t, 0, i*2+1, TAB_LEFT,
762                 var_get_name (cmd->v_variables[i]));
763
764       if (val_lab0)
765         tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF, 
766                   "%s%s", prefix[0], val_lab0);
767       else
768           tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF, 
769                     "%s%g", prefix[0], indep_value[0]);
770
771
772       if (val_lab1)
773         tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF, 
774                   "%s%s", prefix[1], val_lab1);
775       else
776           tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF, 
777                     "%s%g", prefix[1], indep_value[1]);
778
779
780       /* Fill in the group statistics */
781       for ( count = 0 ; count < 2 ; ++count ) 
782         {
783           union value search_val;
784
785           struct group_statistics *gs;
786
787           if ( gp.criterion == CMP_LE ) 
788             {
789               if ( count == 0 ) 
790                 {
791                   /*  less than ( < )  case */
792                   search_val.f = gp.v.critical_value - 1.0;
793                 }
794               else
795                 {
796                   /* >= case  */
797                   search_val.f = gp.v.critical_value + 1.0;
798                 }
799             }
800           else
801             {
802               search_val = gp.v.g_value[count];
803             }
804
805           gs = hsh_find(grp_hash, (void *) &search_val);
806           assert(gs);
807
808           tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 10, 0);
809           tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
810           tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
811           tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
812         }
813     }
814 }
815
816
817 void ssbox_paired_populate(struct ssbox *ssb,
818                            struct cmd_t_test *cmd);
819
820 /* Initialize the paired values ssbox */
821 void 
822 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
823 {
824   int hsize=6;
825
826   int vsize = n_pairs*2+1;
827
828   this->populate = ssbox_paired_populate;
829
830   ssbox_base_init(this, hsize,vsize);
831   tab_title (this->t, _("Paired Sample Statistics"));
832   tab_vline(this->t,TAL_GAP,1,0,vsize-1);
833   tab_vline(this->t,TAL_2,2,0,vsize-1);
834   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
835   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
836   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
837   tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
838 }
839
840
841 /* Populate the ssbox for paired values */
842 void 
843 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
844 {
845   int i;
846
847   assert(ssb->t);
848
849   for (i=0; i < n_pairs; ++i)
850     {
851       int j;
852
853       tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
854
855       for (j=0 ; j < 2 ; ++j) 
856         {
857           struct group_statistics *gs;
858
859           gs = &group_proc_get (pairs[i].v[j])->ugs;
860
861           /* Titles */
862
863           tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT,
864                     var_get_name (pairs[i].v[j]));
865
866           /* Values */
867           tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
868           tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 10, 0);
869           tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
870           tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
871
872         }
873     }
874 }
875
876 /* Populate the one sample ssbox */
877 void 
878 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
879 {
880   int i;
881
882   assert(ssb->t);
883
884   for (i=0; i < cmd->n_variables; ++i)
885     {
886       struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
887
888       tab_text (ssb->t, 0, i+1, TAB_LEFT, var_get_name (cmd->v_variables[i]));
889       tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 10, 0);
890       tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
891       tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
892       tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
893     }
894   
895 }
896
897
898
899 /* Implementation of the Test Results box struct */
900
901 void trbox_base_init(struct trbox *self,size_t n_vars, int cols);
902 void trbox_base_finalize(struct trbox *trb);
903
904 void trbox_independent_samples_init(struct trbox *trb,
905                                     struct cmd_t_test *cmd );
906
907 void trbox_independent_samples_populate(struct trbox *trb,
908                                         struct cmd_t_test *cmd);
909
910 void trbox_one_sample_init(struct trbox *self,
911                       struct cmd_t_test *cmd );
912
913 void trbox_one_sample_populate(struct trbox *trb,
914                                struct cmd_t_test *cmd);
915
916 void trbox_paired_init(struct trbox *self,
917                        struct cmd_t_test *cmd );
918
919 void trbox_paired_populate(struct trbox *trb,
920                       struct cmd_t_test *cmd);
921
922
923
924 /* Create a trbox according to mode*/
925 void 
926 trbox_create(struct trbox *trb,   
927              struct cmd_t_test *cmd, int mode)
928 {
929     switch (mode) 
930       {
931       case T_1_SAMPLE:
932         trbox_one_sample_init(trb,cmd);
933         break;
934       case T_IND_SAMPLES:
935         trbox_independent_samples_init(trb,cmd);
936         break;
937       case T_PAIRED:
938         trbox_paired_init(trb,cmd);
939         break;
940       default:
941         NOT_REACHED ();
942       }
943 }
944
945 /* Populate a trbox according to cmd */
946 void 
947 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
948 {
949   trb->populate(trb,cmd);
950 }
951
952 /* Submit and destroy a trbox */
953 void 
954 trbox_finalize(struct trbox *trb)
955 {
956   trb->finalize(trb);
957 }
958
959 /* Initialize the independent samples trbox */
960 void 
961 trbox_independent_samples_init(struct trbox *self,
962                            struct cmd_t_test *cmd UNUSED)
963 {
964   const int hsize=11;
965   const int vsize=cmd->n_variables*2+3;
966
967   assert(self);
968   self->populate = trbox_independent_samples_populate;
969
970   trbox_base_init(self,cmd->n_variables*2,hsize);
971   tab_title(self->t,_("Independent Samples Test"));
972   tab_hline(self->t,TAL_1,2,hsize-1,1);
973   tab_vline(self->t,TAL_2,2,0,vsize-1);
974   tab_vline(self->t,TAL_1,4,0,vsize-1);
975   tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
976   tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
977   tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
978   tab_joint_text(self->t, 2, 0, 3, 0, 
979                  TAB_CENTER,_("Levene's Test for Equality of Variances"));
980   tab_joint_text(self->t, 4,0,hsize-1,0,
981                  TAB_CENTER,_("t-test for Equality of Means"));
982
983   tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
984   tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
985   tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
986   tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
987   tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
988   tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
989   tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
990   tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
991   tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
992
993   tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF, 
994                  _("%g%% Confidence Interval of the Difference"),
995                  cmd->criteria*100.0);
996
997 }
998
999 /* Populate the independent samples trbox */
1000 void 
1001 trbox_independent_samples_populate(struct trbox *self,
1002                                    struct cmd_t_test *cmd )
1003 {
1004   int i;
1005
1006   assert(self);
1007   for (i=0; i < cmd->n_variables; ++i)
1008     {
1009       double p,q;
1010
1011       double t;
1012       double df;
1013
1014       double df1, df2;
1015
1016       double pooled_variance;
1017       double std_err_diff;
1018       double mean_diff;
1019
1020       const struct variable *var = cmd->v_variables[i];
1021       struct group_proc *grp_data = group_proc_get (var);
1022
1023       struct hsh_table *grp_hash = grp_data->group_hash;
1024
1025       struct group_statistics *gs0 ;
1026       struct group_statistics *gs1 ;
1027           
1028       union value search_val;
1029           
1030       if ( gp.criterion == CMP_LE ) 
1031         search_val.f = gp.v.critical_value - 1.0;
1032       else
1033         search_val = gp.v.g_value[0];
1034
1035       gs0 = hsh_find(grp_hash, (void *) &search_val);
1036       assert(gs0);
1037
1038       if ( gp.criterion == CMP_LE ) 
1039         search_val.f = gp.v.critical_value + 1.0;
1040       else
1041         search_val = gp.v.g_value[1];
1042
1043       gs1 = hsh_find(grp_hash, (void *) &search_val);
1044       assert(gs1);
1045
1046           
1047       tab_text (self->t, 0, i*2+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1048
1049       tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1050
1051
1052       tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1053
1054       /* Now work out the significance of the Levene test */
1055       df1 = 1; df2 = grp_data->ugs.n - 2;
1056       q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1057
1058       tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1059
1060       df = gs0->n + gs1->n - 2.0 ;
1061       tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 10, 0);
1062
1063       pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1064                           + 
1065                           (gs1->n )*pow2(gs1->s_std_dev) 
1066                         ) / df  ;
1067
1068       t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1069       t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n)); 
1070
1071       tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1072
1073       p = gsl_cdf_tdist_P(t, df);
1074       q = gsl_cdf_tdist_Q(t, df);
1075
1076       tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1077
1078       mean_diff = gs0->mean - gs1->mean;
1079       tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1080
1081
1082       std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1083       tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1084
1085
1086       /* Now work out the confidence interval */
1087       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1088
1089       t = gsl_cdf_tdist_Qinv(q,df);
1090       tab_float(self->t, 9, i*2+3, TAB_RIGHT, 
1091                 mean_diff - t * std_err_diff, 8, 3); 
1092
1093       tab_float(self->t, 10, i*2+3, TAB_RIGHT, 
1094                 mean_diff + t * std_err_diff, 8, 3); 
1095
1096
1097       {
1098         double se2;
1099       /* Now for the \sigma_1 != \sigma_2 case */
1100       tab_text (self->t, 1, i*2+3+1, 
1101                 TAB_LEFT, _("Equal variances not assumed"));
1102
1103
1104       se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1105         (pow2(gs1->s_std_dev)/(gs1->n -1) );
1106
1107       t = mean_diff / sqrt(se2) ;
1108       tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1109                 
1110       df = pow2(se2) / ( 
1111                        (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 )) 
1112                         /(gs0->n -1 )
1113                         )
1114                        + 
1115                        (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1116                         /(gs1->n -1 )
1117                         )
1118                        ) ;
1119       tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1120
1121       p = gsl_cdf_tdist_P(t, df);
1122       q = gsl_cdf_tdist_Q(t, df);
1123
1124       tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1125
1126       /* Now work out the confidence interval */
1127       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1128
1129       t = gsl_cdf_tdist_Qinv(q, df);
1130
1131       tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1132
1133
1134       tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1135
1136
1137       tab_float(self->t, 9, i*2+3+1, TAB_RIGHT, 
1138                 mean_diff - t * std_err_diff, 8, 3); 
1139
1140       tab_float(self->t, 10, i*2+3+1, TAB_RIGHT, 
1141                 mean_diff + t * std_err_diff, 8, 3); 
1142
1143       }
1144     }
1145 }
1146
1147 /* Initialize the paired samples trbox */
1148 void 
1149 trbox_paired_init(struct trbox *self,
1150                            struct cmd_t_test *cmd UNUSED)
1151 {
1152
1153   const int hsize=10;
1154   const int vsize=n_pairs+3;
1155
1156   self->populate = trbox_paired_populate;
1157
1158   trbox_base_init(self,n_pairs,hsize);
1159   tab_title (self->t, _("Paired Samples Test"));
1160   tab_hline(self->t,TAL_1,2,6,1);
1161   tab_vline(self->t,TAL_2,2,0,vsize - 1);
1162   tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1163   tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1164   tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1165   tab_hline(self->t,TAL_1,5,6, 2);
1166   tab_vline(self->t,TAL_GAP,6,0,1);
1167
1168   tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF, 
1169                  _("%g%% Confidence Interval of the Difference"),
1170                  cmd->criteria*100.0);
1171
1172   tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1173   tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1174   tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1175   tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1176   tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1177   tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1178   tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1179   tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1180 }
1181
1182 /* Populate the paired samples trbox */
1183 void 
1184 trbox_paired_populate(struct trbox *trb,
1185                               struct cmd_t_test *cmd UNUSED)
1186 {
1187   int i;
1188
1189   for (i=0; i < n_pairs; ++i)
1190     {
1191       double p,q;
1192       double se_mean;
1193
1194       double n = pairs[i].n;
1195       double t;
1196       double df = n - 1;
1197       
1198       tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i); 
1199
1200       tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1201                 var_get_name (pairs[i].v[0]),
1202                 var_get_name (pairs[i].v[1]));
1203
1204       tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1205
1206       tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1207
1208       /* SE Mean */
1209       se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1210       tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1211
1212       /* Now work out the confidence interval */
1213       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1214
1215       t = gsl_cdf_tdist_Qinv(q, df);
1216
1217       tab_float(trb->t, 5, i+3, TAB_RIGHT, 
1218                 pairs[i].mean_diff - t * se_mean , 8, 4); 
1219
1220       tab_float(trb->t, 6, i+3, TAB_RIGHT, 
1221                 pairs[i].mean_diff + t * se_mean , 8, 4); 
1222
1223       t = (pairs[i].mean[0] - pairs[i].mean[1])
1224         / sqrt (
1225                 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1226                   2 * pairs[i].correlation * 
1227                   pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1228                 / (n - 1)
1229                 );
1230
1231       tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1232
1233       /* Degrees of freedom */
1234       tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 10, 0 );
1235
1236       p = gsl_cdf_tdist_P(t,df);
1237       q = gsl_cdf_tdist_P(t,df);
1238
1239       tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1240
1241     }
1242 }
1243
1244 /* Initialize the one sample trbox */
1245 void 
1246 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1247 {
1248   const int hsize=7;
1249   const int vsize=cmd->n_variables+3;
1250
1251   self->populate = trbox_one_sample_populate;
1252
1253   trbox_base_init(self, cmd->n_variables,hsize);
1254   tab_title (self->t, _("One-Sample Test"));
1255   tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1256   tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1257
1258   tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF, 
1259                  _("Test Value = %f"), cmd->n_testval[0]);
1260
1261   tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1262
1263
1264   tab_joint_text(self->t,5,1,6,1,TAB_CENTER  | TAT_PRINTF, 
1265                  _("%g%% Confidence Interval of the Difference"),
1266                  cmd->criteria*100.0);
1267
1268   tab_vline(self->t,TAL_GAP,6,1,1);
1269   tab_hline(self->t,TAL_1,5,6,2);
1270   tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1271   tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1272   tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1273   tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1274   tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1275   tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1276
1277 }
1278
1279
1280 /* Populate the one sample trbox */
1281 void 
1282 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1283 {
1284   int i;
1285
1286   assert(trb->t);
1287
1288   for (i=0; i < cmd->n_variables; ++i)
1289     {
1290       double t;
1291       double p,q;
1292       double df;
1293       struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1294
1295
1296       tab_text (trb->t, 0, i+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1297
1298       t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1299
1300       tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1301
1302       /* degrees of freedom */
1303       df = gs->n - 1;
1304
1305       tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1306
1307       p = gsl_cdf_tdist_P(t, df);
1308       q = gsl_cdf_tdist_Q(t, df);
1309
1310       /* Multiply by 2 to get 2-tailed significance, makeing sure we've got 
1311          the correct tail*/
1312       tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1313
1314       tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1315
1316
1317       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1318       t = gsl_cdf_tdist_Qinv(q, df);
1319
1320       tab_float (trb->t, 5, i+3, TAB_RIGHT,
1321                  gs->mean_diff - t * gs->se_mean, 8,4);
1322
1323       tab_float (trb->t, 6, i+3, TAB_RIGHT,
1324                  gs->mean_diff + t * gs->se_mean, 8,4);
1325     }
1326 }
1327
1328 /* Base initializer for the generalized trbox */
1329 void 
1330 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1331 {
1332   const size_t rows = 3 + data_rows;
1333
1334   self->finalize = trbox_base_finalize;
1335   self->t = tab_create (cols, rows, 0);
1336   tab_headers (self->t,0,0,3,0); 
1337   tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1338   tab_hline(self->t, TAL_2,0,cols-1,3);
1339   tab_dim (self->t, tab_natural_dimensions);
1340 }
1341
1342
1343 /* Base finalizer for the trbox */
1344 void 
1345 trbox_base_finalize(struct trbox *trb)
1346 {
1347   tab_submit(trb->t);
1348 }
1349
1350
1351 /* Create , populate and submit the Paired Samples Correlation box */
1352 void
1353 pscbox(void)
1354 {
1355   const int rows=1+n_pairs;
1356   const int cols=5;
1357   int i;
1358   
1359   struct tab_table *table;
1360   
1361   table = tab_create (cols,rows,0);
1362
1363   tab_columns (table, SOM_COL_DOWN, 1);
1364   tab_headers (table,0,0,1,0); 
1365   tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1366   tab_hline(table, TAL_2, 0, cols - 1, 1);
1367   tab_vline(table, TAL_2, 2, 0, rows - 1);
1368   tab_dim(table, tab_natural_dimensions);
1369   tab_title(table, _("Paired Samples Correlations"));
1370
1371   /* column headings */
1372   tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1373   tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1374   tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1375
1376   for (i=0; i < n_pairs; ++i)
1377     {
1378       double p,q;
1379
1380       double df = pairs[i].n -2;
1381
1382       double correlation_t = 
1383         pairs[i].correlation * sqrt(df) /
1384         sqrt(1 - pow2(pairs[i].correlation));
1385
1386
1387       /* row headings */
1388       tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF, 
1389                _("Pair %d"), i);
1390       
1391       tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF, 
1392                _("%s & %s"),
1393                var_get_name (pairs[i].v[0]),
1394                var_get_name (pairs[i].v[1]));
1395
1396
1397       /* row data */
1398       tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1399       tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1400
1401       p = gsl_cdf_tdist_P(correlation_t, df);
1402       q = gsl_cdf_tdist_Q(correlation_t, df);
1403
1404       tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1405     }
1406
1407   tab_submit(table);
1408 }
1409
1410
1411
1412
1413 /* Calculation Implementation */
1414
1415 /* Per case calculations common to all variants of the T test */
1416 static int 
1417 common_calc (const struct dictionary *dict, 
1418              const struct ccase *c, 
1419              void *_cmd, 
1420              enum mv_class exclude)
1421 {
1422   int i;
1423   struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;  
1424
1425   double weight = dict_get_case_weight (dict, c, NULL);
1426
1427
1428   /* Listwise has to be implicit if the independent variable is missing ?? */
1429   if ( cmd->sbc_groups )
1430     {
1431       if (var_is_value_missing (indep_var, case_data (c, indep_var), exclude))
1432         return 0;
1433     }
1434
1435   for(i = 0; i < cmd->n_variables ; ++i) 
1436     {
1437       const struct variable *v = cmd->v_variables[i];
1438       const union value *val = case_data (c, v);
1439       
1440       if (!var_is_value_missing (v, val, exclude))
1441         {
1442           struct group_statistics *gs;
1443           gs = &group_proc_get (v)->ugs;
1444
1445           gs->n += weight;
1446           gs->sum += weight * val->f;
1447           gs->ssq += weight * val->f * val->f;
1448         }
1449     }
1450   return 0;
1451 }
1452
1453 /* Pre calculations common to all variants of the T test */
1454 static void 
1455 common_precalc ( struct cmd_t_test *cmd )
1456 {
1457   int i=0;
1458
1459   for(i=0; i< cmd->n_variables ; ++i) 
1460     {
1461       struct group_statistics *gs;
1462       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1463       
1464       gs->sum=0;
1465       gs->n=0;
1466       gs->ssq=0;
1467       gs->sum_diff=0;
1468     }
1469 }
1470
1471 /* Post calculations common to all variants of the T test */
1472 void 
1473 common_postcalc (struct cmd_t_test *cmd)
1474 {
1475   int i=0;
1476
1477   for(i=0; i< cmd->n_variables ; ++i) 
1478     {
1479       struct group_statistics *gs;
1480       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1481       
1482       gs->mean=gs->sum / gs->n;
1483       gs->s_std_dev= sqrt(
1484                          ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1485                          ) ;
1486
1487       gs->std_dev= sqrt(
1488                          gs->n/(gs->n-1) *
1489                          ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1490                          ) ;
1491
1492       gs->se_mean = gs->std_dev / sqrt(gs->n);
1493       gs->mean_diff= gs->sum_diff / gs->n;
1494     }
1495 }
1496
1497 /* Per case calculations for one sample t test  */
1498 static int 
1499 one_sample_calc (const struct dictionary *dict, 
1500                  const struct ccase *c, void *cmd_, 
1501                  enum mv_class exclude)
1502 {
1503   int i;
1504
1505   struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1506
1507   double weight = dict_get_case_weight (dict, c, NULL);
1508
1509
1510   for(i=0; i< cmd->n_variables ; ++i) 
1511     {
1512       struct group_statistics *gs;
1513       const struct variable *v = cmd->v_variables[i];
1514       const union value *val = case_data (c, v);
1515
1516       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1517
1518       if (!var_is_value_missing (v, val, exclude))
1519         gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1520     }
1521
1522   return 0;
1523 }
1524
1525 /* Pre calculations for one sample t test */
1526 static void 
1527 one_sample_precalc ( struct cmd_t_test *cmd )
1528 {
1529   int i=0; 
1530  
1531   for(i=0; i< cmd->n_variables ; ++i) 
1532     {
1533       struct group_statistics *gs;
1534       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1535       
1536       gs->sum_diff=0;
1537     }
1538 }
1539
1540 /* Post calculations for one sample t test */
1541 static void 
1542 one_sample_postcalc (struct cmd_t_test *cmd)
1543 {
1544   int i=0;
1545   
1546   for(i=0; i< cmd->n_variables ; ++i) 
1547     {
1548       struct group_statistics *gs;
1549       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1550
1551       gs->mean_diff = gs->sum_diff / gs->n ;
1552     }
1553 }
1554
1555
1556
1557 static void 
1558 paired_precalc (struct cmd_t_test *cmd UNUSED)
1559 {
1560   int i;
1561
1562   for(i=0; i < n_pairs ; ++i )
1563     {
1564       pairs[i].n = 0;
1565       pairs[i].sum[0] = 0;      pairs[i].sum[1] = 0;
1566       pairs[i].ssq[0] = 0;      pairs[i].ssq[1] = 0;
1567       pairs[i].sum_of_prod = 0;
1568       pairs[i].correlation = 0;
1569       pairs[i].sum_of_diffs = 0;
1570       pairs[i].ssq_diffs = 0;
1571     }
1572
1573 }
1574
1575
1576 static int  
1577 paired_calc (const struct dictionary *dict, const struct ccase *c, 
1578              struct cmd_t_test *cmd UNUSED, enum mv_class exclude)
1579 {
1580   int i;
1581
1582   double weight = dict_get_case_weight (dict, c, NULL);
1583
1584   for(i=0; i < n_pairs ; ++i )
1585     {
1586       const struct variable *v0 = pairs[i].v[0];
1587       const struct variable *v1 = pairs[i].v[1];
1588
1589       const union value *val0 = case_data (c, v0);
1590       const union value *val1 = case_data (c, v1);
1591
1592       if (!var_is_value_missing (v0, val0, exclude) &&
1593           !var_is_value_missing (v1, val1, exclude))
1594         {
1595           pairs[i].n += weight;
1596           pairs[i].sum[0] += weight * val0->f;
1597           pairs[i].sum[1] += weight * val1->f;
1598
1599           pairs[i].ssq[0] += weight * pow2(val0->f);
1600           pairs[i].ssq[1] += weight * pow2(val1->f);
1601
1602           pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1603
1604           pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1605           pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1606         }
1607     }
1608
1609   return 0;
1610 }
1611
1612 static void 
1613 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1614 {
1615   int i;
1616
1617   for(i=0; i < n_pairs ; ++i )
1618     {
1619       int j;
1620       const double n = pairs[i].n;
1621
1622       for (j=0; j < 2 ; ++j) 
1623         {
1624           pairs[i].mean[j] = pairs[i].sum[j] / n ;
1625           pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n - 
1626                                               pow2(pairs[i].mean[j]))
1627                                      );
1628
1629           pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n - 
1630                                               pow2(pairs[i].mean[j]))
1631                                      );
1632         }
1633       
1634       pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n - 
1635         pairs[i].mean[0] * pairs[i].mean[1] ;
1636       /* correlation now actually contains the covariance */
1637       
1638       pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1639       pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1640       
1641       pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1642
1643       pairs[i].std_dev_diff = sqrt (  n / (n - 1) * (
1644                                     ( pairs[i].ssq_diffs / n )
1645                                     - 
1646                                     pow2(pairs[i].mean_diff )
1647                                     ) );
1648     }
1649 }
1650
1651 static void 
1652 group_precalc (struct cmd_t_test *cmd )
1653 {
1654   int i;
1655   int j;
1656
1657   for(i=0; i< cmd->n_variables ; ++i) 
1658     {
1659       struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1660
1661       /* There's always 2 groups for a T - TEST */
1662       ttpr->n_groups = 2;
1663
1664       gp.indep_width = var_get_width (indep_var);
1665       
1666       ttpr->group_hash = hsh_create(2, 
1667                                     (hsh_compare_func *) compare_group_binary,
1668                                     (hsh_hash_func *) hash_group_binary,
1669                                     (hsh_free_func *) free_group,
1670                                     (void *) &gp );
1671
1672       for (j=0 ; j < 2 ; ++j)
1673         {
1674
1675           struct group_statistics *gs = xmalloc (sizeof *gs);
1676
1677           gs->sum = 0;
1678           gs->n = 0;
1679           gs->ssq = 0;
1680         
1681           if ( gp.criterion == CMP_EQ ) 
1682             {
1683               gs->id = gp.v.g_value[j];
1684             }
1685           else
1686             {
1687               if ( j == 0 ) 
1688                 gs->id.f = gp.v.critical_value - 1.0 ;
1689               else
1690                 gs->id.f = gp.v.critical_value + 1.0 ;
1691             }
1692           
1693           hsh_insert ( ttpr->group_hash, (void *) gs );
1694
1695         }
1696     }
1697
1698 }
1699
1700 static int  
1701 group_calc (const struct dictionary *dict, 
1702             const struct ccase *c, struct cmd_t_test *cmd, 
1703             enum mv_class exclude)
1704 {
1705   int i;
1706
1707   const double weight = dict_get_case_weight (dict, c, NULL);
1708
1709   const union value *gv;
1710
1711   if (var_is_value_missing (indep_var, case_data (c, indep_var), exclude))
1712     return 0;
1713
1714   gv = case_data (c, indep_var);
1715
1716   for(i=0; i< cmd->n_variables ; ++i) 
1717     {
1718       const struct variable *var = cmd->v_variables[i];
1719       const union value *val = case_data (c, var);
1720       struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1721       struct group_statistics *gs;
1722
1723       gs = hsh_find(grp_hash, (void *) gv);
1724
1725       /* If the independent variable doesn't match either of the values 
1726          for this case then move on to the next case */
1727       if ( ! gs ) 
1728         return 0;
1729
1730       if (!var_is_value_missing (var, val, exclude))
1731         {
1732           gs->n += weight;
1733           gs->sum += weight * val->f;
1734           gs->ssq += weight * pow2(val->f);
1735         }
1736     }
1737
1738   return 0;
1739 }
1740
1741
1742 static void 
1743 group_postcalc ( struct cmd_t_test *cmd )
1744 {
1745   int i;
1746
1747   for (i = 0; i < cmd->n_variables ; ++i) 
1748     {
1749       const struct variable *var = cmd->v_variables[i];
1750       struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1751       struct hsh_iterator g;
1752       struct group_statistics *gs;
1753       int count=0;
1754
1755       for (gs =  hsh_first (grp_hash,&g); 
1756            gs != 0; 
1757            gs = hsh_next(grp_hash,&g))
1758         {
1759           gs->mean = gs->sum / gs->n;
1760           
1761           gs->s_std_dev= sqrt(
1762                               ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1763                               ) ;
1764
1765           gs->std_dev= sqrt(
1766                             gs->n/(gs->n-1) *
1767                             ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1768                             ) ;
1769           
1770           gs->se_mean = gs->std_dev / sqrt(gs->n);
1771           count ++;
1772         }
1773       assert(count == 2);
1774     }
1775 }
1776
1777
1778
1779 static void
1780 calculate(struct cmd_t_test *cmd,
1781           struct casereader *input, const struct dataset *ds)
1782 {
1783   const struct dictionary *dict = dataset_dict (ds);
1784   struct ssbox stat_summary_box;
1785   struct trbox test_results_box;
1786
1787   struct casereader *pass1, *pass2, *pass3;
1788   struct taint *taint;
1789   struct ccase c;
1790
1791   enum mv_class exclude = cmd->miss != TTS_INCLUDE ? MV_ANY : MV_SYSTEM;
1792
1793   if (!casereader_peek (input, 0, &c))
1794     return;
1795   output_split_file_values (ds, &c);
1796   case_destroy (&c);
1797
1798   if ( cmd->miss == TTS_LISTWISE ) 
1799     input = casereader_create_filter_missing (input,
1800                                               cmd->v_variables,
1801                                               cmd->n_variables,
1802                                               exclude, NULL);
1803
1804   input = casereader_create_filter_weight (input, dict, NULL, NULL);
1805
1806   taint = taint_clone (casereader_get_taint (input));
1807   casereader_split (input, &pass1, &pass2);
1808                                 
1809   common_precalc (cmd);
1810   for (; casereader_read (pass1, &c); case_destroy (&c)) 
1811     common_calc (dict, &c, cmd, exclude);
1812   casereader_destroy (pass1);
1813   common_postcalc (cmd);
1814
1815   switch(mode)
1816     {
1817     case T_1_SAMPLE:
1818       one_sample_precalc (cmd);
1819       for (; casereader_read (pass2, &c); case_destroy (&c)) 
1820         one_sample_calc (dict, &c, cmd, exclude);
1821       one_sample_postcalc (cmd);
1822       break;
1823     case T_PAIRED:
1824       paired_precalc(cmd);
1825       for (; casereader_read (pass2, &c); case_destroy (&c)) 
1826         paired_calc (dict, &c, cmd, exclude);
1827       paired_postcalc (cmd);
1828       break;
1829     case T_IND_SAMPLES:
1830       pass3 = casereader_clone (pass2);
1831
1832       group_precalc(cmd);
1833       for(; casereader_read (pass2, &c); case_destroy (&c)) 
1834         group_calc (dict, &c, cmd, exclude);
1835       group_postcalc(cmd);
1836
1837       levene (dict, pass3, indep_var, cmd->n_variables, cmd->v_variables,
1838               exclude);
1839       break;
1840     }
1841   casereader_destroy (pass2);
1842  
1843   if (!taint_has_tainted_successor (taint)) 
1844     {
1845       ssbox_create(&stat_summary_box,cmd,mode);
1846       ssbox_populate(&stat_summary_box,cmd);
1847       ssbox_finalize(&stat_summary_box);
1848
1849       if ( mode == T_PAIRED ) 
1850         pscbox();
1851   
1852       trbox_create(&test_results_box,cmd,mode);
1853       trbox_populate(&test_results_box,cmd);
1854       trbox_finalize(&test_results_box);
1855     }
1856 }
1857
1858 short which_group(const struct group_statistics *g,
1859                   const struct group_properties *p);
1860
1861 /* Return -1 if the id of a is less than b; +1 if greater than and 
1862    0 if equal */
1863 static int 
1864 compare_group_binary(const struct group_statistics *a, 
1865                      const struct group_statistics *b, 
1866                      const struct group_properties *p)
1867 {
1868   short flag_a;
1869   short flag_b;
1870   
1871   if ( p->criterion == CMP_LE ) 
1872     {
1873       /* less-than-or-equal comparision is not meaningfull for
1874          alpha variables, so we shouldn't ever arrive here */
1875       assert(p->indep_width == 0 ) ;
1876       
1877       flag_a = ( a->id.f < p->v.critical_value ) ;
1878       flag_b = ( b->id.f < p->v.critical_value ) ;
1879     }
1880   else
1881     {
1882       flag_a = which_group(a, p);
1883       flag_b = which_group(b, p);
1884     }
1885
1886   if (flag_a < flag_b ) 
1887     return -1;
1888
1889   return (flag_a > flag_b);
1890 }
1891
1892 /* This is a degenerate case of a hash, since it can only return three possible
1893    values.  It's really a comparison, being used as a hash function */
1894
1895 static unsigned 
1896 hash_group_binary(const struct group_statistics *g, 
1897                   const struct group_properties *p)
1898 {
1899   short flag = -1;
1900
1901   if ( p->criterion == CMP_LE ) 
1902     {
1903       /* Not meaningfull to do a less than compare for alpha values ? */
1904       assert(p->indep_width == 0 ) ;
1905       flag = ( g->id.f < p->v.critical_value ) ; 
1906     }
1907   else if ( p->criterion == CMP_EQ) 
1908     {
1909       flag = which_group(g,p);
1910     }
1911   else
1912     NOT_REACHED ();
1913
1914   return flag;
1915 }
1916
1917 /* return 0 if G belongs to group 0, 
1918           1 if it belongs to group 1,
1919           2 if it belongs to neither group */
1920 short
1921 which_group(const struct group_statistics *g,
1922             const struct group_properties *p)
1923 {
1924  
1925   if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1926     return 0;
1927
1928   if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))
1929     return 1;
1930
1931   return 2;
1932 }
1933