1 /* PSPP - computes sample statistics. -*-c-*-
3 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <gsl/gsl_cdf.h>
27 #include <data/case.h>
28 #include <data/casefile.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/value-labels.h>
32 #include <data/variable.h>
33 #include <data/casefilter.h>
35 #include <language/command.h>
36 #include <language/dictionary/split-file.h>
37 #include <language/lexer/lexer.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/hash.h>
42 #include <libpspp/magic.h>
43 #include <libpspp/message.h>
44 #include <libpspp/message.h>
45 #include <libpspp/misc.h>
46 #include <libpspp/str.h>
47 #include <math/group-proc.h>
48 #include <math/levene.h>
49 #include <output/manager.h>
50 #include <output/table.h>
55 #define _(msgid) gettext (msgid)
63 +variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
65 missing=miss:!analysis/listwise,
66 incl:include/!exclude;
67 +format=fmt:!labels/nolabels;
68 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
74 /* Variable for the GROUPS subcommand, if given. */
75 static struct variable *indep_var;
83 struct group_properties
85 /* The comparison criterion */
86 enum comparison criterion;
88 /* The width of the independent variable */
92 /* The value of the independent variable at which groups are determined to
93 belong to one group or the other */
94 double critical_value;
97 /* The values of the independent variable for each group */
98 union value g_value[2];
104 static struct group_properties gp ;
108 /* PAIRS: Number of pairs to be compared ; each pair. */
109 static int n_pairs = 0 ;
112 /* The variables comprising the pair */
113 struct variable *v[2];
115 /* The number of valid variable pairs */
118 /* The sum of the members */
121 /* sum of squares of the members */
124 /* Std deviation of the members */
128 /* Sample Std deviation of the members */
131 /* The means of the members */
134 /* The correlation coefficient between the variables */
137 /* The sum of the differences */
140 /* The sum of the products */
143 /* The mean of the differences */
146 /* The sum of the squares of the differences */
149 /* The std deviation of the differences */
153 static struct pair *pairs=0;
155 static int parse_value (struct lexer *lexer, union value * v, enum var_type);
157 /* Structures and Functions for the Statistics Summary Box */
159 typedef void populate_ssbox_func(struct ssbox *ssb,
160 struct cmd_t_test *cmd);
161 typedef void finalize_ssbox_func(struct ssbox *ssb);
167 populate_ssbox_func *populate;
168 finalize_ssbox_func *finalize;
173 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
175 /* Populate a ssbox according to cmd */
176 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
178 /* Submit and destroy a ssbox */
179 void ssbox_finalize(struct ssbox *ssb);
181 /* A function to create, populate and submit the Paired Samples Correlation
186 /* Structures and Functions for the Test Results Box */
189 typedef void populate_trbox_func(struct trbox *trb,
190 struct cmd_t_test *cmd);
191 typedef void finalize_trbox_func(struct trbox *trb);
195 populate_trbox_func *populate;
196 finalize_trbox_func *finalize;
200 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
202 /* Populate a ssbox according to cmd */
203 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
205 /* Submit and destroy a ssbox */
206 void trbox_finalize(struct trbox *trb);
208 /* Which mode was T-TEST invoked */
216 static int common_calc (const struct dictionary *dict,
217 const struct ccase *, void *,
218 const struct casefilter *filter);
219 static void common_precalc (struct cmd_t_test *);
220 static void common_postcalc (struct cmd_t_test *);
222 static int one_sample_calc (const struct dictionary *dict, const struct ccase *, void *, const struct casefilter *);
223 static void one_sample_precalc (struct cmd_t_test *);
224 static void one_sample_postcalc (struct cmd_t_test *);
226 static int paired_calc (const struct dictionary *dict, const struct ccase *,
227 struct cmd_t_test*, const struct casefilter *);
228 static void paired_precalc (struct cmd_t_test *);
229 static void paired_postcalc (struct cmd_t_test *);
231 static void group_precalc (struct cmd_t_test *);
232 static int group_calc (const struct dictionary *dict, const struct ccase *,
233 struct cmd_t_test *, const struct casefilter *);
234 static void group_postcalc (struct cmd_t_test *);
237 static bool calculate(const struct ccase *first,
238 const struct casefile *cf, void *_mode,
239 const struct dataset *ds);
243 static struct cmd_t_test cmd;
245 static bool bad_weight_warn = false;
248 static int compare_group_binary(const struct group_statistics *a,
249 const struct group_statistics *b,
250 const struct group_properties *p);
253 static unsigned hash_group_binary(const struct group_statistics *g,
254 const struct group_properties *p);
259 cmd_t_test (struct lexer *lexer, struct dataset *ds)
263 if ( !parse_t_test (lexer, ds, &cmd, NULL) )
266 if (! cmd.sbc_criteria)
271 if (cmd.sbc_testval) ++m;
272 if (cmd.sbc_groups) ++m;
273 if (cmd.sbc_pairs) ++m;
278 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
287 else if (cmd.sbc_groups)
292 if ( mode == T_PAIRED)
294 if (cmd.sbc_variables)
296 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
302 /* Iterate through the pairs and put each variable that is a
303 member of a pair into cmd.v_variables */
306 struct hsh_iterator hi;
307 struct hsh_table *hash;
310 hash = hsh_create (n_pairs, compare_vars_by_name, hash_var_by_name,
313 for (i=0; i < n_pairs; ++i)
315 hsh_insert(hash,pairs[i].v[0]);
316 hsh_insert(hash,pairs[i].v[1]);
319 assert(cmd.n_variables == 0);
320 cmd.n_variables = hsh_count(hash);
322 cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
323 sizeof *cmd.v_variables);
324 /* Iterate through the hash */
325 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
327 v=hsh_next(hash,&hi) )
328 cmd.v_variables[i++]=v;
333 else if ( !cmd.sbc_variables)
335 msg(SE, _("One or more VARIABLES must be specified."));
340 bad_weight_warn = true;
342 ok = multipass_procedure_with_splits (ds, calculate, &cmd);
348 if ( mode == T_IND_SAMPLES)
351 /* Destroy any group statistics we created */
352 for (v = 0 ; v < cmd.n_variables ; ++v )
354 struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
355 hsh_destroy (grpp->group_hash);
360 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
364 tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
366 int n_group_values=0;
368 lex_match (lexer, '=');
370 indep_var = parse_variable (lexer, dataset_dict (ds));
373 lex_error (lexer, "expecting variable name in GROUPS subcommand");
377 if (var_is_long_string (indep_var))
379 msg (SE, _("Long string variable %s is not valid here."),
380 var_get_name (indep_var));
384 if (!lex_match (lexer, '('))
386 if (var_is_numeric (indep_var))
388 gp.v.g_value[0].f = 1;
389 gp.v.g_value[1].f = 2;
391 gp.criterion = CMP_EQ;
399 msg (SE, _("When applying GROUPS to a string variable, two "
400 "values must be specified."));
405 if (!parse_value (lexer, &gp.v.g_value[0], var_get_type (indep_var)))
408 lex_match (lexer, ',');
409 if (lex_match (lexer, ')'))
411 if (var_is_alpha (indep_var))
413 msg (SE, _("When applying GROUPS to a string variable, two "
414 "values must be specified."));
417 gp.criterion = CMP_LE;
418 gp.v.critical_value = gp.v.g_value[0].f;
424 if (!parse_value (lexer, &gp.v.g_value[1], var_get_type (indep_var)))
428 if (!lex_force_match (lexer, ')'))
431 if ( n_group_values == 2 )
432 gp.criterion = CMP_EQ ;
434 gp.criterion = CMP_LE ;
442 tts_custom_pairs (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
444 struct variable **vars;
446 size_t n_pairs_local;
448 size_t n_before_WITH;
449 size_t n_after_WITH = SIZE_MAX;
450 int paired ; /* Was the PAIRED keyword given ? */
452 lex_match (lexer, '=');
455 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
456 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
464 if (lex_match (lexer, T_WITH))
466 n_before_WITH = n_vars;
467 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
468 PV_DUPLICATE | PV_APPEND
469 | PV_NUMERIC | PV_NO_SCRATCH))
474 n_after_WITH = n_vars - n_before_WITH;
477 paired = (lex_match (lexer, '(') && lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
479 /* Determine the number of pairs needed */
482 if (n_before_WITH != n_after_WITH)
485 msg (SE, _("PAIRED was specified but the number of variables "
486 "preceding WITH (%d) did not match the number "
488 n_before_WITH, n_after_WITH );
491 n_pairs_local = n_before_WITH;
493 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
495 n_pairs_local = n_before_WITH * n_after_WITH ;
497 else /* Neither WITH nor PAIRED keyword given */
502 msg (SE, _("At least two variables must be specified "
507 /* how many ways can you pick 2 from n_vars ? */
508 n_pairs_local = n_vars * (n_vars - 1) / 2;
512 /* Allocate storage for the pairs */
513 pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
515 /* Populate the pairs with the appropriate variables */
520 assert(n_pairs_local == n_vars / 2);
521 for (i = 0; i < n_pairs_local; ++i)
523 pairs[i].v[n_pairs] = vars[i];
524 pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
527 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
532 for(i=0 ; i < n_before_WITH ; ++i )
534 for(j=0 ; j < n_after_WITH ; ++j)
536 pairs[p].v[0] = vars[i];
537 pairs[p].v[1] = vars[j+n_before_WITH];
542 else /* Neither WITH nor PAIRED given */
547 for(i=0 ; i < n_vars ; ++i )
549 for(j=i+1 ; j < n_vars ; ++j)
551 pairs[p].v[0] = vars[i];
552 pairs[p].v[1] = vars[j];
558 n_pairs+=n_pairs_local;
564 /* Parses the current token (numeric or string, depending on type)
565 value v and returns success. */
567 parse_value (struct lexer *lexer, union value * v, enum var_type type)
569 if (type == VAR_NUMERIC)
571 if (!lex_force_num (lexer))
573 v->f = lex_tokval (lexer);
577 if (!lex_force_string (lexer))
579 strncpy (v->s, ds_cstr (lex_tokstr (lexer)), ds_length (lex_tokstr (lexer)));
588 /* Implementation of the SSBOX object */
590 void ssbox_base_init(struct ssbox *this, int cols,int rows);
592 void ssbox_base_finalize(struct ssbox *ssb);
594 void ssbox_one_sample_init(struct ssbox *this,
595 struct cmd_t_test *cmd );
597 void ssbox_independent_samples_init(struct ssbox *this,
598 struct cmd_t_test *cmd);
600 void ssbox_paired_init(struct ssbox *this,
601 struct cmd_t_test *cmd);
604 /* Factory to create an ssbox */
606 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
611 ssbox_one_sample_init(ssb,cmd);
614 ssbox_independent_samples_init(ssb,cmd);
617 ssbox_paired_init(ssb,cmd);
626 /* Despatcher for the populate method */
628 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
630 ssb->populate(ssb,cmd);
634 /* Despatcher for finalize */
636 ssbox_finalize(struct ssbox *ssb)
642 /* Submit the box and clear up */
644 ssbox_base_finalize(struct ssbox *ssb)
651 /* Initialize a ssbox struct */
653 ssbox_base_init(struct ssbox *this, int cols,int rows)
655 this->finalize = ssbox_base_finalize;
656 this->t = tab_create (cols, rows, 0);
658 tab_columns (this->t, SOM_COL_DOWN, 1);
659 tab_headers (this->t,0,0,1,0);
660 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
661 tab_hline(this->t, TAL_2,0,cols-1,1);
662 tab_dim (this->t, tab_natural_dimensions);
665 void ssbox_one_sample_populate(struct ssbox *ssb,
666 struct cmd_t_test *cmd);
668 /* Initialize the one_sample ssbox */
670 ssbox_one_sample_init(struct ssbox *this,
671 struct cmd_t_test *cmd )
674 const int vsize=cmd->n_variables+1;
676 this->populate = ssbox_one_sample_populate;
678 ssbox_base_init(this, hsize,vsize);
679 tab_title (this->t, _("One-Sample Statistics"));
680 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
681 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
682 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
683 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
684 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
687 void ssbox_independent_samples_populate(struct ssbox *ssb,
688 struct cmd_t_test *cmd);
690 /* Initialize the independent samples ssbox */
692 ssbox_independent_samples_init(struct ssbox *this,
693 struct cmd_t_test *cmd)
696 int vsize = cmd->n_variables*2 +1;
698 this->populate = ssbox_independent_samples_populate;
700 ssbox_base_init(this, hsize,vsize);
701 tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
702 tab_title (this->t, _("Group Statistics"));
703 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, var_get_name (indep_var));
704 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
705 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
706 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
707 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
711 /* Populate the ssbox for independent samples */
713 ssbox_independent_samples_populate(struct ssbox *ssb,
714 struct cmd_t_test *cmd)
718 const char *val_lab0;
719 const char *val_lab1;
720 double indep_value[2];
722 char prefix[2][3]={"",""};
724 if ( var_is_numeric (indep_var) )
726 val_lab0 = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
727 val_lab1 = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
731 val_lab0 = gp.v.g_value[0].s;
732 val_lab1 = gp.v.g_value[1].s;
735 if (gp.criterion == CMP_LE )
737 strcpy(prefix[0],"< ");
738 strcpy(prefix[1],">=");
739 indep_value[0] = gp.v.critical_value;
740 indep_value[1] = gp.v.critical_value;
744 indep_value[0] = gp.v.g_value[0].f;
745 indep_value[1] = gp.v.g_value[1].f;
750 for (i=0; i < cmd->n_variables; ++i)
752 struct variable *var = cmd->v_variables[i];
753 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
756 tab_text (ssb->t, 0, i*2+1, TAB_LEFT,
757 var_get_name (cmd->v_variables[i]));
760 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
761 "%s%s", prefix[0], val_lab0);
763 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
764 "%s%g", prefix[0], indep_value[0]);
768 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
769 "%s%s", prefix[1], val_lab1);
771 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
772 "%s%g", prefix[1], indep_value[1]);
775 /* Fill in the group statistics */
776 for ( count = 0 ; count < 2 ; ++count )
778 union value search_val;
780 struct group_statistics *gs;
782 if ( gp.criterion == CMP_LE )
786 /* less than ( < ) case */
787 search_val.f = gp.v.critical_value - 1.0;
792 search_val.f = gp.v.critical_value + 1.0;
797 search_val = gp.v.g_value[count];
800 gs = hsh_find(grp_hash, (void *) &search_val);
803 tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 2, 0);
804 tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
805 tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
806 tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
812 void ssbox_paired_populate(struct ssbox *ssb,
813 struct cmd_t_test *cmd);
815 /* Initialize the paired values ssbox */
817 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
821 int vsize = n_pairs*2+1;
823 this->populate = ssbox_paired_populate;
825 ssbox_base_init(this, hsize,vsize);
826 tab_title (this->t, _("Paired Sample Statistics"));
827 tab_vline(this->t,TAL_GAP,1,0,vsize-1);
828 tab_vline(this->t,TAL_2,2,0,vsize-1);
829 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
830 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
831 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
832 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
836 /* Populate the ssbox for paired values */
838 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
844 for (i=0; i < n_pairs; ++i)
848 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
850 for (j=0 ; j < 2 ; ++j)
852 struct group_statistics *gs;
854 gs = &group_proc_get (pairs[i].v[j])->ugs;
858 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT,
859 var_get_name (pairs[i].v[j]));
862 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
863 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 2, 0);
864 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
865 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
871 /* Populate the one sample ssbox */
873 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
879 for (i=0; i < cmd->n_variables; ++i)
881 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
883 tab_text (ssb->t, 0, i+1, TAB_LEFT, var_get_name (cmd->v_variables[i]));
884 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 0);
885 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
886 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
887 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
894 /* Implementation of the Test Results box struct */
896 void trbox_base_init(struct trbox *self,size_t n_vars, int cols);
897 void trbox_base_finalize(struct trbox *trb);
899 void trbox_independent_samples_init(struct trbox *trb,
900 struct cmd_t_test *cmd );
902 void trbox_independent_samples_populate(struct trbox *trb,
903 struct cmd_t_test *cmd);
905 void trbox_one_sample_init(struct trbox *self,
906 struct cmd_t_test *cmd );
908 void trbox_one_sample_populate(struct trbox *trb,
909 struct cmd_t_test *cmd);
911 void trbox_paired_init(struct trbox *self,
912 struct cmd_t_test *cmd );
914 void trbox_paired_populate(struct trbox *trb,
915 struct cmd_t_test *cmd);
919 /* Create a trbox according to mode*/
921 trbox_create(struct trbox *trb,
922 struct cmd_t_test *cmd, int mode)
927 trbox_one_sample_init(trb,cmd);
930 trbox_independent_samples_init(trb,cmd);
933 trbox_paired_init(trb,cmd);
940 /* Populate a trbox according to cmd */
942 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
944 trb->populate(trb,cmd);
947 /* Submit and destroy a trbox */
949 trbox_finalize(struct trbox *trb)
954 /* Initialize the independent samples trbox */
956 trbox_independent_samples_init(struct trbox *self,
957 struct cmd_t_test *cmd UNUSED)
960 const int vsize=cmd->n_variables*2+3;
963 self->populate = trbox_independent_samples_populate;
965 trbox_base_init(self,cmd->n_variables*2,hsize);
966 tab_title(self->t,_("Independent Samples Test"));
967 tab_hline(self->t,TAL_1,2,hsize-1,1);
968 tab_vline(self->t,TAL_2,2,0,vsize-1);
969 tab_vline(self->t,TAL_1,4,0,vsize-1);
970 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
971 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
972 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
973 tab_joint_text(self->t, 2, 0, 3, 0,
974 TAB_CENTER,_("Levene's Test for Equality of Variances"));
975 tab_joint_text(self->t, 4,0,hsize-1,0,
976 TAB_CENTER,_("t-test for Equality of Means"));
978 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
979 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
980 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
981 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
982 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
983 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
984 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
985 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
986 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
988 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
989 _("%g%% Confidence Interval of the Difference"),
990 cmd->criteria*100.0);
994 /* Populate the independent samples trbox */
996 trbox_independent_samples_populate(struct trbox *self,
997 struct cmd_t_test *cmd )
1002 for (i=0; i < cmd->n_variables; ++i)
1011 double pooled_variance;
1012 double std_err_diff;
1015 struct variable *var = cmd->v_variables[i];
1016 struct group_proc *grp_data = group_proc_get (var);
1018 struct hsh_table *grp_hash = grp_data->group_hash;
1020 struct group_statistics *gs0 ;
1021 struct group_statistics *gs1 ;
1023 union value search_val;
1025 if ( gp.criterion == CMP_LE )
1026 search_val.f = gp.v.critical_value - 1.0;
1028 search_val = gp.v.g_value[0];
1030 gs0 = hsh_find(grp_hash, (void *) &search_val);
1033 if ( gp.criterion == CMP_LE )
1034 search_val.f = gp.v.critical_value + 1.0;
1036 search_val = gp.v.g_value[1];
1038 gs1 = hsh_find(grp_hash, (void *) &search_val);
1042 tab_text (self->t, 0, i*2+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1044 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1047 tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1049 /* Now work out the significance of the Levene test */
1050 df1 = 1; df2 = grp_data->ugs.n - 2;
1051 q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1053 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1055 df = gs0->n + gs1->n - 2.0 ;
1056 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
1058 pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1060 (gs1->n )*pow2(gs1->s_std_dev)
1063 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1064 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
1066 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1068 p = gsl_cdf_tdist_P(t, df);
1069 q = gsl_cdf_tdist_Q(t, df);
1071 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1073 mean_diff = gs0->mean - gs1->mean;
1074 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1077 std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1078 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1081 /* Now work out the confidence interval */
1082 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1084 t = gsl_cdf_tdist_Qinv(q,df);
1085 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1086 mean_diff - t * std_err_diff, 8, 3);
1088 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1089 mean_diff + t * std_err_diff, 8, 3);
1094 /* Now for the \sigma_1 != \sigma_2 case */
1095 tab_text (self->t, 1, i*2+3+1,
1096 TAB_LEFT, _("Equal variances not assumed"));
1099 se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1100 (pow2(gs1->s_std_dev)/(gs1->n -1) );
1102 t = mean_diff / sqrt(se2) ;
1103 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1106 (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 ))
1110 (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1114 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1116 p = gsl_cdf_tdist_P(t, df);
1117 q = gsl_cdf_tdist_Q(t, df);
1119 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1121 /* Now work out the confidence interval */
1122 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1124 t = gsl_cdf_tdist_Qinv(q, df);
1126 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1129 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1132 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1133 mean_diff - t * std_err_diff, 8, 3);
1135 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1136 mean_diff + t * std_err_diff, 8, 3);
1142 /* Initialize the paired samples trbox */
1144 trbox_paired_init(struct trbox *self,
1145 struct cmd_t_test *cmd UNUSED)
1149 const int vsize=n_pairs+3;
1151 self->populate = trbox_paired_populate;
1153 trbox_base_init(self,n_pairs,hsize);
1154 tab_title (self->t, _("Paired Samples Test"));
1155 tab_hline(self->t,TAL_1,2,6,1);
1156 tab_vline(self->t,TAL_2,2,0,vsize - 1);
1157 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1158 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1159 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1160 tab_hline(self->t,TAL_1,5,6, 2);
1161 tab_vline(self->t,TAL_GAP,6,0,1);
1163 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1164 _("%g%% Confidence Interval of the Difference"),
1165 cmd->criteria*100.0);
1167 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1168 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1169 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1170 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1171 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1172 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1173 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1174 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1177 /* Populate the paired samples trbox */
1179 trbox_paired_populate(struct trbox *trb,
1180 struct cmd_t_test *cmd UNUSED)
1184 for (i=0; i < n_pairs; ++i)
1189 double n = pairs[i].n;
1193 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1195 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1196 var_get_name (pairs[i].v[0]),
1197 var_get_name (pairs[i].v[1]));
1199 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1201 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1204 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1205 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1207 /* Now work out the confidence interval */
1208 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1210 t = gsl_cdf_tdist_Qinv(q, df);
1212 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1213 pairs[i].mean_diff - t * se_mean , 8, 4);
1215 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1216 pairs[i].mean_diff + t * se_mean , 8, 4);
1218 t = (pairs[i].mean[0] - pairs[i].mean[1])
1220 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1221 2 * pairs[i].correlation *
1222 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1226 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1228 /* Degrees of freedom */
1229 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1231 p = gsl_cdf_tdist_P(t,df);
1232 q = gsl_cdf_tdist_P(t,df);
1234 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1239 /* Initialize the one sample trbox */
1241 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1244 const int vsize=cmd->n_variables+3;
1246 self->populate = trbox_one_sample_populate;
1248 trbox_base_init(self, cmd->n_variables,hsize);
1249 tab_title (self->t, _("One-Sample Test"));
1250 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1251 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1253 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1254 _("Test Value = %f"), cmd->n_testval[0]);
1256 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1259 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1260 _("%g%% Confidence Interval of the Difference"),
1261 cmd->criteria*100.0);
1263 tab_vline(self->t,TAL_GAP,6,1,1);
1264 tab_hline(self->t,TAL_1,5,6,2);
1265 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1266 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1267 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1268 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1269 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1270 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1275 /* Populate the one sample trbox */
1277 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1283 for (i=0; i < cmd->n_variables; ++i)
1288 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1291 tab_text (trb->t, 0, i+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1293 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1295 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1297 /* degrees of freedom */
1300 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1302 p = gsl_cdf_tdist_P(t, df);
1303 q = gsl_cdf_tdist_Q(t, df);
1305 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1307 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1309 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1312 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1313 t = gsl_cdf_tdist_Qinv(q, df);
1315 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1316 gs->mean_diff - t * gs->se_mean, 8,4);
1318 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1319 gs->mean_diff + t * gs->se_mean, 8,4);
1323 /* Base initializer for the generalized trbox */
1325 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1327 const size_t rows = 3 + data_rows;
1329 self->finalize = trbox_base_finalize;
1330 self->t = tab_create (cols, rows, 0);
1331 tab_headers (self->t,0,0,3,0);
1332 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1333 tab_hline(self->t, TAL_2,0,cols-1,3);
1334 tab_dim (self->t, tab_natural_dimensions);
1338 /* Base finalizer for the trbox */
1340 trbox_base_finalize(struct trbox *trb)
1346 /* Create , populate and submit the Paired Samples Correlation box */
1350 const int rows=1+n_pairs;
1354 struct tab_table *table;
1356 table = tab_create (cols,rows,0);
1358 tab_columns (table, SOM_COL_DOWN, 1);
1359 tab_headers (table,0,0,1,0);
1360 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1361 tab_hline(table, TAL_2, 0, cols - 1, 1);
1362 tab_vline(table, TAL_2, 2, 0, rows - 1);
1363 tab_dim(table, tab_natural_dimensions);
1364 tab_title(table, _("Paired Samples Correlations"));
1366 /* column headings */
1367 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1368 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1369 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1371 for (i=0; i < n_pairs; ++i)
1375 double df = pairs[i].n -2;
1377 double correlation_t =
1378 pairs[i].correlation * sqrt(df) /
1379 sqrt(1 - pow2(pairs[i].correlation));
1383 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1386 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1388 var_get_name (pairs[i].v[0]),
1389 var_get_name (pairs[i].v[1]));
1393 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1394 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1396 p = gsl_cdf_tdist_P(correlation_t, df);
1397 q = gsl_cdf_tdist_Q(correlation_t, df);
1399 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1408 /* Calculation Implementation */
1410 /* Per case calculations common to all variants of the T test */
1412 common_calc (const struct dictionary *dict,
1413 const struct ccase *c,
1415 const struct casefilter *filter)
1418 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1420 double weight = dict_get_case_weight (dict, c, &bad_weight_warn);
1423 /* Listwise has to be implicit if the independent variable is missing ?? */
1424 if ( cmd->sbc_groups )
1426 if ( casefilter_variable_missing (filter, c, indep_var) )
1430 for(i = 0; i < cmd->n_variables ; ++i)
1432 struct variable *v = cmd->v_variables[i];
1434 if (! casefilter_variable_missing (filter, c, v) )
1436 struct group_statistics *gs;
1437 const union value *val = case_data (c, v);
1438 gs = &group_proc_get (cmd->v_variables[i])->ugs;
1441 gs->sum += weight * val->f;
1442 gs->ssq += weight * val->f * val->f;
1448 /* Pre calculations common to all variants of the T test */
1450 common_precalc ( struct cmd_t_test *cmd )
1454 for(i=0; i< cmd->n_variables ; ++i)
1456 struct group_statistics *gs;
1457 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1466 /* Post calculations common to all variants of the T test */
1468 common_postcalc (struct cmd_t_test *cmd)
1472 for(i=0; i< cmd->n_variables ; ++i)
1474 struct group_statistics *gs;
1475 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1477 gs->mean=gs->sum / gs->n;
1478 gs->s_std_dev= sqrt(
1479 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1484 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1487 gs->se_mean = gs->std_dev / sqrt(gs->n);
1488 gs->mean_diff= gs->sum_diff / gs->n;
1492 /* Per case calculations for one sample t test */
1494 one_sample_calc (const struct dictionary *dict,
1495 const struct ccase *c, void *cmd_,
1496 const struct casefilter *filter)
1500 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1502 double weight = dict_get_case_weight (dict, c, &bad_weight_warn);
1505 for(i=0; i< cmd->n_variables ; ++i)
1507 struct group_statistics *gs;
1508 struct variable *v = cmd->v_variables[i];
1509 const union value *val = case_data (c, v);
1511 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1513 if ( ! casefilter_variable_missing (filter, c, v))
1514 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1520 /* Pre calculations for one sample t test */
1522 one_sample_precalc ( struct cmd_t_test *cmd )
1526 for(i=0; i< cmd->n_variables ; ++i)
1528 struct group_statistics *gs;
1529 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1535 /* Post calculations for one sample t test */
1537 one_sample_postcalc (struct cmd_t_test *cmd)
1541 for(i=0; i< cmd->n_variables ; ++i)
1543 struct group_statistics *gs;
1544 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1546 gs->mean_diff = gs->sum_diff / gs->n ;
1553 paired_precalc (struct cmd_t_test *cmd UNUSED)
1557 for(i=0; i < n_pairs ; ++i )
1560 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1561 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1562 pairs[i].sum_of_prod = 0;
1563 pairs[i].correlation = 0;
1564 pairs[i].sum_of_diffs = 0;
1565 pairs[i].ssq_diffs = 0;
1572 paired_calc (const struct dictionary *dict, const struct ccase *c,
1573 struct cmd_t_test *cmd UNUSED, const struct casefilter *filter)
1577 double weight = dict_get_case_weight (dict, c, &bad_weight_warn);
1579 for(i=0; i < n_pairs ; ++i )
1581 struct variable *v0 = pairs[i].v[0];
1582 struct variable *v1 = pairs[i].v[1];
1584 const union value *val0 = case_data (c, v0);
1585 const union value *val1 = case_data (c, v1);
1587 if ( ! casefilter_variable_missing (filter, c, v0) &&
1588 ! casefilter_variable_missing (filter, c, v1) )
1590 pairs[i].n += weight;
1591 pairs[i].sum[0] += weight * val0->f;
1592 pairs[i].sum[1] += weight * val1->f;
1594 pairs[i].ssq[0] += weight * pow2(val0->f);
1595 pairs[i].ssq[1] += weight * pow2(val1->f);
1597 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1599 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1600 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1608 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1612 for(i=0; i < n_pairs ; ++i )
1615 const double n = pairs[i].n;
1617 for (j=0; j < 2 ; ++j)
1619 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1620 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1621 pow2(pairs[i].mean[j]))
1624 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1625 pow2(pairs[i].mean[j]))
1629 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1630 pairs[i].mean[0] * pairs[i].mean[1] ;
1631 /* correlation now actually contains the covariance */
1633 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1634 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1636 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1638 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1639 ( pairs[i].ssq_diffs / n )
1641 pow2(pairs[i].mean_diff )
1647 group_precalc (struct cmd_t_test *cmd )
1652 for(i=0; i< cmd->n_variables ; ++i)
1654 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1656 /* There's always 2 groups for a T - TEST */
1659 gp.indep_width = var_get_width (indep_var);
1661 ttpr->group_hash = hsh_create(2,
1662 (hsh_compare_func *) compare_group_binary,
1663 (hsh_hash_func *) hash_group_binary,
1664 (hsh_free_func *) free_group,
1667 for (j=0 ; j < 2 ; ++j)
1670 struct group_statistics *gs = xmalloc (sizeof *gs);
1676 if ( gp.criterion == CMP_EQ )
1678 gs->id = gp.v.g_value[j];
1683 gs->id.f = gp.v.critical_value - 1.0 ;
1685 gs->id.f = gp.v.critical_value + 1.0 ;
1688 hsh_insert ( ttpr->group_hash, (void *) gs );
1696 group_calc (const struct dictionary *dict,
1697 const struct ccase *c, struct cmd_t_test *cmd,
1698 const struct casefilter *filter)
1702 const double weight =
1703 dict_get_case_weight (dict, c, &bad_weight_warn);
1705 const union value *gv;
1707 if ( casefilter_variable_missing (filter, c, indep_var))
1710 gv = case_data (c, indep_var);
1712 for(i=0; i< cmd->n_variables ; ++i)
1714 struct variable *var = cmd->v_variables[i];
1715 const union value *val = case_data (c, var);
1716 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1717 struct group_statistics *gs;
1719 gs = hsh_find(grp_hash, (void *) gv);
1721 /* If the independent variable doesn't match either of the values
1722 for this case then move on to the next case */
1726 if ( ! casefilter_variable_missing (filter, c, var) )
1729 gs->sum += weight * val->f;
1730 gs->ssq += weight * pow2(val->f);
1739 group_postcalc ( struct cmd_t_test *cmd )
1743 for (i = 0; i < cmd->n_variables ; ++i)
1745 struct variable *var = cmd->v_variables[i];
1746 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1747 struct hsh_iterator g;
1748 struct group_statistics *gs;
1751 for (gs = hsh_first (grp_hash,&g);
1753 gs = hsh_next(grp_hash,&g))
1755 gs->mean = gs->sum / gs->n;
1757 gs->s_std_dev= sqrt(
1758 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1763 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1766 gs->se_mean = gs->std_dev / sqrt(gs->n);
1776 calculate(const struct ccase *first, const struct casefile *cf,
1777 void *cmd_, const struct dataset *ds)
1779 const struct dictionary *dict = dataset_dict (ds);
1780 struct ssbox stat_summary_box;
1781 struct trbox test_results_box;
1783 struct casereader *r;
1786 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1788 struct casefilter *filter = casefilter_create (cmd->miss != TTS_INCLUDE,
1791 if ( cmd->miss == TTS_LISTWISE )
1792 casefilter_add_variables (filter,
1793 cmd->v_variables, cmd->n_variables);
1795 output_split_file_values (ds, first);
1796 common_precalc (cmd);
1797 for(r = casefile_get_reader (cf, filter);
1798 casereader_read (r, &c) ;
1801 common_calc (dict, &c, cmd, filter);
1804 casereader_destroy (r);
1805 common_postcalc (cmd);
1810 one_sample_precalc (cmd);
1811 for(r = casefile_get_reader (cf, filter);
1812 casereader_read (r, &c) ;
1815 one_sample_calc (dict, &c, cmd, filter);
1817 casereader_destroy (r);
1818 one_sample_postcalc (cmd);
1821 paired_precalc(cmd);
1822 for(r = casefile_get_reader (cf, filter);
1823 casereader_read (r, &c) ;
1826 paired_calc (dict, &c, cmd, filter);
1828 casereader_destroy (r);
1829 paired_postcalc (cmd);
1835 for(r = casefile_get_reader (cf, filter);
1836 casereader_read (r, &c) ;
1839 group_calc (dict, &c, cmd, filter);
1841 casereader_destroy (r);
1842 group_postcalc(cmd);
1844 levene (dict, cf, indep_var, cmd->n_variables, cmd->v_variables,
1849 casefilter_destroy (filter);
1851 ssbox_create(&stat_summary_box,cmd,mode);
1852 ssbox_populate(&stat_summary_box,cmd);
1853 ssbox_finalize(&stat_summary_box);
1855 if ( mode == T_PAIRED)
1858 trbox_create(&test_results_box,cmd,mode);
1859 trbox_populate(&test_results_box,cmd);
1860 trbox_finalize(&test_results_box);
1865 short which_group(const struct group_statistics *g,
1866 const struct group_properties *p);
1868 /* Return -1 if the id of a is less than b; +1 if greater than and
1871 compare_group_binary(const struct group_statistics *a,
1872 const struct group_statistics *b,
1873 const struct group_properties *p)
1878 if ( p->criterion == CMP_LE )
1880 /* less-than-or-equal comparision is not meaningfull for
1881 alpha variables, so we shouldn't ever arrive here */
1882 assert(p->indep_width == 0 ) ;
1884 flag_a = ( a->id.f < p->v.critical_value ) ;
1885 flag_b = ( b->id.f < p->v.critical_value ) ;
1889 flag_a = which_group(a, p);
1890 flag_b = which_group(b, p);
1893 if (flag_a < flag_b )
1896 return (flag_a > flag_b);
1899 /* This is a degenerate case of a hash, since it can only return three possible
1900 values. It's really a comparison, being used as a hash function */
1903 hash_group_binary(const struct group_statistics *g,
1904 const struct group_properties *p)
1908 if ( p->criterion == CMP_LE )
1910 /* Not meaningfull to do a less than compare for alpha values ? */
1911 assert(p->indep_width == 0 ) ;
1912 flag = ( g->id.f < p->v.critical_value ) ;
1914 else if ( p->criterion == CMP_EQ)
1916 flag = which_group(g,p);
1924 /* return 0 if G belongs to group 0,
1925 1 if it belongs to group 1,
1926 2 if it belongs to neither group */
1928 which_group(const struct group_statistics *g,
1929 const struct group_properties *p)
1932 if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1935 if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))