output: Add auxiliary data parameter to tab_dim.
[pspp-builds.git] / src / language / stats / t-test.q
index 40bf97788b35e157082ebaf37a460f5216946c7c..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
@@ -81,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
@@ -240,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);
@@ -258,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;
@@ -338,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))
@@ -409,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, ',');
@@ -428,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;
@@ -677,7 +671,7 @@ 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,
@@ -749,15 +743,15 @@ ssbox_independent_samples_populate (struct ssbox *ssb,
       const char *s;
 
       s = var_lookup_value_label (indep_var, &gp.v.g_value[0]);
-      val_lab[0] = s ? strdup (s) : NULL;
+      val_lab[0] = s ? xstrdup (s) : NULL;
 
       s = var_lookup_value_label (indep_var, &gp.v.g_value[1]);
-      val_lab[1] = s ? strdup (s) : NULL;
+      val_lab[1] = s ? xstrdup (s) : NULL;
     }
   else
     {
-      val_lab[0] = calloc (sizeof (char), MAX_SHORT_STRING + 1);
-      val_lab[1] = calloc (sizeof (char), MAX_SHORT_STRING + 1);
+      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);
     }
@@ -1386,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);
 }
 
 
@@ -1418,7 +1412,7 @@ pscbox (const struct dictionary *dict)
   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 */
@@ -1497,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;
@@ -1534,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);
@@ -1714,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,
@@ -1810,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);
@@ -1837,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);
 
@@ -1861,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);
 
@@ -1870,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,
@@ -1926,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 ) ;
     }
@@ -1956,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)
@@ -1977,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;