output: Add auxiliary data parameter to tab_dim.
[pspp-builds.git] / src / language / stats / t-test.q
index bc0240b5f8ad88cb7deb0feebb255bb47d35c572..3affde209b9413d1b7d41172db41640c6a53aa1d 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -43,6 +43,7 @@
 #include <math/levene.h>
 #include <output/manager.h>
 #include <output/table.h>
+#include <data/format.h>
 
 #include "xalloc.h"
 
@@ -80,8 +81,8 @@ struct group_properties
   /* The comparison criterion */
   enum comparison criterion;
 
-  /* The width of the independent variable */
-  int indep_width ;
+  /* The independent variable */
+  struct variable *indep_var;
 
   union {
     /* The value of the independent variable at which groups are determined to
@@ -152,7 +153,8 @@ static int parse_value (struct lexer *lexer, union value * v, enum val_type);
 /* Structures and Functions for the Statistics Summary Box */
 struct ssbox;
 typedef void populate_ssbox_func (struct ssbox *ssb,
-                                           struct cmd_t_test *cmd);
+                                 const struct dictionary *,
+                                 struct cmd_t_test *cmd);
 typedef void finalize_ssbox_func (struct ssbox *ssb);
 
 struct ssbox
@@ -168,21 +170,23 @@ struct ssbox
 void ssbox_create (struct ssbox *ssb,   struct cmd_t_test *cmd, int mode);
 
 /* Populate a ssbox according to cmd */
-void ssbox_populate (struct ssbox *ssb, struct cmd_t_test *cmd);
+void ssbox_populate (struct ssbox *ssb, const struct dictionary *dict,
+                    struct cmd_t_test *cmd);
 
 /* Submit and destroy a ssbox */
 void ssbox_finalize (struct ssbox *ssb);
 
 /* A function to create, populate and submit the Paired Samples Correlation
    box */
-void pscbox (void);
+static void pscbox (const struct dictionary *);
 
 
 /* Structures and Functions for the Test Results Box */
 struct trbox;
 
 typedef void populate_trbox_func (struct trbox *trb,
-                                struct cmd_t_test *cmd);
+                                 const struct dictionary *dict,
+                                 struct cmd_t_test *cmd);
 typedef void finalize_trbox_func (struct trbox *trb);
 
 struct trbox {
@@ -195,7 +199,8 @@ struct trbox {
 void trbox_create (struct trbox *trb,   struct cmd_t_test *cmd, int mode);
 
 /* Populate a ssbox according to cmd */
-void trbox_populate (struct trbox *trb, struct cmd_t_test *cmd);
+static void trbox_populate (struct trbox *trb, const struct dictionary *dict,
+                    struct cmd_t_test *cmd);
 
 /* Submit and destroy a ssbox */
 void trbox_finalize (struct trbox *trb);
@@ -235,11 +240,6 @@ static void calculate (struct cmd_t_test *,
 
 static  int mode;
 
-static struct cmd_t_test cmd;
-
-static bool bad_weight_warn = false;
-
-
 static int compare_group_binary (const struct group_statistics *a,
                                const struct group_statistics *b,
                                const struct group_properties *p);
@@ -253,6 +253,7 @@ static unsigned  hash_group_binary (const struct group_statistics *g,
 int
 cmd_t_test (struct lexer *lexer, struct dataset *ds)
 {
+  struct cmd_t_test cmd;
   struct casegrouper *grouper;
   struct casereader *group;
   bool ok;
@@ -333,8 +334,6 @@ cmd_t_test (struct lexer *lexer, struct dataset *ds)
       return CMD_FAILURE;
     }
 
-  bad_weight_warn = true;
-
   /* Data pass. */
   grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
   while (casegrouper_get_next_group (grouper, &group))
@@ -362,7 +361,8 @@ cmd_t_test (struct lexer *lexer, struct dataset *ds)
 }
 
 static int
-tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, void *aux UNUSED)
+tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *cmd UNUSED, 
+       void *aux UNUSED)
 {
   int n_group_values=0;
 
@@ -403,7 +403,7 @@ tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *c
        }
     }
 
-  if (!parse_value (lexer, &gp.v.g_value[0], var_get_type (indep_var)))
+  if (!parse_value (lexer, &gp.v.g_value[0], var_get_width (indep_var)))
       return 0;
 
   lex_match (lexer, ',');
@@ -422,7 +422,7 @@ tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *c
       return 1;
     }
 
-  if (!parse_value (lexer, &gp.v.g_value[1], var_get_type (indep_var)))
+  if (!parse_value (lexer, &gp.v.g_value[1], var_get_width (indep_var)))
     return 0;
 
   n_group_values = 2;
@@ -435,6 +435,15 @@ tts_custom_groups (struct lexer *lexer, struct dataset *ds, struct cmd_t_test *c
     gp.criterion = CMP_LE ;
 
 
+  if ( var_is_alpha (indep_var))
+    {
+      buf_copy_rpad (gp.v.g_value [0].s, var_get_width (indep_var),
+                    gp.v.g_value [0].s, strlen (gp.v.g_value[0].s));
+
+      buf_copy_rpad (gp.v.g_value [1].s, var_get_width (indep_var),
+                    gp.v.g_value [1].s, strlen (gp.v.g_value[1].s));
+    }
+
   return 1;
 }
 
@@ -577,6 +586,7 @@ parse_value (struct lexer *lexer, union value * v, enum val_type type)
     {
       if (!lex_force_string (lexer))
        return 0;
+      memset  (v->s, ' ', MAX_SHORT_STRING);
       strncpy (v->s, ds_cstr (lex_tokstr (lexer)), ds_length (lex_tokstr (lexer)));
     }
 
@@ -626,9 +636,10 @@ ssbox_create (struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
 
 /* Despatcher for the populate method */
 void
-ssbox_populate (struct ssbox *ssb,struct cmd_t_test *cmd)
+ssbox_populate (struct ssbox *ssb, const struct dictionary *dict,
+               struct cmd_t_test *cmd)
 {
-  ssb->populate (ssb,cmd);
+  ssb->populate (ssb, dict, cmd);
 }
 
 
@@ -660,11 +671,12 @@ ssbox_base_init (struct ssbox *this, int cols,int rows)
   tab_headers (this->t,0,0,1,0);
   tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
   tab_hline (this->t, TAL_2,0,cols-1,1);
-  tab_dim (this->t, tab_natural_dimensions);
+  tab_dim (this->t, tab_natural_dimensions, NULL);
 }
 
 void  ssbox_one_sample_populate (struct ssbox *ssb,
-                             struct cmd_t_test *cmd);
+                                const struct dictionary *,
+                                struct cmd_t_test *cmd);
 
 /* Initialize the one_sample ssbox */
 void
@@ -685,8 +697,9 @@ ssbox_one_sample_init (struct ssbox *this,
   tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _ ("SE. Mean"));
 }
 
-void ssbox_independent_samples_populate (struct ssbox *ssb,
-                                       struct cmd_t_test *cmd);
+static void ssbox_independent_samples_populate (struct ssbox *ssb,
+                                               const struct dictionary *,
+                                               struct cmd_t_test *cmd);
 
 /* Initialize the independent samples ssbox */
 void
@@ -710,27 +723,37 @@ ssbox_independent_samples_init (struct ssbox *this,
 
 
 /* Populate the ssbox for independent samples */
-void
+static void
 ssbox_independent_samples_populate (struct ssbox *ssb,
-                             struct cmd_t_test *cmd)
+                                   const struct dictionary *dict,
+                                   struct cmd_t_test *cmd)
 {
   int i;
 
-  const char *val_lab0;
-  const char *val_lab1;
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
+
+  char *val_lab[2] = {NULL, NULL};
   double indep_value[2];
 
   char prefix[2][3]={"",""};
 
   if ( var_is_numeric (indep_var) )
     {
-      val_lab0 = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
-      val_lab1 = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
+      const char *s;
+
+      s = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
+      val_lab[0] = s ? xstrdup (s) : NULL;
+
+      s = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
+      val_lab[1] = s ? xstrdup (s) : NULL;
     }
   else
     {
-      val_lab0 = gp.v.g_value[0].s;
-      val_lab1 = gp.v.g_value[1].s;
+      val_lab[0] = xcalloc (sizeof (char), MAX_SHORT_STRING + 1);
+      val_lab[1] = xcalloc (sizeof (char), MAX_SHORT_STRING + 1);
+      memcpy (val_lab[0], gp.v.g_value[0].s, MAX_SHORT_STRING);
+      memcpy (val_lab[1], gp.v.g_value[1].s, MAX_SHORT_STRING);
     }
 
   if (gp.criterion == CMP_LE )
@@ -757,17 +780,17 @@ ssbox_independent_samples_populate (struct ssbox *ssb,
       tab_text (ssb->t, 0, i*2+1, TAB_LEFT,
                 var_get_name (cmd->v_variables[i]));
 
-      if (val_lab0)
+      if (val_lab[0])
        tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
-                 "%s%s", prefix[0], val_lab0);
+                 "%s%s", prefix[0], val_lab[0]);
       else
          tab_text (ssb->t, 1, i*2+1, TAB_LEFT | TAT_PRINTF,
                    "%s%g", prefix[0], indep_value[0]);
 
 
-      if (val_lab1)
+      if (val_lab[1])
        tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
-                 "%s%s", prefix[1], val_lab1);
+                 "%s%s", prefix[1], val_lab[1]);
       else
          tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT | TAT_PRINTF,
                    "%s%g", prefix[1], indep_value[1]);
@@ -801,17 +824,20 @@ ssbox_independent_samples_populate (struct ssbox *ssb,
          gs = hsh_find (grp_hash, (void *) &search_val);
          assert (gs);
 
-         tab_float (ssb->t, 2 ,i*2+count+1, TAB_RIGHT, gs->n, 10, 0);
-         tab_float (ssb->t, 3 ,i*2+count+1, TAB_RIGHT, gs->mean, 8, 2);
-         tab_float (ssb->t, 4 ,i*2+count+1, TAB_RIGHT, gs->std_dev, 8, 3);
-         tab_float (ssb->t, 5 ,i*2+count+1, TAB_RIGHT, gs->se_mean, 8, 3);
+         tab_double (ssb->t, 2, i*2+count+1, TAB_RIGHT, gs->n, wfmt);
+         tab_double (ssb->t, 3, i*2+count+1, TAB_RIGHT, gs->mean, NULL);
+         tab_double (ssb->t, 4, i*2+count+1, TAB_RIGHT, gs->std_dev, NULL);
+         tab_double (ssb->t, 5, i*2+count+1, TAB_RIGHT, gs->se_mean, NULL);
        }
     }
+  free (val_lab[0]);
+  free (val_lab[1]);
 }
 
 
-void ssbox_paired_populate (struct ssbox *ssb,
-                          struct cmd_t_test *cmd);
+static void ssbox_paired_populate (struct ssbox *ssb,
+                                  const struct dictionary *dict,
+                                  struct cmd_t_test *cmd);
 
 /* Initialize the paired values ssbox */
 void
@@ -836,10 +862,14 @@ ssbox_paired_init (struct ssbox *this, struct cmd_t_test *cmd UNUSED)
 
 /* Populate the ssbox for paired values */
 void
-ssbox_paired_populate (struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
+ssbox_paired_populate (struct ssbox *ssb, const struct dictionary *dict,
+                      struct cmd_t_test *cmd UNUSED)
 {
   int i;
 
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
+
   assert (ssb->t);
 
   for (i=0; i < n_pairs; ++i)
@@ -860,10 +890,11 @@ ssbox_paired_populate (struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
                     var_get_name (pairs[i].v[j]));
 
          /* Values */
-         tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], 8, 2);
-         tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, 10, 0);
-         tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], 8, 3);
-         tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j]/sqrt (pairs[i].n), 8, 3);
+         tab_double (ssb->t,2, i*2+j+1, TAB_RIGHT, pairs[i].mean[j], NULL);
+         tab_double (ssb->t,3, i*2+j+1, TAB_RIGHT, pairs[i].n, wfmt);
+         tab_double (ssb->t,4, i*2+j+1, TAB_RIGHT, pairs[i].std_dev[j], NULL);
+         tab_double (ssb->t,5, i*2+j+1, TAB_RIGHT,
+                     pairs[i].std_dev[j]/sqrt (pairs[i].n), NULL);
 
        }
     }
@@ -871,10 +902,14 @@ ssbox_paired_populate (struct ssbox *ssb,struct cmd_t_test *cmd UNUSED)
 
 /* Populate the one sample ssbox */
 void
-ssbox_one_sample_populate (struct ssbox *ssb, struct cmd_t_test *cmd)
+ssbox_one_sample_populate (struct ssbox *ssb, const struct dictionary *dict,
+                          struct cmd_t_test *cmd)
 {
   int i;
 
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
+
   assert (ssb->t);
 
   for (i=0; i < cmd->n_variables; ++i)
@@ -882,12 +917,11 @@ ssbox_one_sample_populate (struct ssbox *ssb, struct cmd_t_test *cmd)
       struct group_statistics *gs = &group_proc_get (cmd->v_variables[i])->ugs;
 
       tab_text (ssb->t, 0, i+1, TAB_LEFT, var_get_name (cmd->v_variables[i]));
-      tab_float (ssb->t,1, i+1, TAB_RIGHT, gs->n, 10, 0);
-      tab_float (ssb->t,2, i+1, TAB_RIGHT, gs->mean, 8, 2);
-      tab_float (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, 8, 2);
-      tab_float (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, 8, 3);
+      tab_double (ssb->t,1, i+1, TAB_RIGHT, gs->n, wfmt);
+      tab_double (ssb->t,2, i+1, TAB_RIGHT, gs->mean, NULL);
+      tab_double (ssb->t,3, i+1, TAB_RIGHT, gs->std_dev, NULL);
+      tab_double (ssb->t,4, i+1, TAB_RIGHT, gs->se_mean, NULL);
     }
-
 }
 
 
@@ -900,20 +934,23 @@ void trbox_base_finalize (struct trbox *trb);
 void trbox_independent_samples_init (struct trbox *trb,
                                    struct cmd_t_test *cmd );
 
-void trbox_independent_samples_populate (struct trbox *trb,
-                                       struct cmd_t_test *cmd);
+static void trbox_independent_samples_populate (struct trbox *trb,
+                                        const struct dictionary *dict,
+                                        struct cmd_t_test *cmd);
 
 void trbox_one_sample_init (struct trbox *self,
                      struct cmd_t_test *cmd );
 
-void trbox_one_sample_populate (struct trbox *trb,
-                              struct cmd_t_test *cmd);
+static void trbox_one_sample_populate (struct trbox *trb,
+                               const struct dictionary *,
+                               struct cmd_t_test *cmd);
 
 void trbox_paired_init (struct trbox *self,
                       struct cmd_t_test *cmd );
 
-void trbox_paired_populate (struct trbox *trb,
-                     struct cmd_t_test *cmd);
+static void trbox_paired_populate (struct trbox *trb,
+                                  const struct dictionary *,
+                                  struct cmd_t_test *cmd);
 
 
 
@@ -939,10 +976,11 @@ trbox_create (struct trbox *trb,
 }
 
 /* Populate a trbox according to cmd */
-void
-trbox_populate (struct trbox *trb, struct cmd_t_test *cmd)
+static void
+trbox_populate (struct trbox *trb, const struct dictionary *dict,
+               struct cmd_t_test *cmd)
 {
-  trb->populate (trb,cmd);
+  trb->populate (trb, dict, cmd);
 }
 
 /* Submit and destroy a trbox */
@@ -993,9 +1031,10 @@ trbox_independent_samples_init (struct trbox *self,
 }
 
 /* Populate the independent samples trbox */
-void
+static void
 trbox_independent_samples_populate (struct trbox *self,
-                                  struct cmd_t_test *cmd )
+                                   const struct dictionary *dict UNUSED,
+                                   struct cmd_t_test *cmd)
 {
   int i;
 
@@ -1045,16 +1084,16 @@ trbox_independent_samples_populate (struct trbox *self,
       tab_text (self->t, 1, i*2+3, TAB_LEFT, _ ("Equal variances assumed"));
 
 
-      tab_float (self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, 8,3);
+      tab_double (self->t, 2, i*2+3, TAB_CENTER, grp_data->levene, NULL);
 
       /* Now work out the significance of the Levene test */
       df1 = 1; df2 = grp_data->ugs.n - 2;
       q = gsl_cdf_fdist_Q (grp_data->levene, df1, df2);
 
-      tab_float (self->t, 3, i*2+3, TAB_CENTER, q, 8,3 );
+      tab_double (self->t, 3, i*2+3, TAB_CENTER, q, NULL);
 
       df = gs0->n + gs1->n - 2.0 ;
-      tab_float (self->t, 5, i*2+3, TAB_RIGHT, df, 10, 0);
+      tab_double (self->t, 5, i*2+3, TAB_RIGHT, df, NULL);
 
       pooled_variance = ( (gs0->n )*pow2 (gs0->s_std_dev)
                          +
@@ -1064,30 +1103,30 @@ trbox_independent_samples_populate (struct trbox *self,
       t = (gs0->mean - gs1->mean) / sqrt (pooled_variance) ;
       t /= sqrt ((gs0->n + gs1->n)/ (gs0->n*gs1->n));
 
-      tab_float (self->t, 4, i*2+3, TAB_RIGHT, t, 8, 3);
+      tab_double (self->t, 4, i*2+3, TAB_RIGHT, t, NULL);
 
       p = gsl_cdf_tdist_P (t, df);
       q = gsl_cdf_tdist_Q (t, df);
 
-      tab_float (self->t, 6, i*2+3, TAB_RIGHT, 2.0* (t>0?q:p) , 8, 3);
+      tab_double (self->t, 6, i*2+3, TAB_RIGHT, 2.0* (t>0?q:p), NULL);
 
       mean_diff = gs0->mean - gs1->mean;
-      tab_float (self->t, 7, i*2+3, TAB_RIGHT, mean_diff, 8, 3);
+      tab_double (self->t, 7, i*2+3, TAB_RIGHT, mean_diff, NULL);
 
 
       std_err_diff = sqrt ( pow2 (gs0->se_mean) + pow2 (gs1->se_mean));
-      tab_float (self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, 8, 3);
+      tab_double (self->t, 8, i*2+3, TAB_RIGHT, std_err_diff, NULL);
 
 
       /* Now work out the confidence interval */
       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
 
       t = gsl_cdf_tdist_Qinv (q,df);
-      tab_float (self->t, 9, i*2+3, TAB_RIGHT,
-               mean_diff - t * std_err_diff, 8, 3);
+      tab_double (self->t, 9, i*2+3, TAB_RIGHT,
+               mean_diff - t * std_err_diff, NULL);
 
-      tab_float (self->t, 10, i*2+3, TAB_RIGHT,
-               mean_diff + t * std_err_diff, 8, 3);
+      tab_double (self->t, 10, i*2+3, TAB_RIGHT,
+               mean_diff + t * std_err_diff, NULL);
 
 
       {
@@ -1101,7 +1140,7 @@ trbox_independent_samples_populate (struct trbox *self,
         (pow2 (gs1->s_std_dev)/ (gs1->n -1) );
 
       t = mean_diff / sqrt (se2) ;
-      tab_float (self->t, 4, i*2+3+1, TAB_RIGHT, t, 8, 3);
+      tab_double (self->t, 4, i*2+3+1, TAB_RIGHT, t, NULL);
 
       df = pow2 (se2) / (
                       (pow2 (pow2 (gs0->s_std_dev)/ (gs0->n - 1 ))
@@ -1112,30 +1151,30 @@ trbox_independent_samples_populate (struct trbox *self,
                        / (gs1->n -1 )
                        )
                       ) ;
-      tab_float (self->t, 5, i*2+3+1, TAB_RIGHT, df, 8, 3);
+
+      tab_double (self->t, 5, i*2+3+1, TAB_RIGHT, df, NULL);
 
       p = gsl_cdf_tdist_P (t, df);
       q = gsl_cdf_tdist_Q (t, df);
 
-      tab_float (self->t, 6, i*2+3+1, TAB_RIGHT, 2.0* (t>0?q:p) , 8, 3);
+      tab_double (self->t, 6, i*2+3+1, TAB_RIGHT, 2.0* (t>0?q:p), NULL);
 
       /* Now work out the confidence interval */
       q = (1 - cmd->criteria)/2.0;  /* 2-tailed test */
 
       t = gsl_cdf_tdist_Qinv (q, df);
 
-      tab_float (self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, 8, 3);
-
+      tab_double (self->t, 7, i*2+3+1, TAB_RIGHT, mean_diff, NULL);
 
-      tab_float (self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, 8, 3);
 
+      tab_double (self->t, 8, i*2+3+1, TAB_RIGHT, std_err_diff, NULL);
 
-      tab_float (self->t, 9, i*2+3+1, TAB_RIGHT,
-               mean_diff - t * std_err_diff, 8, 3);
 
-      tab_float (self->t, 10, i*2+3+1, TAB_RIGHT,
-               mean_diff + t * std_err_diff, 8, 3);
+      tab_double (self->t, 9, i*2+3+1, TAB_RIGHT,
+               mean_diff - t * std_err_diff, NULL);
 
+      tab_double (self->t, 10, i*2+3+1, TAB_RIGHT,
+               mean_diff + t * std_err_diff, NULL);
       }
     }
 }
@@ -1176,12 +1215,16 @@ trbox_paired_init (struct trbox *self,
 }
 
 /* Populate the paired samples trbox */
-void
+static void
 trbox_paired_populate (struct trbox *trb,
-                             struct cmd_t_test *cmd UNUSED)
+                      const struct dictionary *dict,
+                      struct cmd_t_test *cmd UNUSED)
 {
   int i;
 
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
+
   for (i=0; i < n_pairs; ++i)
     {
       double p,q;
@@ -1197,42 +1240,42 @@ trbox_paired_populate (struct trbox *trb,
                var_get_name (pairs[i].v[0]),
                 var_get_name (pairs[i].v[1]));
 
-      tab_float (trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
+      tab_double (trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, NULL);
 
-      tab_float (trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
+      tab_double (trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, NULL);
 
       /* SE Mean */
       se_mean = pairs[i].std_dev_diff / sqrt (n) ;
-      tab_float (trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
+      tab_double (trb->t, 4, i+3, TAB_RIGHT, se_mean, NULL);
 
       /* 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_double (trb->t, 5, i+3, TAB_RIGHT,
+               pairs[i].mean_diff - t * se_mean , NULL);
 
-      tab_float (trb->t, 6, i+3, TAB_RIGHT,
-               pairs[i].mean_diff + t * se_mean , 8, 4);
+      tab_double (trb->t, 6, i+3, TAB_RIGHT,
+               pairs[i].mean_diff + t * se_mean , NULL);
 
       t = (pairs[i].mean[0] - pairs[i].mean[1])
        / sqrt (
-                ( pow2 (pairs[i].s_std_dev[0]) + pow2 (pairs[i].s_std_dev[1]) -
+               ( 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 );
+      tab_double (trb->t, 7, i+3, TAB_RIGHT, t, NULL);
 
       /* Degrees of freedom */
-      tab_float (trb->t, 8, i+3, TAB_RIGHT, df , 10, 0 );
+      tab_double (trb->t, 8, i+3, TAB_RIGHT, df, wfmt);
 
       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);
+      tab_double (trb->t, 9, i+3, TAB_RIGHT, 2.0* (t>0?q:p), NULL);
 
     }
 }
@@ -1274,11 +1317,16 @@ trbox_one_sample_init (struct trbox *self, struct cmd_t_test *cmd )
 
 
 /* Populate the one sample trbox */
-void
-trbox_one_sample_populate (struct trbox *trb, struct cmd_t_test *cmd)
+static void
+trbox_one_sample_populate (struct trbox *trb,
+                          const struct dictionary *dict,
+                          struct cmd_t_test *cmd)
 {
   int i;
 
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
+
   assert (trb->t);
 
   for (i=0; i < cmd->n_variables; ++i)
@@ -1293,31 +1341,31 @@ trbox_one_sample_populate (struct trbox *trb, struct cmd_t_test *cmd)
 
       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);
+      tab_double (trb->t, 1, i+3, TAB_RIGHT, t, NULL);
 
       /* degrees of freedom */
       df = gs->n - 1;
 
-      tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
+      tab_double (trb->t, 2, i+3, TAB_RIGHT, df, wfmt);
 
       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_double (trb->t, 3, i+3, TAB_RIGHT, 2.0* (t>0?q:p), NULL);
 
-      tab_float (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, 8,3);
+      tab_double (trb->t, 4, i+3, TAB_RIGHT, gs->mean_diff, NULL);
 
 
       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_double (trb->t, 5, i+3, TAB_RIGHT,
+                gs->mean_diff - t * gs->se_mean, NULL);
 
-      tab_float (trb->t, 6, i+3, TAB_RIGHT,
-                gs->mean_diff + t * gs->se_mean, 8,4);
+      tab_double (trb->t, 6, i+3, TAB_RIGHT,
+                gs->mean_diff + t * gs->se_mean, NULL);
     }
 }
 
@@ -1332,7 +1380,7 @@ trbox_base_init (struct trbox *self, size_t data_rows, int cols)
   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);
+  tab_dim (self->t, tab_natural_dimensions, NULL);
 }
 
 
@@ -1345,11 +1393,14 @@ trbox_base_finalize (struct trbox *trb)
 
 
 /* Create , populate and submit the Paired Samples Correlation box */
-void
-pscbox (void)
+static void
+pscbox (const struct dictionary *dict)
 {
-  const int rows=1+n_pairs;
-  const int cols=5;
+  const struct variable *wv = dict_get_weight (dict);
+  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
+
+  const int rows = 1 + n_pairs;
+  const int cols = 5;
   int i;
 
   struct tab_table *table;
@@ -1361,7 +1412,7 @@ pscbox (void)
   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_dim (table, tab_natural_dimensions, NULL);
   tab_title (table, _ ("Paired Samples Correlations"));
 
   /* column headings */
@@ -1391,13 +1442,13 @@ pscbox (void)
 
 
       /* 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);
+      tab_double (table, 2, i+1, TAB_RIGHT, pairs[i].n, wfmt);
+      tab_double (table, 3, i+1, TAB_RIGHT, pairs[i].correlation, NULL);
 
       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_double (table, 4, i+1, TAB_RIGHT, 2.0* (correlation_t>0?q:p), NULL);
     }
 
   tab_submit (table);
@@ -1440,7 +1491,7 @@ common_calc (const struct dictionary *dict,
 
          gs->n += weight;
          gs->sum += weight * val->f;
-         gs->ssq += weight * val->f * val->f;
+         gs->ssq += weight * pow2 (val->f);
        }
     }
   return 0;
@@ -1477,12 +1528,12 @@ common_postcalc (struct cmd_t_test *cmd)
 
       gs->mean=gs->sum / gs->n;
       gs->s_std_dev= sqrt (
-                        ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
+                        ( (gs->ssq / gs->n ) - pow2 (gs->mean))
                         ) ;
 
       gs->std_dev= sqrt (
                         gs->n/ (gs->n-1) *
-                        ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
+                        ( (gs->ssq / gs->n ) - pow2 (gs->mean))
                         ) ;
 
       gs->se_mean = gs->std_dev / sqrt (gs->n);
@@ -1657,7 +1708,7 @@ group_precalc (struct cmd_t_test *cmd )
       /* There's always 2 groups for a T - TEST */
       ttpr->n_groups = 2;
 
-      gp.indep_width = var_get_width (indep_var);
+      gp.indep_var = indep_var;
 
       ttpr->group_hash = hsh_create (2,
                                    (hsh_compare_func *) compare_group_binary,
@@ -1753,12 +1804,12 @@ group_postcalc ( struct cmd_t_test *cmd )
          gs->mean = gs->sum / gs->n;
 
          gs->s_std_dev= sqrt (
-                             ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
+                             ( (gs->ssq / gs->n ) - pow2 (gs->mean))
                              ) ;
 
          gs->std_dev= sqrt (
                            gs->n/ (gs->n-1) *
-                           ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
+                           ( (gs->ssq / gs->n ) - pow2 (gs->mean))
                            ) ;
 
          gs->se_mean = gs->std_dev / sqrt (gs->n);
@@ -1780,23 +1831,24 @@ calculate (struct cmd_t_test *cmd,
 
   struct casereader *pass1, *pass2, *pass3;
   struct taint *taint;
-  struct ccase c;
+  struct ccase *c;
 
   enum mv_class exclude = cmd->miss != TTS_INCLUDE ? MV_ANY : MV_SYSTEM;
 
-  if (!casereader_peek (input, 0, &c))
+  c = casereader_peek (input, 0);
+  if (c == NULL)
     {
       casereader_destroy (input);
       return;
     }
-  output_split_file_values (ds, &c);
-  case_destroy (&c);
+  output_split_file_values (ds, c);
+  case_unref (c);
 
   if ( cmd->miss == TTS_LISTWISE )
     input = casereader_create_filter_missing (input,
                                               cmd->v_variables,
                                               cmd->n_variables,
-                                              exclude, NULL);
+                                              exclude, NULL, NULL);
 
   input = casereader_create_filter_weight (input, dict, NULL, NULL);
 
@@ -1804,8 +1856,8 @@ calculate (struct cmd_t_test *cmd,
   casereader_split (input, &pass1, &pass2);
 
   common_precalc (cmd);
-  for (; casereader_read (pass1, &c); case_destroy (&c))
-    common_calc (dict, &c, cmd, exclude);
+  for (; (c = casereader_read (pass1)) != NULL; case_unref (c))
+    common_calc (dict, c, cmd, exclude);
   casereader_destroy (pass1);
   common_postcalc (cmd);
 
@@ -1813,22 +1865,22 @@ calculate (struct cmd_t_test *cmd,
     {
     case T_1_SAMPLE:
       one_sample_precalc (cmd);
-      for (; casereader_read (pass2, &c); case_destroy (&c))
-        one_sample_calc (dict, &c, cmd, exclude);
+      for (; (c = casereader_read (pass2)) != NULL; case_unref (c))
+        one_sample_calc (dict, c, cmd, exclude);
       one_sample_postcalc (cmd);
       break;
     case T_PAIRED:
       paired_precalc (cmd);
-      for (; casereader_read (pass2, &c); case_destroy (&c))
-        paired_calc (dict, &c, cmd, exclude);
+      for (; (c = casereader_read (pass2)) != NULL; case_unref (c))
+        paired_calc (dict, c, cmd, exclude);
       paired_postcalc (cmd);
       break;
     case T_IND_SAMPLES:
       pass3 = casereader_clone (pass2);
 
       group_precalc (cmd);
-      for (; casereader_read (pass2, &c); case_destroy (&c))
-        group_calc (dict, &c, cmd, exclude);
+      for (; (c = casereader_read (pass2)) != NULL; case_unref (c))
+        group_calc (dict, c, cmd, exclude);
       group_postcalc (cmd);
 
       levene (dict, pass3, indep_var, cmd->n_variables, cmd->v_variables,
@@ -1840,14 +1892,14 @@ calculate (struct cmd_t_test *cmd,
   if (!taint_has_tainted_successor (taint))
     {
       ssbox_create (&stat_summary_box,cmd,mode);
-      ssbox_populate (&stat_summary_box,cmd);
+      ssbox_populate (&stat_summary_box, dict, cmd);
       ssbox_finalize (&stat_summary_box);
 
       if ( mode == T_PAIRED )
-        pscbox ();
+        pscbox (dict);
 
-      trbox_create (&test_results_box,cmd,mode);
-      trbox_populate (&test_results_box,cmd);
+      trbox_create (&test_results_box, cmd, mode);
+      trbox_populate (&test_results_box, dict, cmd);
       trbox_finalize (&test_results_box);
     }
 
@@ -1869,10 +1921,6 @@ compare_group_binary (const struct group_statistics *a,
 
   if ( p->criterion == CMP_LE )
     {
-      /* less-than comparision is not meaningfull for
-        alpha variables, so we shouldn't ever arrive here */
-      assert (p->indep_width == 0 ) ;
-
       flag_a = ( a->id.f < p->v.critical_value ) ;
       flag_b = ( b->id.f < p->v.critical_value ) ;
     }
@@ -1899,8 +1947,6 @@ hash_group_binary (const struct group_statistics *g,
 
   if ( p->criterion == CMP_LE )
     {
-      /* Not meaningfull to do a less than compare for alpha values ? */
-      assert (p->indep_width == 0 ) ;
       flag = ( g->id.f < p->v.critical_value ) ;
     }
   else if ( p->criterion == CMP_EQ)
@@ -1920,10 +1966,10 @@ short
 which_group (const struct group_statistics *g,
            const struct group_properties *p)
 {
-  if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width))
+  if ( 0 == compare_values_short (&g->id, &p->v.g_value[0], p->indep_var))
     return 0;
 
-  if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width))
+  if ( 0 == compare_values_short (&g->id, &p->v.g_value[1], p->indep_var))
     return 1;
 
   return 2;