Categoricals: Provide separate functions for effects and dummy coding.
[pspp] / src / math / categoricals.c
index ba88227864c5fc363a02b921125f44a3d7c2243e..c088ac00732bd23dc7a9db178f058c434f1579b4 100644 (file)
@@ -34,8 +34,6 @@
 
 #define CATEGORICALS_DEBUG 0
 
-#define EFFECTS_CODING 1
-
 struct value_node
 {
   struct hmap_node node;      /* Node in hash map. */
@@ -96,10 +94,10 @@ lookup_variable (const struct hmap *map, const struct variable *var, unsigned in
 
 struct interact_params
 {
-  /* A map indexed by a interaction_value */
+  /* A map of cases indexed by a interaction_value */
   struct hmap ivmap;
 
-  const struct interaction *iact;
+  struct interaction *iact;
 
   int base_subscript_short;
   int base_subscript_long;
@@ -253,10 +251,8 @@ categoricals_destroy (struct categoricals *cat)
       /* Interate over each interaction value, and unref any cases that we reffed */
       HMAP_FOR_EACH (iv, struct interaction_value, node, &cat->iap[i].ivmap)
        {
-#if 0
-         if (cat->payload)
-           cat->payload->destroy (cat->aux1, iv->user_data);
-#endif
+         if (cat->payload && cat->payload->destroy)
+           cat->payload->destroy (cat->aux1, cat->aux2, iv->user_data);
          case_unref (iv->ccase);
        }
 
@@ -360,7 +356,12 @@ categoricals_update (struct categoricals *cat, const struct ccase *c)
 {
   int i;
   struct variable_node *vn = NULL;
-  const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
+  double weight;
+
+  if (NULL == cat)
+    return;
+
+  weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
 
   assert (NULL == cat->reverse_variable_map_short);
   assert (NULL == cat->reverse_variable_map_long);
@@ -455,6 +456,9 @@ categoricals_n_total (const struct categoricals *cat)
 size_t
 categoricals_df_total (const struct categoricals *cat)
 {
+  if (NULL == cat)
+    return 0;
+
   return cat->df_sum;
 }
 
@@ -481,6 +485,10 @@ categoricals_done (const struct categoricals *cat_)
   int i;
   int idx_short = 0;
   int idx_long = 0;
+
+  if (NULL == cat)
+    return;
+
   cat->df_sum = 0;
   cat->n_cats_total = 0;
 
@@ -544,7 +552,6 @@ categoricals_done (const struct categoricals *cat_)
       HMAP_FOR_EACH (ivn, struct interaction_value, node, &iap->ivmap)
        {
          iap->reverse_interaction_value_map[x++] = ivn;
-
        }
 
       assert (x <= iap->n_cats);
@@ -588,11 +595,11 @@ categoricals_done (const struct categoricals *cat_)
          struct interaction_value *iv = iap->reverse_interaction_value_map[y];
          for (x = iap->base_subscript_short; x < iap->base_subscript_short + df ;++x)
            {
-             const double bin = categoricals_get_code_for_case (cat, x, iv->ccase); \
+             const double bin = categoricals_get_effects_code_for_case (cat, x, iv->ccase);
              iap->enc_sum [x - iap->base_subscript_short] += bin * iv->cc;
            }
-         if (cat->payload && cat->payload->destroy)
-           cat->payload->destroy (cat->aux1, cat->aux2, iv->user_data);
+         if (cat->payload && cat->payload->calculate)
+           cat->payload->calculate (cat->aux1, cat->aux2, iv->user_data);
        }
     }
 
@@ -648,11 +655,13 @@ categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript
   return   vp->enc_sum[subscript - vp->base_subscript_short];
 }
 
+
 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
    for that subscript */
-double
+static double
 categoricals_get_code_for_case (const struct categoricals *cat, int subscript,
-                               const struct ccase *c)
+                               const struct ccase *c,
+                               bool effects_coding)
 {
   const struct interaction *iact = categoricals_get_interaction_by_subscript (cat, subscript);
 
@@ -685,13 +694,10 @@ categoricals_get_code_for_case (const struct categoricals *cat, int subscript,
       const int index = ((subscript - base_index) % iap->df_prod[v] ) / dfp;
       dfp = iap->df_prod [v];
 
-#if EFFECTS_CODING
-      if ( valn->index == df )
+      if (effects_coding && valn->index == df )
        bin = -1.0;
-      else 
-#endif
-       if ( valn->index  != index )
-         bin = 0;
+      else if ( valn->index  != index )
+       bin = 0;
     
       result *= bin;
     }
@@ -700,6 +706,28 @@ categoricals_get_code_for_case (const struct categoricals *cat, int subscript,
 }
 
 
+/* Returns unity if the value in case C at SUBSCRIPT is equal to the category
+   for that subscript */
+double
+categoricals_get_dummy_code_for_case (const struct categoricals *cat, int subscript,
+                                    const struct ccase *c)
+{
+  return categoricals_get_code_for_case (cat, subscript, c, false);
+}
+
+/* Returns unity if the value in case C at SUBSCRIPT is equal to the category
+   for that subscript. 
+   Else if it is the last category, return -1.
+   Otherwise return 0.
+ */
+double
+categoricals_get_effects_code_for_case (const struct categoricals *cat, int subscript,
+                                       const struct ccase *c)
+{
+  return categoricals_get_code_for_case (cat, subscript, c, true);
+}
+
+
 size_t
 categoricals_get_n_variables (const struct categoricals *cat)
 {