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
24 #include <gsl/gsl_cdf.h>
29 #include <data/case.h>
30 #include <data/casefile.h>
31 #include <data/dictionary.h>
32 #include <data/procedure.h>
33 #include <data/value-labels.h>
34 #include <data/variable.h>
35 #include <language/command.h>
36 #include <language/dictionary/split-file.h>
37 #include <language/lexer/lexer.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/hash.h>
42 #include <libpspp/magic.h>
43 #include <libpspp/message.h>
44 #include <libpspp/message.h>
45 #include <libpspp/misc.h>
46 #include <libpspp/str.h>
47 #include <math/group-proc.h>
48 #include <math/levene.h>
49 #include <output/manager.h>
50 #include <output/table.h>
55 #define _(msgid) gettext (msgid)
63 +variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
65 missing=miss:!analysis/listwise,
66 incl:include/!exclude;
67 +format=fmt:!labels/nolabels;
68 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
76 /* Function to use for testing for missing values */
77 static is_missing_func *value_is_missing;
79 /* Variable for the GROUPS subcommand, if given. */
80 static struct variable *indep_var;
88 struct group_properties
90 /* The comparison criterion */
91 enum comparison criterion;
93 /* The width of the independent variable */
97 /* The value of the independent variable at which groups are determined to
98 belong to one group or the other */
99 double critical_value;
102 /* The values of the independent variable for each group */
103 union value g_value[2];
109 static struct group_properties gp ;
113 /* PAIRS: Number of pairs to be compared ; each pair. */
114 static int n_pairs = 0 ;
117 /* The variables comprising the pair */
118 struct variable *v[2];
120 /* The number of valid variable pairs */
123 /* The sum of the members */
126 /* sum of squares of the members */
129 /* Std deviation of the members */
133 /* Sample Std deviation of the members */
136 /* The means of the members */
139 /* The correlation coefficient between the variables */
142 /* The sum of the differences */
145 /* The sum of the products */
148 /* The mean of the differences */
151 /* The sum of the squares of the differences */
154 /* The std deviation of the differences */
158 static struct pair *pairs=0;
160 static int parse_value (union value * v, int type) ;
162 /* Structures and Functions for the Statistics Summary Box */
164 typedef void populate_ssbox_func(struct ssbox *ssb,
165 struct cmd_t_test *cmd);
166 typedef void finalize_ssbox_func(struct ssbox *ssb);
172 populate_ssbox_func *populate;
173 finalize_ssbox_func *finalize;
178 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
180 /* Populate a ssbox according to cmd */
181 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
183 /* Submit and destroy a ssbox */
184 void ssbox_finalize(struct ssbox *ssb);
186 /* A function to create, populate and submit the Paired Samples Correlation
191 /* Structures and Functions for the Test Results Box */
194 typedef void populate_trbox_func(struct trbox *trb,
195 struct cmd_t_test *cmd);
196 typedef void finalize_trbox_func(struct trbox *trb);
200 populate_trbox_func *populate;
201 finalize_trbox_func *finalize;
205 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
207 /* Populate a ssbox according to cmd */
208 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
210 /* Submit and destroy a ssbox */
211 void trbox_finalize(struct trbox *trb);
213 /* Which mode was T-TEST invoked */
221 static int common_calc (const struct ccase *, void *);
222 static void common_precalc (struct cmd_t_test *);
223 static void common_postcalc (struct cmd_t_test *);
225 static int one_sample_calc (const struct ccase *, void *);
226 static void one_sample_precalc (struct cmd_t_test *);
227 static void one_sample_postcalc (struct cmd_t_test *);
229 static int paired_calc (const struct ccase *, void *);
230 static void paired_precalc (struct cmd_t_test *);
231 static void paired_postcalc (struct cmd_t_test *);
233 static void group_precalc (struct cmd_t_test *);
234 static int group_calc (const struct ccase *, struct cmd_t_test *);
235 static void group_postcalc (struct cmd_t_test *);
238 static bool calculate(const struct ccase *first,
239 const struct casefile *cf, void *_mode);
243 static struct cmd_t_test cmd;
245 static int bad_weight_warn;
248 static int compare_group_binary(const struct group_statistics *a,
249 const struct group_statistics *b,
250 const struct group_properties *p);
253 static unsigned hash_group_binary(const struct group_statistics *g,
254 const struct group_properties *p);
263 if ( !parse_t_test(&cmd, NULL) )
266 if (! cmd.sbc_criteria)
271 if (cmd.sbc_testval) ++m;
272 if (cmd.sbc_groups) ++m;
273 if (cmd.sbc_pairs) ++m;
278 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
287 else if (cmd.sbc_groups)
292 if ( mode == T_PAIRED)
294 if (cmd.sbc_variables)
296 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
302 /* Iterate through the pairs and put each variable that is a
303 member of a pair into cmd.v_variables */
306 struct hsh_iterator hi;
307 struct hsh_table *hash;
310 hash = hsh_create (n_pairs, compare_var_names, hash_var_name, 0, 0);
312 for (i=0; i < n_pairs; ++i)
314 hsh_insert(hash,pairs[i].v[0]);
315 hsh_insert(hash,pairs[i].v[1]);
318 assert(cmd.n_variables == 0);
319 cmd.n_variables = hsh_count(hash);
321 cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
322 sizeof *cmd.v_variables);
323 /* Iterate through the hash */
324 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
326 v=hsh_next(hash,&hi) )
327 cmd.v_variables[i++]=v;
332 else if ( !cmd.sbc_variables)
334 msg(SE, _("One or more VARIABLES must be specified."));
340 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
341 if (cmd.incl == TTS_INCLUDE )
342 value_is_missing = mv_is_value_system_missing;
344 value_is_missing = mv_is_value_missing;
348 ok = multipass_procedure_with_splits (calculate, &cmd);
354 if ( mode == T_IND_SAMPLES)
357 /* Destroy any group statistics we created */
358 for (v = 0 ; v < cmd.n_variables ; ++v )
360 struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
361 hsh_destroy (grpp->group_hash);
366 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
370 tts_custom_groups (struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
372 int n_group_values=0;
376 indep_var = parse_variable ();
379 lex_error ("expecting variable name in GROUPS subcommand");
383 if (indep_var->type == T_STRING && indep_var->width > MAX_SHORT_STRING)
385 msg (SE, _("Long string variable %s is not valid here."),
390 if (!lex_match ('('))
392 if (indep_var->type == NUMERIC)
394 gp.v.g_value[0].f = 1;
395 gp.v.g_value[1].f = 2;
397 gp.criterion = CMP_EQ;
405 msg (SE, _("When applying GROUPS to a string variable, two "
406 "values must be specified."));
411 if (!parse_value (&gp.v.g_value[0], indep_var->type))
417 if (indep_var->type != NUMERIC)
420 msg (SE, _("When applying GROUPS to a string variable, two "
421 "values must be specified."));
424 gp.criterion = CMP_LE;
425 gp.v.critical_value = gp.v.g_value[0].f;
431 if (!parse_value (&gp.v.g_value[1], indep_var->type))
435 if (!lex_force_match (')'))
438 if ( n_group_values == 2 )
439 gp.criterion = CMP_EQ ;
441 gp.criterion = CMP_LE ;
449 tts_custom_pairs (struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
451 struct variable **vars;
453 size_t n_pairs_local;
455 size_t n_before_WITH;
456 size_t n_after_WITH = SIZE_MAX;
457 int paired ; /* Was the PAIRED keyword given ? */
462 if (!parse_variables (default_dict, &vars, &n_vars,
463 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
471 if (lex_match (T_WITH))
473 n_before_WITH = n_vars;
474 if (!parse_variables (default_dict, &vars, &n_vars,
475 PV_DUPLICATE | PV_APPEND
476 | PV_NUMERIC | PV_NO_SCRATCH))
481 n_after_WITH = n_vars - n_before_WITH;
484 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
486 /* Determine the number of pairs needed */
489 if (n_before_WITH != n_after_WITH)
492 msg (SE, _("PAIRED was specified but the number of variables "
493 "preceding WITH (%d) did not match the number "
495 n_before_WITH, n_after_WITH );
498 n_pairs_local = n_before_WITH;
500 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
502 n_pairs_local = n_before_WITH * n_after_WITH ;
504 else /* Neither WITH nor PAIRED keyword given */
509 msg (SE, _("At least two variables must be specified "
514 /* how many ways can you pick 2 from n_vars ? */
515 n_pairs_local = n_vars * (n_vars - 1) / 2;
519 /* Allocate storage for the pairs */
520 pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs);
522 /* Populate the pairs with the appropriate variables */
527 assert(n_pairs_local == n_vars / 2);
528 for (i = 0; i < n_pairs_local; ++i)
530 pairs[i].v[n_pairs] = vars[i];
531 pairs[i].v[n_pairs + 1] = vars[i + n_pairs_local];
534 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
539 for(i=0 ; i < n_before_WITH ; ++i )
541 for(j=0 ; j < n_after_WITH ; ++j)
543 pairs[p].v[0] = vars[i];
544 pairs[p].v[1] = vars[j+n_before_WITH];
549 else /* Neither WITH nor PAIRED given */
554 for(i=0 ; i < n_vars ; ++i )
556 for(j=i+1 ; j < n_vars ; ++j)
558 pairs[p].v[0] = vars[i];
559 pairs[p].v[1] = vars[j];
565 n_pairs+=n_pairs_local;
571 /* Parses the current token (numeric or string, depending on type)
572 value v and returns success. */
574 parse_value (union value * v, int type )
578 if (!lex_force_num ())
584 if (!lex_force_string ())
586 strncpy (v->s, ds_cstr (&tokstr), ds_length (&tokstr));
595 /* Implementation of the SSBOX object */
597 void ssbox_base_init(struct ssbox *this, int cols,int rows);
599 void ssbox_base_finalize(struct ssbox *ssb);
601 void ssbox_one_sample_init(struct ssbox *this,
602 struct cmd_t_test *cmd );
604 void ssbox_independent_samples_init(struct ssbox *this,
605 struct cmd_t_test *cmd);
607 void ssbox_paired_init(struct ssbox *this,
608 struct cmd_t_test *cmd);
611 /* Factory to create an ssbox */
613 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
618 ssbox_one_sample_init(ssb,cmd);
621 ssbox_independent_samples_init(ssb,cmd);
624 ssbox_paired_init(ssb,cmd);
633 /* Despatcher for the populate method */
635 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
637 ssb->populate(ssb,cmd);
641 /* Despatcher for finalize */
643 ssbox_finalize(struct ssbox *ssb)
649 /* Submit the box and clear up */
651 ssbox_base_finalize(struct ssbox *ssb)
658 /* Initialize a ssbox struct */
660 ssbox_base_init(struct ssbox *this, int cols,int rows)
662 this->finalize = ssbox_base_finalize;
663 this->t = tab_create (cols, rows, 0);
665 tab_columns (this->t, SOM_COL_DOWN, 1);
666 tab_headers (this->t,0,0,1,0);
667 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
668 tab_hline(this->t, TAL_2,0,cols-1,1);
669 tab_dim (this->t, tab_natural_dimensions);
672 void ssbox_one_sample_populate(struct ssbox *ssb,
673 struct cmd_t_test *cmd);
675 /* Initialize the one_sample ssbox */
677 ssbox_one_sample_init(struct ssbox *this,
678 struct cmd_t_test *cmd )
681 const int vsize=cmd->n_variables+1;
683 this->populate = ssbox_one_sample_populate;
685 ssbox_base_init(this, hsize,vsize);
686 tab_title (this->t, _("One-Sample Statistics"));
687 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
688 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
689 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
690 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
691 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
694 void ssbox_independent_samples_populate(struct ssbox *ssb,
695 struct cmd_t_test *cmd);
697 /* Initialize the independent samples ssbox */
699 ssbox_independent_samples_init(struct ssbox *this,
700 struct cmd_t_test *cmd)
703 int vsize = cmd->n_variables*2 +1;
705 this->populate = ssbox_independent_samples_populate;
707 ssbox_base_init(this, hsize,vsize);
708 tab_vline (this->t, TAL_GAP, 1, 0,vsize - 1);
709 tab_title (this->t, _("Group Statistics"));
710 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, indep_var->name);
711 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
712 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
713 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
714 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
718 /* Populate the ssbox for independent samples */
720 ssbox_independent_samples_populate(struct ssbox *ssb,
721 struct cmd_t_test *cmd)
727 double indep_value[2];
729 char prefix[2][3]={"",""};
731 if ( indep_var->type == NUMERIC )
733 val_lab0 = val_labs_find( indep_var->val_labs,gp.v.g_value[0]);
734 val_lab1 = val_labs_find( indep_var->val_labs,gp.v.g_value[1]);
738 val_lab0 = gp.v.g_value[0].s;
739 val_lab1 = gp.v.g_value[1].s;
742 if (gp.criterion == CMP_LE )
744 strcpy(prefix[0],"< ");
745 strcpy(prefix[1],">=");
746 indep_value[0] = gp.v.critical_value;
747 indep_value[1] = gp.v.critical_value;
751 indep_value[0] = gp.v.g_value[0].f;
752 indep_value[1] = gp.v.g_value[1].f;
757 for (i=0; i < cmd->n_variables; ++i)
759 struct variable *var = cmd->v_variables[i];
760 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
763 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
766 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
767 "%s%s", prefix[0], val_lab0);
769 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
770 "%s%g", prefix[0], indep_value[0]);
774 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
775 "%s%s", prefix[1], val_lab1);
777 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
778 "%s%g", prefix[1], indep_value[1]);
781 /* Fill in the group statistics */
782 for ( count = 0 ; count < 2 ; ++count )
784 union value search_val;
786 struct group_statistics *gs;
788 if ( gp.criterion == CMP_LE )
792 /* less than ( < ) case */
793 search_val.f = gp.v.critical_value - 1.0;
798 search_val.f = gp.v.critical_value + 1.0;
803 search_val = gp.v.g_value[count];
806 gs = hsh_find(grp_hash, (void *) &search_val);
809 tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 2, 0);
810 tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
811 tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
812 tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
818 void ssbox_paired_populate(struct ssbox *ssb,
819 struct cmd_t_test *cmd);
821 /* Initialize the paired values ssbox */
823 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
827 int vsize = n_pairs*2+1;
829 this->populate = ssbox_paired_populate;
831 ssbox_base_init(this, hsize,vsize);
832 tab_title (this->t, _("Paired Sample Statistics"));
833 tab_vline(this->t,TAL_GAP,1,0,vsize-1);
834 tab_vline(this->t,TAL_2,2,0,vsize-1);
835 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
836 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
837 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
838 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
842 /* Populate the ssbox for paired values */
844 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
850 for (i=0; i < n_pairs; ++i)
854 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
856 for (j=0 ; j < 2 ; ++j)
858 struct group_statistics *gs;
860 gs = &group_proc_get (pairs[i].v[j])->ugs;
864 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
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, 2, 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, cmd->v_variables[i]->name);
889 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 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 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, cmd->v_variables[i]->name);
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, 2, 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 pairs[i].v[0]->name, pairs[i].v[1]->name);
1203 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1205 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1208 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1209 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1211 /* Now work out the confidence interval */
1212 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1214 t = gsl_cdf_tdist_Qinv(q, df);
1216 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1217 pairs[i].mean_diff - t * se_mean , 8, 4);
1219 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1220 pairs[i].mean_diff + t * se_mean , 8, 4);
1222 t = (pairs[i].mean[0] - pairs[i].mean[1])
1224 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1225 2 * pairs[i].correlation *
1226 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1230 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1232 /* Degrees of freedom */
1233 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1235 p = gsl_cdf_tdist_P(t,df);
1236 q = gsl_cdf_tdist_P(t,df);
1238 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1243 /* Initialize the one sample trbox */
1245 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1248 const int vsize=cmd->n_variables+3;
1250 self->populate = trbox_one_sample_populate;
1252 trbox_base_init(self, cmd->n_variables,hsize);
1253 tab_title (self->t, _("One-Sample Test"));
1254 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1255 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1257 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1258 _("Test Value = %f"), cmd->n_testval[0]);
1260 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1263 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1264 _("%g%% Confidence Interval of the Difference"),
1265 cmd->criteria*100.0);
1267 tab_vline(self->t,TAL_GAP,6,1,1);
1268 tab_hline(self->t,TAL_1,5,6,2);
1269 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1270 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1271 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1272 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1273 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1274 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1279 /* Populate the one sample trbox */
1281 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1287 for (i=0; i < cmd->n_variables; ++i)
1292 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1295 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1297 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1299 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1301 /* degrees of freedom */
1304 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1306 p = gsl_cdf_tdist_P(t, df);
1307 q = gsl_cdf_tdist_Q(t, df);
1309 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1311 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1313 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1316 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1317 t = gsl_cdf_tdist_Qinv(q, df);
1319 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1320 gs->mean_diff - t * gs->se_mean, 8,4);
1322 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1323 gs->mean_diff + t * gs->se_mean, 8,4);
1327 /* Base initializer for the generalized trbox */
1329 trbox_base_init(struct trbox *self, size_t data_rows, int cols)
1331 const size_t rows = 3 + data_rows;
1333 self->finalize = trbox_base_finalize;
1334 self->t = tab_create (cols, rows, 0);
1335 tab_headers (self->t,0,0,3,0);
1336 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1337 tab_hline(self->t, TAL_2,0,cols-1,3);
1338 tab_dim (self->t, tab_natural_dimensions);
1342 /* Base finalizer for the trbox */
1344 trbox_base_finalize(struct trbox *trb)
1350 /* Create , populate and submit the Paired Samples Correlation box */
1354 const int rows=1+n_pairs;
1358 struct tab_table *table;
1360 table = tab_create (cols,rows,0);
1362 tab_columns (table, SOM_COL_DOWN, 1);
1363 tab_headers (table,0,0,1,0);
1364 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1365 tab_hline(table, TAL_2, 0, cols - 1, 1);
1366 tab_vline(table, TAL_2, 2, 0, rows - 1);
1367 tab_dim(table, tab_natural_dimensions);
1368 tab_title(table, _("Paired Samples Correlations"));
1370 /* column headings */
1371 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1372 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1373 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1375 for (i=0; i < n_pairs; ++i)
1379 double df = pairs[i].n -2;
1381 double correlation_t =
1382 pairs[i].correlation * sqrt(df) /
1383 sqrt(1 - pow2(pairs[i].correlation));
1387 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1390 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1391 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
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 ccase *c, void *_cmd)
1417 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1419 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1422 /* Skip the entire case if /MISSING=LISTWISE is set */
1423 if ( cmd->miss == TTS_LISTWISE )
1425 for(i=0; i< cmd->n_variables ; ++i)
1427 struct variable *v = cmd->v_variables[i];
1428 const union value *val = case_data (c, v->fv);
1430 if (value_is_missing(&v->miss, val) )
1437 /* Listwise has to be implicit if the independent variable is missing ?? */
1438 if ( cmd->sbc_groups )
1440 const union value *gv = case_data (c, indep_var->fv);
1441 if ( value_is_missing(&indep_var->miss, gv) )
1448 for(i=0; i< cmd->n_variables ; ++i)
1450 struct group_statistics *gs;
1451 struct variable *v = cmd->v_variables[i];
1452 const union value *val = case_data (c, v->fv);
1454 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1456 if (! value_is_missing(&v->miss, val) )
1459 gs->sum+=weight * val->f;
1460 gs->ssq+=weight * val->f * val->f;
1466 /* Pre calculations common to all variants of the T test */
1468 common_precalc ( struct cmd_t_test *cmd )
1472 for(i=0; i< cmd->n_variables ; ++i)
1474 struct group_statistics *gs;
1475 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1484 /* Post calculations common to all variants of the T test */
1486 common_postcalc ( struct cmd_t_test *cmd )
1491 for(i=0; i< cmd->n_variables ; ++i)
1493 struct group_statistics *gs;
1494 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1496 gs->mean=gs->sum / gs->n;
1497 gs->s_std_dev= sqrt(
1498 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1503 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1506 gs->se_mean = gs->std_dev / sqrt(gs->n);
1507 gs->mean_diff= gs->sum_diff / gs->n;
1511 /* Per case calculations for one sample t test */
1513 one_sample_calc (const struct ccase *c, void *cmd_)
1516 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1519 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1521 /* Skip the entire case if /MISSING=LISTWISE is set */
1522 if ( cmd->miss == TTS_LISTWISE )
1524 for(i=0; i< cmd->n_variables ; ++i)
1526 struct variable *v = cmd->v_variables[i];
1527 const union value *val = case_data (c, v->fv);
1529 if (value_is_missing(&v->miss, val) )
1536 for(i=0; i< cmd->n_variables ; ++i)
1538 struct group_statistics *gs;
1539 struct variable *v = cmd->v_variables[i];
1540 const union value *val = case_data (c, v->fv);
1542 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1544 if ( ! value_is_missing(&v->miss, val))
1545 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1551 /* Pre calculations for one sample t test */
1553 one_sample_precalc ( struct cmd_t_test *cmd )
1557 for(i=0; i< cmd->n_variables ; ++i)
1559 struct group_statistics *gs;
1560 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1566 /* Post calculations for one sample t test */
1568 one_sample_postcalc (struct cmd_t_test *cmd)
1572 for(i=0; i< cmd->n_variables ; ++i)
1574 struct group_statistics *gs;
1575 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1577 gs->mean_diff = gs->sum_diff / gs->n ;
1584 paired_precalc (struct cmd_t_test *cmd UNUSED)
1588 for(i=0; i < n_pairs ; ++i )
1591 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1592 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1593 pairs[i].sum_of_prod = 0;
1594 pairs[i].correlation = 0;
1595 pairs[i].sum_of_diffs = 0;
1596 pairs[i].ssq_diffs = 0;
1603 paired_calc (const struct ccase *c, void *cmd_)
1607 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1609 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1611 /* Skip the entire case if /MISSING=LISTWISE is set ,
1612 AND one member of a pair is missing */
1613 if ( cmd->miss == TTS_LISTWISE )
1615 for(i=0; i < n_pairs ; ++i )
1617 struct variable *v0 = pairs[i].v[0];
1618 struct variable *v1 = pairs[i].v[1];
1620 const union value *val0 = case_data (c, v0->fv);
1621 const union value *val1 = case_data (c, v1->fv);
1623 if ( value_is_missing(&v0->miss, val0) ||
1624 value_is_missing(&v1->miss, val1) )
1631 for(i=0; i < n_pairs ; ++i )
1633 struct variable *v0 = pairs[i].v[0];
1634 struct variable *v1 = pairs[i].v[1];
1636 const union value *val0 = case_data (c, v0->fv);
1637 const union value *val1 = case_data (c, v1->fv);
1639 if ( ( !value_is_missing(&v0->miss, val0)
1640 && !value_is_missing(&v1->miss, val1) ) )
1642 pairs[i].n += weight;
1643 pairs[i].sum[0] += weight * val0->f;
1644 pairs[i].sum[1] += weight * val1->f;
1646 pairs[i].ssq[0] += weight * pow2(val0->f);
1647 pairs[i].ssq[1] += weight * pow2(val1->f);
1649 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1651 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1652 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1660 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1664 for(i=0; i < n_pairs ; ++i )
1667 const double n = pairs[i].n;
1669 for (j=0; j < 2 ; ++j)
1671 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1672 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1673 pow2(pairs[i].mean[j]))
1676 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1677 pow2(pairs[i].mean[j]))
1681 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1682 pairs[i].mean[0] * pairs[i].mean[1] ;
1683 /* correlation now actually contains the covariance */
1685 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1686 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1688 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1690 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1691 ( pairs[i].ssq_diffs / n )
1693 pow2(pairs[i].mean_diff )
1699 group_precalc (struct cmd_t_test *cmd )
1704 for(i=0; i< cmd->n_variables ; ++i)
1706 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1708 /* There's always 2 groups for a T - TEST */
1711 gp.indep_width = indep_var->width;
1713 ttpr->group_hash = hsh_create(2,
1714 (hsh_compare_func *) compare_group_binary,
1715 (hsh_hash_func *) hash_group_binary,
1716 (hsh_free_func *) free_group,
1719 for (j=0 ; j < 2 ; ++j)
1722 struct group_statistics *gs = xmalloc (sizeof *gs);
1728 if ( gp.criterion == CMP_EQ )
1730 gs->id = gp.v.g_value[j];
1735 gs->id.f = gp.v.critical_value - 1.0 ;
1737 gs->id.f = gp.v.critical_value + 1.0 ;
1740 hsh_insert ( ttpr->group_hash, (void *) gs );
1748 group_calc (const struct ccase *c, struct cmd_t_test *cmd)
1752 const union value *gv = case_data (c, indep_var->fv);
1754 const double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1756 if ( value_is_missing(&indep_var->miss, gv) )
1761 if ( cmd->miss == TTS_LISTWISE )
1763 for(i=0; i< cmd->n_variables ; ++i)
1765 struct variable *v = cmd->v_variables[i];
1766 const union value *val = case_data (c, v->fv);
1768 if (value_is_missing(&v->miss, val) )
1775 gv = case_data (c, indep_var->fv);
1777 for(i=0; i< cmd->n_variables ; ++i)
1779 struct variable *var = cmd->v_variables[i];
1780 const union value *val = case_data (c, var->fv);
1781 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1782 struct group_statistics *gs;
1784 gs = hsh_find(grp_hash, (void *) gv);
1786 /* If the independent variable doesn't match either of the values
1787 for this case then move on to the next case */
1791 if ( !value_is_missing(&var->miss, val) )
1794 gs->sum+=weight * val->f;
1795 gs->ssq+=weight * pow2(val->f);
1804 group_postcalc ( struct cmd_t_test *cmd )
1808 for(i=0; i< cmd->n_variables ; ++i)
1810 struct variable *var = cmd->v_variables[i];
1811 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1812 struct hsh_iterator g;
1813 struct group_statistics *gs;
1816 for (gs = hsh_first (grp_hash,&g);
1818 gs = hsh_next(grp_hash,&g))
1820 gs->mean = gs->sum / gs->n;
1822 gs->s_std_dev= sqrt(
1823 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1828 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1831 gs->se_mean = gs->std_dev / sqrt(gs->n);
1841 calculate(const struct ccase *first, const struct casefile *cf, void *cmd_)
1843 struct ssbox stat_summary_box;
1844 struct trbox test_results_box;
1846 struct casereader *r;
1849 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1851 output_split_file_values (first);
1852 common_precalc(cmd);
1853 for(r = casefile_get_reader (cf);
1854 casereader_read (r, &c) ;
1857 common_calc(&c,cmd);
1859 casereader_destroy (r);
1860 common_postcalc(cmd);
1865 one_sample_precalc(cmd);
1866 for(r = casefile_get_reader (cf);
1867 casereader_read (r, &c) ;
1870 one_sample_calc(&c,cmd);
1872 casereader_destroy (r);
1873 one_sample_postcalc(cmd);
1877 paired_precalc(cmd);
1878 for(r = casefile_get_reader (cf);
1879 casereader_read (r, &c) ;
1882 paired_calc(&c,cmd);
1884 casereader_destroy (r);
1885 paired_postcalc(cmd);
1891 for(r = casefile_get_reader (cf);
1892 casereader_read (r, &c) ;
1897 casereader_destroy (r);
1898 group_postcalc(cmd);
1900 levene(cf, indep_var, cmd->n_variables, cmd->v_variables,
1901 (cmd->miss == TTS_LISTWISE)?LEV_LISTWISE:LEV_ANALYSIS ,
1906 ssbox_create(&stat_summary_box,cmd,mode);
1907 ssbox_populate(&stat_summary_box,cmd);
1908 ssbox_finalize(&stat_summary_box);
1910 if ( mode == T_PAIRED)
1913 trbox_create(&test_results_box,cmd,mode);
1914 trbox_populate(&test_results_box,cmd);
1915 trbox_finalize(&test_results_box);
1920 short which_group(const struct group_statistics *g,
1921 const struct group_properties *p);
1923 /* Return -1 if the id of a is less than b; +1 if greater than and
1926 compare_group_binary(const struct group_statistics *a,
1927 const struct group_statistics *b,
1928 const struct group_properties *p)
1933 if ( p->criterion == CMP_LE )
1935 /* less-than-or-equal comparision is not meaningfull for
1936 alpha variables, so we shouldn't ever arrive here */
1937 assert(p->indep_width == 0 ) ;
1939 flag_a = ( a->id.f < p->v.critical_value ) ;
1940 flag_b = ( b->id.f < p->v.critical_value ) ;
1944 flag_a = which_group(a, p);
1945 flag_b = which_group(b, p);
1948 if (flag_a < flag_b )
1951 return (flag_a > flag_b);
1954 /* This is a degenerate case of a hash, since it can only return three possible
1955 values. It's really a comparison, being used as a hash function */
1958 hash_group_binary(const struct group_statistics *g,
1959 const struct group_properties *p)
1963 if ( p->criterion == CMP_LE )
1965 /* Not meaningfull to do a less than compare for alpha values ? */
1966 assert(p->indep_width == 0 ) ;
1967 flag = ( g->id.f < p->v.critical_value ) ;
1969 else if ( p->criterion == CMP_EQ)
1971 flag = which_group(g,p);
1979 /* return 0 if G belongs to group 0,
1980 1 if it belongs to group 1,
1981 2 if it belongs to neither group */
1983 which_group(const struct group_statistics *g,
1984 const struct group_properties *p)
1987 if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1990 if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))