Fixed t-test so that multiple /pairs subcommands may be given.
[pspp-builds.git] / src / t-test.q
1 /* PSPP - computes sample statistics. -*-c-*-
2
3    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4    Written by John Williams <johnr.williams@stonebow.otago.ac.nz>.
5    Almost completly re-written by John Darrington 2004
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA. */
21
22 #include <config.h>
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include "alloc.h"
28 #include "str.h"
29 #include "dcdflib/cdflib.h"
30 #include "command.h"
31 #include "lexer.h"
32 #include "error.h"
33 #include "magic.h"
34 #include "tab.h"
35 #include "som.h"
36 #include "value-labels.h"
37 #include "var.h"
38 #include "vfm.h"
39 #include "pool.h"
40 #include "hash.h"
41 #include "stats.h"
42
43 /* (specification)
44    "T-TEST" (tts_):
45      +groups=custom;
46      +testval=double;
47      variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
48      pairs=custom;
49      +missing=miss:!analysis/listwise,
50              incl:include/!exclude;
51      format=fmt:!labels/nolabels;
52      criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
53 */
54 /* (declarations) */
55 /* (functions) */
56
57 static struct cmd_t_test cmd;
58
59
60
61 static struct pool *t_test_pool ;
62
63 /* Variable for the GROUPS subcommand, if given. */
64 static struct variable *groups;
65
66 /* GROUPS: Number of values specified by the user; the values
67    specified if any. */
68 static int n_groups_values;
69 static union value groups_values[2];
70
71 /* PAIRS: Number of pairs to be compared ; each pair. */
72 static int n_pairs = 0 ;
73 struct pair 
74 {
75   /* The variables comprising the pair */
76   struct variable *v[2];
77
78   /* The correlation coefficient between the variables */
79   double correlation;
80
81   /* The sum of the differences */
82   double sum_of_diffs;
83
84   /* The mean of the differences */
85   double mean_diff;
86
87   /* The sum of the squares of the differences */
88   double ssq_diffs;
89
90   /* The std deviation of the differences */
91   double std_dev_diff;
92 };
93 static struct pair *pairs=0;
94
95
96 static int parse_value (union value * v, int type) ;
97
98
99 /* Structures and Functions for the Statistics Summary Box */
100 struct ssbox;
101 typedef void populate_ssbox_func(struct ssbox *ssb,
102                                             struct cmd_t_test *cmd);
103 typedef void finalize_ssbox_func(struct ssbox *ssb);
104
105 struct ssbox
106 {
107   struct tab_table *t;
108
109   populate_ssbox_func *populate;
110   finalize_ssbox_func *finalize;
111
112 };
113
114 /* Create a ssbox */
115 void ssbox_create(struct ssbox *ssb,   struct cmd_t_test *cmd, int mode);
116
117 /* Populate a ssbox according to cmd */
118 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
119
120 /* Submit and destroy a ssbox */
121 void ssbox_finalize(struct ssbox *ssb);
122
123 /* A function to create, populate and submit the Paired Samples Correlation 
124    box */
125 void pscbox(struct cmd_t_test *cmd);
126
127
128 /* Structures and Functions for the Test Results Box */
129 struct trbox;
130
131 typedef void populate_trbox_func(struct trbox *trb,
132                                  struct cmd_t_test *cmd);
133 typedef void finalize_trbox_func(struct trbox *trb);
134
135 struct trbox {
136   struct tab_table *t;
137   populate_trbox_func *populate;
138   finalize_trbox_func *finalize;
139 };
140
141 /* Create a trbox */
142 void trbox_create(struct trbox *trb,   struct cmd_t_test *cmd, int mode);
143
144 /* Populate a ssbox according to cmd */
145 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
146
147 /* Submit and destroy a ssbox */
148 void trbox_finalize(struct trbox *trb);
149
150 /* Which mode was T-TEST invoked */
151 enum {
152   T_1_SAMPLE = 0 ,
153   T_IND_SAMPLES, 
154   T_PAIRED
155 };
156
157
158 static int common_calc (struct ccase *);
159 static void common_precalc (void);
160 static void common_postcalc (void);
161
162 static int one_sample_calc (struct ccase *);
163 static void one_sample_precalc (void);
164 static void one_sample_postcalc (void);
165
166 static int  paired_calc (struct ccase *);
167 static void paired_precalc (void);
168 static void paired_postcalc (void);
169
170 static int compare_var_name (const void *a_, const void *b_, void *v_ unused);
171 static unsigned hash_var_name (const void *a_, void *v_ unused);
172
173
174 int
175 cmd_t_test(void)
176 {
177   int mode;
178
179   struct ssbox stat_summary_box;
180   struct trbox test_results_box;
181
182   if (!lex_force_match_id ("T"))
183     return CMD_FAILURE;
184
185   lex_match ('-');
186   lex_match_id ("TEST");
187
188   if ( !parse_t_test(&cmd) )
189     return CMD_FAILURE;
190
191   if (! cmd.sbc_criteria)
192     cmd.criteria=0.95;
193
194   {
195     int m=0;
196     if (cmd.sbc_testval) ++m;
197     if (cmd.sbc_groups) ++m;
198     if (cmd.sbc_pairs) ++m;
199
200     if ( m != 1)
201       {
202         msg(SE, 
203             _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
204             );
205         return CMD_FAILURE;
206       }
207   }
208
209   if (cmd.sbc_testval) 
210     mode=T_1_SAMPLE;
211   else if (cmd.sbc_groups)
212     mode=T_IND_SAMPLES;
213   else
214     mode=T_PAIRED;
215
216   if ( mode == T_PAIRED) 
217     {
218       if (cmd.sbc_variables) 
219         {
220           msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
221           return CMD_FAILURE;
222         }
223       else
224         {
225           /* Iterate through the pairs and put each variable that is a 
226              member of a pair into cmd.v_variables */
227
228           int i;
229           struct hsh_iterator hi;
230           struct hsh_table *hash;
231           struct variable *v;
232
233           hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
234
235           for (i=0; i < n_pairs; ++i)
236             {
237               hsh_insert(hash,pairs[i].v[0]);
238               hsh_insert(hash,pairs[i].v[1]);
239             }
240
241           assert(cmd.n_variables == 0);
242           cmd.n_variables = hsh_count(hash);
243
244           cmd.v_variables = xrealloc(cmd.v_variables,
245                                      sizeof(struct variable) * cmd.n_variables);
246           /* Iterate through the hash */
247           for (i=0,v = (struct variable *) hsh_first(hash,&hi);
248                v != 0;
249                v=hsh_next(hash,&hi) ) 
250             cmd.v_variables[i++]=v;
251
252           hsh_destroy(hash);
253         }
254     }
255
256
257   procedure(common_precalc,common_calc,common_postcalc);
258
259   switch(mode)
260     {
261     case T_1_SAMPLE:
262       procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc);
263       break;
264     case T_PAIRED:
265       procedure(paired_precalc,paired_calc,paired_postcalc);
266       break;
267     }
268   
269
270   t_test_pool = pool_create ();
271
272   ssbox_create(&stat_summary_box,&cmd,mode);
273   ssbox_populate(&stat_summary_box,&cmd);
274   ssbox_finalize(&stat_summary_box);
275
276   if ( mode == T_PAIRED) 
277     {
278       pscbox(&cmd);
279     }
280
281   trbox_create(&test_results_box,&cmd,mode);
282   trbox_populate(&test_results_box,&cmd);
283   trbox_finalize(&test_results_box);
284
285   pool_destroy (t_test_pool);
286
287   t_test_pool=0;
288
289
290   n_pairs=0;
291   free(pairs);
292   pairs=0;
293     
294   return CMD_SUCCESS;
295 }
296
297 static int
298 tts_custom_groups (struct cmd_t_test *cmd unused)
299 {
300   lex_match('=');
301
302   if (token != T_ALL && 
303       (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
304      ) 
305   {
306     msg(SE,_("`%s' is not a variable name"),tokid);
307     return 0;
308   }
309
310   groups = parse_variable ();
311   if (!groups)
312     {
313       lex_error ("expecting variable name in GROUPS subcommand");
314       return 0;
315     }
316
317   if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
318     {
319       msg (SE, _("Long string variable %s is not valid here."),
320            groups->name);
321       return 0;
322     }
323
324   if (!lex_match ('('))
325     {
326       if (groups->type == NUMERIC)
327         {
328           n_groups_values = 2;
329           groups_values[0].f = 1;
330           groups_values[1].f = 2;
331           return 1;
332         }
333       else
334         {
335           msg (SE, _("When applying GROUPS to a string variable, at "
336                      "least one value must be specified."));
337           return 0;
338         }
339     }
340
341   if (!parse_value (&groups_values[0],groups->type))
342     return 0;
343   n_groups_values = 1;
344
345   lex_match (',');
346   if (lex_match (')'))
347     return 1;
348
349   if (!parse_value (&groups_values[1],groups->type))
350     return 0;
351   n_groups_values = 2;
352
353   if (!lex_force_match (')'))
354     return 0;
355
356   return 1;
357 }
358
359
360
361
362 static int
363 tts_custom_pairs (struct cmd_t_test *cmd unused)
364 {
365   struct variable **vars;
366   int n_vars;
367   int n_pairs_local;
368
369   int n_before_WITH ;
370   int n_after_WITH = -1;
371   int paired ; /* Was the PAIRED keyword given ? */
372
373   lex_match('=');
374
375   if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
376       && token != T_ALL)
377     {
378       msg(SE,_("`%s' is not a variable name"),tokid);
379       return 0;
380     }
381
382   n_vars=0;
383   if (!parse_variables (default_dict, &vars, &n_vars,
384                         PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
385     {
386       free (vars);
387       return 0;
388     }
389   assert (n_vars);
390
391   n_before_WITH=0;
392   if (lex_match (T_WITH))
393     {
394       n_before_WITH = n_vars;
395       if (!parse_variables (default_dict, &vars, &n_vars,
396                             PV_DUPLICATE | PV_APPEND
397                             | PV_NUMERIC | PV_NO_SCRATCH))
398         {
399           free (vars);
400           return 0;
401         }
402       n_after_WITH = n_vars - n_before_WITH;
403     }
404
405   paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
406
407   /* Determine the number of pairs needed */
408   if (paired)
409     {
410       if (n_before_WITH != n_after_WITH)
411         {
412           free (vars);
413           msg (SE, _("PAIRED was specified but the number of variables "
414                      "preceding WITH (%d) did not match the number "
415                      "following (%d)."),
416                n_before_WITH, n_after_WITH );
417           return 0;
418         }
419       n_pairs_local=n_before_WITH;
420     }
421   else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
422     {
423       n_pairs_local=n_before_WITH * n_after_WITH ;
424     }
425   else /* Neither WITH nor PAIRED keyword given */
426     {
427       if (n_vars < 2)
428         {
429           free (vars);
430           msg (SE, _("At least two variables must be specified "
431                      "on PAIRS."));
432           return 0;
433         }
434
435       /* how many ways can you pick 2 from n_vars ? */
436       n_pairs_local = n_vars * (n_vars -1 ) /2 ;
437     }
438
439
440   /* Allocate storage for the pairs */
441   pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
442
443   /* Populate the pairs with the appropriate variables */
444   if ( paired ) 
445     {
446       int i;
447
448       assert(n_pairs_local == n_vars/2);
449       for (i = 0; i < n_pairs_local ; ++i)
450         {
451           pairs[i].v[n_pairs+0] = vars[i];
452           pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
453         }
454     }
455   else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
456     {
457       int i,j;
458       int p=n_pairs;
459
460       for(i=0 ; i < n_before_WITH ; ++i ) 
461         {
462           for(j=0 ; j < n_after_WITH ; ++j)
463             {
464               pairs[p].v[0] = vars[i];
465               pairs[p].v[1] = vars[j+n_before_WITH];
466               ++p;
467             }
468         }
469     }
470   else /* Neither WITH nor PAIRED given */
471     {
472       int i,j;
473       int p=n_pairs;
474       
475       for(i=0 ; i < n_vars ; ++i ) 
476         {
477           for(j=i+1 ; j < n_vars ; ++j)
478             {
479               pairs[p].v[0] = vars[i];
480               pairs[p].v[1] = vars[j];
481               ++p;
482             }
483         }
484     }
485
486   n_pairs+=n_pairs_local;
487
488   return 1;
489 }
490
491 /* Parses the current token (numeric or string, depending on type)
492     value v and returns success. */
493 static int
494 parse_value (union value * v, int type )
495 {
496   if (type == NUMERIC)
497     {
498       if (!lex_force_num ())
499         return 0;
500       v->f = tokval;
501     }
502   else
503     {
504       if (!lex_force_string ())
505         return 0;
506       strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
507     }
508
509   lex_get ();
510
511   return 1;
512 }
513
514
515 /* Implementation of the SSBOX object */
516
517 void ssbox_base_init(struct ssbox *this, int cols,int rows);
518
519 void ssbox_base_finalize(struct ssbox *ssb);
520
521 void ssbox_one_sample_init(struct ssbox *this, 
522                            struct cmd_t_test *cmd );
523
524 void ssbox_independent_samples_init(struct ssbox *this,
525                                     struct cmd_t_test *cmd);
526
527 void ssbox_paired_init(struct ssbox *this,
528                            struct cmd_t_test *cmd);
529
530 /* Factory to create an ssbox */
531 void 
532 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
533 {
534     switch (mode) 
535       {
536       case T_1_SAMPLE:
537         ssbox_one_sample_init(ssb,cmd);
538         break;
539       case T_IND_SAMPLES:
540         ssbox_independent_samples_init(ssb,cmd);
541         break;
542       case T_PAIRED:
543         ssbox_paired_init(ssb,cmd);
544         break;
545       default:
546         assert(0);
547       }
548 }
549
550
551 /* Despatcher for the populate method */
552 void
553 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
554 {
555   ssb->populate(ssb,cmd);
556 }
557
558
559 /* Despatcher for finalize */
560 void
561 ssbox_finalize(struct ssbox *ssb)
562 {
563   ssb->finalize(ssb);
564 }
565
566
567 /* Submit the box and clear up */
568 void 
569 ssbox_base_finalize(struct ssbox *ssb)
570 {
571   tab_submit(ssb->t);
572 }
573
574 /* Initialize a ssbox struct */
575 void 
576 ssbox_base_init(struct ssbox *this, int cols,int rows)
577 {
578   this->finalize = ssbox_base_finalize;
579   this->t = tab_create (cols, rows, 0);
580
581   tab_columns (this->t, SOM_COL_DOWN, 1);
582   tab_headers (this->t,0,0,1,0); 
583   tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
584   tab_hline(this->t, TAL_2,0,cols-1,1);
585   tab_dim (this->t, tab_natural_dimensions);
586 }
587
588 void  ssbox_one_sample_populate(struct ssbox *ssb,
589                               struct cmd_t_test *cmd);
590
591 /* Initialize the one_sample ssbox */
592 void 
593 ssbox_one_sample_init(struct ssbox *this, 
594                            struct cmd_t_test *cmd )
595 {
596   const int hsize=5;
597   const int vsize=cmd->n_variables+1;
598
599   this->populate = ssbox_one_sample_populate;
600
601   ssbox_base_init(this, hsize,vsize);
602   tab_title (this->t, 0, _("One-Sample Statistics"));
603   tab_vline(this->t, TAL_2, 1,0,vsize);
604   tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
605   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
606   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
607   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
608 }
609
610 void ssbox_independent_samples_populate(struct ssbox *ssb,
611                                         struct cmd_t_test *cmd);
612
613 /* Initialize the independent samples ssbox */
614 void 
615 ssbox_independent_samples_init(struct ssbox *this, 
616         struct cmd_t_test *cmd)
617 {
618   int hsize=6;
619   int vsize = cmd->n_variables*2 +1;
620
621   this->populate = ssbox_independent_samples_populate;
622
623   ssbox_base_init(this, hsize,vsize);
624   tab_title (this->t, 0, _("Group Statistics"));
625   tab_vline(this->t,0,1,0,vsize);
626   tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, groups->name);
627   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
628   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
629   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
630   tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
631 }
632
633
634 /* Populate the ssbox for independent samples */
635 void 
636 ssbox_independent_samples_populate(struct ssbox *ssb,
637                               struct cmd_t_test *cmd)
638 {
639   int i;
640
641   char *val_lab1=0;
642   char *val_lab2=0;
643
644   if ( groups->type == NUMERIC ) 
645     {
646       val_lab1 = val_labs_find( groups->val_labs,groups_values[0]); 
647       val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
648     }
649   else
650     {
651       val_lab1 = groups_values[0].s;
652       val_lab2 = groups_values[1].s;
653     }
654
655   assert(ssb->t);
656
657   for (i=0; i < cmd->n_variables; ++i)
658     {
659       tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
660
661       if (val_lab1)
662         tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
663       else
664         tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
665
666       if (val_lab2)
667         tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
668       else
669         tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
670     }
671 }
672
673
674 void ssbox_paired_populate(struct ssbox *ssb,
675                            struct cmd_t_test *cmd);
676
677 /* Initialize the paired values ssbox */
678 void 
679 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd unused)
680 {
681   int hsize=6;
682
683   int vsize = n_pairs*2+1;
684
685   this->populate = ssbox_paired_populate;
686
687   ssbox_base_init(this, hsize,vsize);
688   tab_title (this->t, 0, _("Paired Sample Statistics"));
689   tab_vline(this->t,TAL_0,1,0,vsize-1);
690   tab_vline(this->t,TAL_2,2,0,vsize-1);
691   tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
692   tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
693   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
694   tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
695 }
696
697
698 /* Populate the ssbox for paired values */
699 void 
700 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd unused)
701 {
702   int i;
703
704   assert(ssb->t);
705
706   for (i=0; i < n_pairs; ++i)
707     {
708       int j;
709
710       tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
711
712       for (j=0 ; j < 2 ; ++j) 
713         {
714           struct t_test_proc *ttp;
715
716           ttp=&pairs[i].v[j]->p.t_t;
717
718           /* Titles */
719
720           tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
721
722           /* Values */
723           tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, ttp->mean, 8, 2);
724           tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, ttp->n, 2, 0);
725           tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, ttp->std_dev, 8, 3);
726           tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, ttp->se_mean, 8, 3);
727
728         }
729
730     }
731
732 }
733
734 /* Populate the one sample ssbox */
735 void 
736 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
737 {
738   int i;
739
740   assert(ssb->t);
741
742   for (i=0; i < cmd->n_variables; ++i)
743     {
744       struct t_test_proc *ttp;
745       ttp= &cmd->v_variables[i]->p.t_t;
746
747       tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
748       tab_float (ssb->t,1, i+1, TAB_RIGHT, ttp->n, 2, 0);
749       tab_float (ssb->t,2, i+1, TAB_RIGHT, ttp->mean, 8, 2);
750       tab_float (ssb->t,3, i+1, TAB_RIGHT, ttp->std_dev, 8, 2);
751       tab_float (ssb->t,4, i+1, TAB_RIGHT, ttp->se_mean, 8, 3);
752     }
753   
754 }
755
756
757
758 /* Implementation of the Test Results box struct */
759
760 void trbox_base_init(struct trbox *self,int n_vars, int cols);
761 void trbox_base_finalize(struct trbox *trb);
762
763 void trbox_independent_samples_init(struct trbox *trb,
764                                     struct cmd_t_test *cmd );
765
766 void trbox_independent_samples_populate(struct trbox *trb,
767                                         struct cmd_t_test *cmd);
768
769 void trbox_one_sample_init(struct trbox *self,
770                       struct cmd_t_test *cmd );
771
772 void trbox_one_sample_populate(struct trbox *trb,
773                                struct cmd_t_test *cmd);
774
775 void trbox_paired_init(struct trbox *self,
776                        struct cmd_t_test *cmd );
777
778 void trbox_paired_populate(struct trbox *trb,
779                       struct cmd_t_test *cmd);
780
781
782
783 /* Create a trbox according to mode*/
784 void 
785 trbox_create(struct trbox *trb,   
786              struct cmd_t_test *cmd, int mode)
787 {
788     switch (mode) 
789       {
790       case T_1_SAMPLE:
791         trbox_one_sample_init(trb,cmd);
792         break;
793       case T_IND_SAMPLES:
794         trbox_independent_samples_init(trb,cmd);
795         break;
796       case T_PAIRED:
797         trbox_paired_init(trb,cmd);
798         break;
799       default:
800         assert(0);
801       }
802 }
803
804 /* Populate a trbox according to cmd */
805 void 
806 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
807 {
808   trb->populate(trb,cmd);
809 }
810
811 /* Submit and destroy a trbox */
812 void 
813 trbox_finalize(struct trbox *trb)
814 {
815   trb->finalize(trb);
816 }
817
818 /* Initialize the independent samples trbox */
819 void 
820 trbox_independent_samples_init(struct trbox *self,
821                            struct cmd_t_test *cmd unused)
822 {
823   const int hsize=11;
824   const int vsize=cmd->n_variables*2+3;
825
826   assert(self);
827   self->populate = trbox_independent_samples_populate;
828
829   trbox_base_init(self,cmd->n_variables*2,hsize);
830   tab_title(self->t,0,_("Independent Samples Test"));
831   tab_hline(self->t,TAL_1,2,hsize-1,1);
832   tab_vline(self->t,TAL_2,2,0,vsize-1);
833   tab_vline(self->t,TAL_1,4,0,vsize-1);
834   tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
835   tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
836   tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
837   tab_joint_text(self->t, 2, 0, 3, 0, 
838                  TAB_CENTER,_("Levine's Test for Equality of Variances"));
839   tab_joint_text(self->t, 4,0,hsize-1,0,
840                  TAB_CENTER,_("t-test for Equality of Means"));
841
842   tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
843   tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
844   tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
845   tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
846   tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
847   tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
848   tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
849   tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
850   tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
851
852   tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF, 
853                  _("%d%% Confidence Interval of the Difference"),
854                  (int)round(cmd->criteria*100.0));
855
856 }
857
858 /* Populate the independent samples trbox */
859 void 
860 trbox_independent_samples_populate(struct trbox *self,
861                                    struct cmd_t_test *cmd )
862 {
863   int i;
864
865   assert(self);
866   for (i=0; i < cmd->n_variables; ++i)
867     {
868       tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
869
870       tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
871
872       tab_text (self->t, 1, i*2+3+1, 
873                 TAB_LEFT, _("Equal variances not assumed"));
874     }
875 }
876
877 /* Initialize the paired samples trbox */
878 void 
879 trbox_paired_init(struct trbox *self,
880                            struct cmd_t_test *cmd unused)
881 {
882
883   const int hsize=10;
884   const int vsize=n_pairs+3;
885
886   self->populate = trbox_paired_populate;
887
888   trbox_base_init(self,n_pairs,hsize);
889   tab_title (self->t, 0, _("Paired Samples Test"));
890   tab_hline(self->t,TAL_1,2,6,1);
891   tab_vline(self->t,TAL_2,2,0,vsize);
892   tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
893   tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
894   tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
895   tab_hline(self->t,TAL_1,5,6, 2);
896   tab_vline(self->t,TAL_0,6,0,1);
897
898   tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF, 
899                  _("%d%% Confidence Interval of the Difference"),
900                  (int)round(cmd->criteria*100.0));
901
902   tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
903   tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
904   tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
905   tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
906   tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
907   tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
908   tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
909   tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
910 }
911
912 /* Populate the paired samples trbox */
913 void 
914 trbox_paired_populate(struct trbox *trb,
915                               struct cmd_t_test *cmd unused)
916 {
917   int i;
918
919   for (i=0; i < n_pairs; ++i)
920     {
921       int which =1;
922       double p,q;
923       int status;
924       double bound;
925       double se_mean;
926
927       struct variable *v0 = pairs[i].v[0];
928       struct variable *v1 = pairs[i].v[1];
929
930       struct t_test_proc *ttp0 = &v0->p.t_t;
931       struct t_test_proc *ttp1 = &v1->p.t_t;
932
933       double n = ttp0->n;
934       double t;
935       double df = n - 1;
936       
937       tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i); 
938
939       tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
940                 pairs[i].v[0]->name, pairs[i].v[1]->name);
941
942       tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
943
944       tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
945
946       /* SE Mean */
947       se_mean = pairs[i].std_dev_diff / sqrt(n) ;
948       tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
949
950       /* Now work out the confidence interval */
951       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
952       p = 1 - q ;
953       which=2; /* Calc T from p,q and df */
954       cdft(&which, &p, &q, &t, &df, &status, &bound);
955
956       if ( 0 != status )
957         {
958           msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
959         }
960
961       tab_float(trb->t, 5, i+3, TAB_RIGHT, 
962                 pairs[i].mean_diff - t * se_mean , 8, 4); 
963
964       tab_float(trb->t, 6, i+3, TAB_RIGHT, 
965                 pairs[i].mean_diff + t * se_mean , 8, 4); 
966
967       t = ( ttp0->mean - ttp1->mean)
968         / sqrt ( 
969                 (  sqr(ttp0->s_std_dev) + sqr(ttp1->s_std_dev)  - 
970                    2 * pairs[i].correlation * ttp0->s_std_dev * ttp1->s_std_dev                    )
971                 / (n-1) )
972         ;
973
974       tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
975
976       /* Degrees of freedom */
977       tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
978
979       which=1;
980       cdft(&which, &p, &q, &t, &df, &status, &bound);
981
982       if ( 0 != status )
983         {
984           msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
985         }
986
987
988       tab_float(trb->t, 9, i+3, TAB_RIGHT, p*2.0 , 8, 3);
989
990
991     }
992
993 }
994
995 /* Initialize the one sample trbox */
996 void 
997 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
998 {
999   const int hsize=7;
1000   const int vsize=cmd->n_variables+3;
1001
1002   self->populate = trbox_one_sample_populate;
1003
1004   trbox_base_init(self, cmd->n_variables,hsize);
1005   tab_title (self->t, 0, _("One-Sample Test"));
1006   tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1007   tab_vline(self->t, TAL_2, 1, 0, vsize);
1008
1009   tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF, 
1010                  _("Test Value = %f"),cmd->n_testval);
1011
1012   tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1013
1014
1015   tab_joint_text(self->t,5,1,6,1,TAB_CENTER  | TAT_PRINTF, 
1016                  _("%d%% Confidence Interval of the Difference"),
1017                  (int)round(cmd->criteria*100.0));
1018
1019   tab_vline(self->t,TAL_0,6,1,1);
1020   tab_hline(self->t,TAL_1,5,6,2);
1021   tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1022   tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1023   tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1024   tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1025   tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1026   tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1027
1028 }
1029
1030
1031 /* Populate the one sample trbox */
1032 void 
1033 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1034 {
1035   int i;
1036
1037   assert(trb->t);
1038
1039   for (i=0; i < cmd->n_variables; ++i)
1040     {
1041       int which =1;
1042       double t;
1043       double p,q;
1044       double df;
1045       int status;
1046       double bound;
1047       struct t_test_proc *ttp;
1048       ttp= &cmd->v_variables[i]->p.t_t;
1049
1050
1051       tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1052
1053       t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ;
1054
1055       tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1056
1057       /* degrees of freedom */
1058       df = ttp->n - 1;
1059
1060       tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1061
1062       cdft(&which, &p, &q, &t, &df, &status, &bound);
1063
1064       if ( 0 != status )
1065         {
1066           msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1067         }
1068
1069
1070       /* Multiply by 2 to get 2-tailed significance */
1071       tab_float (trb->t, 3, i+3, TAB_RIGHT, q*2.0, 8,3);
1072
1073       tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3);
1074
1075
1076       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
1077       p = 1 - q ;
1078       which=2; /* Calc T from p,q and df */
1079       cdft(&which, &p, &q, &t, &df, &status, &bound);
1080       if ( 0 != status )
1081         {
1082           msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1083         }
1084
1085       tab_float (trb->t, 5, i+3, TAB_RIGHT,
1086                  ttp->mean_diff - t * ttp->se_mean, 8,4);
1087
1088       tab_float (trb->t, 6, i+3, TAB_RIGHT,
1089                  ttp->mean_diff + t * ttp->se_mean, 8,4);
1090     }
1091 }
1092
1093 /* Base initializer for the generalized trbox */
1094 void 
1095 trbox_base_init(struct trbox *self, int data_rows, int cols)
1096 {
1097   const int rows = 3 + data_rows;
1098
1099   self->finalize = trbox_base_finalize;
1100   self->t = tab_create (cols, rows, 0);
1101   tab_headers (self->t,0,0,3,0); 
1102   tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1103   tab_hline(self->t, TAL_2,0,cols-1,3);
1104   tab_dim (self->t, tab_natural_dimensions);
1105 }
1106
1107
1108 /* Base finalizer for the trbox */
1109 void 
1110 trbox_base_finalize(struct trbox *trb)
1111 {
1112   tab_submit(trb->t);
1113 }
1114
1115
1116 /* Create , populate and submit the Paired Samples Correlation box */
1117 void
1118 pscbox(struct cmd_t_test *cmd)
1119 {
1120   const int rows=1+n_pairs;
1121   const int cols=5;
1122   int i;
1123   
1124   struct tab_table *table;
1125   
1126   table = tab_create (cols,rows,0);
1127
1128   tab_columns (table, SOM_COL_DOWN, 1);
1129   tab_headers (table,0,0,1,0); 
1130   tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1131   tab_hline(table, TAL_2, 0, cols - 1, 1);
1132   tab_vline(table, TAL_2, 2, 0, rows - 1);
1133   tab_dim(table, tab_natural_dimensions);
1134   tab_title(table, 0, _("Paired Samples Correlations"));
1135
1136   /* column headings */
1137   tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1138   tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1139   tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1140
1141
1142   for (i=0; i < n_pairs; ++i)
1143     {
1144       int which =1;
1145       double p,q;
1146
1147       int status;
1148       double bound;
1149
1150       double df = pairs[i].v[0]->p.t_t.n -2;
1151
1152       double correlation_t = 
1153         pairs[i].correlation * sqrt(df) /
1154         sqrt(1 - sqr(pairs[i].correlation));
1155
1156
1157       /* row headings */
1158       tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF, 
1159                _("Pair %d"), i);
1160       
1161       tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF, 
1162                _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1163
1164
1165       /* row data */
1166       tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1167       tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.n , 4, 0);
1168
1169
1170       cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1171
1172       if ( 0 != status )
1173         {
1174           msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1175         }
1176
1177
1178       tab_float(table, 4, i+1, TAB_RIGHT, q*2.0, 8, 3);
1179
1180
1181
1182       
1183     }
1184
1185   tab_submit(table);
1186 }
1187
1188
1189
1190 /* Calculation Implementation */
1191
1192 /* Per case calculations common to all variants of the T test */
1193 static int 
1194 common_calc (struct ccase *c)
1195 {
1196   int i;
1197
1198   double weight = dict_get_case_weight(default_dict,c);
1199
1200   for(i=0; i< cmd.n_variables ; ++i) 
1201     {
1202       struct t_test_proc *ttp;
1203       struct variable *v = cmd.v_variables[i];
1204       union value *val = &c->data[v->fv];
1205
1206       ttp= &cmd.v_variables[i]->p.t_t;
1207
1208       if (val->f != SYSMIS) 
1209         {
1210           ttp->n+=weight;
1211           ttp->sum+=weight * val->f;
1212           ttp->ssq+=weight * val->f * val->f;
1213         }
1214     }
1215   return 0;
1216 }
1217
1218 /* Pre calculations common to all variants of the T test */
1219 static void 
1220 common_precalc (void)
1221 {
1222   int i=0;
1223
1224   for(i=0; i< cmd.n_variables ; ++i) 
1225     {
1226       struct t_test_proc *ttp;
1227       ttp= &cmd.v_variables[i]->p.t_t;
1228       
1229       ttp->sum=0;
1230       ttp->n=0;
1231       ttp->ssq=0;
1232       ttp->sum_diff=0;
1233     }
1234 }
1235
1236 /* Post calculations common to all variants of the T test */
1237 void 
1238 common_postcalc (void)
1239 {
1240   int i=0;
1241
1242   for(i=0; i< cmd.n_variables ; ++i) 
1243     {
1244       struct t_test_proc *ttp;
1245       ttp= &cmd.v_variables[i]->p.t_t;
1246       
1247       ttp->mean=ttp->sum / ttp->n;
1248       ttp->s_std_dev= sqrt(
1249                          ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1250                          ) ;
1251
1252       ttp->std_dev= sqrt(
1253                          ttp->n/(ttp->n-1) *
1254                          ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1255                          ) ;
1256
1257       ttp->se_mean = ttp->std_dev / sqrt(ttp->n);
1258       ttp->mean_diff= ttp->sum_diff / ttp->n;
1259     }
1260 }
1261
1262 /* Per case calculations for one sample t test  */
1263 static int 
1264 one_sample_calc (struct ccase *c)
1265 {
1266   int i;
1267
1268   double weight = dict_get_case_weight(default_dict,c);
1269
1270   for(i=0; i< cmd.n_variables ; ++i) 
1271     {
1272       struct t_test_proc *ttp;
1273       struct variable *v = cmd.v_variables[i];
1274       union value *val = &c->data[v->fv];
1275
1276       ttp= &cmd.v_variables[i]->p.t_t;
1277       
1278       if (val->f != SYSMIS) 
1279         ttp->sum_diff += weight * (val->f - cmd.n_testval);
1280     }
1281
1282   return 0;
1283 }
1284
1285 /* Pre calculations for one sample t test */
1286 static void 
1287 one_sample_precalc (void)
1288 {
1289   int i=0;
1290   
1291   for(i=0; i< cmd.n_variables ; ++i) 
1292     {
1293       struct t_test_proc *ttp;
1294       ttp= &cmd.v_variables[i]->p.t_t;
1295       
1296       ttp->sum_diff=0;
1297     }
1298 }
1299
1300 /* Post calculations for one sample t test */
1301 static void 
1302 one_sample_postcalc (void)
1303 {
1304   int i=0;
1305   
1306   for(i=0; i< cmd.n_variables ; ++i) 
1307     {
1308       struct t_test_proc *ttp;
1309       ttp= &cmd.v_variables[i]->p.t_t;
1310
1311       
1312       ttp->mean_diff = ttp->sum_diff / ttp->n ;
1313     }
1314 }
1315
1316
1317
1318 static int
1319 compare_var_name (const void *a_, const void *b_, void *v_ unused)
1320 {
1321   const struct variable *a = a_;
1322   const struct variable *b = b_;
1323
1324   return strcmp(a->name,b->name);
1325 }
1326
1327 static unsigned
1328 hash_var_name (const void *a_, void *v_ unused)
1329 {
1330   const struct variable *a = a_;
1331
1332   return hsh_hash_bytes (a->name, strlen(a->name));
1333 }
1334
1335
1336 static void 
1337 paired_precalc (void)
1338 {
1339   int i;
1340
1341   for(i=0; i < n_pairs ; ++i )
1342     {
1343       pairs[i].correlation=0;
1344       pairs[i].sum_of_diffs=0;
1345       pairs[i].ssq_diffs=0;
1346     }
1347
1348 }
1349
1350 static int  
1351 paired_calc (struct ccase *c)
1352 {
1353   int i;
1354
1355   for(i=0; i < n_pairs ; ++i )
1356     {
1357       struct variable *v0 = pairs[i].v[0];
1358       struct variable *v1 = pairs[i].v[1];
1359
1360       union value *val0 = &c->data[v0->fv];
1361       union value *val1 = &c->data[v1->fv];
1362
1363       pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.mean )
1364                               *
1365                               ( val1->f - pairs[i].v[1]->p.t_t.mean );
1366
1367       pairs[i].sum_of_diffs += val0->f - val1->f ;
1368       pairs[i].ssq_diffs += sqr(val0->f - val1->f);
1369
1370     }
1371
1372
1373   return 0;
1374 }
1375
1376 static void 
1377 paired_postcalc (void)
1378 {
1379   int i;
1380
1381   for(i=0; i < n_pairs ; ++i )
1382     {
1383       const double n = pairs[i].v[0]->p.t_t.n ;
1384       
1385       pairs[i].correlation /= pairs[i].v[0]->p.t_t.std_dev * 
1386                               pairs[i].v[1]->p.t_t.std_dev ;
1387       pairs[i].correlation /= pairs[i].v[0]->p.t_t.n -1; 
1388
1389
1390       pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1391
1392
1393       pairs[i].std_dev_diff = sqrt (  n / (n - 1) * (
1394                                     ( pairs[i].ssq_diffs / n )
1395                                     - 
1396                                     sqr(pairs[i].mean_diff )
1397                                     ) );
1398
1399     }
1400 }