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