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