+
+
+/* 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)
+ {
+ double p,q;
+
+ double df = pairs[i].n -2;
+
+ double correlation_t =
+ pairs[i].correlation * sqrt(df) /
+ sqrt(1 - pow2(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, 2, i+1, TAB_RIGHT, pairs[i].n, 4, 0);
+ tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
+
+ p = gsl_cdf_tdist_P(correlation_t, df);
+ q = gsl_cdf_tdist_Q(correlation_t, df);
+
+ 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 (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 */
+ if ( cmd->miss == TTS_LISTWISE )
+ {
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct variable *v = cmd->v_variables[i];
+ const union value *val = &c->data[v->fv];
+
+ if (value_is_missing(val,v) )
+ {
+ return 0;
+ }
+ }
+ }
+
+ /* Listwise has to be implicit if the independent variable is missing ?? */
+ if ( cmd->sbc_groups )
+ {
+ const union value *gv = &c->data[indep_var->fv];
+ if ( value_is_missing(gv,indep_var) )
+ {
+ return 0;
+ }
+ }
+
+
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ struct variable *v = cmd->v_variables[i];
+ const union value *val = &c->data[v->fv];
+
+ gs= &cmd->v_variables[i]->p.t_t.ugs;
+
+ if (! value_is_missing(val,v) )
+ {
+ 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 ( struct cmd_t_test *cmd )
+{
+ 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 ( struct cmd_t_test *cmd )
+{
+ 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 (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 */
+ if ( cmd->miss == TTS_LISTWISE )
+ {
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct variable *v = cmd->v_variables[i];
+ const union value *val = &c->data[v->fv];
+
+ if (value_is_missing(val,v) )
+ {
+ return 0;
+ }
+ }
+ }
+
+ for(i=0; i< cmd->n_variables ; ++i)
+ {
+ struct group_statistics *gs;
+ struct variable *v = cmd->v_variables[i];
+ const union value *val = &c->data[v->fv];
+
+ gs= &cmd->v_variables[i]->p.t_t.ugs;
+
+ if ( ! value_is_missing(val,v))
+ gs->sum_diff += weight * (val->f - cmd->n_testval);
+ }
+
+ return 0;
+}
+
+/* Pre calculations for one sample t test */
+static void
+one_sample_precalc ( struct cmd_t_test *cmd )
+{
+ 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 (struct cmd_t_test *cmd)
+{
+ 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 (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 = &c->data[v0->fv];
+ const 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];
+
+ const union value *val0 = &c->data[v0->fv];
+ const 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 * 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 )
+ ) );
+ }
+}
+
+/* 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 (struct cmd_t_test *cmd )
+{
+ 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 (const struct ccase *c, struct cmd_t_test *cmd)
+{
+ int i;
+ int g;
+
+ const union value *gv = &c->data[indep_var->fv];
+
+ const double weight = dict_get_case_weight(default_dict,c,&bad_weight_warn);
+
+ 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];
+ const 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];
+
+ const union value *val=&c->data[var->fv];
+
+ if ( !value_is_missing(val,var) )
+ {
+ 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;
+ 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);
+ }
+ }
+}
+
+
+
+static void
+calculate(const struct casefile *cf, void *cmd_)
+{
+ struct ssbox stat_summary_box;
+ struct trbox test_results_box;
+
+ struct casereader *r;
+ const 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) ; )
+ {
+ common_calc(c,cmd);
+ }
+ casereader_destroy (r);
+ common_postcalc(cmd);
+
+ switch(mode)
+ {
+ case T_1_SAMPLE:
+ one_sample_precalc(cmd);
+ for(r = casefile_get_reader (cf);
+ casereader_read (r, &c) ; )
+ {
+ one_sample_calc(c,cmd);
+ }
+ casereader_destroy (r);
+ one_sample_postcalc(cmd);
+
+ break;
+ case T_PAIRED:
+ paired_precalc(cmd);
+ for(r = casefile_get_reader (cf);
+ casereader_read (r, &c) ; )
+ {
+ paired_calc(c,cmd);
+ }
+ casereader_destroy (r);
+ paired_postcalc(cmd);
+
+ break;
+ case T_IND_SAMPLES:
+
+ group_precalc(cmd);
+ for(r = casefile_get_reader (cf);
+ casereader_read (r, &c) ; )
+ {
+ group_calc(c,cmd);
+ }
+ casereader_destroy (r);
+ group_postcalc(cmd);
+
+
+ levene(cf, indep_var, cmd->n_variables, cmd->v_variables,
+ (cmd->miss == TTS_LISTWISE)?LEV_LISTWISE:LEV_ANALYSIS ,
+ value_is_missing);
+ break;
+ }
+
+ ssbox_create(&stat_summary_box,cmd,mode);
+ ssbox_populate(&stat_summary_box,cmd);
+ ssbox_finalize(&stat_summary_box);
+
+ if ( mode == T_PAIRED)
+ pscbox();
+
+ trbox_create(&test_results_box,cmd,mode);
+ trbox_populate(&test_results_box,cmd);
+ trbox_finalize(&test_results_box);
+
+}