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 const 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 const_hsh_table *hash;
308 const struct variable *v;
310 hash = const_hsh_create (n_pairs, compare_vars_by_name, hash_var_by_name,
313 for (i=0; i < n_pairs; ++i)
315 const_hsh_insert (hash, pairs[i].v[0]);
316 const_hsh_insert (hash, pairs[i].v[1]);
319 assert(cmd.n_variables == 0);
320 cmd.n_variables = const_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 = const_hsh_first (hash, &hi);
327 v = const_hsh_next (hash, &hi) )
328 cmd.v_variables[i++]=v;
329 const_hsh_destroy(hash);
332 else if ( !cmd.sbc_variables)
334 msg(SE, _("One or more VARIABLES must be specified."));
339 bad_weight_warn = true;
341 ok = multipass_procedure_with_splits (ds, calculate, &cmd);
347 if ( mode == T_IND_SAMPLES)
350 /* Destroy any group statistics we created */
351 for (v = 0 ; v < cmd.n_variables ; ++v )
353 struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
354 hsh_destroy (grpp->group_hash);
359 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
363 tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
365 int n_group_values=0;
367 lex_match (lexer, '=');
369 indep_var = parse_variable (lexer, dataset_dict (ds));
372 lex_error (lexer, "expecting variable name in GROUPS subcommand");
376 if (var_is_long_string (indep_var))
378 msg (SE, _("Long string variable %s is not valid here."),
379 var_get_name (indep_var));
383 if (!lex_match (lexer, '('))
385 if (var_is_numeric (indep_var))
387 gp.v.g_value[0].f = 1;
388 gp.v.g_value[1].f = 2;
390 gp.criterion = CMP_EQ;
398 msg (SE, _("When applying GROUPS to a string variable, two "
399 "values must be specified."));
404 if (!parse_value (lexer, &gp.v.g_value[0], var_get_type (indep_var)))
407 lex_match (lexer, ',');
408 if (lex_match (lexer, ')'))
410 if (var_is_alpha (indep_var))
412 msg (SE, _("When applying GROUPS to a string variable, two "
413 "values must be specified."));
416 gp.criterion = CMP_LE;
417 gp.v.critical_value = gp.v.g_value[0].f;
423 if (!parse_value (lexer, &gp.v.g_value[1], var_get_type (indep_var)))
427 if (!lex_force_match (lexer, ')'))
430 if ( n_group_values == 2 )
431 gp.criterion = CMP_EQ ;
433 gp.criterion = CMP_LE ;
441 tts_custom_pairs (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
443 const struct variable **vars;
445 size_t n_pairs_local;
447 size_t n_before_WITH;
448 size_t n_after_WITH = SIZE_MAX;
449 int paired ; /* Was the PAIRED keyword given ? */
451 lex_match (lexer, '=');
454 if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
455 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
463 if (lex_match (lexer, T_WITH))
465 n_before_WITH = n_vars;
466 if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
467 PV_DUPLICATE | PV_APPEND
468 | PV_NUMERIC | PV_NO_SCRATCH))
473 n_after_WITH = n_vars - n_before_WITH;
476 paired = (lex_match (lexer, '(') && lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
478 /* Determine the number of pairs needed */
481 if (n_before_WITH != n_after_WITH)
484 msg (SE, _("PAIRED was specified but the number of variables "
485 "preceding WITH (%d) did not match the number "
487 (int) n_before_WITH, (int) n_after_WITH );
490 n_pairs_local = n_before_WITH;
492 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
494 n_pairs_local = n_before_WITH * n_after_WITH ;
496 else /* Neither WITH nor PAIRED keyword given */
501 msg (SE, _("At least two variables must be specified "
506 /* how many ways can you pick 2 from n_vars ? */
507 n_pairs_local = n_vars * (n_vars - 1) / 2;
511 /* Allocate storage for the pairs */
512 pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
514 /* Populate the pairs with the appropriate variables */
519 assert(n_pairs_local == n_vars / 2);
520 for (i = 0; i < n_pairs_local; ++i)
522 pairs[i].v[n_pairs] = vars[i];
523 pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
526 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
531 for(i=0 ; i < n_before_WITH ; ++i )
533 for(j=0 ; j < n_after_WITH ; ++j)
535 pairs[p].v[0] = vars[i];
536 pairs[p].v[1] = vars[j+n_before_WITH];
541 else /* Neither WITH nor PAIRED given */
546 for(i=0 ; i < n_vars ; ++i )
548 for(j=i+1 ; j < n_vars ; ++j)
550 pairs[p].v[0] = vars[i];
551 pairs[p].v[1] = vars[j];
557 n_pairs+=n_pairs_local;
563 /* Parses the current token (numeric or string, depending on type)
564 value v and returns success. */
566 parse_value (struct lexer *lexer, union value * v, enum var_type type)
568 if (type == VAR_NUMERIC)
570 if (!lex_force_num (lexer))
572 v->f = lex_tokval (lexer);
576 if (!lex_force_string (lexer))
578 strncpy (v->s, ds_cstr (lex_tokstr (lexer)), ds_length (lex_tokstr (lexer)));
587 /* Implementation of the SSBOX object */
589 void ssbox_base_init(struct ssbox *this, int cols,int rows);
591 void ssbox_base_finalize(struct ssbox *ssb);
593 void ssbox_one_sample_init(struct ssbox *this,
594 struct cmd_t_test *cmd );
596 void ssbox_independent_samples_init(struct ssbox *this,
597 struct cmd_t_test *cmd);
599 void ssbox_paired_init(struct ssbox *this,
600 struct cmd_t_test *cmd);
603 /* Factory to create an ssbox */
605 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
610 ssbox_one_sample_init(ssb,cmd);
613 ssbox_independent_samples_init(ssb,cmd);
616 ssbox_paired_init(ssb,cmd);
625 /* Despatcher for the populate method */
627 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
629 ssb->populate(ssb,cmd);
633 /* Despatcher for finalize */
635 ssbox_finalize(struct ssbox *ssb)
641 /* Submit the box and clear up */
643 ssbox_base_finalize(struct ssbox *ssb)
650 /* Initialize a ssbox struct */
652 ssbox_base_init(struct ssbox *this, int cols,int rows)
654 this->finalize = ssbox_base_finalize;
655 this->t = tab_create (cols, rows, 0);
657 tab_columns (this->t, SOM_COL_DOWN, 1);
658 tab_headers (this->t,0,0,1,0);
659 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
660 tab_hline(this->t, TAL_2,0,cols-1,1);
661 tab_dim (this->t, tab_natural_dimensions);
664 void ssbox_one_sample_populate(struct ssbox *ssb,
665 struct cmd_t_test *cmd);
667 /* Initialize the one_sample ssbox */
669 ssbox_one_sample_init(struct ssbox *this,
670 struct cmd_t_test *cmd )
673 const int vsize=cmd->n_variables+1;
675 this->populate = ssbox_one_sample_populate;
677 ssbox_base_init(this, hsize,vsize);
678 tab_title (this->t, _("One-Sample Statistics"));
679 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
680 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
681 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
682 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
683 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
686 void ssbox_independent_samples_populate(struct ssbox *ssb,
687 struct cmd_t_test *cmd);
689 /* Initialize the independent samples ssbox */
691 ssbox_independent_samples_init(struct ssbox *this,
692 struct cmd_t_test *cmd)
695 int vsize = cmd->n_variables*2 +1;
697 this->populate = ssbox_independent_samples_populate;
699 ssbox_base_init(this, hsize,vsize);
700 tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
701 tab_title (this->t, _("Group Statistics"));
702 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, var_get_name (indep_var));
703 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
704 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
705 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
706 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
710 /* Populate the ssbox for independent samples */
712 ssbox_independent_samples_populate(struct ssbox *ssb,
713 struct cmd_t_test *cmd)
717 const char *val_lab0;
718 const char *val_lab1;
719 double indep_value[2];
721 char prefix[2][3]={"",""};
723 if ( var_is_numeric (indep_var) )
725 val_lab0 = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
726 val_lab1 = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
730 val_lab0 = gp.v.g_value[0].s;
731 val_lab1 = gp.v.g_value[1].s;
734 if (gp.criterion == CMP_LE )
736 strcpy(prefix[0],"< ");
737 strcpy(prefix[1],">=");
738 indep_value[0] = gp.v.critical_value;
739 indep_value[1] = gp.v.critical_value;
743 indep_value[0] = gp.v.g_value[0].f;
744 indep_value[1] = gp.v.g_value[1].f;
749 for (i=0; i < cmd->n_variables; ++i)
751 const struct variable *var = cmd->v_variables[i];
752 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
755 tab_text (ssb->t, 0, i*2+1, TAB_LEFT,
756 var_get_name (cmd->v_variables[i]));
759 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
760 "%s%s", prefix[0], val_lab0);
762 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
763 "%s%g", prefix[0], indep_value[0]);
767 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
768 "%s%s", prefix[1], val_lab1);
770 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
771 "%s%g", prefix[1], indep_value[1]);
774 /* Fill in the group statistics */
775 for ( count = 0 ; count < 2 ; ++count )
777 union value search_val;
779 struct group_statistics *gs;
781 if ( gp.criterion == CMP_LE )
785 /* less than ( < ) case */
786 search_val.f = gp.v.critical_value - 1.0;
791 search_val.f = gp.v.critical_value + 1.0;
796 search_val = gp.v.g_value[count];
799 gs = hsh_find(grp_hash, (void *) &search_val);
802 tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 10, 0);
803 tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
804 tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
805 tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
811 void ssbox_paired_populate(struct ssbox *ssb,
812 struct cmd_t_test *cmd);
814 /* Initialize the paired values ssbox */
816 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
820 int vsize = n_pairs*2+1;
822 this->populate = ssbox_paired_populate;
824 ssbox_base_init(this, hsize,vsize);
825 tab_title (this->t, _("Paired Sample Statistics"));
826 tab_vline(this->t,TAL_GAP,1,0,vsize-1);
827 tab_vline(this->t,TAL_2,2,0,vsize-1);
828 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
829 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
830 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
831 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
835 /* Populate the ssbox for paired values */
837 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
843 for (i=0; i < n_pairs; ++i)
847 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
849 for (j=0 ; j < 2 ; ++j)
851 struct group_statistics *gs;
853 gs = &group_proc_get (pairs[i].v[j])->ugs;
857 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT,
858 var_get_name (pairs[i].v[j]));
861 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
862 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 10, 0);
863 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
864 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
870 /* Populate the one sample ssbox */
872 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
878 for (i=0; i < cmd->n_variables; ++i)
880 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
882 tab_text (ssb->t, 0, i+1, TAB_LEFT, var_get_name (cmd->v_variables[i]));
883 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 10, 0);
884 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
885 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
886 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
893 /* Implementation of the Test Results box struct */
895 void trbox_base_init(struct trbox *self,size_t n_vars, int cols);
896 void trbox_base_finalize(struct trbox *trb);
898 void trbox_independent_samples_init(struct trbox *trb,
899 struct cmd_t_test *cmd );
901 void trbox_independent_samples_populate(struct trbox *trb,
902 struct cmd_t_test *cmd);
904 void trbox_one_sample_init(struct trbox *self,
905 struct cmd_t_test *cmd );
907 void trbox_one_sample_populate(struct trbox *trb,
908 struct cmd_t_test *cmd);
910 void trbox_paired_init(struct trbox *self,
911 struct cmd_t_test *cmd );
913 void trbox_paired_populate(struct trbox *trb,
914 struct cmd_t_test *cmd);
918 /* Create a trbox according to mode*/
920 trbox_create(struct trbox *trb,
921 struct cmd_t_test *cmd, int mode)
926 trbox_one_sample_init(trb,cmd);
929 trbox_independent_samples_init(trb,cmd);
932 trbox_paired_init(trb,cmd);
939 /* Populate a trbox according to cmd */
941 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
943 trb->populate(trb,cmd);
946 /* Submit and destroy a trbox */
948 trbox_finalize(struct trbox *trb)
953 /* Initialize the independent samples trbox */
955 trbox_independent_samples_init(struct trbox *self,
956 struct cmd_t_test *cmd UNUSED)
959 const int vsize=cmd->n_variables*2+3;
962 self->populate = trbox_independent_samples_populate;
964 trbox_base_init(self,cmd->n_variables*2,hsize);
965 tab_title(self->t,_("Independent Samples Test"));
966 tab_hline(self->t,TAL_1,2,hsize-1,1);
967 tab_vline(self->t,TAL_2,2,0,vsize-1);
968 tab_vline(self->t,TAL_1,4,0,vsize-1);
969 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
970 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
971 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
972 tab_joint_text(self->t, 2, 0, 3, 0,
973 TAB_CENTER,_("Levene's Test for Equality of Variances"));
974 tab_joint_text(self->t, 4,0,hsize-1,0,
975 TAB_CENTER,_("t-test for Equality of Means"));
977 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
978 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
979 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
980 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
981 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
982 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
983 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
984 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
985 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
987 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
988 _("%g%% Confidence Interval of the Difference"),
989 cmd->criteria*100.0);
993 /* Populate the independent samples trbox */
995 trbox_independent_samples_populate(struct trbox *self,
996 struct cmd_t_test *cmd )
1001 for (i=0; i < cmd->n_variables; ++i)
1010 double pooled_variance;
1011 double std_err_diff;
1014 const struct variable *var = cmd->v_variables[i];
1015 struct group_proc *grp_data = group_proc_get (var);
1017 struct hsh_table *grp_hash = grp_data->group_hash;
1019 struct group_statistics *gs0 ;
1020 struct group_statistics *gs1 ;
1022 union value search_val;
1024 if ( gp.criterion == CMP_LE )
1025 search_val.f = gp.v.critical_value - 1.0;
1027 search_val = gp.v.g_value[0];
1029 gs0 = hsh_find(grp_hash, (void *) &search_val);
1032 if ( gp.criterion == CMP_LE )
1033 search_val.f = gp.v.critical_value + 1.0;
1035 search_val = gp.v.g_value[1];
1037 gs1 = hsh_find(grp_hash, (void *) &search_val);
1041 tab_text (self->t, 0, i*2+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1043 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1046 tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1048 /* Now work out the significance of the Levene test */
1049 df1 = 1; df2 = grp_data->ugs.n - 2;
1050 q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1052 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1054 df = gs0->n + gs1->n - 2.0 ;
1055 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 10, 0);
1057 pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1059 (gs1->n )*pow2(gs1->s_std_dev)
1062 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1063 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
1065 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1067 p = gsl_cdf_tdist_P(t, df);
1068 q = gsl_cdf_tdist_Q(t, df);
1070 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1072 mean_diff = gs0->mean - gs1->mean;
1073 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1076 std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1077 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1080 /* Now work out the confidence interval */
1081 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1083 t = gsl_cdf_tdist_Qinv(q,df);
1084 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1085 mean_diff - t * std_err_diff, 8, 3);
1087 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1088 mean_diff + t * std_err_diff, 8, 3);
1093 /* Now for the \sigma_1 != \sigma_2 case */
1094 tab_text (self->t, 1, i*2+3+1,
1095 TAB_LEFT, _("Equal variances not assumed"));
1098 se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1099 (pow2(gs1->s_std_dev)/(gs1->n -1) );
1101 t = mean_diff / sqrt(se2) ;
1102 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1105 (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 ))
1109 (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1113 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1115 p = gsl_cdf_tdist_P(t, df);
1116 q = gsl_cdf_tdist_Q(t, df);
1118 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1120 /* Now work out the confidence interval */
1121 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1123 t = gsl_cdf_tdist_Qinv(q, df);
1125 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1128 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1131 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1132 mean_diff - t * std_err_diff, 8, 3);
1134 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1135 mean_diff + t * std_err_diff, 8, 3);
1141 /* Initialize the paired samples trbox */
1143 trbox_paired_init(struct trbox *self,
1144 struct cmd_t_test *cmd UNUSED)
1148 const int vsize=n_pairs+3;
1150 self->populate = trbox_paired_populate;
1152 trbox_base_init(self,n_pairs,hsize);
1153 tab_title (self->t, _("Paired Samples Test"));
1154 tab_hline(self->t,TAL_1,2,6,1);
1155 tab_vline(self->t,TAL_2,2,0,vsize - 1);
1156 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1157 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1158 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1159 tab_hline(self->t,TAL_1,5,6, 2);
1160 tab_vline(self->t,TAL_GAP,6,0,1);
1162 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1163 _("%g%% Confidence Interval of the Difference"),
1164 cmd->criteria*100.0);
1166 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1167 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1168 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1169 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1170 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1171 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1172 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1173 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1176 /* Populate the paired samples trbox */
1178 trbox_paired_populate(struct trbox *trb,
1179 struct cmd_t_test *cmd UNUSED)
1183 for (i=0; i < n_pairs; ++i)
1188 double n = pairs[i].n;
1192 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1194 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1195 var_get_name (pairs[i].v[0]),
1196 var_get_name (pairs[i].v[1]));
1198 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1200 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1203 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1204 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1206 /* Now work out the confidence interval */
1207 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1209 t = gsl_cdf_tdist_Qinv(q, df);
1211 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1212 pairs[i].mean_diff - t * se_mean , 8, 4);
1214 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1215 pairs[i].mean_diff + t * se_mean , 8, 4);
1217 t = (pairs[i].mean[0] - pairs[i].mean[1])
1219 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1220 2 * pairs[i].correlation *
1221 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1225 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1227 /* Degrees of freedom */
1228 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 10, 0 );
1230 p = gsl_cdf_tdist_P(t,df);
1231 q = gsl_cdf_tdist_P(t,df);
1233 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1238 /* Initialize the one sample trbox */
1240 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1243 const int vsize=cmd->n_variables+3;
1245 self->populate = trbox_one_sample_populate;
1247 trbox_base_init(self, cmd->n_variables,hsize);
1248 tab_title (self->t, _("One-Sample Test"));
1249 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1250 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1252 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1253 _("Test Value = %f"), cmd->n_testval[0]);
1255 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1258 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1259 _("%g%% Confidence Interval of the Difference"),
1260 cmd->criteria*100.0);
1262 tab_vline(self->t,TAL_GAP,6,1,1);
1263 tab_hline(self->t,TAL_1,5,6,2);
1264 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1265 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1266 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1267 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1268 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1269 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1274 /* Populate the one sample trbox */
1276 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1282 for (i=0; i < cmd->n_variables; ++i)
1287 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1290 tab_text (trb->t, 0, i+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1292 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1294 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1296 /* degrees of freedom */
1299 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1301 p = gsl_cdf_tdist_P(t, df);
1302 q = gsl_cdf_tdist_Q(t, df);
1304 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1306 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1308 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1311 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1312 t = gsl_cdf_tdist_Qinv(q, df);
1314 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1315 gs->mean_diff - t * gs->se_mean, 8,4);
1317 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1318 gs->mean_diff + t * gs->se_mean, 8,4);
1322 /* Base initializer for the generalized trbox */
1324 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1326 const size_t rows = 3 + data_rows;
1328 self->finalize = trbox_base_finalize;
1329 self->t = tab_create (cols, rows, 0);
1330 tab_headers (self->t,0,0,3,0);
1331 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1332 tab_hline(self->t, TAL_2,0,cols-1,3);
1333 tab_dim (self->t, tab_natural_dimensions);
1337 /* Base finalizer for the trbox */
1339 trbox_base_finalize(struct trbox *trb)
1345 /* Create , populate and submit the Paired Samples Correlation box */
1349 const int rows=1+n_pairs;
1353 struct tab_table *table;
1355 table = tab_create (cols,rows,0);
1357 tab_columns (table, SOM_COL_DOWN, 1);
1358 tab_headers (table,0,0,1,0);
1359 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1360 tab_hline(table, TAL_2, 0, cols - 1, 1);
1361 tab_vline(table, TAL_2, 2, 0, rows - 1);
1362 tab_dim(table, tab_natural_dimensions);
1363 tab_title(table, _("Paired Samples Correlations"));
1365 /* column headings */
1366 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1367 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1368 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1370 for (i=0; i < n_pairs; ++i)
1374 double df = pairs[i].n -2;
1376 double correlation_t =
1377 pairs[i].correlation * sqrt(df) /
1378 sqrt(1 - pow2(pairs[i].correlation));
1382 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1385 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1387 var_get_name (pairs[i].v[0]),
1388 var_get_name (pairs[i].v[1]));
1392 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1393 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1395 p = gsl_cdf_tdist_P(correlation_t, df);
1396 q = gsl_cdf_tdist_Q(correlation_t, df);
1398 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1407 /* Calculation Implementation */
1409 /* Per case calculations common to all variants of the T test */
1411 common_calc (const struct dictionary *dict,
1412 const struct ccase *c,
1414 const struct casefilter *filter)
1417 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1419 double weight = dict_get_case_weight (dict, c, &bad_weight_warn);
1422 /* Listwise has to be implicit if the independent variable is missing ?? */
1423 if ( cmd->sbc_groups )
1425 if ( casefilter_variable_missing (filter, c, indep_var) )
1429 for(i = 0; i < cmd->n_variables ; ++i)
1431 const struct variable *v = cmd->v_variables[i];
1433 if (! casefilter_variable_missing (filter, c, v) )
1435 struct group_statistics *gs;
1436 const union value *val = case_data (c, v);
1437 gs = &group_proc_get (cmd->v_variables[i])->ugs;
1440 gs->sum += weight * val->f;
1441 gs->ssq += weight * val->f * val->f;
1447 /* Pre calculations common to all variants of the T test */
1449 common_precalc ( struct cmd_t_test *cmd )
1453 for(i=0; i< cmd->n_variables ; ++i)
1455 struct group_statistics *gs;
1456 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1465 /* Post calculations common to all variants of the T test */
1467 common_postcalc (struct cmd_t_test *cmd)
1471 for(i=0; i< cmd->n_variables ; ++i)
1473 struct group_statistics *gs;
1474 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1476 gs->mean=gs->sum / gs->n;
1477 gs->s_std_dev= sqrt(
1478 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1483 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1486 gs->se_mean = gs->std_dev / sqrt(gs->n);
1487 gs->mean_diff= gs->sum_diff / gs->n;
1491 /* Per case calculations for one sample t test */
1493 one_sample_calc (const struct dictionary *dict,
1494 const struct ccase *c, void *cmd_,
1495 const struct casefilter *filter)
1499 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1501 double weight = dict_get_case_weight (dict, c, &bad_weight_warn);
1504 for(i=0; i< cmd->n_variables ; ++i)
1506 struct group_statistics *gs;
1507 const struct variable *v = cmd->v_variables[i];
1508 const union value *val = case_data (c, v);
1510 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1512 if ( ! casefilter_variable_missing (filter, c, v))
1513 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1519 /* Pre calculations for one sample t test */
1521 one_sample_precalc ( struct cmd_t_test *cmd )
1525 for(i=0; i< cmd->n_variables ; ++i)
1527 struct group_statistics *gs;
1528 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1534 /* Post calculations for one sample t test */
1536 one_sample_postcalc (struct cmd_t_test *cmd)
1540 for(i=0; i< cmd->n_variables ; ++i)
1542 struct group_statistics *gs;
1543 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1545 gs->mean_diff = gs->sum_diff / gs->n ;
1552 paired_precalc (struct cmd_t_test *cmd UNUSED)
1556 for(i=0; i < n_pairs ; ++i )
1559 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1560 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1561 pairs[i].sum_of_prod = 0;
1562 pairs[i].correlation = 0;
1563 pairs[i].sum_of_diffs = 0;
1564 pairs[i].ssq_diffs = 0;
1571 paired_calc (const struct dictionary *dict, const struct ccase *c,
1572 struct cmd_t_test *cmd UNUSED, const struct casefilter *filter)
1576 double weight = dict_get_case_weight (dict, c, &bad_weight_warn);
1578 for(i=0; i < n_pairs ; ++i )
1580 const struct variable *v0 = pairs[i].v[0];
1581 const struct variable *v1 = pairs[i].v[1];
1583 const union value *val0 = case_data (c, v0);
1584 const union value *val1 = case_data (c, v1);
1586 if ( ! casefilter_variable_missing (filter, c, v0) &&
1587 ! casefilter_variable_missing (filter, c, v1) )
1589 pairs[i].n += weight;
1590 pairs[i].sum[0] += weight * val0->f;
1591 pairs[i].sum[1] += weight * val1->f;
1593 pairs[i].ssq[0] += weight * pow2(val0->f);
1594 pairs[i].ssq[1] += weight * pow2(val1->f);
1596 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1598 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1599 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1607 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1611 for(i=0; i < n_pairs ; ++i )
1614 const double n = pairs[i].n;
1616 for (j=0; j < 2 ; ++j)
1618 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1619 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1620 pow2(pairs[i].mean[j]))
1623 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1624 pow2(pairs[i].mean[j]))
1628 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1629 pairs[i].mean[0] * pairs[i].mean[1] ;
1630 /* correlation now actually contains the covariance */
1632 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1633 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1635 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1637 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1638 ( pairs[i].ssq_diffs / n )
1640 pow2(pairs[i].mean_diff )
1646 group_precalc (struct cmd_t_test *cmd )
1651 for(i=0; i< cmd->n_variables ; ++i)
1653 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1655 /* There's always 2 groups for a T - TEST */
1658 gp.indep_width = var_get_width (indep_var);
1660 ttpr->group_hash = hsh_create(2,
1661 (hsh_compare_func *) compare_group_binary,
1662 (hsh_hash_func *) hash_group_binary,
1663 (hsh_free_func *) free_group,
1666 for (j=0 ; j < 2 ; ++j)
1669 struct group_statistics *gs = xmalloc (sizeof *gs);
1675 if ( gp.criterion == CMP_EQ )
1677 gs->id = gp.v.g_value[j];
1682 gs->id.f = gp.v.critical_value - 1.0 ;
1684 gs->id.f = gp.v.critical_value + 1.0 ;
1687 hsh_insert ( ttpr->group_hash, (void *) gs );
1695 group_calc (const struct dictionary *dict,
1696 const struct ccase *c, struct cmd_t_test *cmd,
1697 const struct casefilter *filter)
1701 const double weight =
1702 dict_get_case_weight (dict, c, &bad_weight_warn);
1704 const union value *gv;
1706 if ( casefilter_variable_missing (filter, c, indep_var))
1709 gv = case_data (c, indep_var);
1711 for(i=0; i< cmd->n_variables ; ++i)
1713 const struct variable *var = cmd->v_variables[i];
1714 const union value *val = case_data (c, var);
1715 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1716 struct group_statistics *gs;
1718 gs = hsh_find(grp_hash, (void *) gv);
1720 /* If the independent variable doesn't match either of the values
1721 for this case then move on to the next case */
1725 if ( ! casefilter_variable_missing (filter, c, var) )
1728 gs->sum += weight * val->f;
1729 gs->ssq += weight * pow2(val->f);
1738 group_postcalc ( struct cmd_t_test *cmd )
1742 for (i = 0; i < cmd->n_variables ; ++i)
1744 const struct variable *var = cmd->v_variables[i];
1745 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1746 struct hsh_iterator g;
1747 struct group_statistics *gs;
1750 for (gs = hsh_first (grp_hash,&g);
1752 gs = hsh_next(grp_hash,&g))
1754 gs->mean = gs->sum / gs->n;
1756 gs->s_std_dev= sqrt(
1757 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1762 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1765 gs->se_mean = gs->std_dev / sqrt(gs->n);
1775 calculate(const struct ccase *first, const struct casefile *cf,
1776 void *cmd_, const struct dataset *ds)
1778 const struct dictionary *dict = dataset_dict (ds);
1779 struct ssbox stat_summary_box;
1780 struct trbox test_results_box;
1782 struct casereader *r;
1785 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1787 struct casefilter *filter = casefilter_create ((cmd->miss != TTS_INCLUDE
1788 ? MV_ANY : MV_SYSTEM),
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))