Encapsulated the static data of procedure.[ch] into a single object, to be
[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    Written by John Williams <johnr.williams@stonebow.otago.ac.nz>.
5    Almost completly re-written by John Darrington 2004
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA. */
21
22 #include <config.h>
23
24 #include <gsl/gsl_cdf.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <data/case.h>
30 #include <data/casefile.h>
31 #include <data/dictionary.h>
32 #include <data/procedure.h>
33 #include <data/value-labels.h>
34 #include <data/variable.h>
35 #include <language/command.h>
36 #include <language/dictionary/split-file.h>
37 #include <language/lexer/lexer.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/hash.h>
42 #include <libpspp/magic.h>
43 #include <libpspp/message.h>
44 #include <libpspp/message.h>
45 #include <libpspp/misc.h>
46 #include <libpspp/str.h>
47 #include <math/group-proc.h>
48 #include <math/levene.h>
49 #include <output/manager.h>
50 #include <output/table.h>
51
52 #include "size_max.h"
53
54 #include "gettext.h"
55 #define _(msgid) gettext (msgid)
56
57 /* (headers) */
58
59 /* (specification)
60    "T-TEST" (tts_):
61      +groups=custom;
62      testval=double;
63      +variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
64      +pairs=custom;
65      missing=miss:!analysis/listwise,
66             incl:include/!exclude;
67      +format=fmt:!labels/nolabels;
68      criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
69 */
70 /* (declarations) */
71 /* (functions) */
72
73
74
75
76 /* Function to use for testing for missing values */
77 static is_missing_func *value_is_missing;
78
79 /* Variable for the GROUPS subcommand, if given. */
80 static struct variable *indep_var;
81
82 enum comparison
83   {
84     CMP_LE = -2,
85     CMP_EQ = 0,
86   };
87
88 struct group_properties
89 {
90   /* The comparison criterion */
91   enum comparison criterion;
92
93   /* The width of the independent variable */
94   int indep_width ;  
95
96   union {
97     /* The value of the independent variable at which groups are determined to 
98        belong to one group or the other */
99     double critical_value;
100     
101
102     /* The values of the independent variable for each group */
103     union value g_value[2];
104   } v ;
105
106 };
107
108
109 static struct group_properties gp ;
110
111
112
113 /* PAIRS: Number of pairs to be compared ; each pair. */
114 static int n_pairs = 0 ;
115 struct pair 
116 {
117   /* The variables comprising the pair */
118   struct variable *v[2];
119
120   /* The number of valid variable pairs */
121   double n;
122
123   /* The sum of the members */
124   double sum[2];
125
126   /* sum of squares of the members */
127   double ssq[2];
128
129   /* Std deviation of the members */
130   double std_dev[2];
131
132
133   /* Sample Std deviation of the members */
134   double s_std_dev[2];
135
136   /* The means of the members */
137   double mean[2];
138
139   /* The correlation coefficient between the variables */
140   double correlation;
141
142   /* The sum of the differences */
143   double sum_of_diffs;
144
145   /* The sum of the products */
146   double sum_of_prod;
147
148   /* The mean of the differences */
149   double mean_diff;
150
151   /* The sum of the squares of the differences */
152   double ssq_diffs;
153
154   /* The std deviation of the differences */
155   double std_dev_diff;
156 };
157
158 static struct pair *pairs=0;
159
160 static int parse_value (union value * v, int type) ;
161
162 /* Structures and Functions for the Statistics Summary Box */
163 struct ssbox;
164 typedef void populate_ssbox_func(struct ssbox *ssb,
165                                             struct cmd_t_test *cmd);
166 typedef void finalize_ssbox_func(struct ssbox *ssb);
167
168 struct ssbox
169 {
170   struct tab_table *t;
171
172   populate_ssbox_func *populate;
173   finalize_ssbox_func *finalize;
174
175 };
176
177 /* Create a ssbox */
178 void ssbox_create(struct ssbox *ssb,   struct cmd_t_test *cmd, int mode);
179
180 /* Populate a ssbox according to cmd */
181 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
182
183 /* Submit and destroy a ssbox */
184 void ssbox_finalize(struct ssbox *ssb);
185
186 /* A function to create, populate and submit the Paired Samples Correlation 
187    box */
188 void pscbox(void);
189
190
191 /* Structures and Functions for the Test Results Box */
192 struct trbox;
193
194 typedef void populate_trbox_func(struct trbox *trb,
195                                  struct cmd_t_test *cmd);
196 typedef void finalize_trbox_func(struct trbox *trb);
197
198 struct trbox {
199   struct tab_table *t;
200   populate_trbox_func *populate;
201   finalize_trbox_func *finalize;
202 };
203
204 /* Create a trbox */
205 void trbox_create(struct trbox *trb,   struct cmd_t_test *cmd, int mode);
206
207 /* Populate a ssbox according to cmd */
208 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
209
210 /* Submit and destroy a ssbox */
211 void trbox_finalize(struct trbox *trb);
212
213 /* Which mode was T-TEST invoked */
214 enum {
215   T_1_SAMPLE = 0 ,
216   T_IND_SAMPLES, 
217   T_PAIRED
218 };
219
220
221 static int common_calc (const struct ccase *, void *);
222 static void common_precalc (struct cmd_t_test *);
223 static void common_postcalc (struct cmd_t_test *);
224
225 static int one_sample_calc (const struct ccase *, void *);
226 static void one_sample_precalc (struct cmd_t_test *);
227 static void one_sample_postcalc (struct cmd_t_test *);
228
229 static int  paired_calc (const struct ccase *, void *);
230 static void paired_precalc (struct cmd_t_test *);
231 static void paired_postcalc (struct cmd_t_test *);
232
233 static void group_precalc (struct cmd_t_test *);
234 static int  group_calc (const struct ccase *, struct cmd_t_test *);
235 static void group_postcalc (struct cmd_t_test *);
236
237
238 static bool calculate(const struct ccase *first,
239                       const struct casefile *cf, void *_mode);
240
241 static  int mode;
242
243 static struct cmd_t_test cmd;
244
245 static bool bad_weight_warn = false;
246
247
248 static int compare_group_binary(const struct group_statistics *a, 
249                                 const struct group_statistics *b, 
250                                 const struct group_properties *p);
251
252
253 static unsigned  hash_group_binary(const struct group_statistics *g, 
254                                    const struct group_properties *p);
255
256
257
258 int
259 cmd_t_test(void)
260 {
261   bool ok;
262   
263   if ( !parse_t_test(&cmd, NULL) )
264     return CMD_FAILURE;
265
266   if (! cmd.sbc_criteria)
267     cmd.criteria=0.95;
268
269   {
270     int m=0;
271     if (cmd.sbc_testval) ++m;
272     if (cmd.sbc_groups) ++m;
273     if (cmd.sbc_pairs) ++m;
274
275     if ( m != 1)
276       {
277         msg(SE, 
278             _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
279             );
280         free_t_test(&cmd);
281         return CMD_FAILURE;
282       }
283   }
284
285   if (cmd.sbc_testval) 
286     mode=T_1_SAMPLE;
287   else if (cmd.sbc_groups)
288     mode=T_IND_SAMPLES;
289   else
290     mode=T_PAIRED;
291
292   if ( mode == T_PAIRED) 
293     {
294       if (cmd.sbc_variables) 
295         {
296           msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
297           free_t_test(&cmd);
298           return CMD_FAILURE;
299         }
300       else
301         {
302           /* Iterate through the pairs and put each variable that is a 
303              member of a pair into cmd.v_variables */
304
305           int i;
306           struct hsh_iterator hi;
307           struct hsh_table *hash;
308           struct variable *v;
309
310           hash = hsh_create (n_pairs, compare_var_names, hash_var_name, 0, 0);
311
312           for (i=0; i < n_pairs; ++i)
313             {
314               hsh_insert(hash,pairs[i].v[0]);
315               hsh_insert(hash,pairs[i].v[1]);
316             }
317
318           assert(cmd.n_variables == 0);
319           cmd.n_variables = hsh_count(hash);
320
321           cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
322                                        sizeof *cmd.v_variables);
323           /* Iterate through the hash */
324           for (i=0,v = (struct variable *) hsh_first(hash,&hi);
325                v != 0;
326                v=hsh_next(hash,&hi) ) 
327             cmd.v_variables[i++]=v;
328
329           hsh_destroy(hash);
330         }
331     }
332   else if ( !cmd.sbc_variables) 
333     {
334       msg(SE, _("One or more VARIABLES must be specified."));
335       free_t_test(&cmd);
336       return CMD_FAILURE;
337     }
338
339
340   /* If /MISSING=INCLUDE is set, then user missing values are ignored */
341   if (cmd.incl == TTS_INCLUDE ) 
342     value_is_missing = mv_is_value_system_missing;
343   else
344     value_is_missing = mv_is_value_missing;
345
346   bad_weight_warn = true;
347
348   ok = multipass_procedure_with_splits (current_dataset, calculate, &cmd);
349
350   n_pairs=0;
351   free(pairs);
352   pairs=0;
353
354   if ( mode == T_IND_SAMPLES) 
355     {
356       int v;
357       /* Destroy any group statistics we created */
358       for (v = 0 ; v < cmd.n_variables ; ++v ) 
359         {
360           struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
361           hsh_destroy (grpp->group_hash);
362         }
363     }
364     
365   free_t_test(&cmd);
366   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
367 }
368
369 static int
370 tts_custom_groups (struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
371 {
372   int n_group_values=0;
373
374   lex_match('=');
375
376   indep_var = parse_variable ();
377   if (!indep_var)
378     {
379       lex_error ("expecting variable name in GROUPS subcommand");
380       return 0;
381     }
382
383   if (indep_var->type == T_STRING && indep_var->width > MAX_SHORT_STRING)
384     {
385       msg (SE, _("Long string variable %s is not valid here."),
386            indep_var->name);
387       return 0;
388     }
389
390   if (!lex_match ('('))
391     {
392       if (indep_var->type == NUMERIC)
393         {
394           gp.v.g_value[0].f = 1;
395           gp.v.g_value[1].f = 2;
396
397           gp.criterion = CMP_EQ;
398           
399           n_group_values = 2;
400
401           return 1;
402         }
403       else
404         {
405           msg (SE, _("When applying GROUPS to a string variable, two "
406                      "values must be specified."));
407           return 0;
408         }
409     }
410
411   if (!parse_value (&gp.v.g_value[0], indep_var->type))
412       return 0;
413
414   lex_match (',');
415   if (lex_match (')'))
416     {
417       if (indep_var->type != NUMERIC)
418         {
419
420           msg (SE, _("When applying GROUPS to a string variable, two "
421                      "values must be specified."));
422           return 0;
423         }
424       gp.criterion = CMP_LE;
425       gp.v.critical_value = gp.v.g_value[0].f;
426
427       n_group_values = 1;
428       return 1;
429     }
430
431   if (!parse_value (&gp.v.g_value[1], indep_var->type))
432     return 0;
433
434   n_group_values = 2;
435   if (!lex_force_match (')'))
436     return 0;
437
438   if ( n_group_values == 2 ) 
439     gp.criterion = CMP_EQ ;
440   else
441     gp.criterion = CMP_LE ;
442
443
444   return 1;
445 }
446
447
448 static int
449 tts_custom_pairs (struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
450 {
451   struct variable **vars;
452   size_t n_vars;
453   size_t n_pairs_local;
454
455   size_t n_before_WITH;
456   size_t n_after_WITH = SIZE_MAX;
457   int paired ; /* Was the PAIRED keyword given ? */
458
459   lex_match('=');
460
461   n_vars=0;
462   if (!parse_variables (dataset_dict (current_dataset), &vars, &n_vars,
463                         PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
464     {
465       free (vars);
466       return 0;
467     }
468   assert (n_vars);
469
470   n_before_WITH = 0;
471   if (lex_match (T_WITH))
472     {
473       n_before_WITH = n_vars;
474       if (!parse_variables (dataset_dict (current_dataset), &vars, &n_vars,
475                             PV_DUPLICATE | PV_APPEND
476                             | PV_NUMERIC | PV_NO_SCRATCH))
477         {
478           free (vars);
479           return 0;
480         }
481       n_after_WITH = n_vars - n_before_WITH;
482     }
483
484   paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
485
486   /* Determine the number of pairs needed */
487   if (paired)
488     {
489       if (n_before_WITH != n_after_WITH)
490         {
491           free (vars);
492           msg (SE, _("PAIRED was specified but the number of variables "
493                      "preceding WITH (%d) did not match the number "
494                      "following (%d)."),
495                n_before_WITH, n_after_WITH );
496           return 0;
497         }
498       n_pairs_local = n_before_WITH;
499     }
500   else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
501     {
502       n_pairs_local = n_before_WITH * n_after_WITH ;
503     }
504   else /* Neither WITH nor PAIRED keyword given */
505     {
506       if (n_vars < 2)
507         {
508           free (vars);
509           msg (SE, _("At least two variables must be specified "
510                      "on PAIRS."));
511           return 0;
512         }
513
514       /* how many ways can you pick 2 from n_vars ? */
515       n_pairs_local = n_vars * (n_vars - 1) / 2;
516     }
517
518
519   /* Allocate storage for the pairs */
520   pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
521
522   /* Populate the pairs with the appropriate variables */
523   if ( paired ) 
524     {
525       int i;
526
527       assert(n_pairs_local == n_vars / 2);
528       for (i = 0; i < n_pairs_local; ++i)
529         {
530           pairs[i].v[n_pairs] = vars[i];
531           pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
532         }
533     }
534   else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
535     {
536       int i,j;
537       size_t p = n_pairs;
538
539       for(i=0 ; i < n_before_WITH ; ++i ) 
540         {
541           for(j=0 ; j < n_after_WITH ; ++j)
542             {
543               pairs[p].v[0] = vars[i];
544               pairs[p].v[1] = vars[j+n_before_WITH];
545               ++p;
546             }
547         }
548     }
549   else /* Neither WITH nor PAIRED given */
550     {
551       size_t i,j;
552       size_t p=n_pairs;
553       
554       for(i=0 ; i < n_vars ; ++i ) 
555         {
556           for(j=i+1 ; j < n_vars ; ++j)
557             {
558               pairs[p].v[0] = vars[i];
559               pairs[p].v[1] = vars[j];
560               ++p;
561             }
562         }
563     }
564
565   n_pairs+=n_pairs_local;
566
567   free (vars);
568   return 1;
569 }
570
571 /* Parses the current token (numeric or string, depending on type)
572     value v and returns success. */
573 static int
574 parse_value (union value * v, int type )
575 {
576   if (type == NUMERIC)
577     {
578       if (!lex_force_num ())
579         return 0;
580       v->f = tokval;
581     }
582   else
583     {
584       if (!lex_force_string ())
585         return 0;
586       strncpy (v->s, ds_cstr (&tokstr), ds_length (&tokstr));
587     }
588
589   lex_get ();
590
591   return 1;
592 }
593
594
595 /* Implementation of the SSBOX object */
596
597 void ssbox_base_init(struct ssbox *this, int cols,int rows);
598
599 void ssbox_base_finalize(struct ssbox *ssb);
600
601 void ssbox_one_sample_init(struct ssbox *this, 
602                            struct cmd_t_test *cmd );
603
604 void ssbox_independent_samples_init(struct ssbox *this,
605                                     struct cmd_t_test *cmd);
606
607 void ssbox_paired_init(struct ssbox *this,
608                            struct cmd_t_test *cmd);
609
610
611 /* Factory to create an ssbox */
612 void 
613 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
614 {
615     switch (mode) 
616       {
617       case T_1_SAMPLE:
618         ssbox_one_sample_init(ssb,cmd);
619         break;
620       case T_IND_SAMPLES:
621         ssbox_independent_samples_init(ssb,cmd);
622         break;
623       case T_PAIRED:
624         ssbox_paired_init(ssb,cmd);
625         break;
626       default:
627         NOT_REACHED ();
628       }
629 }
630
631
632
633 /* Despatcher for the populate method */
634 void
635 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
636 {
637   ssb->populate(ssb,cmd);
638 }
639
640
641 /* Despatcher for finalize */
642 void
643 ssbox_finalize(struct ssbox *ssb)
644 {
645   ssb->finalize(ssb);
646 }
647
648
649 /* Submit the box and clear up */
650 void 
651 ssbox_base_finalize(struct ssbox *ssb)
652 {
653   tab_submit(ssb->t);
654 }
655
656
657
658 /* Initialize a ssbox struct */
659 void 
660 ssbox_base_init(struct ssbox *this, int cols,int rows)
661 {
662   this->finalize = ssbox_base_finalize;
663   this->t = tab_create (cols, rows, 0);
664
665   tab_columns (this->t, SOM_COL_DOWN, 1);
666   tab_headers (this->t,0,0,1,0); 
667   tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
668   tab_hline(this->t, TAL_2,0,cols-1,1);
669   tab_dim (this->t, tab_natural_dimensions);
670 }
671
672 void  ssbox_one_sample_populate(struct ssbox *ssb,
673                               struct cmd_t_test *cmd);
674
675 /* Initialize the one_sample ssbox */
676 void 
677 ssbox_one_sample_init(struct ssbox *this, 
678                            struct cmd_t_test *cmd )
679 {
680   const int hsize=5;
681   const int vsize=cmd->n_variables+1;
682
683   this->populate = ssbox_one_sample_populate;
684
685   ssbox_base_init(this, hsize,vsize);
686   tab_title (this->t, _("One-Sample Statistics"));
687   tab_vline(this->t, TAL_2, 1,0,vsize - 1);
688   tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
689   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
690   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
691   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
692 }
693
694 void ssbox_independent_samples_populate(struct ssbox *ssb,
695                                         struct cmd_t_test *cmd);
696
697 /* Initialize the independent samples ssbox */
698 void 
699 ssbox_independent_samples_init(struct ssbox *this, 
700         struct cmd_t_test *cmd)
701 {
702   int hsize=6;
703   int vsize = cmd->n_variables*2 +1;
704
705   this->populate = ssbox_independent_samples_populate;
706
707   ssbox_base_init(this, hsize,vsize);
708   tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
709   tab_title (this->t, _("Group Statistics"));
710   tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, indep_var->name);
711   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
712   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
713   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
714   tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
715 }
716
717
718 /* Populate the ssbox for independent samples */
719 void 
720 ssbox_independent_samples_populate(struct ssbox *ssb,
721                               struct cmd_t_test *cmd)
722 {
723   int i;
724
725   char *val_lab0=0;
726   char *val_lab1=0;
727   double indep_value[2];
728
729   char prefix[2][3]={"",""};
730
731   if ( indep_var->type == NUMERIC ) 
732     {
733       val_lab0 = val_labs_find( indep_var->val_labs,gp.v.g_value[0]); 
734       val_lab1 = val_labs_find( indep_var->val_labs,gp.v.g_value[1]);
735     }
736   else
737     {
738       val_lab0 = gp.v.g_value[0].s;
739       val_lab1 = gp.v.g_value[1].s;
740     }
741
742   if (gp.criterion == CMP_LE ) 
743     {
744       strcpy(prefix[0],"< ");
745       strcpy(prefix[1],">=");
746       indep_value[0] = gp.v.critical_value;
747       indep_value[1] = gp.v.critical_value;
748     }
749   else
750     {
751       indep_value[0] = gp.v.g_value[0].f;
752       indep_value[1] = gp.v.g_value[1].f;
753     }
754
755   assert(ssb->t);
756
757   for (i=0; i < cmd->n_variables; ++i)
758     {
759       struct variable *var = cmd->v_variables[i];
760       struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
761       int count=0;
762
763       tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
764
765       if (val_lab0)
766         tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF, 
767                   "%s%s", prefix[0], val_lab0);
768       else
769           tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF, 
770                     "%s%g", prefix[0], indep_value[0]);
771
772
773       if (val_lab1)
774         tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF, 
775                   "%s%s", prefix[1], val_lab1);
776       else
777           tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF, 
778                     "%s%g", prefix[1], indep_value[1]);
779
780
781       /* Fill in the group statistics */
782       for ( count = 0 ; count < 2 ; ++count ) 
783         {
784           union value search_val;
785
786           struct group_statistics *gs;
787
788           if ( gp.criterion == CMP_LE ) 
789             {
790               if ( count == 0 ) 
791                 {
792                   /*  less than ( < )  case */
793                   search_val.f = gp.v.critical_value - 1.0;
794                 }
795               else
796                 {
797                   /* >= case  */
798                   search_val.f = gp.v.critical_value + 1.0;
799                 }
800             }
801           else
802             {
803               search_val = gp.v.g_value[count];
804             }
805
806           gs = hsh_find(grp_hash, (void *) &search_val);
807           assert(gs);
808
809           tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 2, 0);
810           tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
811           tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
812           tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
813         }
814     }
815 }
816
817
818 void ssbox_paired_populate(struct ssbox *ssb,
819                            struct cmd_t_test *cmd);
820
821 /* Initialize the paired values ssbox */
822 void 
823 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
824 {
825   int hsize=6;
826
827   int vsize = n_pairs*2+1;
828
829   this->populate = ssbox_paired_populate;
830
831   ssbox_base_init(this, hsize,vsize);
832   tab_title (this->t, _("Paired Sample Statistics"));
833   tab_vline(this->t,TAL_GAP,1,0,vsize-1);
834   tab_vline(this->t,TAL_2,2,0,vsize-1);
835   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
836   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
837   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
838   tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
839 }
840
841
842 /* Populate the ssbox for paired values */
843 void 
844 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
845 {
846   int i;
847
848   assert(ssb->t);
849
850   for (i=0; i < n_pairs; ++i)
851     {
852       int j;
853
854       tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
855
856       for (j=0 ; j < 2 ; ++j) 
857         {
858           struct group_statistics *gs;
859
860           gs = &group_proc_get (pairs[i].v[j])->ugs;
861
862           /* Titles */
863
864           tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
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, 2, 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, cmd->v_variables[i]->name);
889       tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 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       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, cmd->v_variables[i]->name);
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, 2, 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                 pairs[i].v[0]->name, pairs[i].v[1]->name);
1202
1203       tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1204
1205       tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1206
1207       /* SE Mean */
1208       se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1209       tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1210
1211       /* Now work out the confidence interval */
1212       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1213
1214       t = gsl_cdf_tdist_Qinv(q, df);
1215
1216       tab_float(trb->t, 5, i+3, TAB_RIGHT, 
1217                 pairs[i].mean_diff - t * se_mean , 8, 4); 
1218
1219       tab_float(trb->t, 6, i+3, TAB_RIGHT, 
1220                 pairs[i].mean_diff + t * se_mean , 8, 4); 
1221
1222       t = (pairs[i].mean[0] - pairs[i].mean[1])
1223         / sqrt (
1224                 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1225                   2 * pairs[i].correlation * 
1226                   pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1227                 / (n - 1)
1228                 );
1229
1230       tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1231
1232       /* Degrees of freedom */
1233       tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1234
1235       p = gsl_cdf_tdist_P(t,df);
1236       q = gsl_cdf_tdist_P(t,df);
1237
1238       tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1239
1240     }
1241 }
1242
1243 /* Initialize the one sample trbox */
1244 void 
1245 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1246 {
1247   const int hsize=7;
1248   const int vsize=cmd->n_variables+3;
1249
1250   self->populate = trbox_one_sample_populate;
1251
1252   trbox_base_init(self, cmd->n_variables,hsize);
1253   tab_title (self->t, _("One-Sample Test"));
1254   tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1255   tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1256
1257   tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF, 
1258                  _("Test Value = %f"), cmd->n_testval[0]);
1259
1260   tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1261
1262
1263   tab_joint_text(self->t,5,1,6,1,TAB_CENTER  | TAT_PRINTF, 
1264                  _("%g%% Confidence Interval of the Difference"),
1265                  cmd->criteria*100.0);
1266
1267   tab_vline(self->t,TAL_GAP,6,1,1);
1268   tab_hline(self->t,TAL_1,5,6,2);
1269   tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1270   tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1271   tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1272   tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1273   tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1274   tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1275
1276 }
1277
1278
1279 /* Populate the one sample trbox */
1280 void 
1281 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1282 {
1283   int i;
1284
1285   assert(trb->t);
1286
1287   for (i=0; i < cmd->n_variables; ++i)
1288     {
1289       double t;
1290       double p,q;
1291       double df;
1292       struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1293
1294
1295       tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1296
1297       t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1298
1299       tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1300
1301       /* degrees of freedom */
1302       df = gs->n - 1;
1303
1304       tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1305
1306       p = gsl_cdf_tdist_P(t, df);
1307       q = gsl_cdf_tdist_Q(t, df);
1308
1309       /* Multiply by 2 to get 2-tailed significance, makeing sure we've got 
1310          the correct tail*/
1311       tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1312
1313       tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1314
1315
1316       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1317       t = gsl_cdf_tdist_Qinv(q, df);
1318
1319       tab_float (trb->t, 5, i+3, TAB_RIGHT,
1320                  gs->mean_diff - t * gs->se_mean, 8,4);
1321
1322       tab_float (trb->t, 6, i+3, TAB_RIGHT,
1323                  gs->mean_diff + t * gs->se_mean, 8,4);
1324     }
1325 }
1326
1327 /* Base initializer for the generalized trbox */
1328 void 
1329 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1330 {
1331   const size_t rows = 3 + data_rows;
1332
1333   self->finalize = trbox_base_finalize;
1334   self->t = tab_create (cols, rows, 0);
1335   tab_headers (self->t,0,0,3,0); 
1336   tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1337   tab_hline(self->t, TAL_2,0,cols-1,3);
1338   tab_dim (self->t, tab_natural_dimensions);
1339 }
1340
1341
1342 /* Base finalizer for the trbox */
1343 void 
1344 trbox_base_finalize(struct trbox *trb)
1345 {
1346   tab_submit(trb->t);
1347 }
1348
1349
1350 /* Create , populate and submit the Paired Samples Correlation box */
1351 void
1352 pscbox(void)
1353 {
1354   const int rows=1+n_pairs;
1355   const int cols=5;
1356   int i;
1357   
1358   struct tab_table *table;
1359   
1360   table = tab_create (cols,rows,0);
1361
1362   tab_columns (table, SOM_COL_DOWN, 1);
1363   tab_headers (table,0,0,1,0); 
1364   tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1365   tab_hline(table, TAL_2, 0, cols - 1, 1);
1366   tab_vline(table, TAL_2, 2, 0, rows - 1);
1367   tab_dim(table, tab_natural_dimensions);
1368   tab_title(table, _("Paired Samples Correlations"));
1369
1370   /* column headings */
1371   tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1372   tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1373   tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1374
1375   for (i=0; i < n_pairs; ++i)
1376     {
1377       double p,q;
1378
1379       double df = pairs[i].n -2;
1380
1381       double correlation_t = 
1382         pairs[i].correlation * sqrt(df) /
1383         sqrt(1 - pow2(pairs[i].correlation));
1384
1385
1386       /* row headings */
1387       tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF, 
1388                _("Pair %d"), i);
1389       
1390       tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF, 
1391                _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1392
1393
1394       /* row data */
1395       tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1396       tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1397
1398       p = gsl_cdf_tdist_P(correlation_t, df);
1399       q = gsl_cdf_tdist_Q(correlation_t, df);
1400
1401       tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1402     }
1403
1404   tab_submit(table);
1405 }
1406
1407
1408
1409
1410 /* Calculation Implementation */
1411
1412 /* Per case calculations common to all variants of the T test */
1413 static int 
1414 common_calc (const struct ccase *c, void *_cmd)
1415 {
1416   int i;
1417   struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;  
1418
1419   double weight = dict_get_case_weight (dataset_dict (current_dataset), c, &bad_weight_warn);
1420
1421
1422   /* Skip the entire case if /MISSING=LISTWISE is set */
1423   if ( cmd->miss == TTS_LISTWISE ) 
1424     {
1425       for(i=0; i< cmd->n_variables ; ++i) 
1426         {
1427           struct variable *v = cmd->v_variables[i];
1428           const union value *val = case_data (c, v->fv);
1429
1430           if (value_is_missing(&v->miss, val) )
1431             {
1432               return 0;
1433             }
1434         }
1435     }
1436
1437   /* Listwise has to be implicit if the independent variable is missing ?? */
1438   if ( cmd->sbc_groups )
1439     {
1440       const union value *gv = case_data (c, indep_var->fv);
1441       if ( value_is_missing(&indep_var->miss, gv) )
1442         {
1443           return 0;
1444         }
1445     }
1446
1447
1448   for(i=0; i< cmd->n_variables ; ++i) 
1449     {
1450       struct group_statistics *gs;
1451       struct variable *v = cmd->v_variables[i];
1452       const union value *val = case_data (c, v->fv);
1453
1454       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1455
1456       if (! value_is_missing(&v->miss, val) )
1457         {
1458           gs->n+=weight;
1459           gs->sum+=weight * val->f;
1460           gs->ssq+=weight * val->f * val->f;
1461         }
1462     }
1463   return 0;
1464 }
1465
1466 /* Pre calculations common to all variants of the T test */
1467 static void 
1468 common_precalc ( struct cmd_t_test *cmd )
1469 {
1470   int i=0;
1471
1472   for(i=0; i< cmd->n_variables ; ++i) 
1473     {
1474       struct group_statistics *gs;
1475       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1476       
1477       gs->sum=0;
1478       gs->n=0;
1479       gs->ssq=0;
1480       gs->sum_diff=0;
1481     }
1482 }
1483
1484 /* Post calculations common to all variants of the T test */
1485 void 
1486 common_postcalc (  struct cmd_t_test *cmd )
1487 {
1488   int i=0;
1489
1490
1491   for(i=0; i< cmd->n_variables ; ++i) 
1492     {
1493       struct group_statistics *gs;
1494       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1495       
1496       gs->mean=gs->sum / gs->n;
1497       gs->s_std_dev= sqrt(
1498                          ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1499                          ) ;
1500
1501       gs->std_dev= sqrt(
1502                          gs->n/(gs->n-1) *
1503                          ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1504                          ) ;
1505
1506       gs->se_mean = gs->std_dev / sqrt(gs->n);
1507       gs->mean_diff= gs->sum_diff / gs->n;
1508     }
1509 }
1510
1511 /* Per case calculations for one sample t test  */
1512 static int 
1513 one_sample_calc (const struct ccase *c, void *cmd_)
1514 {
1515   int i;
1516   struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1517
1518
1519   double weight = dict_get_case_weight (dataset_dict (current_dataset), c, &bad_weight_warn);
1520
1521   /* Skip the entire case if /MISSING=LISTWISE is set */
1522   if ( cmd->miss == TTS_LISTWISE ) 
1523     {
1524       for(i=0; i< cmd->n_variables ; ++i) 
1525         {
1526           struct variable *v = cmd->v_variables[i];
1527           const union value *val = case_data (c, v->fv);
1528
1529           if (value_is_missing(&v->miss, val) )
1530             {
1531               return 0;
1532             }
1533         }
1534     }
1535
1536   for(i=0; i< cmd->n_variables ; ++i) 
1537     {
1538       struct group_statistics *gs;
1539       struct variable *v = cmd->v_variables[i];
1540       const union value *val = case_data (c, v->fv);
1541
1542       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1543       
1544       if ( ! value_is_missing(&v->miss, val))
1545         gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1546     }
1547
1548   return 0;
1549 }
1550
1551 /* Pre calculations for one sample t test */
1552 static void 
1553 one_sample_precalc ( struct cmd_t_test *cmd )
1554 {
1555   int i=0; 
1556  
1557   for(i=0; i< cmd->n_variables ; ++i) 
1558     {
1559       struct group_statistics *gs;
1560       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1561       
1562       gs->sum_diff=0;
1563     }
1564 }
1565
1566 /* Post calculations for one sample t test */
1567 static void 
1568 one_sample_postcalc (struct cmd_t_test *cmd)
1569 {
1570   int i=0;
1571   
1572   for(i=0; i< cmd->n_variables ; ++i) 
1573     {
1574       struct group_statistics *gs;
1575       gs= &group_proc_get (cmd->v_variables[i])->ugs;
1576
1577       gs->mean_diff = gs->sum_diff / gs->n ;
1578     }
1579 }
1580
1581
1582
1583 static void 
1584 paired_precalc (struct cmd_t_test *cmd UNUSED)
1585 {
1586   int i;
1587
1588   for(i=0; i < n_pairs ; ++i )
1589     {
1590       pairs[i].n = 0;
1591       pairs[i].sum[0] = 0;      pairs[i].sum[1] = 0;
1592       pairs[i].ssq[0] = 0;      pairs[i].ssq[1] = 0;
1593       pairs[i].sum_of_prod = 0;
1594       pairs[i].correlation = 0;
1595       pairs[i].sum_of_diffs = 0;
1596       pairs[i].ssq_diffs = 0;
1597     }
1598
1599 }
1600
1601
1602 static int  
1603 paired_calc (const struct ccase *c, void *cmd_)
1604 {
1605   int i;
1606
1607   struct cmd_t_test *cmd  = (struct cmd_t_test *) cmd_;
1608
1609   double weight = dict_get_case_weight (dataset_dict (current_dataset), c, &bad_weight_warn);
1610
1611   /* Skip the entire case if /MISSING=LISTWISE is set , 
1612    AND one member of a pair is missing */
1613   if ( cmd->miss == TTS_LISTWISE ) 
1614     {
1615       for(i=0; i < n_pairs ; ++i )
1616         {
1617           struct variable *v0 = pairs[i].v[0];
1618           struct variable *v1 = pairs[i].v[1];
1619
1620           const union value *val0 = case_data (c, v0->fv);
1621           const union value *val1 = case_data (c, v1->fv);
1622           
1623           if ( value_is_missing(&v0->miss, val0) ||
1624                value_is_missing(&v1->miss, val1) )
1625             {
1626               return 0;
1627             }
1628         }
1629     }
1630
1631   for(i=0; i < n_pairs ; ++i )
1632     {
1633       struct variable *v0 = pairs[i].v[0];
1634       struct variable *v1 = pairs[i].v[1];
1635
1636       const union value *val0 = case_data (c, v0->fv);
1637       const union value *val1 = case_data (c, v1->fv);
1638
1639       if ( ( !value_is_missing(&v0->miss, val0)
1640              && !value_is_missing(&v1->miss, val1) ) )
1641       {
1642         pairs[i].n += weight;
1643         pairs[i].sum[0] += weight * val0->f;
1644         pairs[i].sum[1] += weight * val1->f;
1645
1646         pairs[i].ssq[0] += weight * pow2(val0->f);
1647         pairs[i].ssq[1] += weight * pow2(val1->f);
1648
1649         pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1650
1651         pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1652         pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1653       }
1654     }
1655
1656   return 0;
1657 }
1658
1659 static void 
1660 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1661 {
1662   int i;
1663
1664   for(i=0; i < n_pairs ; ++i )
1665     {
1666       int j;
1667       const double n = pairs[i].n;
1668
1669       for (j=0; j < 2 ; ++j) 
1670         {
1671           pairs[i].mean[j] = pairs[i].sum[j] / n ;
1672           pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n - 
1673                                               pow2(pairs[i].mean[j]))
1674                                      );
1675
1676           pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n - 
1677                                               pow2(pairs[i].mean[j]))
1678                                      );
1679         }
1680       
1681       pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n - 
1682         pairs[i].mean[0] * pairs[i].mean[1] ;
1683       /* correlation now actually contains the covariance */
1684       
1685       pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1686       pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1687       
1688       pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1689
1690       pairs[i].std_dev_diff = sqrt (  n / (n - 1) * (
1691                                     ( pairs[i].ssq_diffs / n )
1692                                     - 
1693                                     pow2(pairs[i].mean_diff )
1694                                     ) );
1695     }
1696 }
1697
1698 static void 
1699 group_precalc (struct cmd_t_test *cmd )
1700 {
1701   int i;
1702   int j;
1703
1704   for(i=0; i< cmd->n_variables ; ++i) 
1705     {
1706       struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1707
1708       /* There's always 2 groups for a T - TEST */
1709       ttpr->n_groups = 2;
1710
1711       gp.indep_width = indep_var->width;
1712       
1713       ttpr->group_hash = hsh_create(2, 
1714                                     (hsh_compare_func *) compare_group_binary,
1715                                     (hsh_hash_func *) hash_group_binary,
1716                                     (hsh_free_func *) free_group,
1717                                     (void *) &gp );
1718
1719       for (j=0 ; j < 2 ; ++j)
1720         {
1721
1722           struct group_statistics *gs = xmalloc (sizeof *gs);
1723
1724           gs->sum = 0;
1725           gs->n = 0;
1726           gs->ssq = 0;
1727         
1728           if ( gp.criterion == CMP_EQ ) 
1729             {
1730               gs->id = gp.v.g_value[j];
1731             }
1732           else
1733             {
1734               if ( j == 0 ) 
1735                 gs->id.f = gp.v.critical_value - 1.0 ;
1736               else
1737                 gs->id.f = gp.v.critical_value + 1.0 ;
1738             }
1739           
1740           hsh_insert ( ttpr->group_hash, (void *) gs );
1741
1742         }
1743     }
1744
1745 }
1746
1747 static int  
1748 group_calc (const struct ccase *c, struct cmd_t_test *cmd)
1749 {
1750   int i;
1751
1752   const union value *gv = case_data (c, indep_var->fv);
1753
1754   const double weight = 
1755     dict_get_case_weight (dataset_dict (current_dataset), c, &bad_weight_warn);
1756
1757   if ( value_is_missing(&indep_var->miss, gv) )
1758     {
1759       return 0;
1760     }
1761
1762   if ( cmd->miss == TTS_LISTWISE ) 
1763     {
1764       for(i=0; i< cmd->n_variables ; ++i) 
1765         {
1766           struct variable *v = cmd->v_variables[i];
1767           const union value *val = case_data (c, v->fv);
1768
1769           if (value_is_missing(&v->miss, val) )
1770             {
1771               return 0;
1772             }
1773         }
1774     }
1775
1776   gv = case_data (c, indep_var->fv);
1777
1778   for(i=0; i< cmd->n_variables ; ++i) 
1779     {
1780       struct variable *var = cmd->v_variables[i];
1781       const union value *val = case_data (c, var->fv);
1782       struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1783       struct group_statistics *gs;
1784
1785       gs = hsh_find(grp_hash, (void *) gv);
1786
1787       /* If the independent variable doesn't match either of the values 
1788          for this case then move on to the next case */
1789       if ( ! gs ) 
1790         return 0;
1791
1792       if ( !value_is_missing(&var->miss, val) )
1793         {
1794           gs->n+=weight;
1795           gs->sum+=weight * val->f;
1796           gs->ssq+=weight * pow2(val->f);
1797         }
1798     }
1799
1800   return 0;
1801 }
1802
1803
1804 static void 
1805 group_postcalc ( struct cmd_t_test *cmd )
1806 {
1807   int i;
1808
1809   for(i=0; i< cmd->n_variables ; ++i) 
1810     {
1811       struct variable *var = cmd->v_variables[i];
1812       struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1813       struct hsh_iterator g;
1814       struct group_statistics *gs;
1815       int count=0;
1816
1817       for (gs =  hsh_first (grp_hash,&g); 
1818            gs != 0; 
1819            gs = hsh_next(grp_hash,&g))
1820         {
1821           gs->mean = gs->sum / gs->n;
1822           
1823           gs->s_std_dev= sqrt(
1824                               ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1825                               ) ;
1826
1827           gs->std_dev= sqrt(
1828                             gs->n/(gs->n-1) *
1829                             ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1830                             ) ;
1831           
1832           gs->se_mean = gs->std_dev / sqrt(gs->n);
1833           count ++;
1834         }
1835       assert(count == 2);
1836     }
1837 }
1838
1839
1840
1841 static bool
1842 calculate(const struct ccase *first, const struct casefile *cf, void *cmd_)
1843 {
1844   struct ssbox stat_summary_box;
1845   struct trbox test_results_box;
1846
1847   struct casereader *r;
1848   struct ccase c;
1849
1850   struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1851
1852   output_split_file_values (first);
1853   common_precalc(cmd);
1854   for(r = casefile_get_reader (cf);
1855       casereader_read (r, &c) ;
1856       case_destroy (&c)) 
1857     {
1858       common_calc(&c,cmd);
1859     }
1860   casereader_destroy (r);
1861   common_postcalc(cmd);
1862
1863   switch(mode)
1864     {
1865     case T_1_SAMPLE:
1866       one_sample_precalc(cmd);
1867       for(r = casefile_get_reader (cf);
1868           casereader_read (r, &c) ;
1869           case_destroy (&c)) 
1870         {
1871           one_sample_calc(&c,cmd);
1872         }
1873       casereader_destroy (r);
1874       one_sample_postcalc(cmd);
1875
1876       break;
1877     case T_PAIRED:
1878       paired_precalc(cmd);
1879       for(r = casefile_get_reader (cf);
1880           casereader_read (r, &c) ;
1881           case_destroy (&c)) 
1882         {
1883           paired_calc(&c,cmd);
1884         }
1885       casereader_destroy (r);
1886       paired_postcalc(cmd);
1887
1888       break;
1889     case T_IND_SAMPLES:
1890
1891       group_precalc(cmd);
1892       for(r = casefile_get_reader (cf);
1893           casereader_read (r, &c) ;
1894           case_destroy (&c)) 
1895         {
1896           group_calc(&c,cmd);
1897         }
1898       casereader_destroy (r);
1899       group_postcalc(cmd);
1900
1901       levene(cf, indep_var, cmd->n_variables, cmd->v_variables,
1902              (cmd->miss == TTS_LISTWISE)?LEV_LISTWISE:LEV_ANALYSIS ,
1903              value_is_missing);
1904       break;
1905     }
1906
1907   ssbox_create(&stat_summary_box,cmd,mode);
1908   ssbox_populate(&stat_summary_box,cmd);
1909   ssbox_finalize(&stat_summary_box);
1910
1911   if ( mode == T_PAIRED) 
1912       pscbox();
1913
1914   trbox_create(&test_results_box,cmd,mode);
1915   trbox_populate(&test_results_box,cmd);
1916   trbox_finalize(&test_results_box);
1917
1918   return true;
1919 }
1920
1921 short which_group(const struct group_statistics *g,
1922                   const struct group_properties *p);
1923
1924 /* Return -1 if the id of a is less than b; +1 if greater than and 
1925    0 if equal */
1926 static int 
1927 compare_group_binary(const struct group_statistics *a, 
1928                      const struct group_statistics *b, 
1929                      const struct group_properties *p)
1930 {
1931   short flag_a;
1932   short flag_b;
1933   
1934   if ( p->criterion == CMP_LE ) 
1935     {
1936       /* less-than-or-equal comparision is not meaningfull for
1937          alpha variables, so we shouldn't ever arrive here */
1938       assert(p->indep_width == 0 ) ;
1939       
1940       flag_a = ( a->id.f < p->v.critical_value ) ;
1941       flag_b = ( b->id.f < p->v.critical_value ) ;
1942     }
1943   else
1944     {
1945       flag_a = which_group(a, p);
1946       flag_b = which_group(b, p);
1947     }
1948
1949   if (flag_a < flag_b ) 
1950     return -1;
1951
1952   return (flag_a > flag_b);
1953 }
1954
1955 /* This is a degenerate case of a hash, since it can only return three possible
1956    values.  It's really a comparison, being used as a hash function */
1957
1958 static unsigned 
1959 hash_group_binary(const struct group_statistics *g, 
1960                   const struct group_properties *p)
1961 {
1962   short flag = -1;
1963
1964   if ( p->criterion == CMP_LE ) 
1965     {
1966       /* Not meaningfull to do a less than compare for alpha values ? */
1967       assert(p->indep_width == 0 ) ;
1968       flag = ( g->id.f < p->v.critical_value ) ; 
1969     }
1970   else if ( p->criterion == CMP_EQ) 
1971     {
1972       flag = which_group(g,p);
1973     }
1974   else
1975     NOT_REACHED ();
1976
1977   return flag;
1978 }
1979
1980 /* return 0 if G belongs to group 0, 
1981           1 if it belongs to group 1,
1982           2 if it belongs to neither group */
1983 short
1984 which_group(const struct group_statistics *g,
1985             const struct group_properties *p)
1986 {
1987  
1988   if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1989     return 0;
1990
1991   if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))
1992     return 1;
1993
1994   return 2;
1995 }
1996