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"
49 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
51 +missing=miss:!analysis/listwise,
52 incl:include/!exclude;
53 format=fmt:!labels/nolabels;
54 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
59 static struct cmd_t_test cmd;
62 static struct pool *t_test_pool ;
64 /* Variable for the GROUPS subcommand, if given. */
65 static struct variable *groups;
67 /* GROUPS: Number of values specified by the user; the values
70 static int n_groups_values;
71 static union value groups_values[2];
74 /* PAIRS: Number of pairs to be compared ; each pair. */
75 static int n_pairs = 0 ;
78 /* The variables comprising the pair */
79 struct variable *v[2];
81 /* The correlation coefficient between the variables */
84 /* The sum of the differences */
87 /* The mean of the differences */
90 /* The sum of the squares of the differences */
93 /* The std deviation of the differences */
96 static struct pair *pairs=0;
99 static int parse_value (union value * v, int type) ;
102 /* Structures and Functions for the Statistics Summary Box */
104 typedef void populate_ssbox_func(struct ssbox *ssb,
105 struct cmd_t_test *cmd);
106 typedef void finalize_ssbox_func(struct ssbox *ssb);
112 populate_ssbox_func *populate;
113 finalize_ssbox_func *finalize;
118 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
120 /* Populate a ssbox according to cmd */
121 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
123 /* Submit and destroy a ssbox */
124 void ssbox_finalize(struct ssbox *ssb);
126 /* A function to create, populate and submit the Paired Samples Correlation
131 /* Structures and Functions for the Test Results Box */
134 typedef void populate_trbox_func(struct trbox *trb,
135 struct cmd_t_test *cmd);
136 typedef void finalize_trbox_func(struct trbox *trb);
140 populate_trbox_func *populate;
141 finalize_trbox_func *finalize;
145 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
147 /* Populate a ssbox according to cmd */
148 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
150 /* Submit and destroy a ssbox */
151 void trbox_finalize(struct trbox *trb);
153 /* Which mode was T-TEST invoked */
161 static int common_calc (struct ccase *, void *);
162 static void common_precalc (void *);
163 static void common_postcalc (void *);
165 static int one_sample_calc (struct ccase *, void *);
166 static void one_sample_precalc (void *);
167 static void one_sample_postcalc (void *);
169 static int paired_calc (struct ccase *, void *);
170 static void paired_precalc (void *);
171 static void paired_postcalc (void *);
173 static void group_precalc (void *);
174 static int group_calc (struct ccase *, void *);
175 static void group_postcalc (void *);
178 static int compare_var_name (const void *a_, const void *b_, void *v_ UNUSED);
179 static unsigned hash_var_name (const void *a_, void *v_ UNUSED);
188 struct ssbox stat_summary_box;
189 struct trbox test_results_box;
191 if (!lex_force_match_id ("T"))
195 lex_match_id ("TEST");
197 if ( !parse_t_test(&cmd) )
200 if (! cmd.sbc_criteria)
205 if (cmd.sbc_testval) ++m;
206 if (cmd.sbc_groups) ++m;
207 if (cmd.sbc_pairs) ++m;
212 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
220 else if (cmd.sbc_groups)
225 if ( mode == T_PAIRED)
227 if (cmd.sbc_variables)
229 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
234 /* Iterate through the pairs and put each variable that is a
235 member of a pair into cmd.v_variables */
238 struct hsh_iterator hi;
239 struct hsh_table *hash;
242 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
244 for (i=0; i < n_pairs; ++i)
246 hsh_insert(hash,pairs[i].v[0]);
247 hsh_insert(hash,pairs[i].v[1]);
250 assert(cmd.n_variables == 0);
251 cmd.n_variables = hsh_count(hash);
253 cmd.v_variables = xrealloc(cmd.v_variables,
254 sizeof(struct variable) * cmd.n_variables);
255 /* Iterate through the hash */
256 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
258 v=hsh_next(hash,&hi) )
259 cmd.v_variables[i++]=v;
266 procedure(common_precalc,common_calc,common_postcalc, NULL);
271 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc, NULL);
274 procedure(paired_precalc,paired_calc,paired_postcalc, NULL);
277 procedure(group_precalc,group_calc,group_postcalc, NULL);
278 levene(groups, cmd.n_variables, cmd.v_variables);
282 t_test_pool = pool_create ();
284 ssbox_create(&stat_summary_box,&cmd,mode);
285 ssbox_populate(&stat_summary_box,&cmd);
286 ssbox_finalize(&stat_summary_box);
288 if ( mode == T_PAIRED)
291 trbox_create(&test_results_box,&cmd,mode);
292 trbox_populate(&test_results_box,&cmd);
293 trbox_finalize(&test_results_box);
295 pool_destroy (t_test_pool);
305 if ( mode == T_IND_SAMPLES)
308 /* Destroy any group statistics we created */
309 for (i= 0 ; i < cmd.n_variables ; ++i )
311 free(cmd.v_variables[i]->p.t_t.gs);
319 tts_custom_groups (struct cmd_t_test *cmd UNUSED)
323 if (token != T_ALL &&
324 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
327 msg(SE,_("`%s' is not a variable name"),tokid);
331 groups = parse_variable ();
334 lex_error ("expecting variable name in GROUPS subcommand");
338 if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
340 msg (SE, _("Long string variable %s is not valid here."),
345 if (!lex_match ('('))
347 if (groups->type == NUMERIC)
350 groups_values[0].f = 1;
351 groups_values[1].f = 2;
356 msg (SE, _("When applying GROUPS to a string variable, at "
357 "least one value must be specified."));
362 if (!parse_value (&groups_values[0],groups->type))
370 if (!parse_value (&groups_values[1],groups->type))
374 if (!lex_force_match (')'))
384 tts_custom_pairs (struct cmd_t_test *cmd UNUSED)
386 struct variable **vars;
391 int n_after_WITH = -1;
392 int paired ; /* Was the PAIRED keyword given ? */
396 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
399 msg(SE,_("`%s' is not a variable name"),tokid);
404 if (!parse_variables (default_dict, &vars, &n_vars,
405 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
413 if (lex_match (T_WITH))
415 n_before_WITH = n_vars;
416 if (!parse_variables (default_dict, &vars, &n_vars,
417 PV_DUPLICATE | PV_APPEND
418 | PV_NUMERIC | PV_NO_SCRATCH))
423 n_after_WITH = n_vars - n_before_WITH;
426 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
428 /* Determine the number of pairs needed */
431 if (n_before_WITH != n_after_WITH)
434 msg (SE, _("PAIRED was specified but the number of variables "
435 "preceding WITH (%d) did not match the number "
437 n_before_WITH, n_after_WITH );
440 n_pairs_local=n_before_WITH;
442 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
444 n_pairs_local=n_before_WITH * n_after_WITH ;
446 else /* Neither WITH nor PAIRED keyword given */
451 msg (SE, _("At least two variables must be specified "
456 /* how many ways can you pick 2 from n_vars ? */
457 n_pairs_local = n_vars * (n_vars -1 ) /2 ;
461 /* Allocate storage for the pairs */
462 pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
464 /* Populate the pairs with the appropriate variables */
469 assert(n_pairs_local == n_vars/2);
470 for (i = 0; i < n_pairs_local ; ++i)
472 pairs[i].v[n_pairs+0] = vars[i];
473 pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
476 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
481 for(i=0 ; i < n_before_WITH ; ++i )
483 for(j=0 ; j < n_after_WITH ; ++j)
485 pairs[p].v[0] = vars[i];
486 pairs[p].v[1] = vars[j+n_before_WITH];
491 else /* Neither WITH nor PAIRED given */
496 for(i=0 ; i < n_vars ; ++i )
498 for(j=i+1 ; j < n_vars ; ++j)
500 pairs[p].v[0] = vars[i];
501 pairs[p].v[1] = vars[j];
507 n_pairs+=n_pairs_local;
512 /* Parses the current token (numeric or string, depending on type)
513 value v and returns success. */
515 parse_value (union value * v, int type )
519 if (!lex_force_num ())
525 if (!lex_force_string ())
527 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
536 /* Implementation of the SSBOX object */
538 void ssbox_base_init(struct ssbox *this, int cols,int rows);
540 void ssbox_base_finalize(struct ssbox *ssb);
542 void ssbox_one_sample_init(struct ssbox *this,
543 struct cmd_t_test *cmd );
545 void ssbox_independent_samples_init(struct ssbox *this,
546 struct cmd_t_test *cmd);
548 void ssbox_paired_init(struct ssbox *this,
549 struct cmd_t_test *cmd);
551 /* Factory to create an ssbox */
553 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
558 ssbox_one_sample_init(ssb,cmd);
561 ssbox_independent_samples_init(ssb,cmd);
564 ssbox_paired_init(ssb,cmd);
572 /* Despatcher for the populate method */
574 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
576 ssb->populate(ssb,cmd);
580 /* Despatcher for finalize */
582 ssbox_finalize(struct ssbox *ssb)
588 /* Submit the box and clear up */
590 ssbox_base_finalize(struct ssbox *ssb)
595 /* Initialize a ssbox struct */
597 ssbox_base_init(struct ssbox *this, int cols,int rows)
599 this->finalize = ssbox_base_finalize;
600 this->t = tab_create (cols, rows, 0);
602 tab_columns (this->t, SOM_COL_DOWN, 1);
603 tab_headers (this->t,0,0,1,0);
604 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
605 tab_hline(this->t, TAL_2,0,cols-1,1);
606 tab_dim (this->t, tab_natural_dimensions);
609 void ssbox_one_sample_populate(struct ssbox *ssb,
610 struct cmd_t_test *cmd);
612 /* Initialize the one_sample ssbox */
614 ssbox_one_sample_init(struct ssbox *this,
615 struct cmd_t_test *cmd )
618 const int vsize=cmd->n_variables+1;
620 this->populate = ssbox_one_sample_populate;
622 ssbox_base_init(this, hsize,vsize);
623 tab_title (this->t, 0, _("One-Sample Statistics"));
624 tab_vline(this->t, TAL_2, 1,0,vsize);
625 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
626 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
627 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
628 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
631 void ssbox_independent_samples_populate(struct ssbox *ssb,
632 struct cmd_t_test *cmd);
634 /* Initialize the independent samples ssbox */
636 ssbox_independent_samples_init(struct ssbox *this,
637 struct cmd_t_test *cmd)
640 int vsize = cmd->n_variables*2 +1;
642 this->populate = ssbox_independent_samples_populate;
644 ssbox_base_init(this, hsize,vsize);
645 tab_title (this->t, 0, _("Group Statistics"));
646 tab_vline(this->t,0,1,0,vsize);
647 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, groups->name);
648 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
649 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
650 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
651 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
655 /* Populate the ssbox for independent samples */
657 ssbox_independent_samples_populate(struct ssbox *ssb,
658 struct cmd_t_test *cmd)
665 if ( groups->type == NUMERIC )
667 val_lab1 = val_labs_find( groups->val_labs,groups_values[0]);
668 val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
672 val_lab1 = groups_values[0].s;
673 val_lab2 = groups_values[1].s;
678 for (i=0; i < cmd->n_variables; ++i)
682 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
685 tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
687 tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
691 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
693 tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
695 /* Fill in the group statistics */
696 for ( g=0; g < 2 ; ++g )
698 struct group_statistics *gs = &cmd->v_variables[i]->p.t_t.gs[g];
700 tab_float(ssb->t, 2 ,i*2+g+1, TAB_RIGHT, gs->n, 2, 0);
701 tab_float(ssb->t, 3 ,i*2+g+1, TAB_RIGHT, gs->mean, 8, 2);
702 tab_float(ssb->t, 4 ,i*2+g+1, TAB_RIGHT, gs->std_dev, 8, 3);
703 tab_float(ssb->t, 5 ,i*2+g+1, TAB_RIGHT, gs->se_mean, 8, 3);
709 void ssbox_paired_populate(struct ssbox *ssb,
710 struct cmd_t_test *cmd);
712 /* Initialize the paired values ssbox */
714 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
718 int vsize = n_pairs*2+1;
720 this->populate = ssbox_paired_populate;
722 ssbox_base_init(this, hsize,vsize);
723 tab_title (this->t, 0, _("Paired Sample Statistics"));
724 tab_vline(this->t,TAL_0,1,0,vsize-1);
725 tab_vline(this->t,TAL_2,2,0,vsize-1);
726 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
727 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
728 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
729 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
733 /* Populate the ssbox for paired values */
735 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
741 for (i=0; i < n_pairs; ++i)
745 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
747 for (j=0 ; j < 2 ; ++j)
749 struct group_statistics *gs;
751 gs=&pairs[i].v[j]->p.t_t.ugs;
755 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
758 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, gs->mean, 8, 2);
759 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, gs->n, 2, 0);
760 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, gs->std_dev, 8, 3);
761 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, gs->se_mean, 8, 3);
769 /* Populate the one sample ssbox */
771 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
777 for (i=0; i < cmd->n_variables; ++i)
779 struct group_statistics *gs;
780 gs= &cmd->v_variables[i]->p.t_t.ugs;
782 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
783 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 0);
784 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
785 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
786 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
793 /* Implementation of the Test Results box struct */
795 void trbox_base_init(struct trbox *self,int n_vars, int cols);
796 void trbox_base_finalize(struct trbox *trb);
798 void trbox_independent_samples_init(struct trbox *trb,
799 struct cmd_t_test *cmd );
801 void trbox_independent_samples_populate(struct trbox *trb,
802 struct cmd_t_test *cmd);
804 void trbox_one_sample_init(struct trbox *self,
805 struct cmd_t_test *cmd );
807 void trbox_one_sample_populate(struct trbox *trb,
808 struct cmd_t_test *cmd);
810 void trbox_paired_init(struct trbox *self,
811 struct cmd_t_test *cmd );
813 void trbox_paired_populate(struct trbox *trb,
814 struct cmd_t_test *cmd);
818 /* Create a trbox according to mode*/
820 trbox_create(struct trbox *trb,
821 struct cmd_t_test *cmd, int mode)
826 trbox_one_sample_init(trb,cmd);
829 trbox_independent_samples_init(trb,cmd);
832 trbox_paired_init(trb,cmd);
839 /* Populate a trbox according to cmd */
841 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
843 trb->populate(trb,cmd);
846 /* Submit and destroy a trbox */
848 trbox_finalize(struct trbox *trb)
853 /* Initialize the independent samples trbox */
855 trbox_independent_samples_init(struct trbox *self,
856 struct cmd_t_test *cmd UNUSED)
859 const int vsize=cmd->n_variables*2+3;
862 self->populate = trbox_independent_samples_populate;
864 trbox_base_init(self,cmd->n_variables*2,hsize);
865 tab_title(self->t,0,_("Independent Samples Test"));
866 tab_hline(self->t,TAL_1,2,hsize-1,1);
867 tab_vline(self->t,TAL_2,2,0,vsize-1);
868 tab_vline(self->t,TAL_1,4,0,vsize-1);
869 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
870 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
871 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
872 tab_joint_text(self->t, 2, 0, 3, 0,
873 TAB_CENTER,_("Levene's Test for Equality of Variances"));
874 tab_joint_text(self->t, 4,0,hsize-1,0,
875 TAB_CENTER,_("t-test for Equality of Means"));
877 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
878 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
879 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
880 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
881 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
882 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
883 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
884 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
885 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
887 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
888 _("%d%% Confidence Interval of the Difference"),
889 (int)round(cmd->criteria*100.0));
893 /* Populate the independent samples trbox */
895 trbox_independent_samples_populate(struct trbox *self,
896 struct cmd_t_test *cmd )
901 for (i=0; i < cmd->n_variables; ++i)
913 double pooled_variance;
917 struct group_statistics *gs0 = &cmd->v_variables[i]->p.t_t.gs[0];
918 struct group_statistics *gs1 = &cmd->v_variables[i]->p.t_t.gs[1];
920 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
922 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
925 tab_float(self->t, 2, i*2+3, TAB_CENTER,
926 cmd->v_variables[i]->p.t_t.levene, 8,3);
929 /* Now work out the significance of the Levene test */
931 which=1; df1 = 1; df2 = cmd->v_variables[i]->p.t_t.ugs.n - 2;
932 cdff(&which,&p,&q,&cmd->v_variables[i]->p.t_t.levene,
933 &df1,&df2,&status,&bound);
937 msg( SE, _("Error calculating F statistic (cdff returned %d)."),status);
940 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
942 df = gs0->n + gs1->n - 2.0 ;
943 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
945 pooled_variance = ( (gs0->n )*sqr(gs0->s_std_dev)
947 (gs1->n )*sqr(gs1->s_std_dev)
950 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
951 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
953 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
956 which=1; /* get p & q from t & df */
957 cdft(&which, &p, &q, &t, &df, &status, &bound);
960 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
963 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
965 mean_diff = gs0->mean - gs1->mean;
966 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
969 std_err_diff = sqrt( sqr(gs0->se_mean) + sqr(gs1->se_mean));
970 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
973 /* Now work out the confidence interval */
974 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
976 which=2; /* Calc T from p,q and df */
977 cdft(&which, &p, &q, &t, &df, &status, &bound);
980 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
983 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
984 mean_diff - t * std_err_diff, 8, 3);
986 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
987 mean_diff + t * std_err_diff, 8, 3);
992 /* Now for the \sigma_1 != \sigma_2 case */
993 tab_text (self->t, 1, i*2+3+1,
994 TAB_LEFT, _("Equal variances not assumed"));
997 se2 = (sqr(gs0->s_std_dev)/(gs0->n -1) ) +
998 (sqr(gs1->s_std_dev)/(gs1->n -1) );
1000 t = mean_diff / sqrt(se2) ;
1001 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1004 (sqr(sqr(gs0->s_std_dev)/(gs0->n - 1 ))
1008 (sqr(sqr(gs1->s_std_dev)/(gs1->n - 1 ))
1012 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1014 which=1; /* get p & q from t & df */
1015 cdft(&which, &p, &q, &t, &df, &status, &bound);
1018 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1021 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1023 /* Now work out the confidence interval */
1024 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1026 which=2; /* Calc T from p,q and df */
1027 cdft(&which, &p, &q, &t, &df, &status, &bound);
1030 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1034 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1037 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1040 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1041 mean_diff - t * std_err_diff, 8, 3);
1043 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1044 mean_diff + t * std_err_diff, 8, 3);
1050 /* Initialize the paired samples trbox */
1052 trbox_paired_init(struct trbox *self,
1053 struct cmd_t_test *cmd UNUSED)
1057 const int vsize=n_pairs+3;
1059 self->populate = trbox_paired_populate;
1061 trbox_base_init(self,n_pairs,hsize);
1062 tab_title (self->t, 0, _("Paired Samples Test"));
1063 tab_hline(self->t,TAL_1,2,6,1);
1064 tab_vline(self->t,TAL_2,2,0,vsize);
1065 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1066 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1067 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1068 tab_hline(self->t,TAL_1,5,6, 2);
1069 tab_vline(self->t,TAL_0,6,0,1);
1071 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1072 _("%d%% Confidence Interval of the Difference"),
1073 (int)round(cmd->criteria*100.0));
1075 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1076 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1077 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1078 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1079 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1080 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1081 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1082 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1085 /* Populate the paired samples trbox */
1087 trbox_paired_populate(struct trbox *trb,
1088 struct cmd_t_test *cmd UNUSED)
1092 for (i=0; i < n_pairs; ++i)
1100 struct variable *v0 = pairs[i].v[0];
1101 struct variable *v1 = pairs[i].v[1];
1103 struct group_statistics *gs0 = &v0->p.t_t.ugs;
1104 struct group_statistics *gs1 = &v1->p.t_t.ugs;
1110 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1112 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1113 pairs[i].v[0]->name, pairs[i].v[1]->name);
1115 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1117 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1120 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1121 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1123 /* Now work out the confidence interval */
1124 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1126 which=2; /* Calc T from p,q and df */
1127 cdft(&which, &p, &q, &t, &df, &status, &bound);
1131 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1134 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1135 pairs[i].mean_diff - t * se_mean , 8, 4);
1137 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1138 pairs[i].mean_diff + t * se_mean , 8, 4);
1140 t = ( gs0->mean - gs1->mean)
1142 ( sqr(gs0->s_std_dev) + sqr(gs1->s_std_dev) -
1143 2 * pairs[i].correlation * gs0->s_std_dev * gs1->s_std_dev )
1147 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1149 /* Degrees of freedom */
1150 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1153 cdft(&which, &p, &q, &t, &df, &status, &bound);
1157 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1161 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1166 /* Initialize the one sample trbox */
1168 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1171 const int vsize=cmd->n_variables+3;
1173 self->populate = trbox_one_sample_populate;
1175 trbox_base_init(self, cmd->n_variables,hsize);
1176 tab_title (self->t, 0, _("One-Sample Test"));
1177 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1178 tab_vline(self->t, TAL_2, 1, 0, vsize);
1180 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1181 _("Test Value = %f"),cmd->n_testval);
1183 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1186 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1187 _("%d%% Confidence Interval of the Difference"),
1188 (int)round(cmd->criteria*100.0));
1190 tab_vline(self->t,TAL_0,6,1,1);
1191 tab_hline(self->t,TAL_1,5,6,2);
1192 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1193 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1194 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1195 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1196 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1197 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1202 /* Populate the one sample trbox */
1204 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1210 for (i=0; i < cmd->n_variables; ++i)
1218 struct group_statistics *gs;
1219 gs= &cmd->v_variables[i]->p.t_t.ugs;
1222 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1224 t = (gs->mean - cmd->n_testval ) * sqrt(gs->n) / gs->std_dev ;
1226 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1228 /* degrees of freedom */
1231 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1233 cdft(&which, &p, &q, &t, &df, &status, &bound);
1237 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1241 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1243 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1245 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1248 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1250 which=2; /* Calc T from p,q and df */
1251 cdft(&which, &p, &q, &t, &df, &status, &bound);
1254 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1257 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1258 gs->mean_diff - t * gs->se_mean, 8,4);
1260 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1261 gs->mean_diff + t * gs->se_mean, 8,4);
1265 /* Base initializer for the generalized trbox */
1267 trbox_base_init(struct trbox *self, int data_rows, int cols)
1269 const int rows = 3 + data_rows;
1271 self->finalize = trbox_base_finalize;
1272 self->t = tab_create (cols, rows, 0);
1273 tab_headers (self->t,0,0,3,0);
1274 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1275 tab_hline(self->t, TAL_2,0,cols-1,3);
1276 tab_dim (self->t, tab_natural_dimensions);
1280 /* Base finalizer for the trbox */
1282 trbox_base_finalize(struct trbox *trb)
1288 /* Create , populate and submit the Paired Samples Correlation box */
1292 const int rows=1+n_pairs;
1296 struct tab_table *table;
1298 table = tab_create (cols,rows,0);
1300 tab_columns (table, SOM_COL_DOWN, 1);
1301 tab_headers (table,0,0,1,0);
1302 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1303 tab_hline(table, TAL_2, 0, cols - 1, 1);
1304 tab_vline(table, TAL_2, 2, 0, rows - 1);
1305 tab_dim(table, tab_natural_dimensions);
1306 tab_title(table, 0, _("Paired Samples Correlations"));
1308 /* column headings */
1309 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1310 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1311 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1313 for (i=0; i < n_pairs; ++i)
1321 double df = pairs[i].v[0]->p.t_t.ugs.n -2;
1323 double correlation_t =
1324 pairs[i].correlation * sqrt(df) /
1325 sqrt(1 - sqr(pairs[i].correlation));
1329 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1332 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1333 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1337 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1338 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.ugs.n , 4, 0);
1341 cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1345 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1349 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1358 /* Calculation Implementation */
1360 /* Per case calculations common to all variants of the T test */
1362 common_calc (struct ccase *c, void *aux UNUSED)
1366 double weight = dict_get_case_weight(default_dict,c);
1368 for(i=0; i< cmd.n_variables ; ++i)
1370 struct group_statistics *gs;
1371 struct variable *v = cmd.v_variables[i];
1372 union value *val = &c->data[v->fv];
1374 gs= &cmd.v_variables[i]->p.t_t.ugs;
1376 if (val->f != SYSMIS)
1379 gs->sum+=weight * val->f;
1380 gs->ssq+=weight * val->f * val->f;
1386 /* Pre calculations common to all variants of the T test */
1388 common_precalc (void *aux UNUSED)
1392 for(i=0; i< cmd.n_variables ; ++i)
1394 struct group_statistics *gs;
1395 gs= &cmd.v_variables[i]->p.t_t.ugs;
1404 /* Post calculations common to all variants of the T test */
1406 common_postcalc (void *aux UNUSED)
1410 for(i=0; i< cmd.n_variables ; ++i)
1412 struct group_statistics *gs;
1413 gs= &cmd.v_variables[i]->p.t_t.ugs;
1415 gs->mean=gs->sum / gs->n;
1416 gs->s_std_dev= sqrt(
1417 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1422 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1425 gs->se_mean = gs->std_dev / sqrt(gs->n);
1426 gs->mean_diff= gs->sum_diff / gs->n;
1430 /* Per case calculations for one sample t test */
1432 one_sample_calc (struct ccase *c, void *aux UNUSED)
1436 double weight = dict_get_case_weight(default_dict,c);
1438 for(i=0; i< cmd.n_variables ; ++i)
1440 struct group_statistics *gs;
1441 struct variable *v = cmd.v_variables[i];
1442 union value *val = &c->data[v->fv];
1444 gs= &cmd.v_variables[i]->p.t_t.ugs;
1446 if (val->f != SYSMIS)
1447 gs->sum_diff += weight * (val->f - cmd.n_testval);
1453 /* Pre calculations for one sample t test */
1455 one_sample_precalc (void *aux UNUSED)
1459 for(i=0; i< cmd.n_variables ; ++i)
1461 struct group_statistics *gs;
1462 gs= &cmd.v_variables[i]->p.t_t.ugs;
1468 /* Post calculations for one sample t test */
1470 one_sample_postcalc (void *aux UNUSED)
1474 for(i=0; i< cmd.n_variables ; ++i)
1476 struct group_statistics *gs;
1477 gs= &cmd.v_variables[i]->p.t_t.ugs;
1480 gs->mean_diff = gs->sum_diff / gs->n ;
1487 compare_var_name (const void *a_, const void *b_, void *v_ UNUSED)
1489 const struct variable *a = a_;
1490 const struct variable *b = b_;
1492 return strcmp(a->name,b->name);
1496 hash_var_name (const void *a_, void *v_ UNUSED)
1498 const struct variable *a = a_;
1500 return hsh_hash_bytes (a->name, strlen(a->name));
1506 paired_precalc (void *aux UNUSED)
1510 for(i=0; i < n_pairs ; ++i )
1512 pairs[i].correlation=0;
1513 pairs[i].sum_of_diffs=0;
1514 pairs[i].ssq_diffs=0;
1521 paired_calc (struct ccase *c, void *aux UNUSED)
1525 for(i=0; i < n_pairs ; ++i )
1527 struct variable *v0 = pairs[i].v[0];
1528 struct variable *v1 = pairs[i].v[1];
1530 union value *val0 = &c->data[v0->fv];
1531 union value *val1 = &c->data[v1->fv];
1533 pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.ugs.mean )
1535 ( val1->f - pairs[i].v[1]->p.t_t.ugs.mean );
1537 pairs[i].sum_of_diffs += val0->f - val1->f ;
1538 pairs[i].ssq_diffs += sqr(val0->f - val1->f);
1546 paired_postcalc (void *aux UNUSED)
1550 for(i=0; i < n_pairs ; ++i )
1552 const double n = pairs[i].v[0]->p.t_t.ugs.n ;
1554 pairs[i].correlation /= pairs[i].v[0]->p.t_t.ugs.std_dev *
1555 pairs[i].v[1]->p.t_t.ugs.std_dev ;
1556 pairs[i].correlation /= pairs[i].v[0]->p.t_t.ugs.n -1;
1559 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1562 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1563 ( pairs[i].ssq_diffs / n )
1565 sqr(pairs[i].mean_diff )
1571 get_group(const union value *val, struct variable *var)
1573 if ( 0 == compare_values(val,&groups_values[0],var->width) )
1575 else if (0 == compare_values(val,&groups_values[1],var->width) )
1585 group_precalc (void *aux UNUSED)
1590 for(i=0; i< cmd.n_variables ; ++i)
1592 struct t_test_proc *ttpr = &cmd.v_variables[i]->p.t_t;
1594 /* There's always 2 groups for a T - TEST */
1596 ttpr->gs = xmalloc(sizeof(struct group_statistics) * 2) ;
1598 for (j=0 ; j < 2 ; ++j)
1603 ttpr->gs[j].id = groups_values[j];
1610 group_calc (struct ccase *c, void *aux UNUSED)
1613 union value *gv = &c->data[groups->fv];
1615 double weight = dict_get_case_weight(default_dict,c);
1617 gv = &c->data[groups->fv];
1619 for(i=0; i< cmd.n_variables ; ++i)
1621 int g = get_group(gv,groups);
1623 struct group_statistics *gs = &cmd.v_variables[i]->p.t_t.gs[g];
1625 union value *val=&c->data[cmd.v_variables[i]->fv];
1628 gs->sum+=weight * val->f;
1629 gs->ssq+=weight * sqr(val->f);
1637 group_postcalc (void *aux UNUSED)
1642 for(i=0; i< cmd.n_variables ; ++i)
1644 for (j=0 ; j < 2 ; ++j)
1646 struct group_statistics *gs;
1647 gs=&cmd.v_variables[i]->p.t_t.gs[j];
1649 gs->mean = gs->sum / gs->n;
1651 gs->s_std_dev= sqrt(
1652 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1657 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1660 gs->se_mean = gs->std_dev / sqrt(gs->n);