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"
39 #include "value-labels.h"
43 #include "group_proc.h"
52 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
54 +missing=miss:!analysis/listwise,
55 incl:include/!exclude;
56 format=fmt:!labels/nolabels;
57 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
65 /* Function to use for testing for missing values */
66 static is_missing_func value_is_missing;
68 /* Variable for the GROUPS subcommand, if given. */
69 static struct variable *indep_var;
77 struct group_properties
79 /* The comparison criterion */
80 enum comparison criterion;
82 /* The width of the independent variable */
86 /* The value of the independent variable at which groups are determined to
87 belong to one group or the other */
88 double critical_value;
91 /* The values of the independent variable for each group */
92 union value g_value[2];
98 static struct group_properties gp ;
102 /* PAIRS: Number of pairs to be compared ; each pair. */
103 static int n_pairs = 0 ;
106 /* The variables comprising the pair */
107 struct variable *v[2];
109 /* The number of valid variable pairs */
112 /* The sum of the members */
115 /* sum of squares of the members */
118 /* Std deviation of the members */
122 /* Sample Std deviation of the members */
125 /* The means of the members */
128 /* The correlation coefficient between the variables */
131 /* The sum of the differences */
134 /* The sum of the products */
137 /* The mean of the differences */
140 /* The sum of the squares of the differences */
143 /* The std deviation of the differences */
147 static struct pair *pairs=0;
149 static int parse_value (union value * v, int type) ;
151 /* Structures and Functions for the Statistics Summary Box */
153 typedef void populate_ssbox_func(struct ssbox *ssb,
154 struct cmd_t_test *cmd);
155 typedef void finalize_ssbox_func(struct ssbox *ssb);
161 populate_ssbox_func *populate;
162 finalize_ssbox_func *finalize;
167 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
169 /* Populate a ssbox according to cmd */
170 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
172 /* Submit and destroy a ssbox */
173 void ssbox_finalize(struct ssbox *ssb);
175 /* A function to create, populate and submit the Paired Samples Correlation
180 /* Structures and Functions for the Test Results Box */
183 typedef void populate_trbox_func(struct trbox *trb,
184 struct cmd_t_test *cmd);
185 typedef void finalize_trbox_func(struct trbox *trb);
189 populate_trbox_func *populate;
190 finalize_trbox_func *finalize;
194 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
196 /* Populate a ssbox according to cmd */
197 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
199 /* Submit and destroy a ssbox */
200 void trbox_finalize(struct trbox *trb);
202 /* Which mode was T-TEST invoked */
210 static int common_calc (const struct ccase *, void *);
211 static void common_precalc (struct cmd_t_test *);
212 static void common_postcalc (struct cmd_t_test *);
214 static int one_sample_calc (const struct ccase *, void *);
215 static void one_sample_precalc (struct cmd_t_test *);
216 static void one_sample_postcalc (struct cmd_t_test *);
218 static int paired_calc (const struct ccase *, void *);
219 static void paired_precalc (struct cmd_t_test *);
220 static void paired_postcalc (struct cmd_t_test *);
222 static void group_precalc (struct cmd_t_test *);
223 static int group_calc (const struct ccase *, struct cmd_t_test *);
224 static void group_postcalc (struct cmd_t_test *);
227 static void calculate(const struct casefile *cf, void *_mode);
231 static struct cmd_t_test cmd;
233 static int bad_weight_warn;
236 static int compare_group_binary(const struct group_statistics *a,
237 const struct group_statistics *b,
238 const struct group_properties *p);
241 static unsigned hash_group_binary(const struct group_statistics *g,
242 const struct group_properties *p);
250 if ( !parse_t_test(&cmd) )
253 if (! cmd.sbc_criteria)
258 if (cmd.sbc_testval) ++m;
259 if (cmd.sbc_groups) ++m;
260 if (cmd.sbc_pairs) ++m;
265 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
274 else if (cmd.sbc_groups)
279 if ( mode == T_PAIRED)
281 if (cmd.sbc_variables)
283 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
289 /* Iterate through the pairs and put each variable that is a
290 member of a pair into cmd.v_variables */
293 struct hsh_iterator hi;
294 struct hsh_table *hash;
297 hash = hsh_create (n_pairs, compare_var_names, hash_var_name, 0, 0);
299 for (i=0; i < n_pairs; ++i)
301 hsh_insert(hash,pairs[i].v[0]);
302 hsh_insert(hash,pairs[i].v[1]);
305 assert(cmd.n_variables == 0);
306 cmd.n_variables = hsh_count(hash);
308 cmd.v_variables = xrealloc(cmd.v_variables,
309 sizeof(struct variable) * cmd.n_variables);
310 /* Iterate through the hash */
311 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
313 v=hsh_next(hash,&hi) )
314 cmd.v_variables[i++]=v;
319 else if ( !cmd.sbc_variables)
321 msg(SE, _("One or more VARIABLES must be specified."));
327 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
328 if (cmd.incl == TTS_INCLUDE )
329 value_is_missing = is_system_missing;
331 value_is_missing = is_missing;
335 multipass_procedure_with_splits (calculate, &cmd);
341 if ( mode == T_IND_SAMPLES)
344 /* Destroy any group statistics we created */
345 for (v = 0 ; v < cmd.n_variables ; ++v )
347 struct group_proc *grpp = group_proc_get (cmd.v_variables[v]);
348 hsh_destroy (grpp->group_hash);
357 tts_custom_groups (struct cmd_t_test *cmd UNUSED)
359 int n_group_values=0;
363 indep_var = parse_variable ();
366 lex_error ("expecting variable name in GROUPS subcommand");
370 if (indep_var->type == T_STRING && indep_var->width > MAX_SHORT_STRING)
372 msg (SE, _("Long string variable %s is not valid here."),
377 if (!lex_match ('('))
379 if (indep_var->type == NUMERIC)
381 gp.v.g_value[0].f = 1;
382 gp.v.g_value[1].f = 2;
384 gp.criterion = CMP_EQ;
392 msg (SE, _("When applying GROUPS to a string variable, two "
393 "values must be specified."));
398 if (!parse_value (&gp.v.g_value[0], indep_var->type))
404 if (indep_var->type != NUMERIC)
407 msg (SE, _("When applying GROUPS to a string variable, two "
408 "values must be specified."));
411 gp.criterion = CMP_LE;
412 gp.v.critical_value = gp.v.g_value[0].f;
418 if (!parse_value (&gp.v.g_value[1], indep_var->type))
422 if (!lex_force_match (')'))
425 if ( n_group_values == 2 )
426 gp.criterion = CMP_EQ ;
428 gp.criterion = CMP_LE ;
436 tts_custom_pairs (struct cmd_t_test *cmd UNUSED)
438 struct variable **vars;
443 int n_after_WITH = -1;
444 int paired ; /* Was the PAIRED keyword given ? */
449 if (!parse_variables (default_dict, &vars, &n_vars,
450 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
458 if (lex_match (T_WITH))
460 n_before_WITH = n_vars;
461 if (!parse_variables (default_dict, &vars, &n_vars,
462 PV_DUPLICATE | PV_APPEND
463 | PV_NUMERIC | PV_NO_SCRATCH))
468 n_after_WITH = n_vars - n_before_WITH;
471 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
473 /* Determine the number of pairs needed */
476 if (n_before_WITH != n_after_WITH)
479 msg (SE, _("PAIRED was specified but the number of variables "
480 "preceding WITH (%d) did not match the number "
482 n_before_WITH, n_after_WITH );
485 n_pairs_local=n_before_WITH;
487 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
489 n_pairs_local=n_before_WITH * n_after_WITH ;
491 else /* Neither WITH nor PAIRED keyword given */
496 msg (SE, _("At least two variables must be specified "
501 /* how many ways can you pick 2 from n_vars ? */
502 n_pairs_local = n_vars * (n_vars -1 ) /2 ;
506 /* Allocate storage for the pairs */
507 pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
509 /* Populate the pairs with the appropriate variables */
514 assert(n_pairs_local == n_vars/2);
515 for (i = 0; i < n_pairs_local ; ++i)
517 pairs[i].v[n_pairs+0] = vars[i];
518 pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
521 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
526 for(i=0 ; i < n_before_WITH ; ++i )
528 for(j=0 ; j < n_after_WITH ; ++j)
530 pairs[p].v[0] = vars[i];
531 pairs[p].v[1] = vars[j+n_before_WITH];
536 else /* Neither WITH nor PAIRED given */
541 for(i=0 ; i < n_vars ; ++i )
543 for(j=i+1 ; j < n_vars ; ++j)
545 pairs[p].v[0] = vars[i];
546 pairs[p].v[1] = vars[j];
552 n_pairs+=n_pairs_local;
558 /* Parses the current token (numeric or string, depending on type)
559 value v and returns success. */
561 parse_value (union value * v, int type )
565 if (!lex_force_num ())
571 if (!lex_force_string ())
573 strncpy (v->s, ds_c_str (&tokstr), ds_length (&tokstr));
582 /* Implementation of the SSBOX object */
584 void ssbox_base_init(struct ssbox *this, int cols,int rows);
586 void ssbox_base_finalize(struct ssbox *ssb);
588 void ssbox_one_sample_init(struct ssbox *this,
589 struct cmd_t_test *cmd );
591 void ssbox_independent_samples_init(struct ssbox *this,
592 struct cmd_t_test *cmd);
594 void ssbox_paired_init(struct ssbox *this,
595 struct cmd_t_test *cmd);
598 /* Factory to create an ssbox */
600 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
605 ssbox_one_sample_init(ssb,cmd);
608 ssbox_independent_samples_init(ssb,cmd);
611 ssbox_paired_init(ssb,cmd);
620 /* Despatcher for the populate method */
622 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
624 ssb->populate(ssb,cmd);
628 /* Despatcher for finalize */
630 ssbox_finalize(struct ssbox *ssb)
636 /* Submit the box and clear up */
638 ssbox_base_finalize(struct ssbox *ssb)
645 /* Initialize a ssbox struct */
647 ssbox_base_init(struct ssbox *this, int cols,int rows)
649 this->finalize = ssbox_base_finalize;
650 this->t = tab_create (cols, rows, 0);
652 tab_columns (this->t, SOM_COL_DOWN, 1);
653 tab_headers (this->t,0,0,1,0);
654 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
655 tab_hline(this->t, TAL_2,0,cols-1,1);
656 tab_dim (this->t, tab_natural_dimensions);
659 void ssbox_one_sample_populate(struct ssbox *ssb,
660 struct cmd_t_test *cmd);
662 /* Initialize the one_sample ssbox */
664 ssbox_one_sample_init(struct ssbox *this,
665 struct cmd_t_test *cmd )
668 const int vsize=cmd->n_variables+1;
670 this->populate = ssbox_one_sample_populate;
672 ssbox_base_init(this, hsize,vsize);
673 tab_title (this->t, 0, _("One-Sample Statistics"));
674 tab_vline(this->t, TAL_2, 1,0,vsize - 1);
675 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
676 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
677 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
678 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
681 void ssbox_independent_samples_populate(struct ssbox *ssb,
682 struct cmd_t_test *cmd);
684 /* Initialize the independent samples ssbox */
686 ssbox_independent_samples_init(struct ssbox *this,
687 struct cmd_t_test *cmd)
690 int vsize = cmd->n_variables*2 +1;
692 this->populate = ssbox_independent_samples_populate;
694 ssbox_base_init(this, hsize,vsize);
695 tab_title (this->t, 0, _("Group Statistics"));
696 tab_vline(this->t,0,1,0,vsize - 1);
697 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, indep_var->name);
698 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
699 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
700 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
701 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
705 /* Populate the ssbox for independent samples */
707 ssbox_independent_samples_populate(struct ssbox *ssb,
708 struct cmd_t_test *cmd)
714 double indep_value[2];
716 char prefix[2][3]={"",""};
718 if ( indep_var->type == NUMERIC )
720 val_lab0 = val_labs_find( indep_var->val_labs,gp.v.g_value[0]);
721 val_lab1 = val_labs_find( indep_var->val_labs,gp.v.g_value[1]);
725 val_lab0 = gp.v.g_value[0].s;
726 val_lab1 = gp.v.g_value[1].s;
729 if (gp.criterion == CMP_LE )
731 strcpy(prefix[0],"< ");
732 strcpy(prefix[1],">=");
733 indep_value[0] = gp.v.critical_value;
734 indep_value[1] = gp.v.critical_value;
738 indep_value[0] = gp.v.g_value[0].f;
739 indep_value[1] = gp.v.g_value[1].f;
744 for (i=0; i < cmd->n_variables; ++i)
746 struct variable *var = cmd->v_variables[i];
747 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
750 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
753 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
754 "%s%s", prefix[0], val_lab0);
756 tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
757 "%s%g", prefix[0], indep_value[0]);
761 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
762 "%s%s", prefix[1], val_lab1);
764 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
765 "%s%g", prefix[1], indep_value[1]);
768 /* Fill in the group statistics */
769 for ( count = 0 ; count < 2 ; ++count )
771 union value search_val;
773 struct group_statistics *gs;
775 if ( gp.criterion == CMP_LE )
779 /* less than ( < ) case */
780 search_val.f = gp.v.critical_value - 1.0;
785 search_val.f = gp.v.critical_value + 1.0;
790 search_val = gp.v.g_value[count];
793 gs = hsh_find(grp_hash, (void *) &search_val);
796 tab_float(ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 2, 0);
797 tab_float(ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
798 tab_float(ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
799 tab_float(ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
805 void ssbox_paired_populate(struct ssbox *ssb,
806 struct cmd_t_test *cmd);
808 /* Initialize the paired values ssbox */
810 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd UNUSED)
814 int vsize = n_pairs*2+1;
816 this->populate = ssbox_paired_populate;
818 ssbox_base_init(this, hsize,vsize);
819 tab_title (this->t, 0, _("Paired Sample Statistics"));
820 tab_vline(this->t,TAL_0,1,0,vsize-1);
821 tab_vline(this->t,TAL_2,2,0,vsize-1);
822 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
823 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
824 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
825 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
829 /* Populate the ssbox for paired values */
831 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
837 for (i=0; i < n_pairs; ++i)
841 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
843 for (j=0 ; j < 2 ; ++j)
845 struct group_statistics *gs;
847 gs = &group_proc_get (pairs[i].v[j])->ugs;
851 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
854 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
855 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 2, 0);
856 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
857 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt(pairs[i].n), 8, 3);
863 /* Populate the one sample ssbox */
865 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
871 for (i=0; i < cmd->n_variables; ++i)
873 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
875 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
876 tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 2, 0);
877 tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
878 tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
879 tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
886 /* Implementation of the Test Results box struct */
888 void trbox_base_init(struct trbox *self,int n_vars, int cols);
889 void trbox_base_finalize(struct trbox *trb);
891 void trbox_independent_samples_init(struct trbox *trb,
892 struct cmd_t_test *cmd );
894 void trbox_independent_samples_populate(struct trbox *trb,
895 struct cmd_t_test *cmd);
897 void trbox_one_sample_init(struct trbox *self,
898 struct cmd_t_test *cmd );
900 void trbox_one_sample_populate(struct trbox *trb,
901 struct cmd_t_test *cmd);
903 void trbox_paired_init(struct trbox *self,
904 struct cmd_t_test *cmd );
906 void trbox_paired_populate(struct trbox *trb,
907 struct cmd_t_test *cmd);
911 /* Create a trbox according to mode*/
913 trbox_create(struct trbox *trb,
914 struct cmd_t_test *cmd, int mode)
919 trbox_one_sample_init(trb,cmd);
922 trbox_independent_samples_init(trb,cmd);
925 trbox_paired_init(trb,cmd);
932 /* Populate a trbox according to cmd */
934 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
936 trb->populate(trb,cmd);
939 /* Submit and destroy a trbox */
941 trbox_finalize(struct trbox *trb)
946 /* Initialize the independent samples trbox */
948 trbox_independent_samples_init(struct trbox *self,
949 struct cmd_t_test *cmd UNUSED)
952 const int vsize=cmd->n_variables*2+3;
955 self->populate = trbox_independent_samples_populate;
957 trbox_base_init(self,cmd->n_variables*2,hsize);
958 tab_title(self->t,0,_("Independent Samples Test"));
959 tab_hline(self->t,TAL_1,2,hsize-1,1);
960 tab_vline(self->t,TAL_2,2,0,vsize-1);
961 tab_vline(self->t,TAL_1,4,0,vsize-1);
962 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
963 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
964 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
965 tab_joint_text(self->t, 2, 0, 3, 0,
966 TAB_CENTER,_("Levene's Test for Equality of Variances"));
967 tab_joint_text(self->t, 4,0,hsize-1,0,
968 TAB_CENTER,_("t-test for Equality of Means"));
970 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
971 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
972 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
973 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
974 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
975 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
976 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
977 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
978 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
980 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
981 _("%g%% Confidence Interval of the Difference"),
982 cmd->criteria*100.0);
986 /* Populate the independent samples trbox */
988 trbox_independent_samples_populate(struct trbox *self,
989 struct cmd_t_test *cmd )
994 for (i=0; i < cmd->n_variables; ++i)
1003 double pooled_variance;
1004 double std_err_diff;
1007 struct variable *var = cmd->v_variables[i];
1008 struct group_proc *grp_data = group_proc_get (var);
1010 struct hsh_table *grp_hash = grp_data->group_hash;
1012 struct group_statistics *gs0 ;
1013 struct group_statistics *gs1 ;
1015 union value search_val;
1017 if ( gp.criterion == CMP_LE )
1018 search_val.f = gp.v.critical_value - 1.0;
1020 search_val = gp.v.g_value[0];
1022 gs0 = hsh_find(grp_hash, (void *) &search_val);
1025 if ( gp.criterion == CMP_LE )
1026 search_val.f = gp.v.critical_value + 1.0;
1028 search_val = gp.v.g_value[1];
1030 gs1 = hsh_find(grp_hash, (void *) &search_val);
1034 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
1036 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
1039 tab_float(self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
1041 /* Now work out the significance of the Levene test */
1042 df1 = 1; df2 = grp_data->ugs.n - 2;
1043 q = gsl_cdf_fdist_Q(grp_data->levene, df1, df2);
1045 tab_float(self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
1047 df = gs0->n + gs1->n - 2.0 ;
1048 tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 2, 0);
1050 pooled_variance = ( (gs0->n )*pow2(gs0->s_std_dev)
1052 (gs1->n )*pow2(gs1->s_std_dev)
1055 t = (gs0->mean - gs1->mean) / sqrt(pooled_variance) ;
1056 t /= sqrt((gs0->n + gs1->n)/(gs0->n*gs1->n));
1058 tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
1060 p = gsl_cdf_tdist_P(t, df);
1061 q = gsl_cdf_tdist_Q(t, df);
1063 tab_float(self->t, 6, i*2+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1065 mean_diff = gs0->mean - gs1->mean;
1066 tab_float(self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
1069 std_err_diff = sqrt( pow2(gs0->se_mean) + pow2(gs1->se_mean));
1070 tab_float(self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
1073 /* Now work out the confidence interval */
1074 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1076 t = gsl_cdf_tdist_Qinv(q,df);
1077 tab_float(self->t, 9, i*2+3, TAB_RIGHT,
1078 mean_diff - t * std_err_diff, 8, 3);
1080 tab_float(self->t, 10, i*2+3, TAB_RIGHT,
1081 mean_diff + t * std_err_diff, 8, 3);
1086 /* Now for the \sigma_1 != \sigma_2 case */
1087 tab_text (self->t, 1, i*2+3+1,
1088 TAB_LEFT, _("Equal variances not assumed"));
1091 se2 = (pow2(gs0->s_std_dev)/(gs0->n -1) ) +
1092 (pow2(gs1->s_std_dev)/(gs1->n -1) );
1094 t = mean_diff / sqrt(se2) ;
1095 tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
1098 (pow2(pow2(gs0->s_std_dev)/(gs0->n - 1 ))
1102 (pow2(pow2(gs1->s_std_dev)/(gs1->n - 1 ))
1106 tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
1108 p = gsl_cdf_tdist_P(t, df);
1109 q = gsl_cdf_tdist_Q(t, df);
1111 tab_float(self->t, 6, i*2+3+1, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1113 /* Now work out the confidence interval */
1114 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1116 t = gsl_cdf_tdist_Qinv(q, df);
1118 tab_float(self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
1121 tab_float(self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
1124 tab_float(self->t, 9, i*2+3+1, TAB_RIGHT,
1125 mean_diff - t * std_err_diff, 8, 3);
1127 tab_float(self->t, 10, i*2+3+1, TAB_RIGHT,
1128 mean_diff + t * std_err_diff, 8, 3);
1134 /* Initialize the paired samples trbox */
1136 trbox_paired_init(struct trbox *self,
1137 struct cmd_t_test *cmd UNUSED)
1141 const int vsize=n_pairs+3;
1143 self->populate = trbox_paired_populate;
1145 trbox_base_init(self,n_pairs,hsize);
1146 tab_title (self->t, 0, _("Paired Samples Test"));
1147 tab_hline(self->t,TAL_1,2,6,1);
1148 tab_vline(self->t,TAL_2,2,0,vsize - 1);
1149 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
1150 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
1151 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
1152 tab_hline(self->t,TAL_1,5,6, 2);
1153 tab_vline(self->t,TAL_0,6,0,1);
1155 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
1156 _("%g%% Confidence Interval of the Difference"),
1157 cmd->criteria*100.0);
1159 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
1160 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1161 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
1162 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1163 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1164 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
1165 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
1166 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1169 /* Populate the paired samples trbox */
1171 trbox_paired_populate(struct trbox *trb,
1172 struct cmd_t_test *cmd UNUSED)
1176 for (i=0; i < n_pairs; ++i)
1181 double n = pairs[i].n;
1185 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
1187 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
1188 pairs[i].v[0]->name, pairs[i].v[1]->name);
1190 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
1192 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
1195 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
1196 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
1198 /* Now work out the confidence interval */
1199 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1201 t = gsl_cdf_tdist_Qinv(q, df);
1203 tab_float(trb->t, 5, i+3, TAB_RIGHT,
1204 pairs[i].mean_diff - t * se_mean , 8, 4);
1206 tab_float(trb->t, 6, i+3, TAB_RIGHT,
1207 pairs[i].mean_diff + t * se_mean , 8, 4);
1209 t = (pairs[i].mean[0] - pairs[i].mean[1])
1211 ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
1212 2 * pairs[i].correlation *
1213 pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
1217 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
1219 /* Degrees of freedom */
1220 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
1222 p = gsl_cdf_tdist_P(t,df);
1223 q = gsl_cdf_tdist_P(t,df);
1225 tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
1230 /* Initialize the one sample trbox */
1232 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1235 const int vsize=cmd->n_variables+3;
1237 self->populate = trbox_one_sample_populate;
1239 trbox_base_init(self, cmd->n_variables,hsize);
1240 tab_title (self->t, 0, _("One-Sample Test"));
1241 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1242 tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
1244 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1245 _("Test Value = %f"), cmd->n_testval[0]);
1247 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1250 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1251 _("%g%% Confidence Interval of the Difference"),
1252 cmd->criteria*100.0);
1254 tab_vline(self->t,TAL_0,6,1,1);
1255 tab_hline(self->t,TAL_1,5,6,2);
1256 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1257 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1258 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1259 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1260 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1261 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1266 /* Populate the one sample trbox */
1268 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1274 for (i=0; i < cmd->n_variables; ++i)
1279 struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
1282 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1284 t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
1286 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1288 /* degrees of freedom */
1291 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1293 p = gsl_cdf_tdist_P(t, df);
1294 q = gsl_cdf_tdist_Q(t, df);
1296 /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
1298 tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
1300 tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
1303 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1304 t = gsl_cdf_tdist_Qinv(q, df);
1306 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1307 gs->mean_diff - t * gs->se_mean, 8,4);
1309 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1310 gs->mean_diff + t * gs->se_mean, 8,4);
1314 /* Base initializer for the generalized trbox */
1316 trbox_base_init(struct trbox *self, int data_rows, int cols)
1318 const int rows = 3 + data_rows;
1320 self->finalize = trbox_base_finalize;
1321 self->t = tab_create (cols, rows, 0);
1322 tab_headers (self->t,0,0,3,0);
1323 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1324 tab_hline(self->t, TAL_2,0,cols-1,3);
1325 tab_dim (self->t, tab_natural_dimensions);
1329 /* Base finalizer for the trbox */
1331 trbox_base_finalize(struct trbox *trb)
1337 /* Create , populate and submit the Paired Samples Correlation box */
1341 const int rows=1+n_pairs;
1345 struct tab_table *table;
1347 table = tab_create (cols,rows,0);
1349 tab_columns (table, SOM_COL_DOWN, 1);
1350 tab_headers (table,0,0,1,0);
1351 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1352 tab_hline(table, TAL_2, 0, cols - 1, 1);
1353 tab_vline(table, TAL_2, 2, 0, rows - 1);
1354 tab_dim(table, tab_natural_dimensions);
1355 tab_title(table, 0, _("Paired Samples Correlations"));
1357 /* column headings */
1358 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1359 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1360 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1362 for (i=0; i < n_pairs; ++i)
1366 double df = pairs[i].n -2;
1368 double correlation_t =
1369 pairs[i].correlation * sqrt(df) /
1370 sqrt(1 - pow2(pairs[i].correlation));
1374 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1377 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1378 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1382 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
1383 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1385 p = gsl_cdf_tdist_P(correlation_t, df);
1386 q = gsl_cdf_tdist_Q(correlation_t, df);
1388 tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
1397 /* Calculation Implementation */
1399 /* Per case calculations common to all variants of the T test */
1401 common_calc (const struct ccase *c, void *_cmd)
1404 struct cmd_t_test *cmd = (struct cmd_t_test *)_cmd;
1406 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1409 /* Skip the entire case if /MISSING=LISTWISE is set */
1410 if ( cmd->miss == TTS_LISTWISE )
1412 for(i=0; i< cmd->n_variables ; ++i)
1414 struct variable *v = cmd->v_variables[i];
1415 const union value *val = case_data (c, v->fv);
1417 if (value_is_missing(val,v) )
1424 /* Listwise has to be implicit if the independent variable is missing ?? */
1425 if ( cmd->sbc_groups )
1427 const union value *gv = case_data (c, indep_var->fv);
1428 if ( value_is_missing(gv,indep_var) )
1435 for(i=0; i< cmd->n_variables ; ++i)
1437 struct group_statistics *gs;
1438 struct variable *v = cmd->v_variables[i];
1439 const union value *val = case_data (c, v->fv);
1441 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1443 if (! value_is_missing(val,v) )
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 )
1478 for(i=0; i< cmd->n_variables ; ++i)
1480 struct group_statistics *gs;
1481 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1483 gs->mean=gs->sum / gs->n;
1484 gs->s_std_dev= sqrt(
1485 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1490 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1493 gs->se_mean = gs->std_dev / sqrt(gs->n);
1494 gs->mean_diff= gs->sum_diff / gs->n;
1498 /* Per case calculations for one sample t test */
1500 one_sample_calc (const struct ccase *c, void *cmd_)
1503 struct cmd_t_test *cmd = (struct cmd_t_test *)cmd_;
1506 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1508 /* Skip the entire case if /MISSING=LISTWISE is set */
1509 if ( cmd->miss == TTS_LISTWISE )
1511 for(i=0; i< cmd->n_variables ; ++i)
1513 struct variable *v = cmd->v_variables[i];
1514 const union value *val = case_data (c, v->fv);
1516 if (value_is_missing(val,v) )
1523 for(i=0; i< cmd->n_variables ; ++i)
1525 struct group_statistics *gs;
1526 struct variable *v = cmd->v_variables[i];
1527 const union value *val = case_data (c, v->fv);
1529 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1531 if ( ! value_is_missing(val,v))
1532 gs->sum_diff += weight * (val->f - cmd->n_testval[0]);
1538 /* Pre calculations for one sample t test */
1540 one_sample_precalc ( struct cmd_t_test *cmd )
1544 for(i=0; i< cmd->n_variables ; ++i)
1546 struct group_statistics *gs;
1547 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1553 /* Post calculations for one sample t test */
1555 one_sample_postcalc (struct cmd_t_test *cmd)
1559 for(i=0; i< cmd->n_variables ; ++i)
1561 struct group_statistics *gs;
1562 gs= &group_proc_get (cmd->v_variables[i])->ugs;
1564 gs->mean_diff = gs->sum_diff / gs->n ;
1571 paired_precalc (struct cmd_t_test *cmd UNUSED)
1575 for(i=0; i < n_pairs ; ++i )
1578 pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
1579 pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
1580 pairs[i].sum_of_prod = 0;
1581 pairs[i].correlation = 0;
1582 pairs[i].sum_of_diffs = 0;
1583 pairs[i].ssq_diffs = 0;
1590 paired_calc (const struct ccase *c, void *cmd_)
1594 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1596 double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1598 /* Skip the entire case if /MISSING=LISTWISE is set ,
1599 AND one member of a pair is missing */
1600 if ( cmd->miss == TTS_LISTWISE )
1602 for(i=0; i < n_pairs ; ++i )
1604 struct variable *v0 = pairs[i].v[0];
1605 struct variable *v1 = pairs[i].v[1];
1607 const union value *val0 = case_data (c, v0->fv);
1608 const union value *val1 = case_data (c, v1->fv);
1610 if ( value_is_missing(val0,v0) ||
1611 value_is_missing(val1,v1) )
1618 for(i=0; i < n_pairs ; ++i )
1620 struct variable *v0 = pairs[i].v[0];
1621 struct variable *v1 = pairs[i].v[1];
1623 const union value *val0 = case_data (c, v0->fv);
1624 const union value *val1 = case_data (c, v1->fv);
1626 if ( ( !value_is_missing(val0,v0) && !value_is_missing(val1,v1) ) )
1628 pairs[i].n += weight;
1629 pairs[i].sum[0] += weight * val0->f;
1630 pairs[i].sum[1] += weight * val1->f;
1632 pairs[i].ssq[0] += weight * pow2(val0->f);
1633 pairs[i].ssq[1] += weight * pow2(val1->f);
1635 pairs[i].sum_of_prod += weight * val0->f * val1->f ;
1637 pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
1638 pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
1646 paired_postcalc (struct cmd_t_test *cmd UNUSED)
1650 for(i=0; i < n_pairs ; ++i )
1653 const double n = pairs[i].n;
1655 for (j=0; j < 2 ; ++j)
1657 pairs[i].mean[j] = pairs[i].sum[j] / n ;
1658 pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
1659 pow2(pairs[i].mean[j]))
1662 pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
1663 pow2(pairs[i].mean[j]))
1667 pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
1668 pairs[i].mean[0] * pairs[i].mean[1] ;
1669 /* correlation now actually contains the covariance */
1671 pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
1672 pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
1674 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1676 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1677 ( pairs[i].ssq_diffs / n )
1679 pow2(pairs[i].mean_diff )
1685 group_precalc (struct cmd_t_test *cmd )
1690 for(i=0; i< cmd->n_variables ; ++i)
1692 struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
1694 /* There's always 2 groups for a T - TEST */
1697 gp.indep_width = indep_var->width;
1699 ttpr->group_hash = hsh_create(2,
1700 (hsh_compare_func *) compare_group_binary,
1701 (hsh_hash_func *) hash_group_binary,
1702 (hsh_free_func *) free_group,
1705 for (j=0 ; j < 2 ; ++j)
1708 struct group_statistics *gs = (struct group_statistics *)
1709 xmalloc (sizeof(struct group_statistics));
1715 if ( gp.criterion == CMP_EQ )
1717 gs->id = gp.v.g_value[j];
1722 gs->id.f = gp.v.critical_value - 1.0 ;
1724 gs->id.f = gp.v.critical_value + 1.0 ;
1727 hsh_insert ( ttpr->group_hash, (void *) gs );
1735 group_calc (const struct ccase *c, struct cmd_t_test *cmd)
1739 const union value *gv = case_data (c, indep_var->fv);
1741 const double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
1743 if ( value_is_missing(gv,indep_var) )
1748 if ( cmd->miss == TTS_LISTWISE )
1750 for(i=0; i< cmd->n_variables ; ++i)
1752 struct variable *v = cmd->v_variables[i];
1753 const union value *val = case_data (c, v->fv);
1755 if (value_is_missing(val,v) )
1762 gv = case_data (c, indep_var->fv);
1764 for(i=0; i< cmd->n_variables ; ++i)
1766 struct variable *var = cmd->v_variables[i];
1767 const union value *val = case_data (c, var->fv);
1768 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1769 struct group_statistics *gs;
1771 gs = hsh_find(grp_hash, (void *) gv);
1773 /* If the independent variable doesn't match either of the values
1774 for this case then move on to the next case */
1778 if ( !value_is_missing(val,var) )
1781 gs->sum+=weight * val->f;
1782 gs->ssq+=weight * pow2(val->f);
1791 group_postcalc ( struct cmd_t_test *cmd )
1795 for(i=0; i< cmd->n_variables ; ++i)
1797 struct variable *var = cmd->v_variables[i];
1798 struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
1799 struct hsh_iterator g;
1800 struct group_statistics *gs;
1803 for (gs = hsh_first (grp_hash,&g);
1805 gs = hsh_next(grp_hash,&g))
1807 gs->mean = gs->sum / gs->n;
1809 gs->s_std_dev= sqrt(
1810 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1815 ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1818 gs->se_mean = gs->std_dev / sqrt(gs->n);
1828 calculate(const struct casefile *cf, void *cmd_)
1830 struct ssbox stat_summary_box;
1831 struct trbox test_results_box;
1833 struct casereader *r;
1836 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
1838 common_precalc(cmd);
1839 for(r = casefile_get_reader (cf);
1840 casereader_read (r, &c) ;
1843 common_calc(&c,cmd);
1845 casereader_destroy (r);
1846 common_postcalc(cmd);
1851 one_sample_precalc(cmd);
1852 for(r = casefile_get_reader (cf);
1853 casereader_read (r, &c) ;
1856 one_sample_calc(&c,cmd);
1858 casereader_destroy (r);
1859 one_sample_postcalc(cmd);
1863 paired_precalc(cmd);
1864 for(r = casefile_get_reader (cf);
1865 casereader_read (r, &c) ;
1868 paired_calc(&c,cmd);
1870 casereader_destroy (r);
1871 paired_postcalc(cmd);
1877 for(r = casefile_get_reader (cf);
1878 casereader_read (r, &c) ;
1883 casereader_destroy (r);
1884 group_postcalc(cmd);
1886 levene(cf, indep_var, cmd->n_variables, cmd->v_variables,
1887 (cmd->miss == TTS_LISTWISE)?LEV_LISTWISE:LEV_ANALYSIS ,
1892 ssbox_create(&stat_summary_box,cmd,mode);
1893 ssbox_populate(&stat_summary_box,cmd);
1894 ssbox_finalize(&stat_summary_box);
1896 if ( mode == T_PAIRED)
1899 trbox_create(&test_results_box,cmd,mode);
1900 trbox_populate(&test_results_box,cmd);
1901 trbox_finalize(&test_results_box);
1905 short which_group(const struct group_statistics *g,
1906 const struct group_properties *p);
1908 /* Return -1 if the id of a is less than b; +1 if greater than and
1911 compare_group_binary(const struct group_statistics *a,
1912 const struct group_statistics *b,
1913 const struct group_properties *p)
1918 if ( p->criterion == CMP_LE )
1920 /* less-than-or-equal comparision is not meaningfull for
1921 alpha variables, so we shouldn't ever arrive here */
1922 assert(p->indep_width == 0 ) ;
1924 flag_a = ( a->id.f < p->v.critical_value ) ;
1925 flag_b = ( b->id.f < p->v.critical_value ) ;
1929 flag_a = which_group(a, p);
1930 flag_b = which_group(b, p);
1933 if (flag_a < flag_b )
1936 return (flag_a > flag_b);
1939 /* This is a degenerate case of a hash, since it can only return three possible
1940 values. It's really a comparison, being used as a hash function */
1943 hash_group_binary(const struct group_statistics *g,
1944 const struct group_properties *p)
1948 if ( p->criterion == CMP_LE )
1950 /* Not meaningfull to do a less than compare for alpha values ? */
1951 assert(p->indep_width == 0 ) ;
1952 flag = ( g->id.f < p->v.critical_value ) ;
1954 else if ( p->criterion == CMP_EQ)
1956 flag = which_group(g,p);
1964 /* return 0 if G belongs to group 0,
1965 1 if it belongs to group 1,
1966 2 if it belongs to neither group */
1968 which_group(const struct group_statistics *g,
1969 const struct group_properties *p)
1972 if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
1975 if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))