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