1 /* PSPP - computes sample statistics. -*-c-*-
3 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 Written by John Williams <johnr.williams@stonebow.otago.ac.nz>.
5 Almost completly re-written by John Darrington 2004
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <gsl/gsl_cdf.h>
32 #include "dictionary.h"
34 #include "group-proc.h"
44 #include "value-labels.h"
46 #include "procedure.h"
49 #define _(msgid) gettext (msgid)
57 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
59 +missing=miss:!analysis/listwise,
60 incl:include/!exclude;
61 format=fmt:!labels/nolabels;
62 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
70 /* Function to use for testing for missing values */
71 static is_missing_func *value_is_missing;
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 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 (union value * v, int 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 ccase *, void *);
216 static void common_precalc (struct cmd_t_test *);
217 static void common_postcalc (struct cmd_t_test *);
219 static int one_sample_calc (const struct ccase *, void *);
220 static void one_sample_precalc (struct cmd_t_test *);
221 static void one_sample_postcalc (struct cmd_t_test *);
223 static int paired_calc (const struct ccase *, void *);
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 ccase *, struct cmd_t_test *);
229 static void group_postcalc (struct cmd_t_test *);
232 static bool calculate(const struct casefile *cf, void *_mode);
236 static struct cmd_t_test cmd;
238 static int bad_weight_warn;
241 static int compare_group_binary(const struct group_statistics *a,
242 const struct group_statistics *b,
243 const struct group_properties *p);
246 static unsigned hash_group_binary(const struct group_statistics *g,
247 const struct group_properties *p);
256 if ( !parse_t_test(&cmd) )
259 if (! cmd.sbc_criteria)
264 if (cmd.sbc_testval) ++m;
265 if (cmd.sbc_groups) ++m;
266 if (cmd.sbc_pairs) ++m;
271 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
280 else if (cmd.sbc_groups)
285 if ( mode == T_PAIRED)
287 if (cmd.sbc_variables)
289 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
295 /* Iterate through the pairs and put each variable that is a
296 member of a pair into cmd.v_variables */
299 struct hsh_iterator hi;
300 struct hsh_table *hash;
303 hash = hsh_create (n_pairs, compare_var_names, hash_var_name, 0, 0);
305 for (i=0; i < n_pairs; ++i)
307 hsh_insert(hash,pairs[i].v[0]);
308 hsh_insert(hash,pairs[i].v[1]);
311 assert(cmd.n_variables == 0);
312 cmd.n_variables = hsh_count(hash);
314 cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
315 sizeof *cmd.v_variables);
316 /* Iterate through the hash */
317 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
319 v=hsh_next(hash,&hi) )
320 cmd.v_variables[i++]=v;
325 else if ( !cmd.sbc_variables)
327 msg(SE, _("One or more VARIABLES must be specified."));
333 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
334 if (cmd.incl == TTS_INCLUDE )
335 value_is_missing = mv_is_value_system_missing;
337 value_is_missing = mv_is_value_missing;
341 ok = multipass_procedure_with_splits (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 cmd_t_test *cmd UNUSED)
365 int n_group_values=0;
369 indep_var = parse_variable ();
372 lex_error ("expecting variable name in GROUPS subcommand");
376 if (indep_var->type == T_STRING && indep_var->width > MAX_SHORT_STRING)
378 msg (SE, _("Long string variable %s is not valid here."),
383 if (!lex_match ('('))
385 if (indep_var->type == NUMERIC)
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 (&gp.v.g_value[0], indep_var->type))
410 if (indep_var->type != NUMERIC)
413 msg (SE, _("When applying GROUPS to a string variable, two "
414 "values must be specified."));
417 gp.criterion = CMP_LE;
418 gp.v.critical_value = gp.v.g_value[0].f;
424 if (!parse_value (&gp.v.g_value[1], indep_var->type))
428 if (!lex_force_match (')'))
431 if ( n_group_values == 2 )
432 gp.criterion = CMP_EQ ;
434 gp.criterion = CMP_LE ;
442 tts_custom_pairs (struct cmd_t_test *cmd UNUSED)
444 struct variable **vars;
446 size_t n_pairs_local;
448 size_t n_before_WITH;
449 size_t n_after_WITH = SIZE_MAX;
450 int paired ; /* Was the PAIRED keyword given ? */
455 if (!parse_variables (default_dict, &vars, &n_vars,
456 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
464 if (lex_match (T_WITH))
466 n_before_WITH = n_vars;
467 if (!parse_variables (default_dict, &vars, &n_vars,
468 PV_DUPLICATE | PV_APPEND
469 | PV_NUMERIC | PV_NO_SCRATCH))
474 n_after_WITH = n_vars - n_before_WITH;
477 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
479 /* Determine the number of pairs needed */
482 if (n_before_WITH != n_after_WITH)
485 msg (SE, _("PAIRED was specified but the number of variables "
486 "preceding WITH (%d) did not match the number "
488 n_before_WITH, n_after_WITH );
491 n_pairs_local = n_before_WITH;
493 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
495 n_pairs_local = n_before_WITH * n_after_WITH ;
497 else /* Neither WITH nor PAIRED keyword given */
502 msg (SE, _("At least two variables must be specified "
507 /* how many ways can you pick 2 from n_vars ? */
508 n_pairs_local = n_vars * (n_vars - 1) / 2;
512 /* Allocate storage for the pairs */
513 pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
515 /* Populate the pairs with the appropriate variables */
520 assert(n_pairs_local == n_vars / 2);
521 for (i = 0; i < n_pairs_local; ++i)
523 pairs[i].v[n_pairs] = vars[i];
524 pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
527 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
532 for(i=0 ; i < n_before_WITH ; ++i )
534 for(j=0 ; j < n_after_WITH ; ++j)
536 pairs[p].v[0] = vars[i];
537 pairs[p].v[1] = vars[j+n_before_WITH];
542 else /* Neither WITH nor PAIRED given */
547 for(i=0 ; i < n_vars ; ++i )
549 for(j=i+1 ; j < n_vars ; ++j)
551 pairs[p].v[0] = vars[i];
552 pairs[p].v[1] = vars[j];
558 n_pairs+=n_pairs_local;
564 /* Parses the current token (numeric or string, depending on type)
565 value v and returns success. */
567 parse_value (union value * v, int type )
571 if (!lex_force_num ())
577 if (!lex_force_string ())
579 strncpy (v->s, ds_c_str (&tokstr), ds_length (&tokstr));
588 /* Implementation of the SSBOX object */
590 void ssbox_base_init(struct ssbox *this, int cols,int rows);
592 void ssbox_base_finalize(struct ssbox *ssb);
594 void ssbox_one_sample_init(struct ssbox *this,
595 struct cmd_t_test *cmd );
597 void ssbox_independent_samples_init(struct ssbox *this,
598 struct cmd_t_test *cmd);
600 void ssbox_paired_init(struct ssbox *this,
601 struct cmd_t_test *cmd);
604 /* Factory to create an ssbox */
606 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
611 ssbox_one_sample_init(ssb,cmd);
614 ssbox_independent_samples_init(ssb,cmd);
617 ssbox_paired_init(ssb,cmd);
626 /* Despatcher for the populate method */
628 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
630 ssb->populate(ssb,cmd);
634 /* Despatcher for finalize */
636 ssbox_finalize(struct ssbox *ssb)
642 /* Submit the box and clear up */
644 ssbox_base_finalize(struct ssbox *ssb)
651 /* Initialize a ssbox struct */
653 ssbox_base_init(struct ssbox *this, int cols,int rows)
655 this->finalize = ssbox_base_finalize;
656 this->t = tab_create (cols, rows, 0);
658 tab_columns (this->t, SOM_COL_DOWN, 1);
659 tab_headers (this->t,0,0,1,0);
660 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
661 tab_hline(this->t, TAL_2,0,cols-1,1);
662 tab_dim (this->t, tab_natural_dimensions);
665 void ssbox_one_sample_populate(struct ssbox *ssb,
666 struct cmd_t_test *cmd);
668 /* Initialize the one_sample ssbox */
670 ssbox_one_sample_init(struct ssbox *this,
671 struct cmd_t_test *cmd )
674 const int vsize=cmd->n_variables+1;
676 this->populate = ssbox_one_sample_populate;
678 ssbox_base_init(this, hsize,vsize);
679 tab_title (this->t, 0, _("One-Sample Statistics"));
680 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
681 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
682 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
683 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
684 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
687 void ssbox_independent_samples_populate(struct ssbox *ssb,
688 struct cmd_t_test *cmd);
690 /* Initialize the independent samples ssbox */
692 ssbox_independent_samples_init(struct ssbox *this,
693 struct cmd_t_test *cmd)
696 int vsize = cmd->n_variables*2 +1;
698 this->populate = ssbox_independent_samples_populate;
700 ssbox_base_init(this, hsize,vsize);
701 tab_title (this->t, 0, _("Group Statistics"));
702 tab_vline(this->t,0,1,0,vsize - 1);
703 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, indep_var->name);
704 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
705 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
706 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
707 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
711 /* Populate the ssbox for independent samples */
713 ssbox_independent_samples_populate(struct ssbox *ssb,
714 struct cmd_t_test *cmd)
720 double indep_value[2];
722 char prefix[2][3]={"",""};
724 if ( indep_var->type == NUMERIC )
726 val_lab0 = val_labs_find( indep_var->val_labs,gp.v.g_value[0]);
727 val_lab1 = val_labs_find( indep_var->val_labs,gp.v.g_value[1]);
731 val_lab0 = gp.v.g_value[0].s;
732 val_lab1 = gp.v.g_value[1].s;
735 if (gp.criterion == CMP_LE )
737 strcpy(prefix[0],"< ");
738 strcpy(prefix[1],">=");
739 indep_value[0] = gp.v.critical_value;
740 indep_value[1] = gp.v.critical_value;
744 indep_value[0] = gp.v.g_value[0].f;
745 indep_value[1] = gp.v.g_value[1].f;
750 for (i=0; i < cmd->n_variables; ++i)
752 struct variable *var = cmd->v_variables[i];
753 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
756 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
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, 2, 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, 0, _("Paired Sample Statistics"));
826 tab_vline(this->t,TAL_0,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, pairs[i].v[j]->name);
860 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
861 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 2, 0);
862 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
863 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
869 /* Populate the one sample ssbox */
871 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
877 for (i=0; i < cmd->n_variables; ++i)
879 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
881 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
882 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 0);
883 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
884 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
885 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
892 /* Implementation of the Test Results box struct */
894 void trbox_base_init(struct trbox *self,size_t n_vars, int cols);
895 void trbox_base_finalize(struct trbox *trb);
897 void trbox_independent_samples_init(struct trbox *trb,
898 struct cmd_t_test *cmd );
900 void trbox_independent_samples_populate(struct trbox *trb,
901 struct cmd_t_test *cmd);
903 void trbox_one_sample_init(struct trbox *self,
904 struct cmd_t_test *cmd );
906 void trbox_one_sample_populate(struct trbox *trb,
907 struct cmd_t_test *cmd);
909 void trbox_paired_init(struct trbox *self,
910 struct cmd_t_test *cmd );
912 void trbox_paired_populate(struct trbox *trb,
913 struct cmd_t_test *cmd);
917 /* Create a trbox according to mode*/
919 trbox_create(struct trbox *trb,
920 struct cmd_t_test *cmd, int mode)
925 trbox_one_sample_init(trb,cmd);
928 trbox_independent_samples_init(trb,cmd);
931 trbox_paired_init(trb,cmd);
938 /* Populate a trbox according to cmd */
940 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
942 trb->populate(trb,cmd);
945 /* Submit and destroy a trbox */
947 trbox_finalize(struct trbox *trb)
952 /* Initialize the independent samples trbox */
954 trbox_independent_samples_init(struct trbox *self,
955 struct cmd_t_test *cmd UNUSED)
958 const int vsize=cmd->n_variables*2+3;
961 self->populate = trbox_independent_samples_populate;
963 trbox_base_init(self,cmd->n_variables*2,hsize);
964 tab_title(self->t,0,_("Independent Samples Test"));
965 tab_hline(self->t,TAL_1,2,hsize-1,1);
966 tab_vline(self->t,TAL_2,2,0,vsize-1);
967 tab_vline(self->t,TAL_1,4,0,vsize-1);
968 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
969 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
970 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
971 tab_joint_text(self->t, 2, 0, 3, 0,
972 TAB_CENTER,_("Levene's Test for Equality of Variances"));
973 tab_joint_text(self->t, 4,0,hsize-1,0,
974 TAB_CENTER,_("t-test for Equality of Means"));
976 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
977 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
978 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
979 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
980 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
981 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
982 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
983 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
984 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
986 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
987 _("%g%% Confidence Interval of the Difference"),
988 cmd->criteria*100.0);
992 /* Populate the independent samples trbox */
994 trbox_independent_samples_populate(struct trbox *self,
995 struct cmd_t_test *cmd )
1000 for (i=0; i < cmd->n_variables; ++i)
1009 double pooled_variance;
1010 double std_err_diff;
1013 struct variable *var = cmd->v_variables[i];
1014 struct group_proc *grp_data = group_proc_get (var);
1016 struct hsh_table *grp_hash = grp_data->group_hash;
1018 struct group_statistics *gs0 ;
1019 struct group_statistics *gs1 ;
1021 union value search_val;
1023 if ( gp.criterion == CMP_LE )
1024 search_val.f = gp.v.critical_value - 1.0;
1026 search_val = gp.v.g_value[0];
1028 gs0 = hsh_find(grp_hash, (void *) &search_val);
1031 if ( gp.criterion == CMP_LE )
1032 search_val.f = gp.v.critical_value + 1.0;
1034 search_val = gp.v.g_value[1];
1036 gs1 = hsh_find(grp_hash, (void *) &search_val);
1040 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
1042 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1045 tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1047 /* Now work out the significance of the Levene test */
1048 df1 = 1; df2 = grp_data->ugs.n - 2;
1049 q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1051 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1053 df = gs0->n + gs1->n - 2.0 ;
1054 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
1056 pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1058 (gs1->n )*pow2(gs1->s_std_dev)
1061 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1062 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
1064 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1066 p = gsl_cdf_tdist_P(t, df);
1067 q = gsl_cdf_tdist_Q(t, df);
1069 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1071 mean_diff = gs0->mean - gs1->mean;
1072 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1075 std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1076 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1079 /* Now work out the confidence interval */
1080 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1082 t = gsl_cdf_tdist_Qinv(q,df);
1083 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1084 mean_diff - t * std_err_diff, 8, 3);
1086 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1087 mean_diff + t * std_err_diff, 8, 3);
1092 /* Now for the \sigma_1 != \sigma_2 case */
1093 tab_text (self->t, 1, i*2+3+1,
1094 TAB_LEFT, _("Equal variances not assumed"));
1097 se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1098 (pow2(gs1->s_std_dev)/(gs1->n -1) );
1100 t = mean_diff / sqrt(se2) ;
1101 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1104 (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 ))
1108 (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1112 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1114 p = gsl_cdf_tdist_P(t, df);
1115 q = gsl_cdf_tdist_Q(t, df);
1117 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1119 /* Now work out the confidence interval */
1120 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1122 t = gsl_cdf_tdist_Qinv(q, df);
1124 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1127 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1130 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1131 mean_diff - t * std_err_diff, 8, 3);
1133 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1134 mean_diff + t * std_err_diff, 8, 3);
1140 /* Initialize the paired samples trbox */
1142 trbox_paired_init(struct trbox *self,
1143 struct cmd_t_test *cmd UNUSED)
1147 const int vsize=n_pairs+3;
1149 self->populate = trbox_paired_populate;
1151 trbox_base_init(self,n_pairs,hsize);
1152 tab_title (self->t, 0, _("Paired Samples Test"));
1153 tab_hline(self->t,TAL_1,2,6,1);
1154 tab_vline(self->t,TAL_2,2,0,vsize - 1);
1155 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1156 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1157 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1158 tab_hline(self->t,TAL_1,5,6, 2);
1159 tab_vline(self->t,TAL_0,6,0,1);
1161 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1162 _("%g%% Confidence Interval of the Difference"),
1163 cmd->criteria*100.0);
1165 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1166 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1167 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1168 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1169 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1170 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1171 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1172 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1175 /* Populate the paired samples trbox */
1177 trbox_paired_populate(struct trbox *trb,
1178 struct cmd_t_test *cmd UNUSED)
1182 for (i=0; i < n_pairs; ++i)
1187 double n = pairs[i].n;
1191 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1193 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1194 pairs[i].v[0]->name, pairs[i].v[1]->name);
1196 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1198 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1201 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1202 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1204 /* Now work out the confidence interval */
1205 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1207 t = gsl_cdf_tdist_Qinv(q, df);
1209 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1210 pairs[i].mean_diff - t * se_mean , 8, 4);
1212 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1213 pairs[i].mean_diff + t * se_mean , 8, 4);
1215 t = (pairs[i].mean[0] - pairs[i].mean[1])
1217 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1218 2 * pairs[i].correlation *
1219 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1223 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1225 /* Degrees of freedom */
1226 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1228 p = gsl_cdf_tdist_P(t,df);
1229 q = gsl_cdf_tdist_P(t,df);
1231 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1236 /* Initialize the one sample trbox */
1238 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1241 const int vsize=cmd->n_variables+3;
1243 self->populate = trbox_one_sample_populate;
1245 trbox_base_init(self, cmd->n_variables,hsize);
1246 tab_title (self->t, 0, _("One-Sample Test"));
1247 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1248 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1250 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1251 _("Test Value = %f"), cmd->n_testval[0]);
1253 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1256 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1257 _("%g%% Confidence Interval of the Difference"),
1258 cmd->criteria*100.0);
1260 tab_vline(self->t,TAL_0,6,1,1);
1261 tab_hline(self->t,TAL_1,5,6,2);
1262 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1263 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1264 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1265 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1266 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1267 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1272 /* Populate the one sample trbox */
1274 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1280 for (i=0; i < cmd->n_variables; ++i)
1285 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1288 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1290 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1292 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1294 /* degrees of freedom */
1297 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1299 p = gsl_cdf_tdist_P(t, df);
1300 q = gsl_cdf_tdist_Q(t, df);
1302 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1304 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1306 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1309 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1310 t = gsl_cdf_tdist_Qinv(q, df);
1312 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1313 gs->mean_diff - t * gs->se_mean, 8,4);
1315 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1316 gs->mean_diff + t * gs->se_mean, 8,4);
1320 /* Base initializer for the generalized trbox */
1322 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1324 const size_t rows = 3 + data_rows;
1326 self->finalize = trbox_base_finalize;
1327 self->t = tab_create (cols, rows, 0);
1328 tab_headers (self->t,0,0,3,0);
1329 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1330 tab_hline(self->t, TAL_2,0,cols-1,3);
1331 tab_dim (self->t, tab_natural_dimensions);
1335 /* Base finalizer for the trbox */
1337 trbox_base_finalize(struct trbox *trb)
1343 /* Create , populate and submit the Paired Samples Correlation box */
1347 const int rows=1+n_pairs;
1351 struct tab_table *table;
1353 table = tab_create (cols,rows,0);
1355 tab_columns (table, SOM_COL_DOWN, 1);
1356 tab_headers (table,0,0,1,0);
1357 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1358 tab_hline(table, TAL_2, 0, cols - 1, 1);
1359 tab_vline(table, TAL_2, 2, 0, rows - 1);
1360 tab_dim(table, tab_natural_dimensions);
1361 tab_title(table, 0, _("Paired Samples Correlations"));
1363 /* column headings */
1364 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1365 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1366 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1368 for (i=0; i < n_pairs; ++i)
1372 double df = pairs[i].n -2;
1374 double correlation_t =
1375 pairs[i].correlation * sqrt(df) /
1376 sqrt(1 - pow2(pairs[i].correlation));
1380 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1383 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1384 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1388 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1389 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1391 p = gsl_cdf_tdist_P(correlation_t, df);
1392 q = gsl_cdf_tdist_Q(correlation_t, df);
1394 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1403 /* Calculation Implementation */
1405 /* Per case calculations common to all variants of the T test */
1407 common_calc (const struct ccase *c, void *_cmd)
1410 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1412 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1415 /* Skip the entire case if /MISSING=LISTWISE is set */
1416 if ( cmd->miss == TTS_LISTWISE )
1418 for(i=0; i< cmd->n_variables ; ++i)
1420 struct variable *v = cmd->v_variables[i];
1421 const union value *val = case_data (c, v->fv);
1423 if (value_is_missing(&v->miss, val) )
1430 /* Listwise has to be implicit if the independent variable is missing ?? */
1431 if ( cmd->sbc_groups )
1433 const union value *gv = case_data (c, indep_var->fv);
1434 if ( value_is_missing(&indep_var->miss, gv) )
1441 for(i=0; i< cmd->n_variables ; ++i)
1443 struct group_statistics *gs;
1444 struct variable *v = cmd->v_variables[i];
1445 const union value *val = case_data (c, v->fv);
1447 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1449 if (! value_is_missing(&v->miss, val) )
1452 gs->sum+=weight * val->f;
1453 gs->ssq+=weight * val->f * val->f;
1459 /* Pre calculations common to all variants of the T test */
1461 common_precalc ( struct cmd_t_test *cmd )
1465 for(i=0; i< cmd->n_variables ; ++i)
1467 struct group_statistics *gs;
1468 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1477 /* Post calculations common to all variants of the T test */
1479 common_postcalc ( struct cmd_t_test *cmd )
1484 for(i=0; i< cmd->n_variables ; ++i)
1486 struct group_statistics *gs;
1487 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1489 gs->mean=gs->sum / gs->n;
1490 gs->s_std_dev= sqrt(
1491 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1496 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1499 gs->se_mean = gs->std_dev / sqrt(gs->n);
1500 gs->mean_diff= gs->sum_diff / gs->n;
1504 /* Per case calculations for one sample t test */
1506 one_sample_calc (const struct ccase *c, void *cmd_)
1509 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1512 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1514 /* Skip the entire case if /MISSING=LISTWISE is set */
1515 if ( cmd->miss == TTS_LISTWISE )
1517 for(i=0; i< cmd->n_variables ; ++i)
1519 struct variable *v = cmd->v_variables[i];
1520 const union value *val = case_data (c, v->fv);
1522 if (value_is_missing(&v->miss, val) )
1529 for(i=0; i< cmd->n_variables ; ++i)
1531 struct group_statistics *gs;
1532 struct variable *v = cmd->v_variables[i];
1533 const union value *val = case_data (c, v->fv);
1535 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1537 if ( ! value_is_missing(&v->miss, val))
1538 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1544 /* Pre calculations for one sample t test */
1546 one_sample_precalc ( struct cmd_t_test *cmd )
1550 for(i=0; i< cmd->n_variables ; ++i)
1552 struct group_statistics *gs;
1553 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1559 /* Post calculations for one sample t test */
1561 one_sample_postcalc (struct cmd_t_test *cmd)
1565 for(i=0; i< cmd->n_variables ; ++i)
1567 struct group_statistics *gs;
1568 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1570 gs->mean_diff = gs->sum_diff / gs->n ;
1577 paired_precalc (struct cmd_t_test *cmd UNUSED)
1581 for(i=0; i < n_pairs ; ++i )
1584 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1585 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1586 pairs[i].sum_of_prod = 0;
1587 pairs[i].correlation = 0;
1588 pairs[i].sum_of_diffs = 0;
1589 pairs[i].ssq_diffs = 0;
1596 paired_calc (const struct ccase *c, void *cmd_)
1600 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1602 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1604 /* Skip the entire case if /MISSING=LISTWISE is set ,
1605 AND one member of a pair is missing */
1606 if ( cmd->miss == TTS_LISTWISE )
1608 for(i=0; i < n_pairs ; ++i )
1610 struct variable *v0 = pairs[i].v[0];
1611 struct variable *v1 = pairs[i].v[1];
1613 const union value *val0 = case_data (c, v0->fv);
1614 const union value *val1 = case_data (c, v1->fv);
1616 if ( value_is_missing(&v0->miss, val0) ||
1617 value_is_missing(&v1->miss, val1) )
1624 for(i=0; i < n_pairs ; ++i )
1626 struct variable *v0 = pairs[i].v[0];
1627 struct variable *v1 = pairs[i].v[1];
1629 const union value *val0 = case_data (c, v0->fv);
1630 const union value *val1 = case_data (c, v1->fv);
1632 if ( ( !value_is_missing(&v0->miss, val0)
1633 && !value_is_missing(&v1->miss, val1) ) )
1635 pairs[i].n += weight;
1636 pairs[i].sum[0] += weight * val0->f;
1637 pairs[i].sum[1] += weight * val1->f;
1639 pairs[i].ssq[0] += weight * pow2(val0->f);
1640 pairs[i].ssq[1] += weight * pow2(val1->f);
1642 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1644 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1645 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1653 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1657 for(i=0; i < n_pairs ; ++i )
1660 const double n = pairs[i].n;
1662 for (j=0; j < 2 ; ++j)
1664 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1665 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1666 pow2(pairs[i].mean[j]))
1669 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1670 pow2(pairs[i].mean[j]))
1674 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1675 pairs[i].mean[0] * pairs[i].mean[1] ;
1676 /* correlation now actually contains the covariance */
1678 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1679 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1681 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1683 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1684 ( pairs[i].ssq_diffs / n )
1686 pow2(pairs[i].mean_diff )
1692 group_precalc (struct cmd_t_test *cmd )
1697 for(i=0; i< cmd->n_variables ; ++i)
1699 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1701 /* There's always 2 groups for a T - TEST */
1704 gp.indep_width = indep_var->width;
1706 ttpr->group_hash = hsh_create(2,
1707 (hsh_compare_func *) compare_group_binary,
1708 (hsh_hash_func *) hash_group_binary,
1709 (hsh_free_func *) free_group,
1712 for (j=0 ; j < 2 ; ++j)
1715 struct group_statistics *gs = xmalloc (sizeof *gs);
1721 if ( gp.criterion == CMP_EQ )
1723 gs->id = gp.v.g_value[j];
1728 gs->id.f = gp.v.critical_value - 1.0 ;
1730 gs->id.f = gp.v.critical_value + 1.0 ;
1733 hsh_insert ( ttpr->group_hash, (void *) gs );
1741 group_calc (const struct ccase *c, struct cmd_t_test *cmd)
1745 const union value *gv = case_data (c, indep_var->fv);
1747 const double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1749 if ( value_is_missing(&indep_var->miss, gv) )
1754 if ( cmd->miss == TTS_LISTWISE )
1756 for(i=0; i< cmd->n_variables ; ++i)
1758 struct variable *v = cmd->v_variables[i];
1759 const union value *val = case_data (c, v->fv);
1761 if (value_is_missing(&v->miss, val) )
1768 gv = case_data (c, indep_var->fv);
1770 for(i=0; i< cmd->n_variables ; ++i)
1772 struct variable *var = cmd->v_variables[i];
1773 const union value *val = case_data (c, var->fv);
1774 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1775 struct group_statistics *gs;
1777 gs = hsh_find(grp_hash, (void *) gv);
1779 /* If the independent variable doesn't match either of the values
1780 for this case then move on to the next case */
1784 if ( !value_is_missing(&var->miss, val) )
1787 gs->sum+=weight * val->f;
1788 gs->ssq+=weight * pow2(val->f);
1797 group_postcalc ( struct cmd_t_test *cmd )
1801 for(i=0; i< cmd->n_variables ; ++i)
1803 struct variable *var = cmd->v_variables[i];
1804 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1805 struct hsh_iterator g;
1806 struct group_statistics *gs;
1809 for (gs = hsh_first (grp_hash,&g);
1811 gs = hsh_next(grp_hash,&g))
1813 gs->mean = gs->sum / gs->n;
1815 gs->s_std_dev= sqrt(
1816 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1821 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1824 gs->se_mean = gs->std_dev / sqrt(gs->n);
1834 calculate(const struct casefile *cf, void *cmd_)
1836 struct ssbox stat_summary_box;
1837 struct trbox test_results_box;
1839 struct casereader *r;
1842 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1844 common_precalc(cmd);
1845 for(r = casefile_get_reader (cf);
1846 casereader_read (r, &c) ;
1849 common_calc(&c,cmd);
1851 casereader_destroy (r);
1852 common_postcalc(cmd);
1857 one_sample_precalc(cmd);
1858 for(r = casefile_get_reader (cf);
1859 casereader_read (r, &c) ;
1862 one_sample_calc(&c,cmd);
1864 casereader_destroy (r);
1865 one_sample_postcalc(cmd);
1869 paired_precalc(cmd);
1870 for(r = casefile_get_reader (cf);
1871 casereader_read (r, &c) ;
1874 paired_calc(&c,cmd);
1876 casereader_destroy (r);
1877 paired_postcalc(cmd);
1883 for(r = casefile_get_reader (cf);
1884 casereader_read (r, &c) ;
1889 casereader_destroy (r);
1890 group_postcalc(cmd);
1892 levene(cf, indep_var, cmd->n_variables, cmd->v_variables,
1893 (cmd->miss == TTS_LISTWISE)?LEV_LISTWISE:LEV_ANALYSIS ,
1898 ssbox_create(&stat_summary_box,cmd,mode);
1899 ssbox_populate(&stat_summary_box,cmd);
1900 ssbox_finalize(&stat_summary_box);
1902 if ( mode == T_PAIRED)
1905 trbox_create(&test_results_box,cmd,mode);
1906 trbox_populate(&test_results_box,cmd);
1907 trbox_finalize(&test_results_box);
1912 short which_group(const struct group_statistics *g,
1913 const struct group_properties *p);
1915 /* Return -1 if the id of a is less than b; +1 if greater than and
1918 compare_group_binary(const struct group_statistics *a,
1919 const struct group_statistics *b,
1920 const struct group_properties *p)
1925 if ( p->criterion == CMP_LE )
1927 /* less-than-or-equal comparision is not meaningfull for
1928 alpha variables, so we shouldn't ever arrive here */
1929 assert(p->indep_width == 0 ) ;
1931 flag_a = ( a->id.f < p->v.critical_value ) ;
1932 flag_b = ( b->id.f < p->v.critical_value ) ;
1936 flag_a = which_group(a, p);
1937 flag_b = which_group(b, p);
1940 if (flag_a < flag_b )
1943 return (flag_a > flag_b);
1946 /* This is a degenerate case of a hash, since it can only return three possible
1947 values. It's really a comparison, being used as a hash function */
1950 hash_group_binary(const struct group_statistics *g,
1951 const struct group_properties *p)
1955 if ( p->criterion == CMP_LE )
1957 /* Not meaningfull to do a less than compare for alpha values ? */
1958 assert(p->indep_width == 0 ) ;
1959 flag = ( g->id.f < p->v.critical_value ) ;
1961 else if ( p->criterion == CMP_EQ)
1963 flag = which_group(g,p);
1971 /* return 0 if G belongs to group 0,
1972 1 if it belongs to group 1,
1973 2 if it belongs to neither group */
1975 which_group(const struct group_statistics *g,
1976 const struct group_properties *p)
1979 if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1982 if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))