1 /* PSPP - computes sample statistics. -*-c-*-
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
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.
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.
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
29 #include "dcdflib/cdflib.h"
36 #include "value-labels.h"
45 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
47 +missing=miss:!analysis/listwise,
48 incl:include/!exclude;
49 format=fmt:!labels/nolabels;
50 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
55 static struct cmd_t_test cmd;
59 static struct pool *t_test_pool ;
61 /* Variable for the GROUPS subcommand, if given. */
62 static struct variable *groups;
64 /* GROUPS: Number of values specified by the user; the values
66 static int n_groups_values;
67 static union value groups_values[2];
69 /* PAIRS: Number of pairs to be compared ; each pair. */
71 typedef struct variable *pair_t[2] ;
75 static int parse_value (union value * v, int type) ;
78 /* Structures and Functions for the Statistics Summary Box */
80 typedef void populate_ssbox_func(struct ssbox *ssb,
81 struct cmd_t_test *cmd);
82 typedef void finalize_ssbox_func(struct ssbox *ssb);
88 populate_ssbox_func *populate;
89 finalize_ssbox_func *finalize;
94 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
96 /* Populate a ssbox according to cmd */
97 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
99 /* Submit and destroy a ssbox */
100 void ssbox_finalize(struct ssbox *ssb);
102 /* A function to create, populate and submit the Paired Samples Correlation
104 void pscbox(struct cmd_t_test *cmd);
107 /* Structures and Functions for the Test Results Box */
110 typedef void populate_trbox_func(struct trbox *trb,
111 struct cmd_t_test *cmd);
112 typedef void finalize_trbox_func(struct trbox *trb);
116 populate_trbox_func *populate;
117 finalize_trbox_func *finalize;
121 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
123 /* Populate a ssbox according to cmd */
124 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
126 /* Submit and destroy a ssbox */
127 void trbox_finalize(struct trbox *trb);
129 /* Which mode was T-TEST invoked */
137 static int common_calc (struct ccase *);
138 static void common_precalc (void);
139 static void common_postcalc (void);
141 static int one_sample_calc (struct ccase *);
142 static void one_sample_precalc (void);
143 static void one_sample_postcalc (void);
151 struct ssbox stat_summary_box;
152 struct trbox test_results_box;
154 if (!lex_force_match_id ("T"))
158 lex_match_id ("TEST");
160 if ( !parse_t_test(&cmd) )
163 if (! cmd.sbc_criteria)
166 if ( cmd.sbc_testval + cmd.sbc_groups + cmd.sbc_pairs != 1 )
169 _("Exactly one of TESTVAL, GROUPS or PAIRS subcommands is required")
176 else if (cmd.sbc_groups)
181 if ( mode == T_PAIRED && cmd.sbc_variables)
183 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
187 procedure(common_precalc,common_calc,common_postcalc);
189 if (mode == T_1_SAMPLE)
190 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc);
192 t_test_pool = pool_create ();
194 ssbox_create(&stat_summary_box,&cmd,mode);
195 ssbox_populate(&stat_summary_box,&cmd);
196 ssbox_finalize(&stat_summary_box);
198 if ( mode == T_PAIRED)
203 trbox_create(&test_results_box,&cmd,mode);
204 trbox_populate(&test_results_box,&cmd);
205 trbox_finalize(&test_results_box);
207 pool_destroy (t_test_pool);
215 tts_custom_groups (struct cmd_t_test *cmd unused)
219 if (token != T_ALL &&
220 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
223 msg(SE,_("`%s' is not a variable name"),tokid);
227 groups = parse_variable ();
230 lex_error ("expecting variable name in GROUPS subcommand");
234 if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
236 msg (SE, _("Long string variable %s is not valid here."),
241 if (!lex_match ('('))
243 if (groups->type == NUMERIC)
246 groups_values[0].f = 1;
247 groups_values[1].f = 2;
252 msg (SE, _("When applying GROUPS to a string variable, at "
253 "least one value must be specified."));
258 if (!parse_value (&groups_values[0],groups->type))
266 if (!parse_value (&groups_values[1],groups->type))
270 if (!lex_force_match (')'))
280 tts_custom_pairs (struct cmd_t_test *cmd unused)
282 struct variable **vars;
286 int n_after_WITH = -1;
287 int paired ; /* Was the PAIRED keyword given ? */
291 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
294 msg(SE,_("`%s' is not a variable name"),tokid);
299 if (!parse_variables (default_dict, &vars, &n_vars,
300 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
308 if (lex_match (T_WITH))
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))
318 n_after_WITH = n_vars - n_before_WITH;
321 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
323 /* Determine the number of pairs needed */
326 if (n_before_WITH != n_after_WITH)
329 msg (SE, _("PAIRED was specified but the number of variables "
330 "preceding WITH (%d) did not match the number "
332 n_before_WITH, n_after_WITH );
335 n_pairs=n_before_WITH;
337 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
339 n_pairs=n_before_WITH * n_after_WITH ;
341 else /* Neither WITH nor PAIRED keyword given */
346 msg (SE, _("At least two variables must be specified "
351 /* how many ways can you pick 2 from n_vars ? */
352 n_pairs = n_vars * (n_vars -1 ) /2 ;
355 /* Allocate storage for the pairs */
356 pairs = xrealloc(pairs,sizeof(pair_t) *n_pairs);
358 /* Populate the pairs with the appropriate variables */
363 assert(n_pairs == n_vars/2);
364 for (i = 0; i < n_pairs ; ++i)
366 pairs[i][0] = vars[i];
367 pairs[i][1] = vars[i+n_pairs];
370 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
375 for(i=0 ; i < n_before_WITH ; ++i )
377 for(j=0 ; j < n_after_WITH ; ++j)
379 pairs[p][0] = vars[i];
380 pairs[p][1] = vars[j+n_before_WITH];
385 else /* Neither WITH nor PAIRED given */
390 for(i=0 ; i < n_vars ; ++i )
392 for(j=i+1 ; j < n_vars ; ++j)
394 pairs[p][0] = vars[i];
395 pairs[p][1] = vars[j];
404 /* Parses the current token (numeric or string, depending on type)
405 value v and returns success. */
407 parse_value (union value * v, int type )
411 if (!lex_force_num ())
417 if (!lex_force_string ())
419 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
428 /* Implementation of the SSBOX object */
430 void ssbox_base_init(struct ssbox *this, int cols,int rows);
432 void ssbox_base_finalize(struct ssbox *ssb);
434 void ssbox_one_sample_init(struct ssbox *this,
435 struct cmd_t_test *cmd );
437 void ssbox_independent_samples_init(struct ssbox *this,
438 struct cmd_t_test *cmd);
440 void ssbox_paired_init(struct ssbox *this,
441 struct cmd_t_test *cmd);
443 /* Factory to create an ssbox */
445 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
450 ssbox_one_sample_init(ssb,cmd);
453 ssbox_independent_samples_init(ssb,cmd);
456 ssbox_paired_init(ssb,cmd);
464 /* Despatcher for the populate method */
466 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
468 ssb->populate(ssb,cmd);
472 /* Despatcher for finalize */
474 ssbox_finalize(struct ssbox *ssb)
480 /* Submit the box and clear up */
482 ssbox_base_finalize(struct ssbox *ssb)
487 /* Initialize a ssbox struct */
489 ssbox_base_init(struct ssbox *this, int cols,int rows)
491 this->finalize = ssbox_base_finalize;
492 this->t = tab_create (cols, rows, 0);
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);
501 void ssbox_one_sample_populate(struct ssbox *ssb,
502 struct cmd_t_test *cmd);
504 /* Initialize the one_sample ssbox */
506 ssbox_one_sample_init(struct ssbox *this,
507 struct cmd_t_test *cmd )
510 const int vsize=cmd->n_variables+1;
512 this->populate = ssbox_one_sample_populate;
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"));
523 void ssbox_independent_samples_populate(struct ssbox *ssb,
524 struct cmd_t_test *cmd);
526 /* Initialize the independent samples ssbox */
528 ssbox_independent_samples_init(struct ssbox *this,
529 struct cmd_t_test *cmd)
532 int vsize = cmd->n_variables*2 +1;
534 this->populate = ssbox_independent_samples_populate;
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"));
547 /* Populate the ssbox for independent samples */
549 ssbox_independent_samples_populate(struct ssbox *ssb,
550 struct cmd_t_test *cmd)
557 if ( groups->type == NUMERIC )
559 val_lab1 = val_labs_find( groups->val_labs,groups_values[0]);
560 val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
564 val_lab1 = groups_values[0].s;
565 val_lab2 = groups_values[1].s;
570 for (i=0; i < cmd->n_variables; ++i)
572 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
575 tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
577 tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
580 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
582 tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
587 void ssbox_paired_populate(struct ssbox *ssb,
588 struct cmd_t_test *cmd);
590 /* Initialize the paired values ssbox */
592 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd unused)
596 int vsize = n_pairs*2+1;
598 this->populate = ssbox_paired_populate;
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"));
611 /* Populate the ssbox for paired values */
613 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd unused)
619 ds_init(t_test_pool,&ds,15);
621 for (i=0; i < n_pairs; ++i)
625 ds_printf(&ds,_("Pair %d"),i);
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);
635 /* Populate the one sample ssbox */
637 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
643 for (i=0; i < cmd->n_variables; ++i)
645 struct t_test_proc *ttp;
646 ttp= &cmd->v_variables[i]->p.t_t;
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);
659 /* Implementation of the Test Results box struct */
661 void trbox_base_init(struct trbox *self,int n_vars, int cols);
662 void trbox_base_finalize(struct trbox *trb);
664 void trbox_independent_samples_init(struct trbox *trb,
665 struct cmd_t_test *cmd );
667 void trbox_independent_samples_populate(struct trbox *trb,
668 struct cmd_t_test *cmd);
670 void trbox_one_sample_init(struct trbox *self,
671 struct cmd_t_test *cmd );
673 void trbox_one_sample_populate(struct trbox *trb,
674 struct cmd_t_test *cmd);
676 void trbox_paired_init(struct trbox *self,
677 struct cmd_t_test *cmd );
679 void trbox_paired_populate(struct trbox *trb,
680 struct cmd_t_test *cmd);
684 /* Create a trbox according to mode*/
686 trbox_create(struct trbox *trb,
687 struct cmd_t_test *cmd, int mode)
692 trbox_one_sample_init(trb,cmd);
695 trbox_independent_samples_init(trb,cmd);
698 trbox_paired_init(trb,cmd);
705 /* Populate a trbox according to cmd */
707 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
709 trb->populate(trb,cmd);
712 /* Submit and destroy a trbox */
714 trbox_finalize(struct trbox *trb)
719 /* Initialize the independent samples trbox */
721 trbox_independent_samples_init(struct trbox *self,
722 struct cmd_t_test *cmd unused)
725 const int vsize=cmd->n_variables*2+3;
730 self->populate = trbox_independent_samples_populate;
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"));
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"));
755 ds_init(t_test_pool,&ds,80);
757 ds_printf(&ds,_("%d%% Confidence Interval of the Difference"),
758 (int)round(cmd->criteria*100.0));
760 tab_joint_text(self->t,9,1,10,1,TAB_CENTER, ds.string);
765 /* Populate the independent samples trbox */
767 trbox_independent_samples_populate(struct trbox *self,
768 struct cmd_t_test *cmd )
773 for (i=0; i < cmd->n_variables; ++i)
775 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
777 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
779 tab_text (self->t, 1, i*2+3+1,
780 TAB_LEFT, _("Equal variances not assumed"));
784 /* Initialize the paired samples trbox */
786 trbox_paired_init(struct trbox *self,
787 struct cmd_t_test *cmd unused)
791 const int vsize=n_pairs*2+3;
795 self->populate = trbox_paired_populate;
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);
807 ds_init(t_test_pool,&ds,80);
809 ds_printf(&ds,_("%d%% Confidence Interval of the Difference"),
810 (int)round(cmd->criteria*100.0));
812 tab_joint_text(self->t,5,1,6,1,TAB_CENTER, ds.string);
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)"));
826 /* Populate the paired samples trbox */
828 trbox_paired_populate(struct trbox *trb,
829 struct cmd_t_test *cmd unused)
834 ds_init(t_test_pool,&ds,15);
836 for (i=0; i < n_pairs; ++i)
839 ds_printf(&ds,_("Pair %d"),i);
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);
848 /* Initialize the one sample trbox */
850 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
853 const int vsize=cmd->n_variables+3;
857 self->populate = trbox_one_sample_populate;
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);
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);
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"));
885 /* Populate the one sample trbox */
887 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
893 for (i=0; i < cmd->n_variables; ++i)
901 struct t_test_proc *ttp;
902 ttp= &cmd->v_variables[i]->p.t_t;
905 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
907 t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ;
909 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
911 /* degrees of freedom */
914 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
916 cdft(&which, &p, &q, &t, &df, &status, &bound);
920 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
924 /* Multiply by 2 to get 2-tailed significance */
925 tab_float (trb->t, 3, i+3, TAB_RIGHT, q*2.0, 8,3);
927 tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3);
930 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
932 which=2; /* Calc T from p,q and df */
933 cdft(&which, &p, &q, &t, &df, &status, &bound);
936 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
939 tab_float (trb->t, 5, i+3, TAB_RIGHT,
940 ttp->mean_diff - t * ttp->se_mean, 8,4);
942 tab_float (trb->t, 6, i+3, TAB_RIGHT,
943 ttp->mean_diff + t * ttp->se_mean, 8,4);
947 /* Base initializer for the generalized trbox */
949 trbox_base_init(struct trbox *self, int data_rows, int cols)
951 const int rows = 3 + data_rows;
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);
962 /* Base finalizer for the trbox */
964 trbox_base_finalize(struct trbox *trb)
970 /* Create , populate and submit the Paired Samples Correlation box */
972 pscbox(struct cmd_t_test *cmd)
974 const int rows=1+n_pairs;
978 struct tab_table *table;
980 table = tab_create (cols,rows,0);
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"));
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."));
999 ds_init(t_test_pool,&ds,15);
1001 for (i=0; i < n_pairs; ++i)
1004 ds_printf(&ds,_("Pair %d"),i);
1005 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE, ds.string);
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);
1021 /* Calculation Implementation */
1023 /* Per case calculations common to all variants of the T test */
1025 common_calc (struct ccase *c)
1029 double weight = dict_get_case_weight(default_dict,c);
1031 for(i=0; i< cmd.n_variables ; ++i)
1033 struct t_test_proc *ttp;
1034 struct variable *v = cmd.v_variables[i];
1035 union value *val = &c->data[v->fv];
1037 ttp= &cmd.v_variables[i]->p.t_t;
1039 if (val->f != SYSMIS)
1042 ttp->sum+=weight * val->f;
1043 ttp->ssq+=weight * val->f * val->f;
1049 /* Pre calculations common to all variants of the T test */
1051 common_precalc (void)
1055 for(i=0; i< cmd.n_variables ; ++i)
1057 struct t_test_proc *ttp;
1058 ttp= &cmd.v_variables[i]->p.t_t;
1067 /* Post calculations common to all variants of the T test */
1069 common_postcalc (void)
1073 for(i=0; i< cmd.n_variables ; ++i)
1075 struct t_test_proc *ttp;
1076 ttp= &cmd.v_variables[i]->p.t_t;
1078 ttp->mean=ttp->sum / ttp->n;
1081 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1084 ttp->se_mean = ttp->std_dev / sqrt(ttp->n);
1086 ttp->mean_diff= ttp->sum_diff / ttp->n;
1090 /* Per case calculations for one sample t test */
1092 one_sample_calc (struct ccase *c)
1096 double weight = dict_get_case_weight(default_dict,c);
1098 for(i=0; i< cmd.n_variables ; ++i)
1100 struct t_test_proc *ttp;
1101 struct variable *v = cmd.v_variables[i];
1102 union value *val = &c->data[v->fv];
1104 ttp= &cmd.v_variables[i]->p.t_t;
1106 if (val->f != SYSMIS)
1107 ttp->sum_diff += weight * (val->f - cmd.n_testval);
1113 /* Pre calculations for one sample t test */
1115 one_sample_precalc (void)
1119 for(i=0; i< cmd.n_variables ; ++i)
1121 struct t_test_proc *ttp;
1122 ttp= &cmd.v_variables[i]->p.t_t;
1128 /* Post calculations for one sample t test */
1130 one_sample_postcalc (void)
1134 for(i=0; i< cmd.n_variables ; ++i)
1136 struct t_test_proc *ttp;
1137 ttp= &cmd.v_variables[i]->p.t_t;
1140 ttp->mean_diff = ttp->sum_diff / ttp->n ;