+}
+
+/* Initialize the paired samples trbox */
+void
+trbox_paired_init(struct trbox *self,
+ struct cmd_t_test *cmd UNUSED)
+{
+
+ const int hsize=10;
+ const int vsize=n_pairs+3;
+
+ self->populate = trbox_paired_populate;
+
+ trbox_base_init(self,n_pairs,hsize);
+ tab_title (self->t, 0, _("Paired Samples Test"));
+ tab_hline(self->t,TAL_1,2,6,1);
+ tab_vline(self->t,TAL_2,2,0,vsize - 1);
+ tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
+ tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
+ tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
+ tab_hline(self->t,TAL_1,5,6, 2);
+ tab_vline(self->t,TAL_0,6,0,1);
+
+ tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
+ _("%g%% Confidence Interval of the Difference"),
+ cmd->criteria*100.0);
+
+ tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
+ tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
+ tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
+ tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
+ tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
+ tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
+ tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
+ tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
+}
+
+/* Populate the paired samples trbox */
+void
+trbox_paired_populate(struct trbox *trb,
+ struct cmd_t_test *cmd UNUSED)
+{
+ int i;
+
+ for (i=0; i < n_pairs; ++i)
+ {
+ double p,q;
+ double se_mean;
+
+ double n = pairs[i].n;
+ double t;
+ double df = n - 1;
+
+ tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
+
+ tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
+ pairs[i].v[0]->name, pairs[i].v[1]->name);
+
+ tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
+
+ tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
+
+ /* SE Mean */
+ se_mean = pairs[i].std_dev_diff / sqrt(n) ;
+ tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
+
+ /* Now work out the confidence interval */
+ q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
+
+ t = gsl_cdf_tdist_Qinv(q, df);
+
+ tab_float(trb->t, 5, i+3, TAB_RIGHT,
+ pairs[i].mean_diff - t * se_mean , 8, 4);
+
+ tab_float(trb->t, 6, i+3, TAB_RIGHT,
+ pairs[i].mean_diff + t * se_mean , 8, 4);
+
+ t = (pairs[i].mean[0] - pairs[i].mean[1])
+ / sqrt (
+ ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
+ 2 * pairs[i].correlation *
+ pairs[i].s_std_dev[0] * pairs[i].s_std_dev[1] )
+ / (n - 1)
+ );
+
+ tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
+
+ /* Degrees of freedom */
+ tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
+
+ p = gsl_cdf_tdist_P(t,df);
+ q = gsl_cdf_tdist_P(t,df);
+
+ tab_float(trb->t, 9, i+3, TAB_RIGHT, 2.0*(t>0?q:p) , 8, 3);
+
+ }
+}
+
+/* Initialize the one sample trbox */
+void
+trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
+{
+ const int hsize=7;
+ const int vsize=cmd->n_variables+3;
+
+ self->populate = trbox_one_sample_populate;
+
+ trbox_base_init(self, cmd->n_variables,hsize);
+ tab_title (self->t, 0, _("One-Sample Test"));
+ tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
+ tab_vline(self->t, TAL_2, 1, 0, vsize - 1);
+
+ tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
+ _("Test Value = %f"), cmd->n_testval[0]);
+
+ tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
+
+
+ tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
+ _("%g%% Confidence Interval of the Difference"),
+ cmd->criteria*100.0);
+
+ tab_vline(self->t,TAL_0,6,1,1);
+ tab_hline(self->t,TAL_1,5,6,2);
+ tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
+ tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
+ tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
+ tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
+ tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
+ tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
+
+}
+
+
+/* Populate the one sample trbox */
+void
+trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
+{
+ int i;
+
+ assert(trb->t);
+
+ for (i=0; i < cmd->n_variables; ++i)
+ {
+ double t;
+ double p,q;
+ double df;
+ struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
+
+
+ tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
+
+ t = (gs->mean - cmd->n_testval[0] ) * sqrt(gs->n) / gs->std_dev ;
+
+ tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
+
+ /* degrees of freedom */
+ df = gs->n - 1;
+
+ tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
+
+ p = gsl_cdf_tdist_P(t, df);
+ q = gsl_cdf_tdist_Q(t, df);
+
+ /* Multiply by 2 to get 2-tailed significance, makeing sure we've got
+ the correct tail*/
+ tab_float (trb->t, 3, i+3, TAB_RIGHT, 2.0*(t>0?q:p), 8,3);
+
+ tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
+
+
+ q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
+ t = gsl_cdf_tdist_Qinv(q, df);
+
+ tab_float (trb->t, 5, i+3, TAB_RIGHT,
+ gs->mean_diff - t * gs->se_mean, 8,4);
+
+ tab_float (trb->t, 6, i+3, TAB_RIGHT,
+ gs->mean_diff + t * gs->se_mean, 8,4);
+ }
+}
+
+/* Base initializer for the generalized trbox */
+void
+trbox_base_init(struct trbox *self, size_t data_rows, int cols)
+{
+ const size_t rows = 3 + data_rows;
+
+ self->finalize = trbox_base_finalize;
+ self->t = tab_create (cols, rows, 0);
+ tab_headers (self->t,0,0,3,0);
+ tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
+ tab_hline(self->t, TAL_2,0,cols-1,3);
+ tab_dim (self->t, tab_natural_dimensions);
+}
+
+
+/* Base finalizer for the trbox */
+void
+trbox_base_finalize(struct trbox *trb)
+{
+ tab_submit(trb->t);
+}
+
+
+/* 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 = case_data (c, v->fv);
+
+ if (value_is_missing(&v->miss, val) )
+ {
+ return 0;
+ }
+ }
+ }