+
+/* Pre calculations for one sample t test */
+static void
+one_sample_precalc (void *aux UNUSED)
+{
+ int i=0;
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ gs= &cmd.v_variables[i]->p.t_t.ugs;
+
+ gs->sum_diff=0;
+ }
+}
+
+/* Post calculations for one sample t test */
+static void
+one_sample_postcalc (void *aux UNUSED)
+{
+ int i=0;
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ gs= &cmd.v_variables[i]->p.t_t.ugs;
+
+
+ gs->mean_diff = gs->sum_diff / gs->n ;
+ }
+}
+
+
+
+static int
+compare_var_name (const void *a_, const void *b_, void *v_ UNUSED)
+{
+ const struct variable *a = a_;
+ const struct variable *b = b_;
+
+ return strcmp(a->name,b->name);
+}
+
+static unsigned
+hash_var_name (const void *a_, void *v_ UNUSED)
+{
+ const struct variable *a = a_;
+
+ return hsh_hash_bytes (a->name, strlen(a->name));
+}
+
+
+
+static void
+paired_precalc (void *aux 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 (struct ccase *c, void *aux UNUSED)
+{
+ int i;
+
+ double weight = dict_get_case_weight(default_dict,c);
+
+ /* 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];
+
+ union value *val0 = &c->data[v0->fv];
+ union value *val1 = &c->data[v1->fv];
+
+ if ( value_is_missing(val0,v0) ||
+ value_is_missing(val1,v1) )
+ {
+ return 0;
+ }
+ }
+ }
+
+ for(i=0; i < n_pairs ; ++i )
+ {
+ struct variable *v0 = pairs[i].v[0];
+ struct variable *v1 = pairs[i].v[1];
+
+ union value *val0 = &c->data[v0->fv];
+ union value *val1 = &c->data[v1->fv];
+
+ if ( ( !value_is_missing(val0,v0) && !value_is_missing(val1,v1) ) )
+ {
+ pairs[i].n += weight;
+ pairs[i].sum[0] += weight * val0->f;
+ pairs[i].sum[1] += weight * val1->f;
+
+ pairs[i].ssq[0] += weight * sqr(val0->f);
+ pairs[i].ssq[1] += weight * sqr(val1->f);
+
+#if 0
+ pairs[i].correlation += weight *
+ ( val0->f - pairs[i].v[0]->p.t_t.ugs.mean )
+ *
+ ( val1->f - pairs[i].v[1]->p.t_t.ugs.mean );
+#endif
+
+ 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 * sqr(val0->f - val1->f);
+ }
+ }
+
+ return 0;
+}
+
+static void
+paired_postcalc (void *aux 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 -
+ sqr(pairs[i].mean[j]))
+ );
+
+ pairs[i].std_dev[j] = sqrt(n/(n-1)*(pairs[i].ssq[j] / n -
+ sqr(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 );
+
+#if 0
+ pairs[i].correlation /= pairs[i].v[0]->p.t_t.ugs.std_dev *
+ pairs[i].v[1]->p.t_t.ugs.std_dev ;
+ pairs[i].correlation /= n - 1;
+#endif
+
+
+ pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
+
+
+ pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
+ ( pairs[i].ssq_diffs / n )
+ -
+ sqr(pairs[i].mean_diff )
+ ) );
+ }
+}
+
+/* Return the group # corresponding to the
+ independent variable with the value val
+*/
+static int
+get_group(const union value *val, struct variable *indep)
+{
+ int i;
+
+ for (i = 0; i < 2 ; ++i )
+ {
+ const int cmp = compare_values(val,&groups_values[i],indep->width) ;
+ switch ( criteria[i])
+ {
+ case CMP_EQ:
+ if ( 0 == cmp ) return i;
+ break;
+ case CMP_LT:
+ if ( 0 > cmp ) return i;
+ break;
+ case CMP_LE:
+ if ( cmp <= 0 ) return i;
+ break;
+ case CMP_GT:
+ if ( cmp > 0 ) return i;
+ break;
+ case CMP_GE:
+ if ( cmp >= 0 ) return i;
+ break;
+ default:
+ assert(0);
+ };
+ }
+
+ /* No groups matched */
+ return -1;
+}
+
+
+static void
+group_precalc (void *aux UNUSED)
+{
+ int i;
+ int j;
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct t_test_proc *ttpr = &cmd.v_variables[i]->p.t_t;
+
+ /* There's always 2 groups for a T - TEST */
+ ttpr->n_groups = 2;
+ ttpr->gs = xmalloc(sizeof(struct group_statistics) * 2) ;
+
+ for (j=0 ; j < 2 ; ++j)
+ {
+ ttpr->gs[j].sum = 0;
+ ttpr->gs[j].n = 0;
+ ttpr->gs[j].ssq = 0;
+
+ if ( n_group_values == 2 )
+ ttpr->gs[j].id = groups_values[j];
+ else
+ ttpr->gs[j].id = groups_values[0];
+ ttpr->gs[j].criterion = criteria[j];
+ }
+ }
+
+}
+
+static int
+group_calc (struct ccase *c, void *aux UNUSED)
+{
+ int i;
+ int g;
+ union value *gv = &c->data[indep_var->fv];
+
+ double weight = dict_get_case_weight(default_dict,c);
+
+ if ( value_is_missing(gv,indep_var) )
+ {
+ return 0;
+ }
+
+ if ( cmd.miss == TTS_LISTWISE )
+ {
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct variable *v = cmd.v_variables[i];
+ union value *val = &c->data[v->fv];
+
+ if (value_is_missing(val,v) )
+ {
+ return 0;
+ }
+ }
+ }
+
+
+ gv = &c->data[indep_var->fv];
+
+ g = get_group(gv,indep_var);
+
+ /* If the independent variable doesn't match either of the values
+ for this case then move on to the next case */
+ if (g == -1 )
+ return 0;
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct variable *var = cmd.v_variables[i];
+
+ struct group_statistics *gs = &var->p.t_t.gs[g];
+
+ union value *val=&c->data[var->fv];
+
+ if ( !value_is_missing(val,var) )
+ {
+ gs->n+=weight;
+ gs->sum+=weight * val->f;
+ gs->ssq+=weight * sqr(val->f);
+ }
+ }
+
+ return 0;
+}
+
+
+static void
+group_postcalc (void *aux UNUSED)
+{
+ int i;
+ int j;
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ for (j=0 ; j < 2 ; ++j)
+ {
+ struct group_statistics *gs;
+ gs=&cmd.v_variables[i]->p.t_t.gs[j];
+
+ 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);
+ }
+ }
+}
+