1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <gsl/gsl_cdf.h>
24 #include <data/case.h>
25 #include <data/casegrouper.h>
26 #include <data/casereader.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/dictionary/split-file.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/alloc.h>
35 #include <libpspp/assertion.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/hash.h>
38 #include <libpspp/magic.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/str.h>
42 #include <libpspp/taint.h>
43 #include <math/group-proc.h>
44 #include <math/levene.h>
45 #include <output/manager.h>
46 #include <output/table.h>
51 #define _(msgid) gettext (msgid)
59 +variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
61 missing=miss:!analysis/listwise,
62 incl:include/!exclude;
63 +format=fmt:!labels/nolabels;
64 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
70 /* Variable for the GROUPS subcommand, if given. */
71 static struct variable *indep_var;
79 struct group_properties
81 /* The comparison criterion */
82 enum comparison criterion;
84 /* The width of the independent variable */
88 /* The value of the independent variable at which groups are determined to
89 belong to one group or the other */
90 double critical_value;
93 /* The values of the independent variable for each group */
94 union value g_value[2];
100 static struct group_properties gp ;
104 /* PAIRS: Number of pairs to be compared ; each pair. */
105 static int n_pairs = 0 ;
108 /* The variables comprising the pair */
109 const struct variable *v[2];
111 /* The number of valid variable pairs */
114 /* The sum of the members */
117 /* sum of squares of the members */
120 /* Std deviation of the members */
124 /* Sample Std deviation of the members */
127 /* The means of the members */
130 /* The correlation coefficient between the variables */
133 /* The sum of the differences */
136 /* The sum of the products */
139 /* The mean of the differences */
142 /* The sum of the squares of the differences */
145 /* The std deviation of the differences */
149 static struct pair *pairs=0;
151 static int parse_value (struct lexer *lexer, union value * v, enum var_type);
153 /* Structures and Functions for the Statistics Summary Box */
155 typedef void populate_ssbox_func(struct ssbox *ssb,
156 struct cmd_t_test *cmd);
157 typedef void finalize_ssbox_func(struct ssbox *ssb);
163 populate_ssbox_func *populate;
164 finalize_ssbox_func *finalize;
169 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
171 /* Populate a ssbox according to cmd */
172 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
174 /* Submit and destroy a ssbox */
175 void ssbox_finalize(struct ssbox *ssb);
177 /* A function to create, populate and submit the Paired Samples Correlation
182 /* Structures and Functions for the Test Results Box */
185 typedef void populate_trbox_func(struct trbox *trb,
186 struct cmd_t_test *cmd);
187 typedef void finalize_trbox_func(struct trbox *trb);
191 populate_trbox_func *populate;
192 finalize_trbox_func *finalize;
196 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
198 /* Populate a ssbox according to cmd */
199 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
201 /* Submit and destroy a ssbox */
202 void trbox_finalize(struct trbox *trb);
204 /* Which mode was T-TEST invoked */
212 static int common_calc (const struct dictionary *dict,
213 const struct ccase *, void *,
215 static void common_precalc (struct cmd_t_test *);
216 static void common_postcalc (struct cmd_t_test *);
218 static int one_sample_calc (const struct dictionary *dict, const struct ccase *, void *, enum mv_class);
219 static void one_sample_precalc (struct cmd_t_test *);
220 static void one_sample_postcalc (struct cmd_t_test *);
222 static int paired_calc (const struct dictionary *dict, const struct ccase *,
223 struct cmd_t_test*, enum mv_class);
224 static void paired_precalc (struct cmd_t_test *);
225 static void paired_postcalc (struct cmd_t_test *);
227 static void group_precalc (struct cmd_t_test *);
228 static int group_calc (const struct dictionary *dict, const struct ccase *,
229 struct cmd_t_test *, enum mv_class);
230 static void group_postcalc (struct cmd_t_test *);
233 static void calculate(struct cmd_t_test *,
235 const struct dataset *);
239 static struct cmd_t_test cmd;
241 static bool bad_weight_warn = false;
244 static int compare_group_binary(const struct group_statistics *a,
245 const struct group_statistics *b,
246 const struct group_properties *p);
249 static unsigned hash_group_binary(const struct group_statistics *g,
250 const struct group_properties *p);
255 cmd_t_test (struct lexer *lexer, struct dataset *ds)
257 struct casegrouper *grouper;
258 struct casereader *group;
261 if ( !parse_t_test (lexer, ds, &cmd, NULL) )
264 if (! cmd.sbc_criteria)
269 if (cmd.sbc_testval) ++m;
270 if (cmd.sbc_groups) ++m;
271 if (cmd.sbc_pairs) ++m;
276 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
285 else if (cmd.sbc_groups)
290 if ( mode == T_PAIRED)
292 if (cmd.sbc_variables)
294 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
300 /* Iterate through the pairs and put each variable that is a
301 member of a pair into cmd.v_variables */
304 struct hsh_iterator hi;
305 struct const_hsh_table *hash;
306 const struct variable *v;
308 hash = const_hsh_create (n_pairs, compare_vars_by_name, hash_var_by_name,
311 for (i=0; i < n_pairs; ++i)
313 const_hsh_insert (hash, pairs[i].v[0]);
314 const_hsh_insert (hash, pairs[i].v[1]);
317 assert(cmd.n_variables == 0);
318 cmd.n_variables = const_hsh_count (hash);
320 cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
321 sizeof *cmd.v_variables);
322 /* Iterate through the hash */
323 for (i=0,v = const_hsh_first (hash, &hi);
325 v = const_hsh_next (hash, &hi) )
326 cmd.v_variables[i++]=v;
327 const_hsh_destroy(hash);
330 else if ( !cmd.sbc_variables)
332 msg(SE, _("One or more VARIABLES must be specified."));
337 bad_weight_warn = true;
340 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
341 while (casegrouper_get_next_group (grouper, &group))
342 calculate (&cmd, group, ds);
343 ok = casegrouper_destroy (grouper);
344 ok = proc_commit (ds) && ok;
350 if ( mode == T_IND_SAMPLES)
353 /* Destroy any group statistics we created */
354 for (v = 0 ; v < cmd.n_variables ; ++v )
356 struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
357 hsh_destroy (grpp->group_hash);
362 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
366 tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
368 int n_group_values=0;
370 lex_match (lexer, '=');
372 indep_var = parse_variable (lexer, dataset_dict (ds));
375 lex_error (lexer, "expecting variable name in GROUPS subcommand");
379 if (var_is_long_string (indep_var))
381 msg (SE, _("Long string variable %s is not valid here."),
382 var_get_name (indep_var));
386 if (!lex_match (lexer, '('))
388 if (var_is_numeric (indep_var))
390 gp.v.g_value[0].f = 1;
391 gp.v.g_value[1].f = 2;
393 gp.criterion = CMP_EQ;
401 msg (SE, _("When applying GROUPS to a string variable, two "
402 "values must be specified."));
407 if (!parse_value (lexer, &gp.v.g_value[0], var_get_type (indep_var)))
410 lex_match (lexer, ',');
411 if (lex_match (lexer, ')'))
413 if (var_is_alpha (indep_var))
415 msg (SE, _("When applying GROUPS to a string variable, two "
416 "values must be specified."));
419 gp.criterion = CMP_LE;
420 gp.v.critical_value = gp.v.g_value[0].f;
426 if (!parse_value (lexer, &gp.v.g_value[1], var_get_type (indep_var)))
430 if (!lex_force_match (lexer, ')'))
433 if ( n_group_values == 2 )
434 gp.criterion = CMP_EQ ;
436 gp.criterion = CMP_LE ;
444 tts_custom_pairs (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
446 const struct variable **vars;
448 size_t n_pairs_local;
450 size_t n_before_WITH;
451 size_t n_after_WITH = SIZE_MAX;
452 int paired ; /* Was the PAIRED keyword given ? */
454 lex_match (lexer, '=');
457 if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
458 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
466 if (lex_match (lexer, T_WITH))
468 n_before_WITH = n_vars;
469 if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
470 PV_DUPLICATE | PV_APPEND
471 | PV_NUMERIC | PV_NO_SCRATCH))
476 n_after_WITH = n_vars - n_before_WITH;
479 paired = (lex_match (lexer, '(') && lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
481 /* Determine the number of pairs needed */
484 if (n_before_WITH != n_after_WITH)
487 msg (SE, _("PAIRED was specified but the number of variables "
488 "preceding WITH (%d) did not match the number "
490 (int) n_before_WITH, (int) n_after_WITH );
493 n_pairs_local = n_before_WITH;
495 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
497 n_pairs_local = n_before_WITH * n_after_WITH ;
499 else /* Neither WITH nor PAIRED keyword given */
504 msg (SE, _("At least two variables must be specified "
509 /* how many ways can you pick 2 from n_vars ? */
510 n_pairs_local = n_vars * (n_vars - 1) / 2;
514 /* Allocate storage for the pairs */
515 pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
517 /* Populate the pairs with the appropriate variables */
522 assert(n_pairs_local == n_vars / 2);
523 for (i = 0; i < n_pairs_local; ++i)
525 pairs[i].v[n_pairs] = vars[i];
526 pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
529 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
534 for(i=0 ; i < n_before_WITH ; ++i )
536 for(j=0 ; j < n_after_WITH ; ++j)
538 pairs[p].v[0] = vars[i];
539 pairs[p].v[1] = vars[j+n_before_WITH];
544 else /* Neither WITH nor PAIRED given */
549 for(i=0 ; i < n_vars ; ++i )
551 for(j=i+1 ; j < n_vars ; ++j)
553 pairs[p].v[0] = vars[i];
554 pairs[p].v[1] = vars[j];
560 n_pairs+=n_pairs_local;
566 /* Parses the current token (numeric or string, depending on type)
567 value v and returns success. */
569 parse_value (struct lexer *lexer, union value * v, enum var_type type)
571 if (type == VAR_NUMERIC)
573 if (!lex_force_num (lexer))
575 v->f = lex_tokval (lexer);
579 if (!lex_force_string (lexer))
581 strncpy (v->s, ds_cstr (lex_tokstr (lexer)), ds_length (lex_tokstr (lexer)));
590 /* Implementation of the SSBOX object */
592 void ssbox_base_init(struct ssbox *this, int cols,int rows);
594 void ssbox_base_finalize(struct ssbox *ssb);
596 void ssbox_one_sample_init(struct ssbox *this,
597 struct cmd_t_test *cmd );
599 void ssbox_independent_samples_init(struct ssbox *this,
600 struct cmd_t_test *cmd);
602 void ssbox_paired_init(struct ssbox *this,
603 struct cmd_t_test *cmd);
606 /* Factory to create an ssbox */
608 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
613 ssbox_one_sample_init(ssb,cmd);
616 ssbox_independent_samples_init(ssb,cmd);
619 ssbox_paired_init(ssb,cmd);
628 /* Despatcher for the populate method */
630 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
632 ssb->populate(ssb,cmd);
636 /* Despatcher for finalize */
638 ssbox_finalize(struct ssbox *ssb)
644 /* Submit the box and clear up */
646 ssbox_base_finalize(struct ssbox *ssb)
653 /* Initialize a ssbox struct */
655 ssbox_base_init(struct ssbox *this, int cols,int rows)
657 this->finalize = ssbox_base_finalize;
658 this->t = tab_create (cols, rows, 0);
660 tab_columns (this->t, SOM_COL_DOWN, 1);
661 tab_headers (this->t,0,0,1,0);
662 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
663 tab_hline(this->t, TAL_2,0,cols-1,1);
664 tab_dim (this->t, tab_natural_dimensions);
667 void ssbox_one_sample_populate(struct ssbox *ssb,
668 struct cmd_t_test *cmd);
670 /* Initialize the one_sample ssbox */
672 ssbox_one_sample_init(struct ssbox *this,
673 struct cmd_t_test *cmd )
676 const int vsize=cmd->n_variables+1;
678 this->populate = ssbox_one_sample_populate;
680 ssbox_base_init(this, hsize,vsize);
681 tab_title (this->t, _("One-Sample Statistics"));
682 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
683 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
684 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
685 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
686 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
689 void ssbox_independent_samples_populate(struct ssbox *ssb,
690 struct cmd_t_test *cmd);
692 /* Initialize the independent samples ssbox */
694 ssbox_independent_samples_init(struct ssbox *this,
695 struct cmd_t_test *cmd)
698 int vsize = cmd->n_variables*2 +1;
700 this->populate = ssbox_independent_samples_populate;
702 ssbox_base_init(this, hsize,vsize);
703 tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
704 tab_title (this->t, _("Group Statistics"));
705 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, var_get_name (indep_var));
706 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
707 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
708 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
709 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
713 /* Populate the ssbox for independent samples */
715 ssbox_independent_samples_populate(struct ssbox *ssb,
716 struct cmd_t_test *cmd)
720 const char *val_lab0;
721 const char *val_lab1;
722 double indep_value[2];
724 char prefix[2][3]={"",""};
726 if ( var_is_numeric (indep_var) )
728 val_lab0 = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
729 val_lab1 = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
733 val_lab0 = gp.v.g_value[0].s;
734 val_lab1 = gp.v.g_value[1].s;
737 if (gp.criterion == CMP_LE )
739 strcpy(prefix[0],">=");
740 strcpy(prefix[1],"<");
741 indep_value[0] = gp.v.critical_value;
742 indep_value[1] = gp.v.critical_value;
746 indep_value[0] = gp.v.g_value[0].f;
747 indep_value[1] = gp.v.g_value[1].f;
752 for (i=0; i < cmd->n_variables; ++i)
754 const struct variable *var = cmd->v_variables[i];
755 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
758 tab_text (ssb->t, 0, i*2+1, TAB_LEFT,
759 var_get_name (cmd->v_variables[i]));
762 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
763 "%s%s", prefix[0], val_lab0);
765 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
766 "%s%g", prefix[0], indep_value[0]);
770 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
771 "%s%s", prefix[1], val_lab1);
773 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
774 "%s%g", prefix[1], indep_value[1]);
777 /* Fill in the group statistics */
778 for ( count = 0 ; count < 2 ; ++count )
780 union value search_val;
782 struct group_statistics *gs;
784 if ( gp.criterion == CMP_LE )
789 search_val.f = gp.v.critical_value + 1.0;
793 /* less than ( < ) case */
794 search_val.f = gp.v.critical_value - 1.0;
799 search_val = gp.v.g_value[count];
802 gs = hsh_find(grp_hash, (void *) &search_val);
805 tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 10, 0);
806 tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
807 tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
808 tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
814 void ssbox_paired_populate(struct ssbox *ssb,
815 struct cmd_t_test *cmd);
817 /* Initialize the paired values ssbox */
819 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
823 int vsize = n_pairs*2+1;
825 this->populate = ssbox_paired_populate;
827 ssbox_base_init(this, hsize,vsize);
828 tab_title (this->t, _("Paired Sample Statistics"));
829 tab_vline(this->t,TAL_GAP,1,0,vsize-1);
830 tab_vline(this->t,TAL_2,2,0,vsize-1);
831 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
832 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
833 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
834 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
838 /* Populate the ssbox for paired values */
840 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
846 for (i=0; i < n_pairs; ++i)
850 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
852 for (j=0 ; j < 2 ; ++j)
854 struct group_statistics *gs;
856 gs = &group_proc_get (pairs[i].v[j])->ugs;
860 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT,
861 var_get_name (pairs[i].v[j]));
864 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
865 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 10, 0);
866 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
867 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
873 /* Populate the one sample ssbox */
875 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
881 for (i=0; i < cmd->n_variables; ++i)
883 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
885 tab_text (ssb->t, 0, i+1, TAB_LEFT, var_get_name (cmd->v_variables[i]));
886 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 10, 0);
887 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
888 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
889 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
896 /* Implementation of the Test Results box struct */
898 void trbox_base_init(struct trbox *self,size_t n_vars, int cols);
899 void trbox_base_finalize(struct trbox *trb);
901 void trbox_independent_samples_init(struct trbox *trb,
902 struct cmd_t_test *cmd );
904 void trbox_independent_samples_populate(struct trbox *trb,
905 struct cmd_t_test *cmd);
907 void trbox_one_sample_init(struct trbox *self,
908 struct cmd_t_test *cmd );
910 void trbox_one_sample_populate(struct trbox *trb,
911 struct cmd_t_test *cmd);
913 void trbox_paired_init(struct trbox *self,
914 struct cmd_t_test *cmd );
916 void trbox_paired_populate(struct trbox *trb,
917 struct cmd_t_test *cmd);
921 /* Create a trbox according to mode*/
923 trbox_create(struct trbox *trb,
924 struct cmd_t_test *cmd, int mode)
929 trbox_one_sample_init(trb,cmd);
932 trbox_independent_samples_init(trb,cmd);
935 trbox_paired_init(trb,cmd);
942 /* Populate a trbox according to cmd */
944 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
946 trb->populate(trb,cmd);
949 /* Submit and destroy a trbox */
951 trbox_finalize(struct trbox *trb)
956 /* Initialize the independent samples trbox */
958 trbox_independent_samples_init(struct trbox *self,
959 struct cmd_t_test *cmd UNUSED)
962 const int vsize=cmd->n_variables*2+3;
965 self->populate = trbox_independent_samples_populate;
967 trbox_base_init(self,cmd->n_variables*2,hsize);
968 tab_title(self->t,_("Independent Samples Test"));
969 tab_hline(self->t,TAL_1,2,hsize-1,1);
970 tab_vline(self->t,TAL_2,2,0,vsize-1);
971 tab_vline(self->t,TAL_1,4,0,vsize-1);
972 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
973 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
974 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
975 tab_joint_text(self->t, 2, 0, 3, 0,
976 TAB_CENTER,_("Levene's Test for Equality of Variances"));
977 tab_joint_text(self->t, 4,0,hsize-1,0,
978 TAB_CENTER,_("t-test for Equality of Means"));
980 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
981 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
982 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
983 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
984 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
985 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
986 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
987 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
988 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
990 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
991 _("%g%% Confidence Interval of the Difference"),
992 cmd->criteria*100.0);
996 /* Populate the independent samples trbox */
998 trbox_independent_samples_populate(struct trbox *self,
999 struct cmd_t_test *cmd )
1004 for (i=0; i < cmd->n_variables; ++i)
1013 double pooled_variance;
1014 double std_err_diff;
1017 const struct variable *var = cmd->v_variables[i];
1018 struct group_proc *grp_data = group_proc_get (var);
1020 struct hsh_table *grp_hash = grp_data->group_hash;
1022 struct group_statistics *gs0 ;
1023 struct group_statistics *gs1 ;
1025 union value search_val;
1027 if ( gp.criterion == CMP_LE )
1028 search_val.f = gp.v.critical_value - 1.0;
1030 search_val = gp.v.g_value[0];
1032 gs0 = hsh_find(grp_hash, (void *) &search_val);
1035 if ( gp.criterion == CMP_LE )
1036 search_val.f = gp.v.critical_value + 1.0;
1038 search_val = gp.v.g_value[1];
1040 gs1 = hsh_find(grp_hash, (void *) &search_val);
1044 tab_text (self->t, 0, i*2+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1046 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1049 tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1051 /* Now work out the significance of the Levene test */
1052 df1 = 1; df2 = grp_data->ugs.n - 2;
1053 q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1055 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1057 df = gs0->n + gs1->n - 2.0 ;
1058 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 10, 0);
1060 pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1062 (gs1->n )*pow2(gs1->s_std_dev)
1065 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1066 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
1068 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1070 p = gsl_cdf_tdist_P(t, df);
1071 q = gsl_cdf_tdist_Q(t, df);
1073 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1075 mean_diff = gs0->mean - gs1->mean;
1076 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1079 std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1080 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1083 /* Now work out the confidence interval */
1084 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1086 t = gsl_cdf_tdist_Qinv(q,df);
1087 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1088 mean_diff - t * std_err_diff, 8, 3);
1090 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1091 mean_diff + t * std_err_diff, 8, 3);
1096 /* Now for the \sigma_1 != \sigma_2 case */
1097 tab_text (self->t, 1, i*2+3+1,
1098 TAB_LEFT, _("Equal variances not assumed"));
1101 se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1102 (pow2(gs1->s_std_dev)/(gs1->n -1) );
1104 t = mean_diff / sqrt(se2) ;
1105 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1108 (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 ))
1112 (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1116 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1118 p = gsl_cdf_tdist_P(t, df);
1119 q = gsl_cdf_tdist_Q(t, df);
1121 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1123 /* Now work out the confidence interval */
1124 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1126 t = gsl_cdf_tdist_Qinv(q, df);
1128 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1131 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1134 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1135 mean_diff - t * std_err_diff, 8, 3);
1137 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1138 mean_diff + t * std_err_diff, 8, 3);
1144 /* Initialize the paired samples trbox */
1146 trbox_paired_init(struct trbox *self,
1147 struct cmd_t_test *cmd UNUSED)
1151 const int vsize=n_pairs+3;
1153 self->populate = trbox_paired_populate;
1155 trbox_base_init(self,n_pairs,hsize);
1156 tab_title (self->t, _("Paired Samples Test"));
1157 tab_hline(self->t,TAL_1,2,6,1);
1158 tab_vline(self->t,TAL_2,2,0,vsize - 1);
1159 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1160 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1161 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1162 tab_hline(self->t,TAL_1,5,6, 2);
1163 tab_vline(self->t,TAL_GAP,6,0,1);
1165 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1166 _("%g%% Confidence Interval of the Difference"),
1167 cmd->criteria*100.0);
1169 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1170 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1171 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1172 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1173 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1174 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1175 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1176 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1179 /* Populate the paired samples trbox */
1181 trbox_paired_populate(struct trbox *trb,
1182 struct cmd_t_test *cmd UNUSED)
1186 for (i=0; i < n_pairs; ++i)
1191 double n = pairs[i].n;
1195 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1197 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1198 var_get_name (pairs[i].v[0]),
1199 var_get_name (pairs[i].v[1]));
1201 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1203 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1206 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1207 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1209 /* Now work out the confidence interval */
1210 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1212 t = gsl_cdf_tdist_Qinv(q, df);
1214 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1215 pairs[i].mean_diff - t * se_mean , 8, 4);
1217 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1218 pairs[i].mean_diff + t * se_mean , 8, 4);
1220 t = (pairs[i].mean[0] - pairs[i].mean[1])
1222 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1223 2 * pairs[i].correlation *
1224 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1228 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1230 /* Degrees of freedom */
1231 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 10, 0 );
1233 p = gsl_cdf_tdist_P(t,df);
1234 q = gsl_cdf_tdist_P(t,df);
1236 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1241 /* Initialize the one sample trbox */
1243 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1246 const int vsize=cmd->n_variables+3;
1248 self->populate = trbox_one_sample_populate;
1250 trbox_base_init(self, cmd->n_variables,hsize);
1251 tab_title (self->t, _("One-Sample Test"));
1252 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1253 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1255 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1256 _("Test Value = %f"), cmd->n_testval[0]);
1258 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1261 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1262 _("%g%% Confidence Interval of the Difference"),
1263 cmd->criteria*100.0);
1265 tab_vline(self->t,TAL_GAP,6,1,1);
1266 tab_hline(self->t,TAL_1,5,6,2);
1267 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1268 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1269 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1270 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1271 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1272 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1277 /* Populate the one sample trbox */
1279 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1285 for (i=0; i < cmd->n_variables; ++i)
1290 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1293 tab_text (trb->t, 0, i+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1295 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1297 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1299 /* degrees of freedom */
1302 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1304 p = gsl_cdf_tdist_P(t, df);
1305 q = gsl_cdf_tdist_Q(t, df);
1307 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1309 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1311 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1314 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1315 t = gsl_cdf_tdist_Qinv(q, df);
1317 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1318 gs->mean_diff - t * gs->se_mean, 8,4);
1320 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1321 gs->mean_diff + t * gs->se_mean, 8,4);
1325 /* Base initializer for the generalized trbox */
1327 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1329 const size_t rows = 3 + data_rows;
1331 self->finalize = trbox_base_finalize;
1332 self->t = tab_create (cols, rows, 0);
1333 tab_headers (self->t,0,0,3,0);
1334 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1335 tab_hline(self->t, TAL_2,0,cols-1,3);
1336 tab_dim (self->t, tab_natural_dimensions);
1340 /* Base finalizer for the trbox */
1342 trbox_base_finalize(struct trbox *trb)
1348 /* Create , populate and submit the Paired Samples Correlation box */
1352 const int rows=1+n_pairs;
1356 struct tab_table *table;
1358 table = tab_create (cols,rows,0);
1360 tab_columns (table, SOM_COL_DOWN, 1);
1361 tab_headers (table,0,0,1,0);
1362 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1363 tab_hline(table, TAL_2, 0, cols - 1, 1);
1364 tab_vline(table, TAL_2, 2, 0, rows - 1);
1365 tab_dim(table, tab_natural_dimensions);
1366 tab_title(table, _("Paired Samples Correlations"));
1368 /* column headings */
1369 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1370 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1371 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1373 for (i=0; i < n_pairs; ++i)
1377 double df = pairs[i].n -2;
1379 double correlation_t =
1380 pairs[i].correlation * sqrt(df) /
1381 sqrt(1 - pow2(pairs[i].correlation));
1385 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1388 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1390 var_get_name (pairs[i].v[0]),
1391 var_get_name (pairs[i].v[1]));
1395 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1396 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1398 p = gsl_cdf_tdist_P(correlation_t, df);
1399 q = gsl_cdf_tdist_Q(correlation_t, df);
1401 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1410 /* Calculation Implementation */
1412 /* Per case calculations common to all variants of the T test */
1414 common_calc (const struct dictionary *dict,
1415 const struct ccase *c,
1417 enum mv_class exclude)
1420 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1422 double weight = dict_get_case_weight (dict, c, NULL);
1425 /* Listwise has to be implicit if the independent variable is missing ?? */
1426 if ( cmd->sbc_groups )
1428 if (var_is_value_missing (indep_var, case_data (c, indep_var), exclude))
1432 for(i = 0; i < cmd->n_variables ; ++i)
1434 const struct variable *v = cmd->v_variables[i];
1435 const union value *val = case_data (c, v);
1437 if (!var_is_value_missing (v, val, exclude))
1439 struct group_statistics *gs;
1440 gs = &group_proc_get (v)->ugs;
1443 gs->sum += weight * val->f;
1444 gs->ssq += weight * val->f * val->f;
1450 /* Pre calculations common to all variants of the T test */
1452 common_precalc ( struct cmd_t_test *cmd )
1456 for(i=0; i< cmd->n_variables ; ++i)
1458 struct group_statistics *gs;
1459 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1468 /* Post calculations common to all variants of the T test */
1470 common_postcalc (struct cmd_t_test *cmd)
1474 for(i=0; i< cmd->n_variables ; ++i)
1476 struct group_statistics *gs;
1477 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1479 gs->mean=gs->sum / gs->n;
1480 gs->s_std_dev= sqrt(
1481 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1486 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1489 gs->se_mean = gs->std_dev / sqrt(gs->n);
1490 gs->mean_diff= gs->sum_diff / gs->n;
1494 /* Per case calculations for one sample t test */
1496 one_sample_calc (const struct dictionary *dict,
1497 const struct ccase *c, void *cmd_,
1498 enum mv_class exclude)
1502 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1504 double weight = dict_get_case_weight (dict, c, NULL);
1507 for(i=0; i< cmd->n_variables ; ++i)
1509 struct group_statistics *gs;
1510 const struct variable *v = cmd->v_variables[i];
1511 const union value *val = case_data (c, v);
1513 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1515 if (!var_is_value_missing (v, val, exclude))
1516 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1522 /* Pre calculations for one sample t test */
1524 one_sample_precalc ( struct cmd_t_test *cmd )
1528 for(i=0; i< cmd->n_variables ; ++i)
1530 struct group_statistics *gs;
1531 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1537 /* Post calculations for one sample t test */
1539 one_sample_postcalc (struct cmd_t_test *cmd)
1543 for(i=0; i< cmd->n_variables ; ++i)
1545 struct group_statistics *gs;
1546 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1548 gs->mean_diff = gs->sum_diff / gs->n ;
1555 paired_precalc (struct cmd_t_test *cmd UNUSED)
1559 for(i=0; i < n_pairs ; ++i )
1562 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1563 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1564 pairs[i].sum_of_prod = 0;
1565 pairs[i].correlation = 0;
1566 pairs[i].sum_of_diffs = 0;
1567 pairs[i].ssq_diffs = 0;
1574 paired_calc (const struct dictionary *dict, const struct ccase *c,
1575 struct cmd_t_test *cmd UNUSED, enum mv_class exclude)
1579 double weight = dict_get_case_weight (dict, c, NULL);
1581 for(i=0; i < n_pairs ; ++i )
1583 const struct variable *v0 = pairs[i].v[0];
1584 const struct variable *v1 = pairs[i].v[1];
1586 const union value *val0 = case_data (c, v0);
1587 const union value *val1 = case_data (c, v1);
1589 if (!var_is_value_missing (v0, val0, exclude) &&
1590 !var_is_value_missing (v1, val1, exclude))
1592 pairs[i].n += weight;
1593 pairs[i].sum[0] += weight * val0->f;
1594 pairs[i].sum[1] += weight * val1->f;
1596 pairs[i].ssq[0] += weight * pow2(val0->f);
1597 pairs[i].ssq[1] += weight * pow2(val1->f);
1599 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1601 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1602 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1610 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1614 for(i=0; i < n_pairs ; ++i )
1617 const double n = pairs[i].n;
1619 for (j=0; j < 2 ; ++j)
1621 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1622 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1623 pow2(pairs[i].mean[j]))
1626 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1627 pow2(pairs[i].mean[j]))
1631 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1632 pairs[i].mean[0] * pairs[i].mean[1] ;
1633 /* correlation now actually contains the covariance */
1635 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1636 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1638 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1640 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1641 ( pairs[i].ssq_diffs / n )
1643 pow2(pairs[i].mean_diff )
1649 group_precalc (struct cmd_t_test *cmd )
1654 for(i=0; i< cmd->n_variables ; ++i)
1656 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1658 /* There's always 2 groups for a T - TEST */
1661 gp.indep_width = var_get_width (indep_var);
1663 ttpr->group_hash = hsh_create(2,
1664 (hsh_compare_func *) compare_group_binary,
1665 (hsh_hash_func *) hash_group_binary,
1666 (hsh_free_func *) free_group,
1669 for (j=0 ; j < 2 ; ++j)
1672 struct group_statistics *gs = xmalloc (sizeof *gs);
1678 if ( gp.criterion == CMP_EQ )
1680 gs->id = gp.v.g_value[j];
1685 gs->id.f = gp.v.critical_value - 1.0 ;
1687 gs->id.f = gp.v.critical_value + 1.0 ;
1690 hsh_insert ( ttpr->group_hash, (void *) gs );
1697 group_calc (const struct dictionary *dict,
1698 const struct ccase *c, struct cmd_t_test *cmd,
1699 enum mv_class exclude)
1703 const double weight = dict_get_case_weight (dict, c, NULL);
1705 const union value *gv;
1707 if (var_is_value_missing (indep_var, case_data (c, indep_var), exclude))
1710 gv = case_data (c, indep_var);
1712 for(i=0; i< cmd->n_variables ; ++i)
1714 const 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 (!var_is_value_missing (var, val, exclude))
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 const 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(struct cmd_t_test *cmd,
1777 struct casereader *input, 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 *pass1, *pass2, *pass3;
1784 struct taint *taint;
1787 enum mv_class exclude = cmd->miss != TTS_INCLUDE ? MV_ANY : MV_SYSTEM;
1789 if (!casereader_peek (input, 0, &c))
1791 casereader_destroy (input);
1794 output_split_file_values (ds, &c);
1797 if ( cmd->miss == TTS_LISTWISE )
1798 input = casereader_create_filter_missing (input,
1803 input = casereader_create_filter_weight (input, dict, NULL, NULL);
1805 taint = taint_clone (casereader_get_taint (input));
1806 casereader_split (input, &pass1, &pass2);
1808 common_precalc (cmd);
1809 for (; casereader_read (pass1, &c); case_destroy (&c))
1810 common_calc (dict, &c, cmd, exclude);
1811 casereader_destroy (pass1);
1812 common_postcalc (cmd);
1817 one_sample_precalc (cmd);
1818 for (; casereader_read (pass2, &c); case_destroy (&c))
1819 one_sample_calc (dict, &c, cmd, exclude);
1820 one_sample_postcalc (cmd);
1823 paired_precalc(cmd);
1824 for (; casereader_read (pass2, &c); case_destroy (&c))
1825 paired_calc (dict, &c, cmd, exclude);
1826 paired_postcalc (cmd);
1829 pass3 = casereader_clone (pass2);
1832 for(; casereader_read (pass2, &c); case_destroy (&c))
1833 group_calc (dict, &c, cmd, exclude);
1834 group_postcalc(cmd);
1836 levene (dict, pass3, indep_var, cmd->n_variables, cmd->v_variables,
1840 casereader_destroy (pass2);
1842 if (!taint_has_tainted_successor (taint))
1844 ssbox_create(&stat_summary_box,cmd,mode);
1845 ssbox_populate(&stat_summary_box,cmd);
1846 ssbox_finalize(&stat_summary_box);
1848 if ( mode == T_PAIRED )
1851 trbox_create(&test_results_box,cmd,mode);
1852 trbox_populate(&test_results_box,cmd);
1853 trbox_finalize(&test_results_box);
1856 taint_destroy (taint);
1859 short which_group(const struct group_statistics *g,
1860 const struct group_properties *p);
1862 /* Return -1 if the id of a is less than b; +1 if greater than and
1865 compare_group_binary(const struct group_statistics *a,
1866 const struct group_statistics *b,
1867 const struct group_properties *p)
1872 if ( p->criterion == CMP_LE )
1874 /* less-than comparision is not meaningfull for
1875 alpha variables, so we shouldn't ever arrive here */
1876 assert(p->indep_width == 0 ) ;
1878 flag_a = ( a->id.f < p->v.critical_value ) ;
1879 flag_b = ( b->id.f < p->v.critical_value ) ;
1883 flag_a = which_group(a, p);
1884 flag_b = which_group(b, p);
1887 if (flag_a < flag_b )
1890 return (flag_a > flag_b);
1893 /* This is a degenerate case of a hash, since it can only return three possible
1894 values. It's really a comparison, being used as a hash function */
1897 hash_group_binary(const struct group_statistics *g,
1898 const struct group_properties *p)
1902 if ( p->criterion == CMP_LE )
1904 /* Not meaningfull to do a less than compare for alpha values ? */
1905 assert(p->indep_width == 0 ) ;
1906 flag = ( g->id.f < p->v.critical_value ) ;
1908 else if ( p->criterion == CMP_EQ)
1910 flag = which_group(g,p);
1918 /* return 0 if G belongs to group 0,
1919 1 if it belongs to group 1,
1920 2 if it belongs to neither group */
1922 which_group(const struct group_statistics *g,
1923 const struct group_properties *p)
1926 if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1929 if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))