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
23 #include <gsl/gsl_cdf.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.").
59 static struct cmd_t_test cmd;
61 /* Function to use for testing for missing values */
62 static is_missing_func value_is_missing;
64 /* Variable for the GROUPS subcommand, if given. */
65 static struct variable *indep_var;
67 /* GROUPS: Number of values specified by the user; the values
70 static int n_group_values;
71 static union value groups_values[2];
72 static enum comparison criteria[2];
76 /* PAIRS: Number of pairs to be compared ; each pair. */
77 static int n_pairs = 0 ;
80 /* The variables comprising the pair */
81 struct variable *v[2];
83 /* The number of valid variable pairs */
86 /* The sum of the members */
89 /* sum of squares of the members */
92 /* Std deviation of the members */
96 /* Sample Std deviation of the members */
99 /* The means of the members */
102 /* The correlation coefficient between the variables */
105 /* The sum of the differences */
108 /* The sum of the products */
111 /* The mean of the differences */
114 /* The sum of the squares of the differences */
117 /* The std deviation of the differences */
121 static struct pair *pairs=0;
123 static int parse_value (union value * v, int type) ;
125 /* Structures and Functions for the Statistics Summary Box */
127 typedef void populate_ssbox_func(struct ssbox *ssb,
128 struct cmd_t_test *cmd);
129 typedef void finalize_ssbox_func(struct ssbox *ssb);
135 populate_ssbox_func *populate;
136 finalize_ssbox_func *finalize;
141 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
143 /* Populate a ssbox according to cmd */
144 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
146 /* Submit and destroy a ssbox */
147 void ssbox_finalize(struct ssbox *ssb);
149 /* A function to create, populate and submit the Paired Samples Correlation
154 /* Structures and Functions for the Test Results Box */
157 typedef void populate_trbox_func(struct trbox *trb,
158 struct cmd_t_test *cmd);
159 typedef void finalize_trbox_func(struct trbox *trb);
163 populate_trbox_func *populate;
164 finalize_trbox_func *finalize;
168 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
170 /* Populate a ssbox according to cmd */
171 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
173 /* Submit and destroy a ssbox */
174 void trbox_finalize(struct trbox *trb);
176 /* Which mode was T-TEST invoked */
184 static int common_calc (struct ccase *, void *);
185 static void common_precalc (void *);
186 static void common_postcalc (void *);
188 static int one_sample_calc (struct ccase *, void *);
189 static void one_sample_precalc (void *);
190 static void one_sample_postcalc (void *);
192 static int paired_calc (struct ccase *, void *);
193 static void paired_precalc (void *);
194 static void paired_postcalc (void *);
196 static void group_precalc (void *);
197 static int group_calc (struct ccase *, void *);
198 static void group_postcalc (void *);
201 static int compare_var_name (const void *a_, const void *b_, void *v_ UNUSED);
202 static unsigned hash_var_name (const void *a_, void *v_ UNUSED);
211 struct ssbox stat_summary_box;
212 struct trbox test_results_box;
214 if ( !parse_t_test(&cmd) )
217 if (! cmd.sbc_criteria)
222 if (cmd.sbc_testval) ++m;
223 if (cmd.sbc_groups) ++m;
224 if (cmd.sbc_pairs) ++m;
229 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
237 else if (cmd.sbc_groups)
242 if ( mode == T_PAIRED)
244 if (cmd.sbc_variables)
246 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
251 /* Iterate through the pairs and put each variable that is a
252 member of a pair into cmd.v_variables */
255 struct hsh_iterator hi;
256 struct hsh_table *hash;
259 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
261 for (i=0; i < n_pairs; ++i)
263 hsh_insert(hash,pairs[i].v[0]);
264 hsh_insert(hash,pairs[i].v[1]);
267 assert(cmd.n_variables == 0);
268 cmd.n_variables = hsh_count(hash);
270 cmd.v_variables = xrealloc(cmd.v_variables,
271 sizeof(struct variable) * cmd.n_variables);
272 /* Iterate through the hash */
273 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
275 v=hsh_next(hash,&hi) )
276 cmd.v_variables[i++]=v;
281 else if ( !cmd.sbc_variables)
283 msg(SE, _("One or more VARIABLES must be specified."));
288 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
289 if (cmd.incl == TTS_INCLUDE )
290 value_is_missing = is_system_missing;
292 value_is_missing = is_missing;
294 procedure_with_splits (common_precalc, common_calc, common_postcalc, NULL);
299 procedure_with_splits (one_sample_precalc, one_sample_calc,
300 one_sample_postcalc, NULL);
303 procedure_with_splits (paired_precalc, paired_calc, paired_postcalc,
307 procedure_with_splits(group_precalc,group_calc,group_postcalc, NULL);
308 levene(indep_var, cmd.n_variables, cmd.v_variables,
309 (cmd.miss == TTS_LISTWISE)?LEV_LISTWISE:LEV_ANALYSIS ,
314 ssbox_create(&stat_summary_box,&cmd,mode);
315 ssbox_populate(&stat_summary_box,&cmd);
316 ssbox_finalize(&stat_summary_box);
318 if ( mode == T_PAIRED)
321 trbox_create(&test_results_box,&cmd,mode);
322 trbox_populate(&test_results_box,&cmd);
323 trbox_finalize(&test_results_box);
329 if ( mode == T_IND_SAMPLES)
332 /* Destroy any group statistics we created */
333 for (i= 0 ; i < cmd.n_variables ; ++i )
335 free(cmd.v_variables[i]->p.t_t.gs);
343 tts_custom_groups (struct cmd_t_test *cmd UNUSED)
348 if (token != T_ALL &&
349 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
352 msg(SE,_("`%s' is not a variable name"),tokid);
356 indep_var = parse_variable ();
359 lex_error ("expecting variable name in GROUPS subcommand");
363 if (indep_var->type == T_STRING && indep_var->width > MAX_SHORT_STRING)
365 msg (SE, _("Long string variable %s is not valid here."),
370 if (!lex_match ('('))
372 if (indep_var->type == NUMERIC)
374 groups_values[0].f = 1;
375 groups_values[1].f = 2;
376 criteria[0] = criteria[1] = CMP_EQ;
382 msg (SE, _("When applying GROUPS to a string variable, at "
383 "least one value must be specified."));
388 if (!parse_value (&groups_values[0],indep_var->type))
394 criteria[0] = CMP_LE;
395 criteria[1] = CMP_GT;
396 groups_values[1] = groups_values[0];
401 if (!parse_value (&groups_values[1],indep_var->type))
405 if (!lex_force_match (')'))
408 criteria[0] = criteria[1] = CMP_EQ;
414 tts_custom_pairs (struct cmd_t_test *cmd UNUSED)
416 struct variable **vars;
421 int n_after_WITH = -1;
422 int paired ; /* Was the PAIRED keyword given ? */
426 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
429 msg(SE,_("`%s' is not a variable name"),tokid);
434 if (!parse_variables (default_dict, &vars, &n_vars,
435 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
443 if (lex_match (T_WITH))
445 n_before_WITH = n_vars;
446 if (!parse_variables (default_dict, &vars, &n_vars,
447 PV_DUPLICATE | PV_APPEND
448 | PV_NUMERIC | PV_NO_SCRATCH))
453 n_after_WITH = n_vars - n_before_WITH;
456 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
458 /* Determine the number of pairs needed */
461 if (n_before_WITH != n_after_WITH)
464 msg (SE, _("PAIRED was specified but the number of variables "
465 "preceding WITH (%d) did not match the number "
467 n_before_WITH, n_after_WITH );
470 n_pairs_local=n_before_WITH;
472 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
474 n_pairs_local=n_before_WITH * n_after_WITH ;
476 else /* Neither WITH nor PAIRED keyword given */
481 msg (SE, _("At least two variables must be specified "
486 /* how many ways can you pick 2 from n_vars ? */
487 n_pairs_local = n_vars * (n_vars -1 ) /2 ;
491 /* Allocate storage for the pairs */
492 pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
494 /* Populate the pairs with the appropriate variables */
499 assert(n_pairs_local == n_vars/2);
500 for (i = 0; i < n_pairs_local ; ++i)
502 pairs[i].v[n_pairs+0] = vars[i];
503 pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
506 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
511 for(i=0 ; i < n_before_WITH ; ++i )
513 for(j=0 ; j < n_after_WITH ; ++j)
515 pairs[p].v[0] = vars[i];
516 pairs[p].v[1] = vars[j+n_before_WITH];
521 else /* Neither WITH nor PAIRED given */
526 for(i=0 ; i < n_vars ; ++i )
528 for(j=i+1 ; j < n_vars ; ++j)
530 pairs[p].v[0] = vars[i];
531 pairs[p].v[1] = vars[j];
537 n_pairs+=n_pairs_local;
542 /* Parses the current token (numeric or string, depending on type)
543 value v and returns success. */
545 parse_value (union value * v, int type )
549 if (!lex_force_num ())
555 if (!lex_force_string ())
557 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
566 /* Implementation of the SSBOX object */
568 void ssbox_base_init(struct ssbox *this, int cols,int rows);
570 void ssbox_base_finalize(struct ssbox *ssb);
572 void ssbox_one_sample_init(struct ssbox *this,
573 struct cmd_t_test *cmd );
575 void ssbox_independent_samples_init(struct ssbox *this,
576 struct cmd_t_test *cmd);
578 void ssbox_paired_init(struct ssbox *this,
579 struct cmd_t_test *cmd);
581 /* Factory to create an ssbox */
583 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
588 ssbox_one_sample_init(ssb,cmd);
591 ssbox_independent_samples_init(ssb,cmd);
594 ssbox_paired_init(ssb,cmd);
602 /* Despatcher for the populate method */
604 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
606 ssb->populate(ssb,cmd);
610 /* Despatcher for finalize */
612 ssbox_finalize(struct ssbox *ssb)
618 /* Submit the box and clear up */
620 ssbox_base_finalize(struct ssbox *ssb)
625 /* Initialize a ssbox struct */
627 ssbox_base_init(struct ssbox *this, int cols,int rows)
629 this->finalize = ssbox_base_finalize;
630 this->t = tab_create (cols, rows, 0);
632 tab_columns (this->t, SOM_COL_DOWN, 1);
633 tab_headers (this->t,0,0,1,0);
634 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
635 tab_hline(this->t, TAL_2,0,cols-1,1);
636 tab_dim (this->t, tab_natural_dimensions);
639 void ssbox_one_sample_populate(struct ssbox *ssb,
640 struct cmd_t_test *cmd);
642 /* Initialize the one_sample ssbox */
644 ssbox_one_sample_init(struct ssbox *this,
645 struct cmd_t_test *cmd )
648 const int vsize=cmd->n_variables+1;
650 this->populate = ssbox_one_sample_populate;
652 ssbox_base_init(this, hsize,vsize);
653 tab_title (this->t, 0, _("One-Sample Statistics"));
654 tab_vline(this->t, TAL_2, 1,0,vsize);
655 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
656 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
657 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
658 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
661 void ssbox_independent_samples_populate(struct ssbox *ssb,
662 struct cmd_t_test *cmd);
664 /* Initialize the independent samples ssbox */
666 ssbox_independent_samples_init(struct ssbox *this,
667 struct cmd_t_test *cmd)
670 int vsize = cmd->n_variables*2 +1;
672 this->populate = ssbox_independent_samples_populate;
674 ssbox_base_init(this, hsize,vsize);
675 tab_title (this->t, 0, _("Group Statistics"));
676 tab_vline(this->t,0,1,0,vsize);
677 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, indep_var->name);
678 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
679 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
680 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
681 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
685 /* Populate the ssbox for independent samples */
687 ssbox_independent_samples_populate(struct ssbox *ssb,
688 struct cmd_t_test *cmd)
695 char prefix[2][3]={"",""};
697 if ( indep_var->type == NUMERIC )
699 val_lab0 = val_labs_find( indep_var->val_labs,groups_values[0]);
700 val_lab1 = val_labs_find( indep_var->val_labs,groups_values[1]);
704 val_lab0 = groups_values[0].s;
705 val_lab1 = groups_values[1].s;
708 if (n_group_values == 1)
710 strcpy(prefix[0],"< ");
711 strcpy(prefix[1],">=");
716 for (i=0; i < cmd->n_variables; ++i)
720 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
723 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
724 "%s%s", prefix[0], val_lab0);
726 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
727 "%s%g", prefix[0], groups_values[0].f);
731 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
732 "%s%s", prefix[1], val_lab1);
734 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
735 "%s%g", prefix[1], groups_values[1].f);
737 /* Fill in the group statistics */
738 for ( g=0; g < 2 ; ++g )
740 struct group_statistics *gs = &cmd->v_variables[i]->p.t_t.gs[g];
742 tab_float(ssb->t, 2 ,i*2+g+1, TAB_RIGHT, gs->n, 2, 0);
743 tab_float(ssb->t, 3 ,i*2+g+1, TAB_RIGHT, gs->mean, 8, 2);
744 tab_float(ssb->t, 4 ,i*2+g+1, TAB_RIGHT, gs->std_dev, 8, 3);
745 tab_float(ssb->t, 5 ,i*2+g+1, TAB_RIGHT, gs->se_mean, 8, 3);
751 void ssbox_paired_populate(struct ssbox *ssb,
752 struct cmd_t_test *cmd);
754 /* Initialize the paired values ssbox */
756 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
760 int vsize = n_pairs*2+1;
762 this->populate = ssbox_paired_populate;
764 ssbox_base_init(this, hsize,vsize);
765 tab_title (this->t, 0, _("Paired Sample Statistics"));
766 tab_vline(this->t,TAL_0,1,0,vsize-1);
767 tab_vline(this->t,TAL_2,2,0,vsize-1);
768 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
769 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
770 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
771 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
775 /* Populate the ssbox for paired values */
777 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
783 for (i=0; i < n_pairs; ++i)
787 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
789 for (j=0 ; j < 2 ; ++j)
791 struct group_statistics *gs;
793 gs=&pairs[i].v[j]->p.t_t.ugs;
797 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
800 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
801 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 2, 0);
802 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
803 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
809 /* Populate the one sample ssbox */
811 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
817 for (i=0; i < cmd->n_variables; ++i)
819 struct group_statistics *gs;
820 gs= &cmd->v_variables[i]->p.t_t.ugs;
822 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
823 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 0);
824 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
825 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
826 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
833 /* Implementation of the Test Results box struct */
835 void trbox_base_init(struct trbox *self,int n_vars, int cols);
836 void trbox_base_finalize(struct trbox *trb);
838 void trbox_independent_samples_init(struct trbox *trb,
839 struct cmd_t_test *cmd );
841 void trbox_independent_samples_populate(struct trbox *trb,
842 struct cmd_t_test *cmd);
844 void trbox_one_sample_init(struct trbox *self,
845 struct cmd_t_test *cmd );
847 void trbox_one_sample_populate(struct trbox *trb,
848 struct cmd_t_test *cmd);
850 void trbox_paired_init(struct trbox *self,
851 struct cmd_t_test *cmd );
853 void trbox_paired_populate(struct trbox *trb,
854 struct cmd_t_test *cmd);
858 /* Create a trbox according to mode*/
860 trbox_create(struct trbox *trb,
861 struct cmd_t_test *cmd, int mode)
866 trbox_one_sample_init(trb,cmd);
869 trbox_independent_samples_init(trb,cmd);
872 trbox_paired_init(trb,cmd);
879 /* Populate a trbox according to cmd */
881 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
883 trb->populate(trb,cmd);
886 /* Submit and destroy a trbox */
888 trbox_finalize(struct trbox *trb)
893 /* Initialize the independent samples trbox */
895 trbox_independent_samples_init(struct trbox *self,
896 struct cmd_t_test *cmd UNUSED)
899 const int vsize=cmd->n_variables*2+3;
902 self->populate = trbox_independent_samples_populate;
904 trbox_base_init(self,cmd->n_variables*2,hsize);
905 tab_title(self->t,0,_("Independent Samples Test"));
906 tab_hline(self->t,TAL_1,2,hsize-1,1);
907 tab_vline(self->t,TAL_2,2,0,vsize-1);
908 tab_vline(self->t,TAL_1,4,0,vsize-1);
909 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
910 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
911 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
912 tab_joint_text(self->t, 2, 0, 3, 0,
913 TAB_CENTER,_("Levene's Test for Equality of Variances"));
914 tab_joint_text(self->t, 4,0,hsize-1,0,
915 TAB_CENTER,_("t-test for Equality of Means"));
917 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
918 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
919 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
920 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
921 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
922 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
923 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
924 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
925 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
927 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
928 _("%d%% Confidence Interval of the Difference"),
929 (int)round(cmd->criteria*100.0));
933 /* Populate the independent samples trbox */
935 trbox_independent_samples_populate(struct trbox *self,
936 struct cmd_t_test *cmd )
941 for (i=0; i < cmd->n_variables; ++i)
950 double pooled_variance;
954 struct group_statistics *gs0 = &cmd->v_variables[i]->p.t_t.gs[0];
955 struct group_statistics *gs1 = &cmd->v_variables[i]->p.t_t.gs[1];
957 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
959 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
962 tab_float(self->t, 2, i*2+3, TAB_CENTER,
963 cmd->v_variables[i]->p.t_t.levene, 8,3);
966 /* Now work out the significance of the Levene test */
967 df1 = 1; df2 = cmd->v_variables[i]->p.t_t.ugs.n - 2;
968 q = gsl_cdf_fdist_Q(cmd->v_variables[i]->p.t_t.levene, df1, df2);
970 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
972 df = gs0->n + gs1->n - 2.0 ;
973 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
975 pooled_variance = ( (gs0->n )*sqr(gs0->s_std_dev)
977 (gs1->n )*sqr(gs1->s_std_dev)
980 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
981 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
983 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
985 p = gsl_cdf_tdist_P(t, df);
986 q = gsl_cdf_tdist_Q(t, df);
988 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
990 mean_diff = gs0->mean - gs1->mean;
991 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
994 std_err_diff = sqrt( sqr(gs0->se_mean) + sqr(gs1->se_mean));
995 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
998 /* Now work out the confidence interval */
999 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1001 t = gsl_cdf_tdist_Qinv(q,df);
1003 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1004 mean_diff - t * std_err_diff, 8, 3);
1006 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1007 mean_diff + t * std_err_diff, 8, 3);
1012 /* Now for the \sigma_1 != \sigma_2 case */
1013 tab_text (self->t, 1, i*2+3+1,
1014 TAB_LEFT, _("Equal variances not assumed"));
1017 se2 = (sqr(gs0->s_std_dev)/(gs0->n -1) ) +
1018 (sqr(gs1->s_std_dev)/(gs1->n -1) );
1020 t = mean_diff / sqrt(se2) ;
1021 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1024 (sqr(sqr(gs0->s_std_dev)/(gs0->n - 1 ))
1028 (sqr(sqr(gs1->s_std_dev)/(gs1->n - 1 ))
1032 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1034 p = gsl_cdf_tdist_P(t, df);
1035 q = gsl_cdf_tdist_Q(t, df);
1037 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1039 /* Now work out the confidence interval */
1040 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1042 t = gsl_cdf_tdist_Qinv(q, df);
1044 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1047 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1050 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1051 mean_diff - t * std_err_diff, 8, 3);
1053 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1054 mean_diff + t * std_err_diff, 8, 3);
1060 /* Initialize the paired samples trbox */
1062 trbox_paired_init(struct trbox *self,
1063 struct cmd_t_test *cmd UNUSED)
1067 const int vsize=n_pairs+3;
1069 self->populate = trbox_paired_populate;
1071 trbox_base_init(self,n_pairs,hsize);
1072 tab_title (self->t, 0, _("Paired Samples Test"));
1073 tab_hline(self->t,TAL_1,2,6,1);
1074 tab_vline(self->t,TAL_2,2,0,vsize);
1075 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1076 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1077 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1078 tab_hline(self->t,TAL_1,5,6, 2);
1079 tab_vline(self->t,TAL_0,6,0,1);
1081 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1082 _("%d%% Confidence Interval of the Difference"),
1083 (int)round(cmd->criteria*100.0));
1085 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1086 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1087 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1088 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1089 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1090 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1091 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1092 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1095 /* Populate the paired samples trbox */
1097 trbox_paired_populate(struct trbox *trb,
1098 struct cmd_t_test *cmd UNUSED)
1102 for (i=0; i < n_pairs; ++i)
1107 double n = pairs[i].n;
1111 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1113 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1114 pairs[i].v[0]->name, pairs[i].v[1]->name);
1116 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1118 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1121 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1122 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1124 /* Now work out the confidence interval */
1125 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1127 t = gsl_cdf_tdist_Qinv(q, df);
1129 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1130 pairs[i].mean_diff - t * se_mean , 8, 4);
1132 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1133 pairs[i].mean_diff + t * se_mean , 8, 4);
1135 t = (pairs[i].mean[0] - pairs[i].mean[1])
1137 ( sqr (pairs[i].s_std_dev[0]) + sqr (pairs[i].s_std_dev[1]) -
1138 2 * pairs[i].correlation *
1139 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1143 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1145 /* Degrees of freedom */
1146 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1148 p = gsl_cdf_tdist_P(t,df);
1149 q = gsl_cdf_tdist_P(t,df);
1151 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1156 /* Initialize the one sample trbox */
1158 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1161 const int vsize=cmd->n_variables+3;
1163 self->populate = trbox_one_sample_populate;
1165 trbox_base_init(self, cmd->n_variables,hsize);
1166 tab_title (self->t, 0, _("One-Sample Test"));
1167 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1168 tab_vline(self->t, TAL_2, 1, 0, vsize);
1170 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1171 _("Test Value = %f"),cmd->n_testval);
1173 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1176 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1177 _("%d%% Confidence Interval of the Difference"),
1178 (int)round(cmd->criteria*100.0));
1180 tab_vline(self->t,TAL_0,6,1,1);
1181 tab_hline(self->t,TAL_1,5,6,2);
1182 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1183 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1184 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1185 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1186 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1187 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1192 /* Populate the one sample trbox */
1194 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1200 for (i=0; i < cmd->n_variables; ++i)
1205 struct group_statistics *gs;
1206 gs= &cmd->v_variables[i]->p.t_t.ugs;
1209 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1211 t = (gs->mean - cmd->n_testval ) * sqrt(gs->n) / gs->std_dev ;
1213 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1215 /* degrees of freedom */
1218 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1220 p = gsl_cdf_tdist_P(t, df);
1221 q = gsl_cdf_tdist_Q(t, df);
1223 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1225 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1227 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1230 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1231 t = gsl_cdf_tdist_Qinv(q, df);
1233 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1234 gs->mean_diff - t * gs->se_mean, 8,4);
1236 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1237 gs->mean_diff + t * gs->se_mean, 8,4);
1241 /* Base initializer for the generalized trbox */
1243 trbox_base_init(struct trbox *self, int data_rows, int cols)
1245 const int rows = 3 + data_rows;
1247 self->finalize = trbox_base_finalize;
1248 self->t = tab_create (cols, rows, 0);
1249 tab_headers (self->t,0,0,3,0);
1250 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1251 tab_hline(self->t, TAL_2,0,cols-1,3);
1252 tab_dim (self->t, tab_natural_dimensions);
1256 /* Base finalizer for the trbox */
1258 trbox_base_finalize(struct trbox *trb)
1264 /* Create , populate and submit the Paired Samples Correlation box */
1268 const int rows=1+n_pairs;
1272 struct tab_table *table;
1274 table = tab_create (cols,rows,0);
1276 tab_columns (table, SOM_COL_DOWN, 1);
1277 tab_headers (table,0,0,1,0);
1278 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1279 tab_hline(table, TAL_2, 0, cols - 1, 1);
1280 tab_vline(table, TAL_2, 2, 0, rows - 1);
1281 tab_dim(table, tab_natural_dimensions);
1282 tab_title(table, 0, _("Paired Samples Correlations"));
1284 /* column headings */
1285 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1286 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1287 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1289 for (i=0; i < n_pairs; ++i)
1293 double df = pairs[i].n -2;
1295 double correlation_t =
1296 pairs[i].correlation * sqrt(df) /
1297 sqrt(1 - sqr(pairs[i].correlation));
1301 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1304 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1305 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1309 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1310 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1312 p = gsl_cdf_tdist_P(correlation_t, df);
1313 q = gsl_cdf_tdist_Q(correlation_t, df);
1315 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1323 /* Calculation Implementation */
1325 /* Per case calculations common to all variants of the T test */
1327 common_calc (struct ccase *c, void *aux UNUSED)
1331 double weight = dict_get_case_weight(default_dict,c);
1334 /* Skip the entire case if /MISSING=LISTWISE is set */
1335 if ( cmd.miss == TTS_LISTWISE )
1337 for(i=0; i< cmd.n_variables ; ++i)
1339 struct variable *v = cmd.v_variables[i];
1340 union value *val = &c->data[v->fv];
1342 if (value_is_missing(val,v) )
1349 /* Listwise has to be implicit if the independent variable is missing ?? */
1350 if ( cmd.sbc_groups )
1352 union value *gv = &c->data[indep_var->fv];
1353 if ( value_is_missing(gv,indep_var) )
1360 for(i=0; i< cmd.n_variables ; ++i)
1362 struct group_statistics *gs;
1363 struct variable *v = cmd.v_variables[i];
1364 union value *val = &c->data[v->fv];
1366 gs= &cmd.v_variables[i]->p.t_t.ugs;
1368 if (! value_is_missing(val,v) )
1371 gs->sum+=weight * val->f;
1372 gs->ssq+=weight * val->f * val->f;
1378 /* Pre calculations common to all variants of the T test */
1380 common_precalc (void *aux UNUSED)
1384 for(i=0; i< cmd.n_variables ; ++i)
1386 struct group_statistics *gs;
1387 gs= &cmd.v_variables[i]->p.t_t.ugs;
1396 /* Post calculations common to all variants of the T test */
1398 common_postcalc (void *aux UNUSED)
1402 for(i=0; i< cmd.n_variables ; ++i)
1404 struct group_statistics *gs;
1405 gs= &cmd.v_variables[i]->p.t_t.ugs;
1407 gs->mean=gs->sum / gs->n;
1408 gs->s_std_dev= sqrt(
1409 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1414 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1417 gs->se_mean = gs->std_dev / sqrt(gs->n);
1418 gs->mean_diff= gs->sum_diff / gs->n;
1422 /* Per case calculations for one sample t test */
1424 one_sample_calc (struct ccase *c, void *aux UNUSED)
1428 double weight = dict_get_case_weight(default_dict,c);
1430 /* Skip the entire case if /MISSING=LISTWISE is set */
1431 if ( cmd.miss == TTS_LISTWISE )
1433 for(i=0; i< cmd.n_variables ; ++i)
1435 struct variable *v = cmd.v_variables[i];
1436 union value *val = &c->data[v->fv];
1438 if (value_is_missing(val,v) )
1445 for(i=0; i< cmd.n_variables ; ++i)
1447 struct group_statistics *gs;
1448 struct variable *v = cmd.v_variables[i];
1449 union value *val = &c->data[v->fv];
1451 gs= &cmd.v_variables[i]->p.t_t.ugs;
1453 if ( ! value_is_missing(val,v))
1454 gs->sum_diff += weight * (val->f - cmd.n_testval);
1460 /* Pre calculations for one sample t test */
1462 one_sample_precalc (void *aux UNUSED)
1466 for(i=0; i< cmd.n_variables ; ++i)
1468 struct group_statistics *gs;
1469 gs= &cmd.v_variables[i]->p.t_t.ugs;
1475 /* Post calculations for one sample t test */
1477 one_sample_postcalc (void *aux UNUSED)
1481 for(i=0; i< cmd.n_variables ; ++i)
1483 struct group_statistics *gs;
1484 gs= &cmd.v_variables[i]->p.t_t.ugs;
1487 gs->mean_diff = gs->sum_diff / gs->n ;
1494 compare_var_name (const void *a_, const void *b_, void *v_ UNUSED)
1496 const struct variable *a = a_;
1497 const struct variable *b = b_;
1499 return strcmp(a->name,b->name);
1503 hash_var_name (const void *a_, void *v_ UNUSED)
1505 const struct variable *a = a_;
1507 return hsh_hash_bytes (a->name, strlen(a->name));
1513 paired_precalc (void *aux UNUSED)
1517 for(i=0; i < n_pairs ; ++i )
1520 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1521 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1522 pairs[i].sum_of_prod = 0;
1523 pairs[i].correlation = 0;
1524 pairs[i].sum_of_diffs = 0;
1525 pairs[i].ssq_diffs = 0;
1532 paired_calc (struct ccase *c, void *aux UNUSED)
1536 double weight = dict_get_case_weight(default_dict,c);
1538 /* Skip the entire case if /MISSING=LISTWISE is set ,
1539 AND one member of a pair is missing */
1540 if ( cmd.miss == TTS_LISTWISE )
1542 for(i=0; i < n_pairs ; ++i )
1544 struct variable *v0 = pairs[i].v[0];
1545 struct variable *v1 = pairs[i].v[1];
1547 union value *val0 = &c->data[v0->fv];
1548 union value *val1 = &c->data[v1->fv];
1550 if ( value_is_missing(val0,v0) ||
1551 value_is_missing(val1,v1) )
1558 for(i=0; i < n_pairs ; ++i )
1560 struct variable *v0 = pairs[i].v[0];
1561 struct variable *v1 = pairs[i].v[1];
1563 union value *val0 = &c->data[v0->fv];
1564 union value *val1 = &c->data[v1->fv];
1566 if ( ( !value_is_missing(val0,v0) && !value_is_missing(val1,v1) ) )
1568 pairs[i].n += weight;
1569 pairs[i].sum[0] += weight * val0->f;
1570 pairs[i].sum[1] += weight * val1->f;
1572 pairs[i].ssq[0] += weight * sqr(val0->f);
1573 pairs[i].ssq[1] += weight * sqr(val1->f);
1575 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1577 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1578 pairs[i].ssq_diffs += weight * sqr(val0->f - val1->f);
1586 paired_postcalc (void *aux UNUSED)
1590 for(i=0; i < n_pairs ; ++i )
1593 const double n = pairs[i].n;
1595 for (j=0; j < 2 ; ++j)
1597 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1598 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1599 sqr(pairs[i].mean[j]))
1602 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1603 sqr(pairs[i].mean[j]))
1607 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1608 pairs[i].mean[0] * pairs[i].mean[1] ;
1609 /* correlation now actually contains the covariance */
1611 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1612 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1614 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1616 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1617 ( pairs[i].ssq_diffs / n )
1619 sqr(pairs[i].mean_diff )
1624 /* Return the group # corresponding to the
1625 independent variable with the value val
1628 get_group(const union value *val, struct variable *indep)
1632 for (i = 0; i < 2 ; ++i )
1634 const int cmp = compare_values(val,&groups_values[i],indep->width) ;
1635 switch ( criteria[i])
1638 if ( 0 == cmp ) return i;
1641 if ( 0 > cmp ) return i;
1644 if ( cmp <= 0 ) return i;
1647 if ( cmp > 0 ) return i;
1650 if ( cmp >= 0 ) return i;
1657 /* No groups matched */
1663 group_precalc (void *aux UNUSED)
1668 for(i=0; i< cmd.n_variables ; ++i)
1670 struct t_test_proc *ttpr = &cmd.v_variables[i]->p.t_t;
1672 /* There's always 2 groups for a T - TEST */
1674 ttpr->gs = xmalloc(sizeof(struct group_statistics) * 2) ;
1676 for (j=0 ; j < 2 ; ++j)
1678 ttpr->gs[j].sum = 0;
1680 ttpr->gs[j].ssq = 0;
1682 if ( n_group_values == 2 )
1683 ttpr->gs[j].id = groups_values[j];
1685 ttpr->gs[j].id = groups_values[0];
1686 ttpr->gs[j].criterion = criteria[j];
1693 group_calc (struct ccase *c, void *aux UNUSED)
1697 union value *gv = &c->data[indep_var->fv];
1699 double weight = dict_get_case_weight(default_dict,c);
1701 if ( value_is_missing(gv,indep_var) )
1706 if ( cmd.miss == TTS_LISTWISE )
1708 for(i=0; i< cmd.n_variables ; ++i)
1710 struct variable *v = cmd.v_variables[i];
1711 union value *val = &c->data[v->fv];
1713 if (value_is_missing(val,v) )
1721 gv = &c->data[indep_var->fv];
1723 g = get_group(gv,indep_var);
1725 /* If the independent variable doesn't match either of the values
1726 for this case then move on to the next case */
1730 for(i=0; i< cmd.n_variables ; ++i)
1732 struct variable *var = cmd.v_variables[i];
1734 struct group_statistics *gs = &var->p.t_t.gs[g];
1736 union value *val=&c->data[var->fv];
1738 if ( !value_is_missing(val,var) )
1741 gs->sum+=weight * val->f;
1742 gs->ssq+=weight * sqr(val->f);
1751 group_postcalc (void *aux UNUSED)
1756 for(i=0; i< cmd.n_variables ; ++i)
1758 for (j=0 ; j < 2 ; ++j)
1760 struct group_statistics *gs;
1761 gs=&cmd.v_variables[i]->p.t_t.gs[j];
1763 gs->mean = gs->sum / gs->n;
1765 gs->s_std_dev= sqrt(
1766 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1771 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1774 gs->se_mean = gs->std_dev / sqrt(gs->n);