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