+
+
+/* Create , populate and submit the Paired Samples Correlation box */
+void
+pscbox(void)
+{
+ const int rows=1+n_pairs;
+ const int cols=5;
+ int i;
+
+ struct tab_table *table;
+
+ table = tab_create (cols,rows,0);
+
+ tab_columns (table, SOM_COL_DOWN, 1);
+ tab_headers (table,0,0,1,0);
+ tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
+ tab_hline(table, TAL_2, 0, cols - 1, 1);
+ tab_vline(table, TAL_2, 2, 0, rows - 1);
+ tab_dim(table, tab_natural_dimensions);
+ tab_title(table, 0, _("Paired Samples Correlations"));
+
+ /* column headings */
+ tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
+ tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
+ tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
+
+ for (i=0; i < n_pairs; ++i)
+ {
+ int which =1;
+ double p,q;
+
+ int status;
+ double bound;
+
+ double df = pairs[i].v[0]->p.t_t.ugs.n -2;
+
+ double correlation_t =
+ pairs[i].correlation * sqrt(df) /
+ sqrt(1 - sqr(pairs[i].correlation));
+
+
+ /* row headings */
+ tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
+ _("Pair %d"), i);
+
+ tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
+ _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
+
+
+ /* row data */
+ tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
+ tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.ugs.n , 4, 0);
+
+
+ cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
+
+ if ( 0 != status )
+ {
+ msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
+ }
+
+
+ tab_float(table, 4, i+1, TAB_RIGHT, 2.0*(correlation_t>0?q:p), 8, 3);
+
+ }
+
+ tab_submit(table);
+}
+
+
+
+/* Calculation Implementation */
+
+/* Per case calculations common to all variants of the T test */
+static int
+common_calc (struct ccase *c, void *aux UNUSED)
+{
+ int i;
+
+ double weight = dict_get_case_weight(default_dict,c);
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ struct variable *v = cmd.v_variables[i];
+ union value *val = &c->data[v->fv];
+
+ gs= &cmd.v_variables[i]->p.t_t.ugs;
+
+ if (val->f != SYSMIS)
+ {
+ gs->n+=weight;
+ gs->sum+=weight * val->f;
+ gs->ssq+=weight * val->f * val->f;
+ }
+ }
+ return 0;
+}
+
+/* Pre calculations common to all variants of the T test */
+static void
+common_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=0;
+ gs->n=0;
+ gs->ssq=0;
+ gs->sum_diff=0;
+ }
+}
+
+/* Post calculations common to all variants of the T test */
+void
+common_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=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);
+ gs->mean_diff= gs->sum_diff / gs->n;
+ }
+}
+
+/* Per case calculations for one sample t test */
+static int
+one_sample_calc (struct ccase *c, void *aux UNUSED)
+{
+ int i;
+
+ double weight = dict_get_case_weight(default_dict,c);
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ struct variable *v = cmd.v_variables[i];
+ union value *val = &c->data[v->fv];
+
+ gs= &cmd.v_variables[i]->p.t_t.ugs;
+
+ if (val->f != SYSMIS)
+ gs->sum_diff += weight * (val->f - cmd.n_testval);
+ }
+
+ return 0;
+}
+
+/* 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].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;
+
+ 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];
+
+ pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.ugs.mean )
+ *
+ ( val1->f - pairs[i].v[1]->p.t_t.ugs.mean );
+
+ pairs[i].sum_of_diffs += val0->f - val1->f ;
+ pairs[i].ssq_diffs += sqr(val0->f - val1->f);
+
+ }
+
+ return 0;
+}
+
+static void
+paired_postcalc (void *aux UNUSED)
+{
+ int i;
+
+ for(i=0; i < n_pairs ; ++i )
+ {
+ const double n = pairs[i].v[0]->p.t_t.ugs.n ;
+
+ 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 /= pairs[i].v[0]->p.t_t.ugs.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 )
+ -
+ sqr(pairs[i].mean_diff )
+ ) );
+ }
+}
+
+static int
+get_group(const union value *val, struct variable *var)
+{
+ if ( 0 == compare_values(val,&groups_values[0],var->width) )
+ return 0;
+ else if (0 == compare_values(val,&groups_values[1],var->width) )
+ return 1;
+
+ /* Never reached */
+ assert(0);
+ 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;
+ ttpr->gs[j].id = groups_values[j];
+ }
+ }
+
+}
+
+static int
+group_calc (struct ccase *c, void *aux UNUSED)
+{
+ int i;
+ union value *gv = &c->data[groups->fv];
+
+ double weight = dict_get_case_weight(default_dict,c);
+
+ gv = &c->data[groups->fv];
+
+ for(i=0; i< cmd.n_variables ; ++i)
+ {
+ int g = get_group(gv,groups);
+
+ struct group_statistics *gs = &cmd.v_variables[i]->p.t_t.gs[g];
+
+ union value *val=&c->data[cmd.v_variables[i]->fv];
+
+ 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);
+ }
+ }
+}
+