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