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