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