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