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"
48 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
50 +missing=miss:!analysis/listwise,
51 incl:include/!exclude;
52 format=fmt:!labels/nolabels;
53 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
58 static struct cmd_t_test cmd;
60 /* Variable for the GROUPS subcommand, if given. */
61 static struct variable *indep_var;
63 /* GROUPS: Number of values specified by the user; the values
66 static int n_group_values;
67 static union value groups_values[2];
68 static enum comparison criteria[2];
71 /* PAIRS: Number of pairs to be compared ; each pair. */
72 static int n_pairs = 0 ;
75 /* The variables comprising the pair */
76 struct variable *v[2];
78 /* The correlation coefficient between the variables */
81 /* The sum of the differences */
84 /* The mean of the differences */
87 /* The sum of the squares of the differences */
90 /* The std deviation of the differences */
94 static struct pair *pairs=0;
96 static int parse_value (union value * v, int type) ;
98 /* Structures and Functions for the Statistics Summary Box */
100 typedef void populate_ssbox_func(struct ssbox *ssb,
101 struct cmd_t_test *cmd);
102 typedef void finalize_ssbox_func(struct ssbox *ssb);
108 populate_ssbox_func *populate;
109 finalize_ssbox_func *finalize;
114 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
116 /* Populate a ssbox according to cmd */
117 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
119 /* Submit and destroy a ssbox */
120 void ssbox_finalize(struct ssbox *ssb);
122 /* A function to create, populate and submit the Paired Samples Correlation
127 /* Structures and Functions for the Test Results Box */
130 typedef void populate_trbox_func(struct trbox *trb,
131 struct cmd_t_test *cmd);
132 typedef void finalize_trbox_func(struct trbox *trb);
136 populate_trbox_func *populate;
137 finalize_trbox_func *finalize;
141 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
143 /* Populate a ssbox according to cmd */
144 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
146 /* Submit and destroy a ssbox */
147 void trbox_finalize(struct trbox *trb);
149 /* Which mode was T-TEST invoked */
157 static int common_calc (struct ccase *, void *);
158 static void common_precalc (void *);
159 static void common_postcalc (void *);
161 static int one_sample_calc (struct ccase *, void *);
162 static void one_sample_precalc (void *);
163 static void one_sample_postcalc (void *);
165 static int paired_calc (struct ccase *, void *);
166 static void paired_precalc (void *);
167 static void paired_postcalc (void *);
169 static void group_precalc (void *);
170 static int group_calc (struct ccase *, void *);
171 static void group_postcalc (void *);
174 static int compare_var_name (const void *a_, const void *b_, void *v_ UNUSED);
175 static unsigned hash_var_name (const void *a_, void *v_ UNUSED);
184 struct ssbox stat_summary_box;
185 struct trbox test_results_box;
187 if (!lex_force_match_id ("T"))
191 lex_match_id ("TEST");
193 if ( !parse_t_test(&cmd) )
196 if (! cmd.sbc_criteria)
201 if (cmd.sbc_testval) ++m;
202 if (cmd.sbc_groups) ++m;
203 if (cmd.sbc_pairs) ++m;
208 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
216 else if (cmd.sbc_groups)
221 if ( mode == T_PAIRED)
223 if (cmd.sbc_variables)
225 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
230 /* Iterate through the pairs and put each variable that is a
231 member of a pair into cmd.v_variables */
234 struct hsh_iterator hi;
235 struct hsh_table *hash;
238 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
240 for (i=0; i < n_pairs; ++i)
242 hsh_insert(hash,pairs[i].v[0]);
243 hsh_insert(hash,pairs[i].v[1]);
246 assert(cmd.n_variables == 0);
247 cmd.n_variables = hsh_count(hash);
249 cmd.v_variables = xrealloc(cmd.v_variables,
250 sizeof(struct variable) * cmd.n_variables);
251 /* Iterate through the hash */
252 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
254 v=hsh_next(hash,&hi) )
255 cmd.v_variables[i++]=v;
262 procedure(common_precalc,common_calc,common_postcalc, NULL);
267 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc, NULL);
270 procedure(paired_precalc,paired_calc,paired_postcalc, NULL);
273 procedure(group_precalc,group_calc,group_postcalc, NULL);
274 levene(indep_var, cmd.n_variables, cmd.v_variables);
278 ssbox_create(&stat_summary_box,&cmd,mode);
279 ssbox_populate(&stat_summary_box,&cmd);
280 ssbox_finalize(&stat_summary_box);
282 if ( mode == T_PAIRED)
285 trbox_create(&test_results_box,&cmd,mode);
286 trbox_populate(&test_results_box,&cmd);
287 trbox_finalize(&test_results_box);
294 if ( mode == T_IND_SAMPLES)
297 /* Destroy any group statistics we created */
298 for (i= 0 ; i < cmd.n_variables ; ++i )
300 free(cmd.v_variables[i]->p.t_t.gs);
308 tts_custom_groups (struct cmd_t_test *cmd UNUSED)
313 if (token != T_ALL &&
314 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
317 msg(SE,_("`%s' is not a variable name"),tokid);
321 indep_var = parse_variable ();
324 lex_error ("expecting variable name in GROUPS subcommand");
328 if (indep_var->type == T_STRING && indep_var->width > MAX_SHORT_STRING)
330 msg (SE, _("Long string variable %s is not valid here."),
335 if (!lex_match ('('))
337 if (indep_var->type == NUMERIC)
339 groups_values[0].f = 1;
340 groups_values[1].f = 2;
341 criteria[0] = criteria[1] = CMP_EQ;
347 msg (SE, _("When applying GROUPS to a string variable, at "
348 "least one value must be specified."));
353 if (!parse_value (&groups_values[0],indep_var->type))
359 criteria[0] = CMP_LE;
360 criteria[1] = CMP_GT;
361 groups_values[1] = groups_values[0];
366 if (!parse_value (&groups_values[1],indep_var->type))
370 if (!lex_force_match (')'))
373 criteria[0] = criteria[1] = CMP_EQ;
381 tts_custom_pairs (struct cmd_t_test *cmd UNUSED)
383 struct variable **vars;
388 int n_after_WITH = -1;
389 int paired ; /* Was the PAIRED keyword given ? */
393 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
396 msg(SE,_("`%s' is not a variable name"),tokid);
401 if (!parse_variables (default_dict, &vars, &n_vars,
402 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
410 if (lex_match (T_WITH))
412 n_before_WITH = n_vars;
413 if (!parse_variables (default_dict, &vars, &n_vars,
414 PV_DUPLICATE | PV_APPEND
415 | PV_NUMERIC | PV_NO_SCRATCH))
420 n_after_WITH = n_vars - n_before_WITH;
423 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
425 /* Determine the number of pairs needed */
428 if (n_before_WITH != n_after_WITH)
431 msg (SE, _("PAIRED was specified but the number of variables "
432 "preceding WITH (%d) did not match the number "
434 n_before_WITH, n_after_WITH );
437 n_pairs_local=n_before_WITH;
439 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
441 n_pairs_local=n_before_WITH * n_after_WITH ;
443 else /* Neither WITH nor PAIRED keyword given */
448 msg (SE, _("At least two variables must be specified "
453 /* how many ways can you pick 2 from n_vars ? */
454 n_pairs_local = n_vars * (n_vars -1 ) /2 ;
458 /* Allocate storage for the pairs */
459 pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
461 /* Populate the pairs with the appropriate variables */
466 assert(n_pairs_local == n_vars/2);
467 for (i = 0; i < n_pairs_local ; ++i)
469 pairs[i].v[n_pairs+0] = vars[i];
470 pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
473 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
478 for(i=0 ; i < n_before_WITH ; ++i )
480 for(j=0 ; j < n_after_WITH ; ++j)
482 pairs[p].v[0] = vars[i];
483 pairs[p].v[1] = vars[j+n_before_WITH];
488 else /* Neither WITH nor PAIRED given */
493 for(i=0 ; i < n_vars ; ++i )
495 for(j=i+1 ; j < n_vars ; ++j)
497 pairs[p].v[0] = vars[i];
498 pairs[p].v[1] = vars[j];
504 n_pairs+=n_pairs_local;
509 /* Parses the current token (numeric or string, depending on type)
510 value v and returns success. */
512 parse_value (union value * v, int type )
516 if (!lex_force_num ())
522 if (!lex_force_string ())
524 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
533 /* Implementation of the SSBOX object */
535 void ssbox_base_init(struct ssbox *this, int cols,int rows);
537 void ssbox_base_finalize(struct ssbox *ssb);
539 void ssbox_one_sample_init(struct ssbox *this,
540 struct cmd_t_test *cmd );
542 void ssbox_independent_samples_init(struct ssbox *this,
543 struct cmd_t_test *cmd);
545 void ssbox_paired_init(struct ssbox *this,
546 struct cmd_t_test *cmd);
548 /* Factory to create an ssbox */
550 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
555 ssbox_one_sample_init(ssb,cmd);
558 ssbox_independent_samples_init(ssb,cmd);
561 ssbox_paired_init(ssb,cmd);
569 /* Despatcher for the populate method */
571 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
573 ssb->populate(ssb,cmd);
577 /* Despatcher for finalize */
579 ssbox_finalize(struct ssbox *ssb)
585 /* Submit the box and clear up */
587 ssbox_base_finalize(struct ssbox *ssb)
592 /* Initialize a ssbox struct */
594 ssbox_base_init(struct ssbox *this, int cols,int rows)
596 this->finalize = ssbox_base_finalize;
597 this->t = tab_create (cols, rows, 0);
599 tab_columns (this->t, SOM_COL_DOWN, 1);
600 tab_headers (this->t,0,0,1,0);
601 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
602 tab_hline(this->t, TAL_2,0,cols-1,1);
603 tab_dim (this->t, tab_natural_dimensions);
606 void ssbox_one_sample_populate(struct ssbox *ssb,
607 struct cmd_t_test *cmd);
609 /* Initialize the one_sample ssbox */
611 ssbox_one_sample_init(struct ssbox *this,
612 struct cmd_t_test *cmd )
615 const int vsize=cmd->n_variables+1;
617 this->populate = ssbox_one_sample_populate;
619 ssbox_base_init(this, hsize,vsize);
620 tab_title (this->t, 0, _("One-Sample Statistics"));
621 tab_vline(this->t, TAL_2, 1,0,vsize);
622 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
623 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
624 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
625 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
628 void ssbox_independent_samples_populate(struct ssbox *ssb,
629 struct cmd_t_test *cmd);
631 /* Initialize the independent samples ssbox */
633 ssbox_independent_samples_init(struct ssbox *this,
634 struct cmd_t_test *cmd)
637 int vsize = cmd->n_variables*2 +1;
639 this->populate = ssbox_independent_samples_populate;
641 ssbox_base_init(this, hsize,vsize);
642 tab_title (this->t, 0, _("Group Statistics"));
643 tab_vline(this->t,0,1,0,vsize);
644 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, indep_var->name);
645 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
646 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
647 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
648 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
652 /* Populate the ssbox for independent samples */
654 ssbox_independent_samples_populate(struct ssbox *ssb,
655 struct cmd_t_test *cmd)
662 char prefix[2][3]={"",""};
664 if ( indep_var->type == NUMERIC )
666 val_lab0 = val_labs_find( indep_var->val_labs,groups_values[0]);
667 val_lab1 = val_labs_find( indep_var->val_labs,groups_values[1]);
671 val_lab0 = groups_values[0].s;
672 val_lab1 = groups_values[1].s;
675 if (n_group_values == 1)
677 strcpy(prefix[0],"< ");
678 strcpy(prefix[1],">=");
683 for (i=0; i < cmd->n_variables; ++i)
687 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
690 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
691 "%s%s", prefix[0], val_lab0);
693 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
694 "%s%g", prefix[0], groups_values[0].f);
698 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
699 "%s%s", prefix[1], val_lab1);
701 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
702 "%s%g", prefix[1], groups_values[1].f);
704 /* Fill in the group statistics */
705 for ( g=0; g < 2 ; ++g )
707 struct group_statistics *gs = &cmd->v_variables[i]->p.t_t.gs[g];
709 tab_float(ssb->t, 2 ,i*2+g+1, TAB_RIGHT, gs->n, 2, 0);
710 tab_float(ssb->t, 3 ,i*2+g+1, TAB_RIGHT, gs->mean, 8, 2);
711 tab_float(ssb->t, 4 ,i*2+g+1, TAB_RIGHT, gs->std_dev, 8, 3);
712 tab_float(ssb->t, 5 ,i*2+g+1, TAB_RIGHT, gs->se_mean, 8, 3);
718 void ssbox_paired_populate(struct ssbox *ssb,
719 struct cmd_t_test *cmd);
721 /* Initialize the paired values ssbox */
723 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
727 int vsize = n_pairs*2+1;
729 this->populate = ssbox_paired_populate;
731 ssbox_base_init(this, hsize,vsize);
732 tab_title (this->t, 0, _("Paired Sample Statistics"));
733 tab_vline(this->t,TAL_0,1,0,vsize-1);
734 tab_vline(this->t,TAL_2,2,0,vsize-1);
735 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
736 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
737 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
738 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
742 /* Populate the ssbox for paired values */
744 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
750 for (i=0; i < n_pairs; ++i)
754 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
756 for (j=0 ; j < 2 ; ++j)
758 struct group_statistics *gs;
760 gs=&pairs[i].v[j]->p.t_t.ugs;
764 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
767 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, gs->mean, 8, 2);
768 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, gs->n, 2, 0);
769 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, gs->std_dev, 8, 3);
770 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, gs->se_mean, 8, 3);
778 /* Populate the one sample ssbox */
780 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
786 for (i=0; i < cmd->n_variables; ++i)
788 struct group_statistics *gs;
789 gs= &cmd->v_variables[i]->p.t_t.ugs;
791 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
792 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 0);
793 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
794 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
795 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
802 /* Implementation of the Test Results box struct */
804 void trbox_base_init(struct trbox *self,int n_vars, int cols);
805 void trbox_base_finalize(struct trbox *trb);
807 void trbox_independent_samples_init(struct trbox *trb,
808 struct cmd_t_test *cmd );
810 void trbox_independent_samples_populate(struct trbox *trb,
811 struct cmd_t_test *cmd);
813 void trbox_one_sample_init(struct trbox *self,
814 struct cmd_t_test *cmd );
816 void trbox_one_sample_populate(struct trbox *trb,
817 struct cmd_t_test *cmd);
819 void trbox_paired_init(struct trbox *self,
820 struct cmd_t_test *cmd );
822 void trbox_paired_populate(struct trbox *trb,
823 struct cmd_t_test *cmd);
827 /* Create a trbox according to mode*/
829 trbox_create(struct trbox *trb,
830 struct cmd_t_test *cmd, int mode)
835 trbox_one_sample_init(trb,cmd);
838 trbox_independent_samples_init(trb,cmd);
841 trbox_paired_init(trb,cmd);
848 /* Populate a trbox according to cmd */
850 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
852 trb->populate(trb,cmd);
855 /* Submit and destroy a trbox */
857 trbox_finalize(struct trbox *trb)
862 /* Initialize the independent samples trbox */
864 trbox_independent_samples_init(struct trbox *self,
865 struct cmd_t_test *cmd UNUSED)
868 const int vsize=cmd->n_variables*2+3;
871 self->populate = trbox_independent_samples_populate;
873 trbox_base_init(self,cmd->n_variables*2,hsize);
874 tab_title(self->t,0,_("Independent Samples Test"));
875 tab_hline(self->t,TAL_1,2,hsize-1,1);
876 tab_vline(self->t,TAL_2,2,0,vsize-1);
877 tab_vline(self->t,TAL_1,4,0,vsize-1);
878 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
879 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
880 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
881 tab_joint_text(self->t, 2, 0, 3, 0,
882 TAB_CENTER,_("Levene's Test for Equality of Variances"));
883 tab_joint_text(self->t, 4,0,hsize-1,0,
884 TAB_CENTER,_("t-test for Equality of Means"));
886 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
887 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
888 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
889 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
890 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
891 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
892 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
893 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
894 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
896 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
897 _("%d%% Confidence Interval of the Difference"),
898 (int)round(cmd->criteria*100.0));
902 /* Populate the independent samples trbox */
904 trbox_independent_samples_populate(struct trbox *self,
905 struct cmd_t_test *cmd )
910 for (i=0; i < cmd->n_variables; ++i)
922 double pooled_variance;
926 struct group_statistics *gs0 = &cmd->v_variables[i]->p.t_t.gs[0];
927 struct group_statistics *gs1 = &cmd->v_variables[i]->p.t_t.gs[1];
929 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
931 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
934 tab_float(self->t, 2, i*2+3, TAB_CENTER,
935 cmd->v_variables[i]->p.t_t.levene, 8,3);
938 /* Now work out the significance of the Levene test */
940 which=1; df1 = 1; df2 = cmd->v_variables[i]->p.t_t.ugs.n - 2;
941 cdff(&which,&p,&q,&cmd->v_variables[i]->p.t_t.levene,
942 &df1,&df2,&status,&bound);
946 msg( SE, _("Error calculating F statistic (cdff returned %d)."),status);
949 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
951 df = gs0->n + gs1->n - 2.0 ;
952 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
954 pooled_variance = ( (gs0->n )*sqr(gs0->s_std_dev)
956 (gs1->n )*sqr(gs1->s_std_dev)
959 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
960 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
962 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
965 which=1; /* get p & q from t & df */
966 cdft(&which, &p, &q, &t, &df, &status, &bound);
969 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
972 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
974 mean_diff = gs0->mean - gs1->mean;
975 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
978 std_err_diff = sqrt( sqr(gs0->se_mean) + sqr(gs1->se_mean));
979 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
982 /* Now work out the confidence interval */
983 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
985 which=2; /* Calc T from p,q and df */
986 cdft(&which, &p, &q, &t, &df, &status, &bound);
989 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
992 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
993 mean_diff - t * std_err_diff, 8, 3);
995 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
996 mean_diff + t * std_err_diff, 8, 3);
1001 /* Now for the \sigma_1 != \sigma_2 case */
1002 tab_text (self->t, 1, i*2+3+1,
1003 TAB_LEFT, _("Equal variances not assumed"));
1006 se2 = (sqr(gs0->s_std_dev)/(gs0->n -1) ) +
1007 (sqr(gs1->s_std_dev)/(gs1->n -1) );
1009 t = mean_diff / sqrt(se2) ;
1010 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1013 (sqr(sqr(gs0->s_std_dev)/(gs0->n - 1 ))
1017 (sqr(sqr(gs1->s_std_dev)/(gs1->n - 1 ))
1021 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1023 which=1; /* get p & q from t & df */
1024 cdft(&which, &p, &q, &t, &df, &status, &bound);
1027 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1030 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1032 /* Now work out the confidence interval */
1033 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1035 which=2; /* Calc T from p,q and df */
1036 cdft(&which, &p, &q, &t, &df, &status, &bound);
1039 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1043 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1046 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1049 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1050 mean_diff - t * std_err_diff, 8, 3);
1052 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1053 mean_diff + t * std_err_diff, 8, 3);
1059 /* Initialize the paired samples trbox */
1061 trbox_paired_init(struct trbox *self,
1062 struct cmd_t_test *cmd UNUSED)
1066 const int vsize=n_pairs+3;
1068 self->populate = trbox_paired_populate;
1070 trbox_base_init(self,n_pairs,hsize);
1071 tab_title (self->t, 0, _("Paired Samples Test"));
1072 tab_hline(self->t,TAL_1,2,6,1);
1073 tab_vline(self->t,TAL_2,2,0,vsize);
1074 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1075 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1076 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1077 tab_hline(self->t,TAL_1,5,6, 2);
1078 tab_vline(self->t,TAL_0,6,0,1);
1080 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1081 _("%d%% Confidence Interval of the Difference"),
1082 (int)round(cmd->criteria*100.0));
1084 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1085 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1086 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1087 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1088 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1089 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1090 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1091 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1094 /* Populate the paired samples trbox */
1096 trbox_paired_populate(struct trbox *trb,
1097 struct cmd_t_test *cmd UNUSED)
1101 for (i=0; i < n_pairs; ++i)
1109 struct variable *v0 = pairs[i].v[0];
1110 struct variable *v1 = pairs[i].v[1];
1112 struct group_statistics *gs0 = &v0->p.t_t.ugs;
1113 struct group_statistics *gs1 = &v1->p.t_t.ugs;
1119 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1121 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1122 pairs[i].v[0]->name, pairs[i].v[1]->name);
1124 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1126 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1129 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1130 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1132 /* Now work out the confidence interval */
1133 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1135 which=2; /* Calc T from p,q and df */
1136 cdft(&which, &p, &q, &t, &df, &status, &bound);
1140 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1143 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1144 pairs[i].mean_diff - t * se_mean , 8, 4);
1146 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1147 pairs[i].mean_diff + t * se_mean , 8, 4);
1149 t = ( gs0->mean - gs1->mean)
1151 ( sqr(gs0->s_std_dev) + sqr(gs1->s_std_dev) -
1152 2 * pairs[i].correlation * gs0->s_std_dev * gs1->s_std_dev )
1156 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1158 /* Degrees of freedom */
1159 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1162 cdft(&which, &p, &q, &t, &df, &status, &bound);
1166 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1170 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1175 /* Initialize the one sample trbox */
1177 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1180 const int vsize=cmd->n_variables+3;
1182 self->populate = trbox_one_sample_populate;
1184 trbox_base_init(self, cmd->n_variables,hsize);
1185 tab_title (self->t, 0, _("One-Sample Test"));
1186 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1187 tab_vline(self->t, TAL_2, 1, 0, vsize);
1189 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1190 _("Test Value = %f"),cmd->n_testval);
1192 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1195 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1196 _("%d%% Confidence Interval of the Difference"),
1197 (int)round(cmd->criteria*100.0));
1199 tab_vline(self->t,TAL_0,6,1,1);
1200 tab_hline(self->t,TAL_1,5,6,2);
1201 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1202 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1203 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1204 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1205 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1206 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1211 /* Populate the one sample trbox */
1213 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1219 for (i=0; i < cmd->n_variables; ++i)
1227 struct group_statistics *gs;
1228 gs= &cmd->v_variables[i]->p.t_t.ugs;
1231 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1233 t = (gs->mean - cmd->n_testval ) * sqrt(gs->n) / gs->std_dev ;
1235 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1237 /* degrees of freedom */
1240 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1242 cdft(&which, &p, &q, &t, &df, &status, &bound);
1246 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1250 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1252 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1254 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1257 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1259 which=2; /* Calc T from p,q and df */
1260 cdft(&which, &p, &q, &t, &df, &status, &bound);
1263 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1266 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1267 gs->mean_diff - t * gs->se_mean, 8,4);
1269 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1270 gs->mean_diff + t * gs->se_mean, 8,4);
1274 /* Base initializer for the generalized trbox */
1276 trbox_base_init(struct trbox *self, int data_rows, int cols)
1278 const int rows = 3 + data_rows;
1280 self->finalize = trbox_base_finalize;
1281 self->t = tab_create (cols, rows, 0);
1282 tab_headers (self->t,0,0,3,0);
1283 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1284 tab_hline(self->t, TAL_2,0,cols-1,3);
1285 tab_dim (self->t, tab_natural_dimensions);
1289 /* Base finalizer for the trbox */
1291 trbox_base_finalize(struct trbox *trb)
1297 /* Create , populate and submit the Paired Samples Correlation box */
1301 const int rows=1+n_pairs;
1305 struct tab_table *table;
1307 table = tab_create (cols,rows,0);
1309 tab_columns (table, SOM_COL_DOWN, 1);
1310 tab_headers (table,0,0,1,0);
1311 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1312 tab_hline(table, TAL_2, 0, cols - 1, 1);
1313 tab_vline(table, TAL_2, 2, 0, rows - 1);
1314 tab_dim(table, tab_natural_dimensions);
1315 tab_title(table, 0, _("Paired Samples Correlations"));
1317 /* column headings */
1318 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1319 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1320 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1322 for (i=0; i < n_pairs; ++i)
1330 double df = pairs[i].v[0]->p.t_t.ugs.n -2;
1332 double correlation_t =
1333 pairs[i].correlation * sqrt(df) /
1334 sqrt(1 - sqr(pairs[i].correlation));
1338 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1341 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1342 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1346 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1347 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.ugs.n , 4, 0);
1350 cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1354 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1358 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1367 /* Calculation Implementation */
1369 /* Per case calculations common to all variants of the T test */
1371 common_calc (struct ccase *c, void *aux UNUSED)
1375 double weight = dict_get_case_weight(default_dict,c);
1377 for(i=0; i< cmd.n_variables ; ++i)
1379 struct group_statistics *gs;
1380 struct variable *v = cmd.v_variables[i];
1381 union value *val = &c->data[v->fv];
1383 gs= &cmd.v_variables[i]->p.t_t.ugs;
1385 if (val->f != SYSMIS)
1388 gs->sum+=weight * val->f;
1389 gs->ssq+=weight * val->f * val->f;
1395 /* Pre calculations common to all variants of the T test */
1397 common_precalc (void *aux UNUSED)
1401 for(i=0; i< cmd.n_variables ; ++i)
1403 struct group_statistics *gs;
1404 gs= &cmd.v_variables[i]->p.t_t.ugs;
1413 /* Post calculations common to all variants of the T test */
1415 common_postcalc (void *aux UNUSED)
1419 for(i=0; i< cmd.n_variables ; ++i)
1421 struct group_statistics *gs;
1422 gs= &cmd.v_variables[i]->p.t_t.ugs;
1424 gs->mean=gs->sum / gs->n;
1425 gs->s_std_dev= sqrt(
1426 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1431 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1434 gs->se_mean = gs->std_dev / sqrt(gs->n);
1435 gs->mean_diff= gs->sum_diff / gs->n;
1439 /* Per case calculations for one sample t test */
1441 one_sample_calc (struct ccase *c, void *aux UNUSED)
1445 double weight = dict_get_case_weight(default_dict,c);
1447 for(i=0; i< cmd.n_variables ; ++i)
1449 struct group_statistics *gs;
1450 struct variable *v = cmd.v_variables[i];
1451 union value *val = &c->data[v->fv];
1453 gs= &cmd.v_variables[i]->p.t_t.ugs;
1455 if (val->f != SYSMIS)
1456 gs->sum_diff += weight * (val->f - cmd.n_testval);
1462 /* Pre calculations for one sample t test */
1464 one_sample_precalc (void *aux UNUSED)
1468 for(i=0; i< cmd.n_variables ; ++i)
1470 struct group_statistics *gs;
1471 gs= &cmd.v_variables[i]->p.t_t.ugs;
1477 /* Post calculations for one sample t test */
1479 one_sample_postcalc (void *aux UNUSED)
1483 for(i=0; i< cmd.n_variables ; ++i)
1485 struct group_statistics *gs;
1486 gs= &cmd.v_variables[i]->p.t_t.ugs;
1489 gs->mean_diff = gs->sum_diff / gs->n ;
1496 compare_var_name (const void *a_, const void *b_, void *v_ UNUSED)
1498 const struct variable *a = a_;
1499 const struct variable *b = b_;
1501 return strcmp(a->name,b->name);
1505 hash_var_name (const void *a_, void *v_ UNUSED)
1507 const struct variable *a = a_;
1509 return hsh_hash_bytes (a->name, strlen(a->name));
1515 paired_precalc (void *aux UNUSED)
1519 for(i=0; i < n_pairs ; ++i )
1521 pairs[i].correlation=0;
1522 pairs[i].sum_of_diffs=0;
1523 pairs[i].ssq_diffs=0;
1530 paired_calc (struct ccase *c, void *aux UNUSED)
1534 for(i=0; i < n_pairs ; ++i )
1536 struct variable *v0 = pairs[i].v[0];
1537 struct variable *v1 = pairs[i].v[1];
1539 union value *val0 = &c->data[v0->fv];
1540 union value *val1 = &c->data[v1->fv];
1542 pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.ugs.mean )
1544 ( val1->f - pairs[i].v[1]->p.t_t.ugs.mean );
1546 pairs[i].sum_of_diffs += val0->f - val1->f ;
1547 pairs[i].ssq_diffs += sqr(val0->f - val1->f);
1555 paired_postcalc (void *aux UNUSED)
1559 for(i=0; i < n_pairs ; ++i )
1561 const double n = pairs[i].v[0]->p.t_t.ugs.n ;
1563 pairs[i].correlation /= pairs[i].v[0]->p.t_t.ugs.std_dev *
1564 pairs[i].v[1]->p.t_t.ugs.std_dev ;
1565 pairs[i].correlation /= pairs[i].v[0]->p.t_t.ugs.n -1;
1568 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1571 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1572 ( pairs[i].ssq_diffs / n )
1574 sqr(pairs[i].mean_diff )
1579 /* Return the group # corresponding to the
1580 independent variable with the value val
1583 get_group(const union value *val, struct variable *indep)
1587 for (i = 0; i < 2 ; ++i )
1589 const int cmp = compare_values(val,&groups_values[i],indep->width) ;
1590 switch ( criteria[i])
1593 if ( 0 == cmp ) return i;
1596 if ( 0 > cmp ) return i;
1599 if ( cmp <= 0 ) return i;
1602 if ( cmp > 0 ) return i;
1605 if ( cmp >= 0 ) return i;
1612 /* No groups matched */
1618 group_precalc (void *aux UNUSED)
1623 for(i=0; i< cmd.n_variables ; ++i)
1625 struct t_test_proc *ttpr = &cmd.v_variables[i]->p.t_t;
1627 /* There's always 2 groups for a T - TEST */
1629 ttpr->gs = xmalloc(sizeof(struct group_statistics) * 2) ;
1631 for (j=0 ; j < 2 ; ++j)
1633 ttpr->gs[j].sum = 0;
1635 ttpr->gs[j].ssq = 0;
1637 if ( n_group_values == 2 )
1638 ttpr->gs[j].id = groups_values[j];
1640 ttpr->gs[j].id = groups_values[0];
1641 ttpr->gs[j].criterion = criteria[j];
1648 group_calc (struct ccase *c, void *aux UNUSED)
1652 union value *gv = &c->data[indep_var->fv];
1654 double weight = dict_get_case_weight(default_dict,c);
1656 gv = &c->data[indep_var->fv];
1658 g = get_group(gv,indep_var);
1660 /* If the independent variable doesn't match either of the values
1661 for this case then move on to the next case */
1665 for(i=0; i< cmd.n_variables ; ++i)
1667 struct group_statistics *gs = &cmd.v_variables[i]->p.t_t.gs[g];
1669 union value *val=&c->data[cmd.v_variables[i]->fv];
1672 gs->sum+=weight * val->f;
1673 gs->ssq+=weight * sqr(val->f);
1681 group_postcalc (void *aux UNUSED)
1686 for(i=0; i< cmd.n_variables ; ++i)
1688 for (j=0 ; j < 2 ; ++j)
1690 struct group_statistics *gs;
1691 gs=&cmd.v_variables[i]->p.t_t.gs[j];
1693 gs->mean = gs->sum / gs->n;
1695 gs->s_std_dev= sqrt(
1696 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1701 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1704 gs->se_mean = gs->std_dev / sqrt(gs->n);