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/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/value-labels.h>
33 #include <data/variable.h>
34 #include <language/command.h>
35 #include <language/dictionary/split-file.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/assertion.h>
39 #include <libpspp/compiler.h>
40 #include <libpspp/hash.h>
41 #include <libpspp/magic.h>
42 #include <libpspp/message.h>
43 #include <libpspp/misc.h>
44 #include <libpspp/str.h>
45 #include <libpspp/taint.h>
46 #include <math/group-proc.h>
47 #include <math/levene.h>
48 #include <output/manager.h>
49 #include <output/table.h>
54 #define _(msgid) gettext (msgid)
62 +variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
64 missing=miss:!analysis/listwise,
65 incl:include/!exclude;
66 +format=fmt:!labels/nolabels;
67 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
73 /* Variable for the GROUPS subcommand, if given. */
74 static struct variable *indep_var;
82 struct group_properties
84 /* The comparison criterion */
85 enum comparison criterion;
87 /* The width of the independent variable */
91 /* The value of the independent variable at which groups are determined to
92 belong to one group or the other */
93 double critical_value;
96 /* The values of the independent variable for each group */
97 union value g_value[2];
103 static struct group_properties gp ;
107 /* PAIRS: Number of pairs to be compared ; each pair. */
108 static int n_pairs = 0 ;
111 /* The variables comprising the pair */
112 const struct variable *v[2];
114 /* The number of valid variable pairs */
117 /* The sum of the members */
120 /* sum of squares of the members */
123 /* Std deviation of the members */
127 /* Sample Std deviation of the members */
130 /* The means of the members */
133 /* The correlation coefficient between the variables */
136 /* The sum of the differences */
139 /* The sum of the products */
142 /* The mean of the differences */
145 /* The sum of the squares of the differences */
148 /* The std deviation of the differences */
152 static struct pair *pairs=0;
154 static int parse_value (struct lexer *lexer, union value * v, enum var_type);
156 /* Structures and Functions for the Statistics Summary Box */
158 typedef void populate_ssbox_func(struct ssbox *ssb,
159 struct cmd_t_test *cmd);
160 typedef void finalize_ssbox_func(struct ssbox *ssb);
166 populate_ssbox_func *populate;
167 finalize_ssbox_func *finalize;
172 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
174 /* Populate a ssbox according to cmd */
175 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
177 /* Submit and destroy a ssbox */
178 void ssbox_finalize(struct ssbox *ssb);
180 /* A function to create, populate and submit the Paired Samples Correlation
185 /* Structures and Functions for the Test Results Box */
188 typedef void populate_trbox_func(struct trbox *trb,
189 struct cmd_t_test *cmd);
190 typedef void finalize_trbox_func(struct trbox *trb);
194 populate_trbox_func *populate;
195 finalize_trbox_func *finalize;
199 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
201 /* Populate a ssbox according to cmd */
202 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
204 /* Submit and destroy a ssbox */
205 void trbox_finalize(struct trbox *trb);
207 /* Which mode was T-TEST invoked */
215 static int common_calc (const struct dictionary *dict,
216 const struct ccase *, void *,
218 static void common_precalc (struct cmd_t_test *);
219 static void common_postcalc (struct cmd_t_test *);
221 static int one_sample_calc (const struct dictionary *dict, const struct ccase *, void *, enum mv_class);
222 static void one_sample_precalc (struct cmd_t_test *);
223 static void one_sample_postcalc (struct cmd_t_test *);
225 static int paired_calc (const struct dictionary *dict, const struct ccase *,
226 struct cmd_t_test*, enum mv_class);
227 static void paired_precalc (struct cmd_t_test *);
228 static void paired_postcalc (struct cmd_t_test *);
230 static void group_precalc (struct cmd_t_test *);
231 static int group_calc (const struct dictionary *dict, const struct ccase *,
232 struct cmd_t_test *, enum mv_class);
233 static void group_postcalc (struct cmd_t_test *);
236 static void calculate(struct cmd_t_test *,
238 const struct dataset *);
242 static struct cmd_t_test cmd;
244 static bool bad_weight_warn = false;
247 static int compare_group_binary(const struct group_statistics *a,
248 const struct group_statistics *b,
249 const struct group_properties *p);
252 static unsigned hash_group_binary(const struct group_statistics *g,
253 const struct group_properties *p);
258 cmd_t_test (struct lexer *lexer, struct dataset *ds)
260 struct casegrouper *grouper;
261 struct casereader *group;
264 if ( !parse_t_test (lexer, ds, &cmd, NULL) )
267 if (! cmd.sbc_criteria)
272 if (cmd.sbc_testval) ++m;
273 if (cmd.sbc_groups) ++m;
274 if (cmd.sbc_pairs) ++m;
279 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
288 else if (cmd.sbc_groups)
293 if ( mode == T_PAIRED)
295 if (cmd.sbc_variables)
297 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
303 /* Iterate through the pairs and put each variable that is a
304 member of a pair into cmd.v_variables */
307 struct hsh_iterator hi;
308 struct const_hsh_table *hash;
309 const struct variable *v;
311 hash = const_hsh_create (n_pairs, compare_vars_by_name, hash_var_by_name,
314 for (i=0; i < n_pairs; ++i)
316 const_hsh_insert (hash, pairs[i].v[0]);
317 const_hsh_insert (hash, pairs[i].v[1]);
320 assert(cmd.n_variables == 0);
321 cmd.n_variables = const_hsh_count (hash);
323 cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
324 sizeof *cmd.v_variables);
325 /* Iterate through the hash */
326 for (i=0,v = const_hsh_first (hash, &hi);
328 v = const_hsh_next (hash, &hi) )
329 cmd.v_variables[i++]=v;
330 const_hsh_destroy(hash);
333 else if ( !cmd.sbc_variables)
335 msg(SE, _("One or more VARIABLES must be specified."));
340 bad_weight_warn = true;
343 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
344 while (casegrouper_get_next_group (grouper, &group))
345 calculate (&cmd, group, ds);
346 ok = casegrouper_destroy (grouper);
347 ok = proc_commit (ds) && ok;
353 if ( mode == T_IND_SAMPLES)
356 /* Destroy any group statistics we created */
357 for (v = 0 ; v < cmd.n_variables ; ++v )
359 struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
360 hsh_destroy (grpp->group_hash);
365 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
369 tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
371 int n_group_values=0;
373 lex_match (lexer, '=');
375 indep_var = parse_variable (lexer, dataset_dict (ds));
378 lex_error (lexer, "expecting variable name in GROUPS subcommand");
382 if (var_is_long_string (indep_var))
384 msg (SE, _("Long string variable %s is not valid here."),
385 var_get_name (indep_var));
389 if (!lex_match (lexer, '('))
391 if (var_is_numeric (indep_var))
393 gp.v.g_value[0].f = 1;
394 gp.v.g_value[1].f = 2;
396 gp.criterion = CMP_EQ;
404 msg (SE, _("When applying GROUPS to a string variable, two "
405 "values must be specified."));
410 if (!parse_value (lexer, &gp.v.g_value[0], var_get_type (indep_var)))
413 lex_match (lexer, ',');
414 if (lex_match (lexer, ')'))
416 if (var_is_alpha (indep_var))
418 msg (SE, _("When applying GROUPS to a string variable, two "
419 "values must be specified."));
422 gp.criterion = CMP_LE;
423 gp.v.critical_value = gp.v.g_value[0].f;
429 if (!parse_value (lexer, &gp.v.g_value[1], var_get_type (indep_var)))
433 if (!lex_force_match (lexer, ')'))
436 if ( n_group_values == 2 )
437 gp.criterion = CMP_EQ ;
439 gp.criterion = CMP_LE ;
447 tts_custom_pairs (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
449 const struct variable **vars;
451 size_t n_pairs_local;
453 size_t n_before_WITH;
454 size_t n_after_WITH = SIZE_MAX;
455 int paired ; /* Was the PAIRED keyword given ? */
457 lex_match (lexer, '=');
460 if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
461 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
469 if (lex_match (lexer, T_WITH))
471 n_before_WITH = n_vars;
472 if (!parse_variables_const (lexer, dataset_dict (ds), &vars, &n_vars,
473 PV_DUPLICATE | PV_APPEND
474 | PV_NUMERIC | PV_NO_SCRATCH))
479 n_after_WITH = n_vars - n_before_WITH;
482 paired = (lex_match (lexer, '(') && lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
484 /* Determine the number of pairs needed */
487 if (n_before_WITH != n_after_WITH)
490 msg (SE, _("PAIRED was specified but the number of variables "
491 "preceding WITH (%d) did not match the number "
493 (int) n_before_WITH, (int) n_after_WITH );
496 n_pairs_local = n_before_WITH;
498 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
500 n_pairs_local = n_before_WITH * n_after_WITH ;
502 else /* Neither WITH nor PAIRED keyword given */
507 msg (SE, _("At least two variables must be specified "
512 /* how many ways can you pick 2 from n_vars ? */
513 n_pairs_local = n_vars * (n_vars - 1) / 2;
517 /* Allocate storage for the pairs */
518 pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
520 /* Populate the pairs with the appropriate variables */
525 assert(n_pairs_local == n_vars / 2);
526 for (i = 0; i < n_pairs_local; ++i)
528 pairs[i].v[n_pairs] = vars[i];
529 pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
532 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
537 for(i=0 ; i < n_before_WITH ; ++i )
539 for(j=0 ; j < n_after_WITH ; ++j)
541 pairs[p].v[0] = vars[i];
542 pairs[p].v[1] = vars[j+n_before_WITH];
547 else /* Neither WITH nor PAIRED given */
552 for(i=0 ; i < n_vars ; ++i )
554 for(j=i+1 ; j < n_vars ; ++j)
556 pairs[p].v[0] = vars[i];
557 pairs[p].v[1] = vars[j];
563 n_pairs+=n_pairs_local;
569 /* Parses the current token (numeric or string, depending on type)
570 value v and returns success. */
572 parse_value (struct lexer *lexer, union value * v, enum var_type type)
574 if (type == VAR_NUMERIC)
576 if (!lex_force_num (lexer))
578 v->f = lex_tokval (lexer);
582 if (!lex_force_string (lexer))
584 strncpy (v->s, ds_cstr (lex_tokstr (lexer)), ds_length (lex_tokstr (lexer)));
593 /* Implementation of the SSBOX object */
595 void ssbox_base_init(struct ssbox *this, int cols,int rows);
597 void ssbox_base_finalize(struct ssbox *ssb);
599 void ssbox_one_sample_init(struct ssbox *this,
600 struct cmd_t_test *cmd );
602 void ssbox_independent_samples_init(struct ssbox *this,
603 struct cmd_t_test *cmd);
605 void ssbox_paired_init(struct ssbox *this,
606 struct cmd_t_test *cmd);
609 /* Factory to create an ssbox */
611 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
616 ssbox_one_sample_init(ssb,cmd);
619 ssbox_independent_samples_init(ssb,cmd);
622 ssbox_paired_init(ssb,cmd);
631 /* Despatcher for the populate method */
633 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
635 ssb->populate(ssb,cmd);
639 /* Despatcher for finalize */
641 ssbox_finalize(struct ssbox *ssb)
647 /* Submit the box and clear up */
649 ssbox_base_finalize(struct ssbox *ssb)
656 /* Initialize a ssbox struct */
658 ssbox_base_init(struct ssbox *this, int cols,int rows)
660 this->finalize = ssbox_base_finalize;
661 this->t = tab_create (cols, rows, 0);
663 tab_columns (this->t, SOM_COL_DOWN, 1);
664 tab_headers (this->t,0,0,1,0);
665 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
666 tab_hline(this->t, TAL_2,0,cols-1,1);
667 tab_dim (this->t, tab_natural_dimensions);
670 void ssbox_one_sample_populate(struct ssbox *ssb,
671 struct cmd_t_test *cmd);
673 /* Initialize the one_sample ssbox */
675 ssbox_one_sample_init(struct ssbox *this,
676 struct cmd_t_test *cmd )
679 const int vsize=cmd->n_variables+1;
681 this->populate = ssbox_one_sample_populate;
683 ssbox_base_init(this, hsize,vsize);
684 tab_title (this->t, _("One-Sample Statistics"));
685 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
686 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
687 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
688 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
689 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
692 void ssbox_independent_samples_populate(struct ssbox *ssb,
693 struct cmd_t_test *cmd);
695 /* Initialize the independent samples ssbox */
697 ssbox_independent_samples_init(struct ssbox *this,
698 struct cmd_t_test *cmd)
701 int vsize = cmd->n_variables*2 +1;
703 this->populate = ssbox_independent_samples_populate;
705 ssbox_base_init(this, hsize,vsize);
706 tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
707 tab_title (this->t, _("Group Statistics"));
708 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, var_get_name (indep_var));
709 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
710 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
711 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
712 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
716 /* Populate the ssbox for independent samples */
718 ssbox_independent_samples_populate(struct ssbox *ssb,
719 struct cmd_t_test *cmd)
723 const char *val_lab0;
724 const char *val_lab1;
725 double indep_value[2];
727 char prefix[2][3]={"",""};
729 if ( var_is_numeric (indep_var) )
731 val_lab0 = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
732 val_lab1 = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
736 val_lab0 = gp.v.g_value[0].s;
737 val_lab1 = gp.v.g_value[1].s;
740 if (gp.criterion == CMP_LE )
742 strcpy(prefix[0],"< ");
743 strcpy(prefix[1],">=");
744 indep_value[0] = gp.v.critical_value;
745 indep_value[1] = gp.v.critical_value;
749 indep_value[0] = gp.v.g_value[0].f;
750 indep_value[1] = gp.v.g_value[1].f;
755 for (i=0; i < cmd->n_variables; ++i)
757 const struct variable *var = cmd->v_variables[i];
758 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
761 tab_text (ssb->t, 0, i*2+1, TAB_LEFT,
762 var_get_name (cmd->v_variables[i]));
765 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
766 "%s%s", prefix[0], val_lab0);
768 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
769 "%s%g", prefix[0], indep_value[0]);
773 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
774 "%s%s", prefix[1], val_lab1);
776 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
777 "%s%g", prefix[1], indep_value[1]);
780 /* Fill in the group statistics */
781 for ( count = 0 ; count < 2 ; ++count )
783 union value search_val;
785 struct group_statistics *gs;
787 if ( gp.criterion == CMP_LE )
791 /* less than ( < ) case */
792 search_val.f = gp.v.critical_value - 1.0;
797 search_val.f = gp.v.critical_value + 1.0;
802 search_val = gp.v.g_value[count];
805 gs = hsh_find(grp_hash, (void *) &search_val);
808 tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 10, 0);
809 tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
810 tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
811 tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
817 void ssbox_paired_populate(struct ssbox *ssb,
818 struct cmd_t_test *cmd);
820 /* Initialize the paired values ssbox */
822 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
826 int vsize = n_pairs*2+1;
828 this->populate = ssbox_paired_populate;
830 ssbox_base_init(this, hsize,vsize);
831 tab_title (this->t, _("Paired Sample Statistics"));
832 tab_vline(this->t,TAL_GAP,1,0,vsize-1);
833 tab_vline(this->t,TAL_2,2,0,vsize-1);
834 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
835 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
836 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
837 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
841 /* Populate the ssbox for paired values */
843 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
849 for (i=0; i < n_pairs; ++i)
853 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
855 for (j=0 ; j < 2 ; ++j)
857 struct group_statistics *gs;
859 gs = &group_proc_get (pairs[i].v[j])->ugs;
863 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT,
864 var_get_name (pairs[i].v[j]));
867 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
868 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 10, 0);
869 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
870 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
876 /* Populate the one sample ssbox */
878 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
884 for (i=0; i < cmd->n_variables; ++i)
886 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
888 tab_text (ssb->t, 0, i+1, TAB_LEFT, var_get_name (cmd->v_variables[i]));
889 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 10, 0);
890 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
891 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
892 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
899 /* Implementation of the Test Results box struct */
901 void trbox_base_init(struct trbox *self,size_t n_vars, int cols);
902 void trbox_base_finalize(struct trbox *trb);
904 void trbox_independent_samples_init(struct trbox *trb,
905 struct cmd_t_test *cmd );
907 void trbox_independent_samples_populate(struct trbox *trb,
908 struct cmd_t_test *cmd);
910 void trbox_one_sample_init(struct trbox *self,
911 struct cmd_t_test *cmd );
913 void trbox_one_sample_populate(struct trbox *trb,
914 struct cmd_t_test *cmd);
916 void trbox_paired_init(struct trbox *self,
917 struct cmd_t_test *cmd );
919 void trbox_paired_populate(struct trbox *trb,
920 struct cmd_t_test *cmd);
924 /* Create a trbox according to mode*/
926 trbox_create(struct trbox *trb,
927 struct cmd_t_test *cmd, int mode)
932 trbox_one_sample_init(trb,cmd);
935 trbox_independent_samples_init(trb,cmd);
938 trbox_paired_init(trb,cmd);
945 /* Populate a trbox according to cmd */
947 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
949 trb->populate(trb,cmd);
952 /* Submit and destroy a trbox */
954 trbox_finalize(struct trbox *trb)
959 /* Initialize the independent samples trbox */
961 trbox_independent_samples_init(struct trbox *self,
962 struct cmd_t_test *cmd UNUSED)
965 const int vsize=cmd->n_variables*2+3;
968 self->populate = trbox_independent_samples_populate;
970 trbox_base_init(self,cmd->n_variables*2,hsize);
971 tab_title(self->t,_("Independent Samples Test"));
972 tab_hline(self->t,TAL_1,2,hsize-1,1);
973 tab_vline(self->t,TAL_2,2,0,vsize-1);
974 tab_vline(self->t,TAL_1,4,0,vsize-1);
975 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
976 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
977 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
978 tab_joint_text(self->t, 2, 0, 3, 0,
979 TAB_CENTER,_("Levene's Test for Equality of Variances"));
980 tab_joint_text(self->t, 4,0,hsize-1,0,
981 TAB_CENTER,_("t-test for Equality of Means"));
983 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
984 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
985 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
986 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
987 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
988 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
989 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
990 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
991 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
993 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
994 _("%g%% Confidence Interval of the Difference"),
995 cmd->criteria*100.0);
999 /* Populate the independent samples trbox */
1001 trbox_independent_samples_populate(struct trbox *self,
1002 struct cmd_t_test *cmd )
1007 for (i=0; i < cmd->n_variables; ++i)
1016 double pooled_variance;
1017 double std_err_diff;
1020 const struct variable *var = cmd->v_variables[i];
1021 struct group_proc *grp_data = group_proc_get (var);
1023 struct hsh_table *grp_hash = grp_data->group_hash;
1025 struct group_statistics *gs0 ;
1026 struct group_statistics *gs1 ;
1028 union value search_val;
1030 if ( gp.criterion == CMP_LE )
1031 search_val.f = gp.v.critical_value - 1.0;
1033 search_val = gp.v.g_value[0];
1035 gs0 = hsh_find(grp_hash, (void *) &search_val);
1038 if ( gp.criterion == CMP_LE )
1039 search_val.f = gp.v.critical_value + 1.0;
1041 search_val = gp.v.g_value[1];
1043 gs1 = hsh_find(grp_hash, (void *) &search_val);
1047 tab_text (self->t, 0, i*2+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1049 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1052 tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1054 /* Now work out the significance of the Levene test */
1055 df1 = 1; df2 = grp_data->ugs.n - 2;
1056 q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1058 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1060 df = gs0->n + gs1->n - 2.0 ;
1061 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 10, 0);
1063 pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1065 (gs1->n )*pow2(gs1->s_std_dev)
1068 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1069 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
1071 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1073 p = gsl_cdf_tdist_P(t, df);
1074 q = gsl_cdf_tdist_Q(t, df);
1076 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1078 mean_diff = gs0->mean - gs1->mean;
1079 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1082 std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1083 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1086 /* Now work out the confidence interval */
1087 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1089 t = gsl_cdf_tdist_Qinv(q,df);
1090 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1091 mean_diff - t * std_err_diff, 8, 3);
1093 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1094 mean_diff + t * std_err_diff, 8, 3);
1099 /* Now for the \sigma_1 != \sigma_2 case */
1100 tab_text (self->t, 1, i*2+3+1,
1101 TAB_LEFT, _("Equal variances not assumed"));
1104 se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1105 (pow2(gs1->s_std_dev)/(gs1->n -1) );
1107 t = mean_diff / sqrt(se2) ;
1108 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1111 (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 ))
1115 (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1119 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1121 p = gsl_cdf_tdist_P(t, df);
1122 q = gsl_cdf_tdist_Q(t, df);
1124 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1126 /* Now work out the confidence interval */
1127 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1129 t = gsl_cdf_tdist_Qinv(q, df);
1131 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1134 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1137 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1138 mean_diff - t * std_err_diff, 8, 3);
1140 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1141 mean_diff + t * std_err_diff, 8, 3);
1147 /* Initialize the paired samples trbox */
1149 trbox_paired_init(struct trbox *self,
1150 struct cmd_t_test *cmd UNUSED)
1154 const int vsize=n_pairs+3;
1156 self->populate = trbox_paired_populate;
1158 trbox_base_init(self,n_pairs,hsize);
1159 tab_title (self->t, _("Paired Samples Test"));
1160 tab_hline(self->t,TAL_1,2,6,1);
1161 tab_vline(self->t,TAL_2,2,0,vsize - 1);
1162 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1163 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1164 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1165 tab_hline(self->t,TAL_1,5,6, 2);
1166 tab_vline(self->t,TAL_GAP,6,0,1);
1168 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1169 _("%g%% Confidence Interval of the Difference"),
1170 cmd->criteria*100.0);
1172 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1173 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1174 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1175 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1176 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1177 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1178 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1179 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1182 /* Populate the paired samples trbox */
1184 trbox_paired_populate(struct trbox *trb,
1185 struct cmd_t_test *cmd UNUSED)
1189 for (i=0; i < n_pairs; ++i)
1194 double n = pairs[i].n;
1198 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1200 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1201 var_get_name (pairs[i].v[0]),
1202 var_get_name (pairs[i].v[1]));
1204 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1206 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1209 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1210 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1212 /* Now work out the confidence interval */
1213 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1215 t = gsl_cdf_tdist_Qinv(q, df);
1217 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1218 pairs[i].mean_diff - t * se_mean , 8, 4);
1220 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1221 pairs[i].mean_diff + t * se_mean , 8, 4);
1223 t = (pairs[i].mean[0] - pairs[i].mean[1])
1225 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1226 2 * pairs[i].correlation *
1227 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1231 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1233 /* Degrees of freedom */
1234 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 10, 0 );
1236 p = gsl_cdf_tdist_P(t,df);
1237 q = gsl_cdf_tdist_P(t,df);
1239 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1244 /* Initialize the one sample trbox */
1246 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1249 const int vsize=cmd->n_variables+3;
1251 self->populate = trbox_one_sample_populate;
1253 trbox_base_init(self, cmd->n_variables,hsize);
1254 tab_title (self->t, _("One-Sample Test"));
1255 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1256 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1258 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1259 _("Test Value = %f"), cmd->n_testval[0]);
1261 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1264 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1265 _("%g%% Confidence Interval of the Difference"),
1266 cmd->criteria*100.0);
1268 tab_vline(self->t,TAL_GAP,6,1,1);
1269 tab_hline(self->t,TAL_1,5,6,2);
1270 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1271 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1272 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1273 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1274 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1275 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1280 /* Populate the one sample trbox */
1282 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1288 for (i=0; i < cmd->n_variables; ++i)
1293 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1296 tab_text (trb->t, 0, i+3, TAB_LEFT, var_get_name (cmd->v_variables[i]));
1298 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1300 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1302 /* degrees of freedom */
1305 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1307 p = gsl_cdf_tdist_P(t, df);
1308 q = gsl_cdf_tdist_Q(t, df);
1310 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1312 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1314 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1317 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1318 t = gsl_cdf_tdist_Qinv(q, df);
1320 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1321 gs->mean_diff - t * gs->se_mean, 8,4);
1323 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1324 gs->mean_diff + t * gs->se_mean, 8,4);
1328 /* Base initializer for the generalized trbox */
1330 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1332 const size_t rows = 3 + data_rows;
1334 self->finalize = trbox_base_finalize;
1335 self->t = tab_create (cols, rows, 0);
1336 tab_headers (self->t,0,0,3,0);
1337 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1338 tab_hline(self->t, TAL_2,0,cols-1,3);
1339 tab_dim (self->t, tab_natural_dimensions);
1343 /* Base finalizer for the trbox */
1345 trbox_base_finalize(struct trbox *trb)
1351 /* Create , populate and submit the Paired Samples Correlation box */
1355 const int rows=1+n_pairs;
1359 struct tab_table *table;
1361 table = tab_create (cols,rows,0);
1363 tab_columns (table, SOM_COL_DOWN, 1);
1364 tab_headers (table,0,0,1,0);
1365 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1366 tab_hline(table, TAL_2, 0, cols - 1, 1);
1367 tab_vline(table, TAL_2, 2, 0, rows - 1);
1368 tab_dim(table, tab_natural_dimensions);
1369 tab_title(table, _("Paired Samples Correlations"));
1371 /* column headings */
1372 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1373 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1374 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1376 for (i=0; i < n_pairs; ++i)
1380 double df = pairs[i].n -2;
1382 double correlation_t =
1383 pairs[i].correlation * sqrt(df) /
1384 sqrt(1 - pow2(pairs[i].correlation));
1388 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1391 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1393 var_get_name (pairs[i].v[0]),
1394 var_get_name (pairs[i].v[1]));
1398 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1399 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1401 p = gsl_cdf_tdist_P(correlation_t, df);
1402 q = gsl_cdf_tdist_Q(correlation_t, df);
1404 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1413 /* Calculation Implementation */
1415 /* Per case calculations common to all variants of the T test */
1417 common_calc (const struct dictionary *dict,
1418 const struct ccase *c,
1420 enum mv_class exclude)
1423 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1425 double weight = dict_get_case_weight (dict, c, NULL);
1428 /* Listwise has to be implicit if the independent variable is missing ?? */
1429 if ( cmd->sbc_groups )
1431 if (var_is_value_missing (indep_var, case_data (c, indep_var), exclude))
1435 for(i = 0; i < cmd->n_variables ; ++i)
1437 const struct variable *v = cmd->v_variables[i];
1438 const union value *val = case_data (c, v);
1440 if (!var_is_value_missing (v, val, exclude))
1442 struct group_statistics *gs;
1443 gs = &group_proc_get (v)->ugs;
1446 gs->sum += weight * val->f;
1447 gs->ssq += weight * val->f * val->f;
1453 /* Pre calculations common to all variants of the T test */
1455 common_precalc ( struct cmd_t_test *cmd )
1459 for(i=0; i< cmd->n_variables ; ++i)
1461 struct group_statistics *gs;
1462 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1471 /* Post calculations common to all variants of the T test */
1473 common_postcalc (struct cmd_t_test *cmd)
1477 for(i=0; i< cmd->n_variables ; ++i)
1479 struct group_statistics *gs;
1480 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1482 gs->mean=gs->sum / gs->n;
1483 gs->s_std_dev= sqrt(
1484 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1489 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1492 gs->se_mean = gs->std_dev / sqrt(gs->n);
1493 gs->mean_diff= gs->sum_diff / gs->n;
1497 /* Per case calculations for one sample t test */
1499 one_sample_calc (const struct dictionary *dict,
1500 const struct ccase *c, void *cmd_,
1501 enum mv_class exclude)
1505 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1507 double weight = dict_get_case_weight (dict, c, NULL);
1510 for(i=0; i< cmd->n_variables ; ++i)
1512 struct group_statistics *gs;
1513 const struct variable *v = cmd->v_variables[i];
1514 const union value *val = case_data (c, v);
1516 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1518 if (!var_is_value_missing (v, val, exclude))
1519 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1525 /* Pre calculations for one sample t test */
1527 one_sample_precalc ( struct cmd_t_test *cmd )
1531 for(i=0; i< cmd->n_variables ; ++i)
1533 struct group_statistics *gs;
1534 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1540 /* Post calculations for one sample t test */
1542 one_sample_postcalc (struct cmd_t_test *cmd)
1546 for(i=0; i< cmd->n_variables ; ++i)
1548 struct group_statistics *gs;
1549 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1551 gs->mean_diff = gs->sum_diff / gs->n ;
1558 paired_precalc (struct cmd_t_test *cmd UNUSED)
1562 for(i=0; i < n_pairs ; ++i )
1565 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1566 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1567 pairs[i].sum_of_prod = 0;
1568 pairs[i].correlation = 0;
1569 pairs[i].sum_of_diffs = 0;
1570 pairs[i].ssq_diffs = 0;
1577 paired_calc (const struct dictionary *dict, const struct ccase *c,
1578 struct cmd_t_test *cmd UNUSED, enum mv_class exclude)
1582 double weight = dict_get_case_weight (dict, c, NULL);
1584 for(i=0; i < n_pairs ; ++i )
1586 const struct variable *v0 = pairs[i].v[0];
1587 const struct variable *v1 = pairs[i].v[1];
1589 const union value *val0 = case_data (c, v0);
1590 const union value *val1 = case_data (c, v1);
1592 if (!var_is_value_missing (v0, val0, exclude) &&
1593 !var_is_value_missing (v1, val1, exclude))
1595 pairs[i].n += weight;
1596 pairs[i].sum[0] += weight * val0->f;
1597 pairs[i].sum[1] += weight * val1->f;
1599 pairs[i].ssq[0] += weight * pow2(val0->f);
1600 pairs[i].ssq[1] += weight * pow2(val1->f);
1602 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1604 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1605 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1613 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1617 for(i=0; i < n_pairs ; ++i )
1620 const double n = pairs[i].n;
1622 for (j=0; j < 2 ; ++j)
1624 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1625 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1626 pow2(pairs[i].mean[j]))
1629 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1630 pow2(pairs[i].mean[j]))
1634 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1635 pairs[i].mean[0] * pairs[i].mean[1] ;
1636 /* correlation now actually contains the covariance */
1638 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1639 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1641 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1643 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1644 ( pairs[i].ssq_diffs / n )
1646 pow2(pairs[i].mean_diff )
1652 group_precalc (struct cmd_t_test *cmd )
1657 for(i=0; i< cmd->n_variables ; ++i)
1659 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1661 /* There's always 2 groups for a T - TEST */
1664 gp.indep_width = var_get_width (indep_var);
1666 ttpr->group_hash = hsh_create(2,
1667 (hsh_compare_func *) compare_group_binary,
1668 (hsh_hash_func *) hash_group_binary,
1669 (hsh_free_func *) free_group,
1672 for (j=0 ; j < 2 ; ++j)
1675 struct group_statistics *gs = xmalloc (sizeof *gs);
1681 if ( gp.criterion == CMP_EQ )
1683 gs->id = gp.v.g_value[j];
1688 gs->id.f = gp.v.critical_value - 1.0 ;
1690 gs->id.f = gp.v.critical_value + 1.0 ;
1693 hsh_insert ( ttpr->group_hash, (void *) gs );
1701 group_calc (const struct dictionary *dict,
1702 const struct ccase *c, struct cmd_t_test *cmd,
1703 enum mv_class exclude)
1707 const double weight = dict_get_case_weight (dict, c, NULL);
1709 const union value *gv;
1711 if (var_is_value_missing (indep_var, case_data (c, indep_var), exclude))
1714 gv = case_data (c, indep_var);
1716 for(i=0; i< cmd->n_variables ; ++i)
1718 const struct variable *var = cmd->v_variables[i];
1719 const union value *val = case_data (c, var);
1720 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1721 struct group_statistics *gs;
1723 gs = hsh_find(grp_hash, (void *) gv);
1725 /* If the independent variable doesn't match either of the values
1726 for this case then move on to the next case */
1730 if (!var_is_value_missing (var, val, exclude))
1733 gs->sum += weight * val->f;
1734 gs->ssq += weight * pow2(val->f);
1743 group_postcalc ( struct cmd_t_test *cmd )
1747 for (i = 0; i < cmd->n_variables ; ++i)
1749 const struct variable *var = cmd->v_variables[i];
1750 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1751 struct hsh_iterator g;
1752 struct group_statistics *gs;
1755 for (gs = hsh_first (grp_hash,&g);
1757 gs = hsh_next(grp_hash,&g))
1759 gs->mean = gs->sum / gs->n;
1761 gs->s_std_dev= sqrt(
1762 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1767 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1770 gs->se_mean = gs->std_dev / sqrt(gs->n);
1780 calculate(struct cmd_t_test *cmd,
1781 struct casereader *input, const struct dataset *ds)
1783 const struct dictionary *dict = dataset_dict (ds);
1784 struct ssbox stat_summary_box;
1785 struct trbox test_results_box;
1787 struct casereader *pass1, *pass2, *pass3;
1788 struct taint *taint;
1791 enum mv_class exclude = cmd->miss != TTS_INCLUDE ? MV_ANY : MV_SYSTEM;
1793 if (!casereader_peek (input, 0, &c))
1795 output_split_file_values (ds, &c);
1798 if ( cmd->miss == TTS_LISTWISE )
1799 input = casereader_create_filter_missing (input,
1804 input = casereader_create_filter_weight (input, dict, NULL, NULL);
1806 taint = taint_clone (casereader_get_taint (input));
1807 casereader_split (input, &pass1, &pass2);
1809 common_precalc (cmd);
1810 for (; casereader_read (pass1, &c); case_destroy (&c))
1811 common_calc (dict, &c, cmd, exclude);
1812 casereader_destroy (pass1);
1813 common_postcalc (cmd);
1818 one_sample_precalc (cmd);
1819 for (; casereader_read (pass2, &c); case_destroy (&c))
1820 one_sample_calc (dict, &c, cmd, exclude);
1821 one_sample_postcalc (cmd);
1824 paired_precalc(cmd);
1825 for (; casereader_read (pass2, &c); case_destroy (&c))
1826 paired_calc (dict, &c, cmd, exclude);
1827 paired_postcalc (cmd);
1830 pass3 = casereader_clone (pass2);
1833 for(; casereader_read (pass2, &c); case_destroy (&c))
1834 group_calc (dict, &c, cmd, exclude);
1835 group_postcalc(cmd);
1837 levene (dict, pass3, indep_var, cmd->n_variables, cmd->v_variables,
1841 casereader_destroy (pass2);
1843 if (!taint_has_tainted_successor (taint))
1845 ssbox_create(&stat_summary_box,cmd,mode);
1846 ssbox_populate(&stat_summary_box,cmd);
1847 ssbox_finalize(&stat_summary_box);
1849 if ( mode == T_PAIRED )
1852 trbox_create(&test_results_box,cmd,mode);
1853 trbox_populate(&test_results_box,cmd);
1854 trbox_finalize(&test_results_box);
1858 short which_group(const struct group_statistics *g,
1859 const struct group_properties *p);
1861 /* Return -1 if the id of a is less than b; +1 if greater than and
1864 compare_group_binary(const struct group_statistics *a,
1865 const struct group_statistics *b,
1866 const struct group_properties *p)
1871 if ( p->criterion == CMP_LE )
1873 /* less-than-or-equal comparision is not meaningfull for
1874 alpha variables, so we shouldn't ever arrive here */
1875 assert(p->indep_width == 0 ) ;
1877 flag_a = ( a->id.f < p->v.critical_value ) ;
1878 flag_b = ( b->id.f < p->v.critical_value ) ;
1882 flag_a = which_group(a, p);
1883 flag_b = which_group(b, p);
1886 if (flag_a < flag_b )
1889 return (flag_a > flag_b);
1892 /* This is a degenerate case of a hash, since it can only return three possible
1893 values. It's really a comparison, being used as a hash function */
1896 hash_group_binary(const struct group_statistics *g,
1897 const struct group_properties *p)
1901 if ( p->criterion == CMP_LE )
1903 /* Not meaningfull to do a less than compare for alpha values ? */
1904 assert(p->indep_width == 0 ) ;
1905 flag = ( g->id.f < p->v.critical_value ) ;
1907 else if ( p->criterion == CMP_EQ)
1909 flag = which_group(g,p);
1917 /* return 0 if G belongs to group 0,
1918 1 if it belongs to group 1,
1919 2 if it belongs to neither group */
1921 which_group(const struct group_statistics *g,
1922 const struct group_properties *p)
1925 if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1928 if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))