+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ gs= &group_proc_get (cmd->v_variables[i])->ugs;
+
+ gs->mean_diff = gs->sum_diff / gs->n ;
+ }
+}
+
+
+
+static void
+paired_precalc (struct cmd_t_test *cmd UNUSED)
+{
+ int i;
+
+ for(i=0; i < n_pairs ; ++i )
+ {
+ pairs[i].n = 0;
+ pairs[i].sum[0] = 0; pairs[i].sum[1] = 0;
+ pairs[i].ssq[0] = 0; pairs[i].ssq[1] = 0;
+ pairs[i].sum_of_prod = 0;
+ pairs[i].correlation = 0;
+ pairs[i].sum_of_diffs = 0;
+ pairs[i].ssq_diffs = 0;
+ }
+
+}
+
+
+static int
+paired_calc (const struct ccase *c, void *cmd_)
+{
+ int i;
+
+ struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
+
+ double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
+
+ /* Skip the entire case if /MISSING=LISTWISE is set ,
+ AND one member of a pair is missing */
+ if ( cmd->miss == TTS_LISTWISE )
+ {
+ for(i=0; i < n_pairs ; ++i )
+ {
+ struct variable *v0 = pairs[i].v[0];
+ struct variable *v1 = pairs[i].v[1];
+
+ const union value *val0 = case_data (c, v0->fv);
+ const union value *val1 = case_data (c, v1->fv);
+
+ if ( value_is_missing(&v0->miss, val0) ||
+ value_is_missing(&v1->miss, val1) )
+ {
+ return 0;
+ }
+ }
+ }
+
+ for(i=0; i < n_pairs ; ++i )
+ {
+ struct variable *v0 = pairs[i].v[0];
+ struct variable *v1 = pairs[i].v[1];
+
+ const union value *val0 = case_data (c, v0->fv);
+ const union value *val1 = case_data (c, v1->fv);
+
+ if ( ( !value_is_missing(&v0->miss, val0)
+ && !value_is_missing(&v1->miss, val1) ) )
+ {
+ pairs[i].n += weight;
+ pairs[i].sum[0] += weight * val0->f;
+ pairs[i].sum[1] += weight * val1->f;
+
+ pairs[i].ssq[0] += weight * pow2(val0->f);
+ pairs[i].ssq[1] += weight * pow2(val1->f);
+
+ pairs[i].sum_of_prod += weight * val0->f * val1->f ;
+
+ pairs[i].sum_of_diffs += weight * ( val0->f - val1->f ) ;
+ pairs[i].ssq_diffs += weight * pow2(val0->f - val1->f);
+ }
+ }
+
+ return 0;
+}
+
+static void
+paired_postcalc (struct cmd_t_test *cmd UNUSED)
+{
+ int i;
+
+ for(i=0; i < n_pairs ; ++i )
+ {
+ int j;
+ const double n = pairs[i].n;
+
+ for (j=0; j < 2 ; ++j)
+ {
+ pairs[i].mean[j] = pairs[i].sum[j] / n ;
+ pairs[i].s_std_dev[j] = sqrt((pairs[i].ssq[j] / n -
+ pow2(pairs[i].mean[j]))
+ );
+
+ pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
+ pow2(pairs[i].mean[j]))
+ );
+ }
+
+ pairs[i].correlation = pairs[i].sum_of_prod / pairs[i].n -
+ pairs[i].mean[0] * pairs[i].mean[1] ;
+ /* correlation now actually contains the covariance */
+
+ pairs[i].correlation /= pairs[i].std_dev[0] * pairs[i].std_dev[1];
+ pairs[i].correlation *= pairs[i].n / ( pairs[i].n - 1 );
+
+ pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
+
+ pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
+ ( pairs[i].ssq_diffs / n )
+ -
+ pow2(pairs[i].mean_diff )
+ ) );
+ }
+}
+
+static void
+group_precalc (struct cmd_t_test *cmd )
+{
+ int i;
+ int j;
+
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct group_proc *ttpr = group_proc_get (cmd->v_variables[i]);
+
+ /* There's always 2 groups for a T - TEST */
+ ttpr->n_groups = 2;
+
+ gp.indep_width = indep_var->width;
+
+ ttpr->group_hash = hsh_create(2,
+ (hsh_compare_func *) compare_group_binary,
+ (hsh_hash_func *) hash_group_binary,
+ (hsh_free_func *) free_group,
+ (void *) &gp );
+
+ for (j=0 ; j < 2 ; ++j)
+ {
+
+ struct group_statistics *gs = xmalloc (sizeof *gs);
+
+ gs->sum = 0;
+ gs->n = 0;
+ gs->ssq = 0;
+
+ if ( gp.criterion == CMP_EQ )
+ {
+ gs->id = gp.v.g_value[j];
+ }
+ else
+ {
+ if ( j == 0 )
+ gs->id.f = gp.v.critical_value - 1.0 ;
+ else
+ gs->id.f = gp.v.critical_value + 1.0 ;
+ }
+
+ hsh_insert ( ttpr->group_hash, (void *) gs );
+
+ }
+ }
+
+}
+
+static int
+group_calc (const struct ccase *c, struct cmd_t_test *cmd)
+{
+ int i;
+
+ const union value *gv = case_data (c, indep_var->fv);
+
+ const double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
+
+ if ( value_is_missing(&indep_var->miss, gv) )
+ {
+ return 0;
+ }
+
+ if ( cmd->miss == TTS_LISTWISE )
+ {
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct variable *v = cmd->v_variables[i];
+ const union value *val = case_data (c, v->fv);
+
+ if (value_is_missing(&v->miss, val) )
+ {
+ return 0;
+ }
+ }
+ }
+
+ gv = case_data (c, indep_var->fv);
+
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct variable *var = cmd->v_variables[i];
+ const union value *val = case_data (c, var->fv);
+ struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
+ struct group_statistics *gs;
+
+ gs = hsh_find(grp_hash, (void *) gv);
+
+ /* If the independent variable doesn't match either of the values
+ for this case then move on to the next case */
+ if ( ! gs )
+ return 0;
+
+ if ( !value_is_missing(&var->miss, val) )
+ {
+ gs->n+=weight;
+ gs->sum+=weight * val->f;
+ gs->ssq+=weight * pow2(val->f);
+ }
+ }
+
+ return 0;
+}
+
+
+static void
+group_postcalc ( struct cmd_t_test *cmd )
+{
+ int i;
+
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct variable *var = cmd->v_variables[i];
+ struct hsh_table *grp_hash = group_proc_get (var)->group_hash;
+ struct hsh_iterator g;
+ struct group_statistics *gs;
+ int count=0;
+
+ for (gs = hsh_first (grp_hash,&g);
+ gs != 0;
+ gs = hsh_next(grp_hash,&g))
+ {
+ gs->mean = gs->sum / gs->n;
+
+ gs->s_std_dev= sqrt(
+ ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
+ ) ;
+
+ gs->std_dev= sqrt(
+ gs->n/(gs->n-1) *
+ ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
+ ) ;
+
+ gs->se_mean = gs->std_dev / sqrt(gs->n);
+ count ++;
+ }
+ assert(count == 2);
+ }
+}
+
+
+
+static void
+calculate(const struct casefile *cf, void *cmd_)
+{
+ struct ssbox stat_summary_box;
+ struct trbox test_results_box;
+
+ struct casereader *r;
+ struct ccase c;
+
+ struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
+
+ common_precalc(cmd);
+ for(r = casefile_get_reader (cf);
+ casereader_read (r, &c) ;
+ case_destroy (&c))