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 int value_compare(const union value *a, const union value *b, int width);
63 static struct pool *t_test_pool ;
65 /* Variable for the GROUPS subcommand, if given. */
66 static struct variable *groups;
68 /* GROUPS: Number of values specified by the user; the values
70 static int n_groups_values;
71 static union value groups_values[2];
73 /* Array of statistics for each group */
74 typedef struct t_test_proc group_stats_t[2];
75 static group_stats_t *groups_stats;
80 /* PAIRS: Number of pairs to be compared ; each pair. */
81 static int n_pairs = 0 ;
84 /* The variables comprising the pair */
85 struct variable *v[2];
87 /* The correlation coefficient between the variables */
90 /* The sum of the differences */
93 /* The mean of the differences */
96 /* The sum of the squares of the differences */
99 /* The std deviation of the differences */
102 static struct pair *pairs=0;
105 static int parse_value (union value * v, int type) ;
108 /* Structures and Functions for the Statistics Summary Box */
110 typedef void populate_ssbox_func(struct ssbox *ssb,
111 struct cmd_t_test *cmd);
112 typedef void finalize_ssbox_func(struct ssbox *ssb);
118 populate_ssbox_func *populate;
119 finalize_ssbox_func *finalize;
124 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
126 /* Populate a ssbox according to cmd */
127 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
129 /* Submit and destroy a ssbox */
130 void ssbox_finalize(struct ssbox *ssb);
132 /* A function to create, populate and submit the Paired Samples Correlation
137 /* Structures and Functions for the Test Results Box */
140 typedef void populate_trbox_func(struct trbox *trb,
141 struct cmd_t_test *cmd);
142 typedef void finalize_trbox_func(struct trbox *trb);
146 populate_trbox_func *populate;
147 finalize_trbox_func *finalize;
151 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
153 /* Populate a ssbox according to cmd */
154 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
156 /* Submit and destroy a ssbox */
157 void trbox_finalize(struct trbox *trb);
159 /* Which mode was T-TEST invoked */
167 static int common_calc (struct ccase *, void *);
168 static void common_precalc (void *);
169 static void common_postcalc (void *);
171 static int one_sample_calc (struct ccase *, void *);
172 static void one_sample_precalc (void *);
173 static void one_sample_postcalc (void *);
175 static int paired_calc (struct ccase *, void *);
176 static void paired_precalc (void *);
177 static void paired_postcalc (void *);
179 static void group_precalc (void *);
180 static int group_calc (struct ccase *, void *);
181 static void group_postcalc (void *);
184 static int compare_var_name (const void *a_, const void *b_, void *v_ UNUSED);
185 static unsigned hash_var_name (const void *a_, void *v_ UNUSED);
194 struct ssbox stat_summary_box;
195 struct trbox test_results_box;
197 if (!lex_force_match_id ("T"))
201 lex_match_id ("TEST");
203 if ( !parse_t_test(&cmd) )
206 if (! cmd.sbc_criteria)
211 if (cmd.sbc_testval) ++m;
212 if (cmd.sbc_groups) ++m;
213 if (cmd.sbc_pairs) ++m;
218 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
226 else if (cmd.sbc_groups)
231 if ( mode == T_PAIRED)
233 if (cmd.sbc_variables)
235 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
240 /* Iterate through the pairs and put each variable that is a
241 member of a pair into cmd.v_variables */
244 struct hsh_iterator hi;
245 struct hsh_table *hash;
248 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
250 for (i=0; i < n_pairs; ++i)
252 hsh_insert(hash,pairs[i].v[0]);
253 hsh_insert(hash,pairs[i].v[1]);
256 assert(cmd.n_variables == 0);
257 cmd.n_variables = hsh_count(hash);
259 cmd.v_variables = xrealloc(cmd.v_variables,
260 sizeof(struct variable) * cmd.n_variables);
261 /* Iterate through the hash */
262 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
264 v=hsh_next(hash,&hi) )
265 cmd.v_variables[i++]=v;
272 procedure(common_precalc,common_calc,common_postcalc, NULL);
277 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc, NULL);
280 procedure(paired_precalc,paired_calc,paired_postcalc, NULL);
283 procedure(group_precalc,group_calc,group_postcalc, NULL);
289 t_test_pool = pool_create ();
291 ssbox_create(&stat_summary_box,&cmd,mode);
292 ssbox_populate(&stat_summary_box,&cmd);
293 ssbox_finalize(&stat_summary_box);
295 if ( mode == T_PAIRED)
300 trbox_create(&test_results_box,&cmd,mode);
301 trbox_populate(&test_results_box,&cmd);
302 trbox_finalize(&test_results_box);
304 pool_destroy (t_test_pool);
317 tts_custom_groups (struct cmd_t_test *cmd UNUSED)
321 if (token != T_ALL &&
322 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
325 msg(SE,_("`%s' is not a variable name"),tokid);
329 groups = parse_variable ();
332 lex_error ("expecting variable name in GROUPS subcommand");
336 if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
338 msg (SE, _("Long string variable %s is not valid here."),
343 if (!lex_match ('('))
345 if (groups->type == NUMERIC)
348 groups_values[0].f = 1;
349 groups_values[1].f = 2;
354 msg (SE, _("When applying GROUPS to a string variable, at "
355 "least one value must be specified."));
360 if (!parse_value (&groups_values[0],groups->type))
368 if (!parse_value (&groups_values[1],groups->type))
372 if (!lex_force_match (')'))
382 tts_custom_pairs (struct cmd_t_test *cmd UNUSED)
384 struct variable **vars;
389 int n_after_WITH = -1;
390 int paired ; /* Was the PAIRED keyword given ? */
394 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
397 msg(SE,_("`%s' is not a variable name"),tokid);
402 if (!parse_variables (default_dict, &vars, &n_vars,
403 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
411 if (lex_match (T_WITH))
413 n_before_WITH = n_vars;
414 if (!parse_variables (default_dict, &vars, &n_vars,
415 PV_DUPLICATE | PV_APPEND
416 | PV_NUMERIC | PV_NO_SCRATCH))
421 n_after_WITH = n_vars - n_before_WITH;
424 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
426 /* Determine the number of pairs needed */
429 if (n_before_WITH != n_after_WITH)
432 msg (SE, _("PAIRED was specified but the number of variables "
433 "preceding WITH (%d) did not match the number "
435 n_before_WITH, n_after_WITH );
438 n_pairs_local=n_before_WITH;
440 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
442 n_pairs_local=n_before_WITH * n_after_WITH ;
444 else /* Neither WITH nor PAIRED keyword given */
449 msg (SE, _("At least two variables must be specified "
454 /* how many ways can you pick 2 from n_vars ? */
455 n_pairs_local = n_vars * (n_vars -1 ) /2 ;
459 /* Allocate storage for the pairs */
460 pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
462 /* Populate the pairs with the appropriate variables */
467 assert(n_pairs_local == n_vars/2);
468 for (i = 0; i < n_pairs_local ; ++i)
470 pairs[i].v[n_pairs+0] = vars[i];
471 pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
474 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
479 for(i=0 ; i < n_before_WITH ; ++i )
481 for(j=0 ; j < n_after_WITH ; ++j)
483 pairs[p].v[0] = vars[i];
484 pairs[p].v[1] = vars[j+n_before_WITH];
489 else /* Neither WITH nor PAIRED given */
494 for(i=0 ; i < n_vars ; ++i )
496 for(j=i+1 ; j < n_vars ; ++j)
498 pairs[p].v[0] = vars[i];
499 pairs[p].v[1] = vars[j];
505 n_pairs+=n_pairs_local;
510 /* Parses the current token (numeric or string, depending on type)
511 value v and returns success. */
513 parse_value (union value * v, int type )
517 if (!lex_force_num ())
523 if (!lex_force_string ())
525 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
534 /* Implementation of the SSBOX object */
536 void ssbox_base_init(struct ssbox *this, int cols,int rows);
538 void ssbox_base_finalize(struct ssbox *ssb);
540 void ssbox_one_sample_init(struct ssbox *this,
541 struct cmd_t_test *cmd );
543 void ssbox_independent_samples_init(struct ssbox *this,
544 struct cmd_t_test *cmd);
546 void ssbox_paired_init(struct ssbox *this,
547 struct cmd_t_test *cmd);
549 /* Factory to create an ssbox */
551 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
556 ssbox_one_sample_init(ssb,cmd);
559 ssbox_independent_samples_init(ssb,cmd);
562 ssbox_paired_init(ssb,cmd);
570 /* Despatcher for the populate method */
572 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
574 ssb->populate(ssb,cmd);
578 /* Despatcher for finalize */
580 ssbox_finalize(struct ssbox *ssb)
586 /* Submit the box and clear up */
588 ssbox_base_finalize(struct ssbox *ssb)
593 /* Initialize a ssbox struct */
595 ssbox_base_init(struct ssbox *this, int cols,int rows)
597 this->finalize = ssbox_base_finalize;
598 this->t = tab_create (cols, rows, 0);
600 tab_columns (this->t, SOM_COL_DOWN, 1);
601 tab_headers (this->t,0,0,1,0);
602 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
603 tab_hline(this->t, TAL_2,0,cols-1,1);
604 tab_dim (this->t, tab_natural_dimensions);
607 void ssbox_one_sample_populate(struct ssbox *ssb,
608 struct cmd_t_test *cmd);
610 /* Initialize the one_sample ssbox */
612 ssbox_one_sample_init(struct ssbox *this,
613 struct cmd_t_test *cmd )
616 const int vsize=cmd->n_variables+1;
618 this->populate = ssbox_one_sample_populate;
620 ssbox_base_init(this, hsize,vsize);
621 tab_title (this->t, 0, _("One-Sample Statistics"));
622 tab_vline(this->t, TAL_2, 1,0,vsize);
623 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
624 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
625 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
626 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
629 void ssbox_independent_samples_populate(struct ssbox *ssb,
630 struct cmd_t_test *cmd);
632 /* Initialize the independent samples ssbox */
634 ssbox_independent_samples_init(struct ssbox *this,
635 struct cmd_t_test *cmd)
638 int vsize = cmd->n_variables*2 +1;
640 this->populate = ssbox_independent_samples_populate;
642 ssbox_base_init(this, hsize,vsize);
643 tab_title (this->t, 0, _("Group Statistics"));
644 tab_vline(this->t,0,1,0,vsize);
645 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, groups->name);
646 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
647 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
648 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
649 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
653 /* Populate the ssbox for independent samples */
655 ssbox_independent_samples_populate(struct ssbox *ssb,
656 struct cmd_t_test *cmd)
663 if ( groups->type == NUMERIC )
665 val_lab1 = val_labs_find( groups->val_labs,groups_values[0]);
666 val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
670 val_lab1 = groups_values[0].s;
671 val_lab2 = groups_values[1].s;
676 for (i=0; i < cmd->n_variables; ++i)
680 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
683 tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
685 tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
689 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
691 tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
693 /* Fill in the group statistics */
694 for ( g=0; g < 2 ; ++g )
696 struct t_test_proc *ttp = &groups_stats[i][g];
698 tab_float(ssb->t, 2 ,i*2+g+1, TAB_RIGHT, ttp->n, 2, 0);
699 tab_float(ssb->t, 3 ,i*2+g+1, TAB_RIGHT, ttp->mean, 8, 2);
700 tab_float(ssb->t, 4 ,i*2+g+1, TAB_RIGHT, ttp->std_dev, 8, 3);
701 tab_float(ssb->t, 5 ,i*2+g+1, TAB_RIGHT, ttp->se_mean, 8, 3);
708 void ssbox_paired_populate(struct ssbox *ssb,
709 struct cmd_t_test *cmd);
711 /* Initialize the paired values ssbox */
713 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
717 int vsize = n_pairs*2+1;
719 this->populate = ssbox_paired_populate;
721 ssbox_base_init(this, hsize,vsize);
722 tab_title (this->t, 0, _("Paired Sample Statistics"));
723 tab_vline(this->t,TAL_0,1,0,vsize-1);
724 tab_vline(this->t,TAL_2,2,0,vsize-1);
725 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
726 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
727 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
728 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
732 /* Populate the ssbox for paired values */
734 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
740 for (i=0; i < n_pairs; ++i)
744 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
746 for (j=0 ; j < 2 ; ++j)
748 struct t_test_proc *ttp;
750 ttp=&pairs[i].v[j]->p.t_t;
754 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
757 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, ttp->mean, 8, 2);
758 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, ttp->n, 2, 0);
759 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, ttp->std_dev, 8, 3);
760 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, ttp->se_mean, 8, 3);
768 /* Populate the one sample ssbox */
770 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
776 for (i=0; i < cmd->n_variables; ++i)
778 struct t_test_proc *ttp;
779 ttp= &cmd->v_variables[i]->p.t_t;
781 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
782 tab_float (ssb->t,1, i+1, TAB_RIGHT, ttp->n, 2, 0);
783 tab_float (ssb->t,2, i+1, TAB_RIGHT, ttp->mean, 8, 2);
784 tab_float (ssb->t,3, i+1, TAB_RIGHT, ttp->std_dev, 8, 2);
785 tab_float (ssb->t,4, i+1, TAB_RIGHT, ttp->se_mean, 8, 3);
792 /* Implementation of the Test Results box struct */
794 void trbox_base_init(struct trbox *self,int n_vars, int cols);
795 void trbox_base_finalize(struct trbox *trb);
797 void trbox_independent_samples_init(struct trbox *trb,
798 struct cmd_t_test *cmd );
800 void trbox_independent_samples_populate(struct trbox *trb,
801 struct cmd_t_test *cmd);
803 void trbox_one_sample_init(struct trbox *self,
804 struct cmd_t_test *cmd );
806 void trbox_one_sample_populate(struct trbox *trb,
807 struct cmd_t_test *cmd);
809 void trbox_paired_init(struct trbox *self,
810 struct cmd_t_test *cmd );
812 void trbox_paired_populate(struct trbox *trb,
813 struct cmd_t_test *cmd);
817 /* Create a trbox according to mode*/
819 trbox_create(struct trbox *trb,
820 struct cmd_t_test *cmd, int mode)
825 trbox_one_sample_init(trb,cmd);
828 trbox_independent_samples_init(trb,cmd);
831 trbox_paired_init(trb,cmd);
838 /* Populate a trbox according to cmd */
840 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
842 trb->populate(trb,cmd);
845 /* Submit and destroy a trbox */
847 trbox_finalize(struct trbox *trb)
852 /* Initialize the independent samples trbox */
854 trbox_independent_samples_init(struct trbox *self,
855 struct cmd_t_test *cmd UNUSED)
858 const int vsize=cmd->n_variables*2+3;
861 self->populate = trbox_independent_samples_populate;
863 trbox_base_init(self,cmd->n_variables*2,hsize);
864 tab_title(self->t,0,_("Independent Samples Test"));
865 tab_hline(self->t,TAL_1,2,hsize-1,1);
866 tab_vline(self->t,TAL_2,2,0,vsize-1);
867 tab_vline(self->t,TAL_1,4,0,vsize-1);
868 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
869 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
870 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
871 tab_joint_text(self->t, 2, 0, 3, 0,
872 TAB_CENTER,_("Levene's Test for Equality of Variances"));
873 tab_joint_text(self->t, 4,0,hsize-1,0,
874 TAB_CENTER,_("t-test for Equality of Means"));
876 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
877 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
878 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
879 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
880 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
881 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
882 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
883 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
884 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
886 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
887 _("%d%% Confidence Interval of the Difference"),
888 (int)round(cmd->criteria*100.0));
892 /* Populate the independent samples trbox */
894 trbox_independent_samples_populate(struct trbox *self,
895 struct cmd_t_test *cmd )
900 for (i=0; i < cmd->n_variables; ++i)
910 double pooled_variance;
914 struct t_test_proc *ttp0;
915 struct t_test_proc *ttp1;
916 ttp0=&groups_stats[i][0];
917 ttp1=&groups_stats[i][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"));
924 df = ttp0->n + ttp1->n - 2.0 ;
925 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
927 pooled_variance = ( (ttp0->n )*sqr(ttp0->s_std_dev)
929 (ttp1->n )*sqr(ttp1->s_std_dev)
932 t = (ttp0->mean - ttp1->mean) / sqrt(pooled_variance) ;
933 t /= sqrt((ttp0->n + ttp1->n)/(ttp0->n*ttp1->n));
935 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
938 which=1; /* get p & q from t & df */
939 cdft(&which, &p, &q, &t, &df, &status, &bound);
942 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
945 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
947 mean_diff = ttp0->mean - ttp1->mean;
948 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
951 std_err_diff = sqrt( sqr(ttp0->se_mean) + sqr(ttp1->se_mean));
952 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
955 /* Now work out the confidence interval */
956 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
958 which=2; /* Calc T from p,q and df */
959 cdft(&which, &p, &q, &t, &df, &status, &bound);
962 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
965 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
966 mean_diff - t * std_err_diff, 8, 3);
968 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
969 mean_diff + t * std_err_diff, 8, 3);
974 /* Now for the \sigma_1 != \sigma_2 case */
975 tab_text (self->t, 1, i*2+3+1,
976 TAB_LEFT, _("Equal variances not assumed"));
979 se2 = (sqr(ttp0->s_std_dev)/(ttp0->n -1) ) +
980 (sqr(ttp1->s_std_dev)/(ttp1->n -1) );
982 t = mean_diff / sqrt(se2) ;
983 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
986 (sqr(sqr(ttp0->s_std_dev)/(ttp0->n - 1 ))
990 (sqr(sqr(ttp1->s_std_dev)/(ttp1->n - 1 ))
994 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
996 which=1; /* get p & q from t & df */
997 cdft(&which, &p, &q, &t, &df, &status, &bound);
1000 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1003 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1005 /* Now work out the confidence interval */
1006 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1008 which=2; /* Calc T from p,q and df */
1009 cdft(&which, &p, &q, &t, &df, &status, &bound);
1012 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1016 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1019 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1022 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1023 mean_diff - t * std_err_diff, 8, 3);
1025 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1026 mean_diff + t * std_err_diff, 8, 3);
1036 /* Initialize the paired samples trbox */
1038 trbox_paired_init(struct trbox *self,
1039 struct cmd_t_test *cmd UNUSED)
1043 const int vsize=n_pairs+3;
1045 self->populate = trbox_paired_populate;
1047 trbox_base_init(self,n_pairs,hsize);
1048 tab_title (self->t, 0, _("Paired Samples Test"));
1049 tab_hline(self->t,TAL_1,2,6,1);
1050 tab_vline(self->t,TAL_2,2,0,vsize);
1051 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1052 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1053 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1054 tab_hline(self->t,TAL_1,5,6, 2);
1055 tab_vline(self->t,TAL_0,6,0,1);
1057 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1058 _("%d%% Confidence Interval of the Difference"),
1059 (int)round(cmd->criteria*100.0));
1061 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1062 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1063 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1064 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1065 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1066 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1067 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1068 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1071 /* Populate the paired samples trbox */
1073 trbox_paired_populate(struct trbox *trb,
1074 struct cmd_t_test *cmd UNUSED)
1078 for (i=0; i < n_pairs; ++i)
1086 struct variable *v0 = pairs[i].v[0];
1087 struct variable *v1 = pairs[i].v[1];
1089 struct t_test_proc *ttp0 = &v0->p.t_t;
1090 struct t_test_proc *ttp1 = &v1->p.t_t;
1096 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1098 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1099 pairs[i].v[0]->name, pairs[i].v[1]->name);
1101 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1103 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1106 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1107 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1109 /* Now work out the confidence interval */
1110 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1112 which=2; /* Calc T from p,q and df */
1113 cdft(&which, &p, &q, &t, &df, &status, &bound);
1117 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1120 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1121 pairs[i].mean_diff - t * se_mean , 8, 4);
1123 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1124 pairs[i].mean_diff + t * se_mean , 8, 4);
1126 t = ( ttp0->mean - ttp1->mean)
1128 ( sqr(ttp0->s_std_dev) + sqr(ttp1->s_std_dev) -
1129 2 * pairs[i].correlation * ttp0->s_std_dev * ttp1->s_std_dev )
1133 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1135 /* Degrees of freedom */
1136 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1139 cdft(&which, &p, &q, &t, &df, &status, &bound);
1143 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1147 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1154 /* Initialize the one sample trbox */
1156 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1159 const int vsize=cmd->n_variables+3;
1161 self->populate = trbox_one_sample_populate;
1163 trbox_base_init(self, cmd->n_variables,hsize);
1164 tab_title (self->t, 0, _("One-Sample Test"));
1165 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1166 tab_vline(self->t, TAL_2, 1, 0, vsize);
1168 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1169 _("Test Value = %f"),cmd->n_testval);
1171 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1174 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1175 _("%d%% Confidence Interval of the Difference"),
1176 (int)round(cmd->criteria*100.0));
1178 tab_vline(self->t,TAL_0,6,1,1);
1179 tab_hline(self->t,TAL_1,5,6,2);
1180 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1181 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1182 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1183 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1184 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1185 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1190 /* Populate the one sample trbox */
1192 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1198 for (i=0; i < cmd->n_variables; ++i)
1206 struct t_test_proc *ttp;
1207 ttp= &cmd->v_variables[i]->p.t_t;
1210 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1212 t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ;
1214 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1216 /* degrees of freedom */
1219 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1221 cdft(&which, &p, &q, &t, &df, &status, &bound);
1225 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1229 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1231 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1233 tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3);
1236 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1238 which=2; /* Calc T from p,q and df */
1239 cdft(&which, &p, &q, &t, &df, &status, &bound);
1242 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1245 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1246 ttp->mean_diff - t * ttp->se_mean, 8,4);
1248 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1249 ttp->mean_diff + t * ttp->se_mean, 8,4);
1253 /* Base initializer for the generalized trbox */
1255 trbox_base_init(struct trbox *self, int data_rows, int cols)
1257 const int rows = 3 + data_rows;
1259 self->finalize = trbox_base_finalize;
1260 self->t = tab_create (cols, rows, 0);
1261 tab_headers (self->t,0,0,3,0);
1262 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1263 tab_hline(self->t, TAL_2,0,cols-1,3);
1264 tab_dim (self->t, tab_natural_dimensions);
1268 /* Base finalizer for the trbox */
1270 trbox_base_finalize(struct trbox *trb)
1276 /* Create , populate and submit the Paired Samples Correlation box */
1280 const int rows=1+n_pairs;
1284 struct tab_table *table;
1286 table = tab_create (cols,rows,0);
1288 tab_columns (table, SOM_COL_DOWN, 1);
1289 tab_headers (table,0,0,1,0);
1290 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1291 tab_hline(table, TAL_2, 0, cols - 1, 1);
1292 tab_vline(table, TAL_2, 2, 0, rows - 1);
1293 tab_dim(table, tab_natural_dimensions);
1294 tab_title(table, 0, _("Paired Samples Correlations"));
1296 /* column headings */
1297 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1298 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1299 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1301 for (i=0; i < n_pairs; ++i)
1309 double df = pairs[i].v[0]->p.t_t.n -2;
1311 double correlation_t =
1312 pairs[i].correlation * sqrt(df) /
1313 sqrt(1 - sqr(pairs[i].correlation));
1317 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1320 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1321 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1325 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1326 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.n , 4, 0);
1329 cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1333 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1337 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1346 /* Calculation Implementation */
1348 /* Per case calculations common to all variants of the T test */
1350 common_calc (struct ccase *c, void *aux UNUSED)
1354 double weight = dict_get_case_weight(default_dict,c);
1356 for(i=0; i< cmd.n_variables ; ++i)
1358 struct t_test_proc *ttp;
1359 struct variable *v = cmd.v_variables[i];
1360 union value *val = &c->data[v->fv];
1362 ttp= &cmd.v_variables[i]->p.t_t;
1364 if (val->f != SYSMIS)
1367 ttp->sum+=weight * val->f;
1368 ttp->ssq+=weight * val->f * val->f;
1374 /* Pre calculations common to all variants of the T test */
1376 common_precalc (void *aux UNUSED)
1380 for(i=0; i< cmd.n_variables ; ++i)
1382 struct t_test_proc *ttp;
1383 ttp= &cmd.v_variables[i]->p.t_t;
1392 /* Post calculations common to all variants of the T test */
1394 common_postcalc (void *aux UNUSED)
1398 for(i=0; i< cmd.n_variables ; ++i)
1400 struct t_test_proc *ttp;
1401 ttp= &cmd.v_variables[i]->p.t_t;
1403 ttp->mean=ttp->sum / ttp->n;
1404 ttp->s_std_dev= sqrt(
1405 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1410 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1413 ttp->se_mean = ttp->std_dev / sqrt(ttp->n);
1414 ttp->mean_diff= ttp->sum_diff / ttp->n;
1418 /* Per case calculations for one sample t test */
1420 one_sample_calc (struct ccase *c, void *aux UNUSED)
1424 double weight = dict_get_case_weight(default_dict,c);
1426 for(i=0; i< cmd.n_variables ; ++i)
1428 struct t_test_proc *ttp;
1429 struct variable *v = cmd.v_variables[i];
1430 union value *val = &c->data[v->fv];
1432 ttp= &cmd.v_variables[i]->p.t_t;
1434 if (val->f != SYSMIS)
1435 ttp->sum_diff += weight * (val->f - cmd.n_testval);
1441 /* Pre calculations for one sample t test */
1443 one_sample_precalc (void *aux UNUSED)
1447 for(i=0; i< cmd.n_variables ; ++i)
1449 struct t_test_proc *ttp;
1450 ttp= &cmd.v_variables[i]->p.t_t;
1456 /* Post calculations for one sample t test */
1458 one_sample_postcalc (void *aux UNUSED)
1462 for(i=0; i< cmd.n_variables ; ++i)
1464 struct t_test_proc *ttp;
1465 ttp= &cmd.v_variables[i]->p.t_t;
1468 ttp->mean_diff = ttp->sum_diff / ttp->n ;
1475 compare_var_name (const void *a_, const void *b_, void *v_ UNUSED)
1477 const struct variable *a = a_;
1478 const struct variable *b = b_;
1480 return strcmp(a->name,b->name);
1484 hash_var_name (const void *a_, void *v_ UNUSED)
1486 const struct variable *a = a_;
1488 return hsh_hash_bytes (a->name, strlen(a->name));
1493 paired_precalc (void *aux UNUSED)
1497 for(i=0; i < n_pairs ; ++i )
1499 pairs[i].correlation=0;
1500 pairs[i].sum_of_diffs=0;
1501 pairs[i].ssq_diffs=0;
1507 paired_calc (struct ccase *c, void *aux UNUSED)
1511 for(i=0; i < n_pairs ; ++i )
1513 struct variable *v0 = pairs[i].v[0];
1514 struct variable *v1 = pairs[i].v[1];
1516 union value *val0 = &c->data[v0->fv];
1517 union value *val1 = &c->data[v1->fv];
1519 pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.mean )
1521 ( val1->f - pairs[i].v[1]->p.t_t.mean );
1523 pairs[i].sum_of_diffs += val0->f - val1->f ;
1524 pairs[i].ssq_diffs += sqr(val0->f - val1->f);
1533 paired_postcalc (void *aux UNUSED)
1537 for(i=0; i < n_pairs ; ++i )
1539 const double n = pairs[i].v[0]->p.t_t.n ;
1541 pairs[i].correlation /= pairs[i].v[0]->p.t_t.std_dev *
1542 pairs[i].v[1]->p.t_t.std_dev ;
1543 pairs[i].correlation /= pairs[i].v[0]->p.t_t.n -1;
1546 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1549 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1550 ( pairs[i].ssq_diffs / n )
1552 sqr(pairs[i].mean_diff )
1559 /* Compare two (union value)s */
1561 value_compare(const union value *a, const union value *b, int width)
1564 return (a->f < b->f) ? -1 : ( a->f > b->f ) ;
1566 return memcmp (a->s, b->s, width);
1571 get_group(const union value *val, struct variable *var)
1573 if ( 0 == value_compare(val,&groups_values[0],var->width) )
1575 else if (0 == value_compare(val,&groups_values[1],var->width) )
1585 group_precalc (void *aux UNUSED)
1590 groups_stats = xmalloc(sizeof(group_stats_t) * cmd.n_variables);
1592 for(i=0; i< cmd.n_variables ; ++i)
1594 for (j=0 ; j < 2 ; ++j)
1596 groups_stats[i][j].sum=0;
1597 groups_stats[i][j].n=0;
1598 groups_stats[i][j].ssq=0;
1605 group_calc (struct ccase *c, void *aux UNUSED)
1608 union value *gv = &c->data[groups->fv];
1610 double weight = dict_get_case_weight(default_dict,c);
1612 gv = &c->data[groups->fv];
1614 for(i=0; i< cmd.n_variables ; ++i)
1616 int g = get_group(gv,groups);
1617 struct t_test_proc *ttp=&groups_stats[i][g];
1618 union value *val=&c->data[cmd.v_variables[i]->fv];
1621 ttp->sum+=weight * val->f;
1622 ttp->ssq+=weight * sqr(val->f);
1629 group_postcalc (void *aux UNUSED)
1634 for(i=0; i< cmd.n_variables ; ++i)
1636 for (j=0 ; j < 2 ; ++j)
1638 struct t_test_proc *ttp;
1639 ttp=&groups_stats[i][j];
1641 ttp->mean = ttp->sum / ttp->n;
1643 ttp->s_std_dev= sqrt(
1644 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1649 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1652 ttp->se_mean = ttp->std_dev / sqrt(ttp->n);