DATA LIST FREE: Warn when a quoted string is not followed by a delimiter.
[pspp] / src / language / stats / examine.c
index 6be74bc8fd6eee766c51bf02ac712e184329cabd..5d308f11abe15d287bb2507318fadd41faaaf766 100644 (file)
@@ -52,6 +52,7 @@
 
 #include "output/charts/boxplot.h"
 #include "output/charts/np-plot.h"
+#include "output/charts/spreadlevel-plot.h"
 #include "output/charts/plot-hist.h"
 
 #include "language/command.h"
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
+static void 
+append_value_name (const struct variable *var, const union value *val, struct string *str)
+{
+  var_append_value_name (var, val, str);
+  if ( var_is_value_missing (var, val, MV_ANY))
+    ds_put_cstr (str, _(" (missing)"));
+}
+
 enum bp_mode
   {
     BP_GROUPS,
@@ -95,7 +104,8 @@ struct examine
   size_t n_iacts;
   struct interaction **iacts;
 
-  enum mv_class exclude;
+  enum mv_class dep_excl;
+  enum mv_class fctr_excl;
 
   const struct dictionary *dict;
 
@@ -121,6 +131,8 @@ struct examine
   bool npplot;
   bool histogram;
   bool boxplot;
+  bool spreadlevel;
+  int sl_power;
 
   enum bp_mode boxplot_mode;
 
@@ -297,7 +309,7 @@ show_boxplot_grouped (const struct examine *cmd, int iact_idx)
               
               ds_put_cstr (&label, var_to_string (ivar));
               ds_put_cstr (&label, " = ");
-              var_append_value_name (ivar, val, &label);
+              append_value_name (ivar, val, &label);
               ds_put_cstr (&label, "; ");
             }
 
@@ -356,7 +368,7 @@ show_boxplot_variabled (const struct examine *cmd, int iact_idx)
               
               ds_put_cstr (&label, var_to_string (ivar));
               ds_put_cstr (&label, " = ");
-              var_append_value_name (ivar, val, &label);
+              append_value_name (ivar, val, &label);
               ds_put_cstr (&label, "; ");
             }
 
@@ -423,7 +435,7 @@ show_npplot (const struct examine *cmd, int iact_idx)
                   
                   ds_put_cstr (&label, var_to_string (ivar));
                   ds_put_cstr (&label, " = ");
-                  var_append_value_name (ivar, val, &label);
+                  append_value_name (ivar, val, &label);
                   ds_put_cstr (&label, "; ");
                   
                 }
@@ -455,6 +467,58 @@ show_npplot (const struct examine *cmd, int iact_idx)
     }
 }
 
+static void
+show_spreadlevel (const struct examine *cmd, int iact_idx)
+{
+  const struct interaction *iact = cmd->iacts[iact_idx];
+  const size_t n_cats =  categoricals_n_count (cmd->cats, iact_idx);
+
+  int v;
+
+  /* Spreadlevel when there are no levels is not useful */
+  if (iact->n_vars == 0)
+    return;
+
+  for (v = 0; v < cmd->n_dep_vars; ++v)
+    {
+      int grp;
+      struct chart_item *sl;
+
+      struct string label;
+      ds_init_cstr (&label, 
+                   var_to_string (cmd->dep_vars[v]));
+
+      if (iact->n_vars > 0)
+       {
+         ds_put_cstr (&label, " (");
+         interaction_to_string (iact, &label);
+         ds_put_cstr (&label, ")");
+       }
+      
+      sl = spreadlevel_plot_create (ds_cstr (&label), cmd->sl_power);
+
+      for (grp = 0; grp < n_cats; ++grp)
+        {
+          const struct exploratory_stats *es =
+            categoricals_get_user_data_by_category_real (cmd->cats, iact_idx, grp);
+
+         double median = percentile_calculate (es[v].quartiles[1], cmd->pc_alg);
+
+         double iqr = percentile_calculate (es[v].quartiles[2], cmd->pc_alg) -
+           percentile_calculate (es[v].quartiles[0], cmd->pc_alg);
+
+         spreadlevel_plot_add (sl, iqr, median);
+       }
+
+      if (sl == NULL)
+       msg (MW, _("Not creating spreadlevel chart for %s"), ds_cstr (&label));
+      else 
+       chart_item_submit (sl);
+
+      ds_destroy (&label);
+    }
+}
+
 
 static void
 show_histogram (const struct examine *cmd, int iact_idx)
@@ -496,7 +560,7 @@ show_histogram (const struct examine *cmd, int iact_idx)
                   
                   ds_put_cstr (&label, var_to_string (ivar));
                   ds_put_cstr (&label, " = ");
-                  var_append_value_name (ivar, val, &label);
+                  append_value_name (ivar, val, &label);
                   ds_put_cstr (&label, "; ");
                   
                 }
@@ -621,7 +685,7 @@ percentiles_report (const struct examine *cmd, int iact_idx)
                    {              
                      struct string str;
                      ds_init_empty (&str);
-                     var_append_value_name (ivar, val, &str);
+                     append_value_name (ivar, val, &str);
               
                      tab_text (t,
                                1 + ivar_idx,
@@ -789,7 +853,7 @@ descriptives_report (const struct examine *cmd, int iact_idx)
                 {              
                   struct string str;
                   ds_init_empty (&str);
-                  var_append_value_name (ivar, val, &str);
+                  append_value_name (ivar, val, &str);
               
                   tab_text (t,
                             1 + ivar_idx,
@@ -1105,7 +1169,7 @@ extremes_report (const struct examine *cmd, int iact_idx)
                 {              
                   struct string str;
                   ds_init_empty (&str);
-                  var_append_value_name (ivar, val, &str);
+                  append_value_name (ivar, val, &str);
               
                   tab_text (t,
                             1 + ivar_idx,
@@ -1323,7 +1387,7 @@ summary_report (const struct examine *cmd, int iact_idx)
                      {              
                        struct string str;
                        ds_init_empty (&str);
-                       var_append_value_name (ivar, val, &str);
+                       append_value_name (ivar, val, &str);
               
                        tab_text (t,
                                  1 + ivar_idx, heading_rows + n_cats * v + i,
@@ -1397,25 +1461,6 @@ summary_report (const struct examine *cmd, int iact_idx)
   tab_submit (t);
 }
 
-
-/* Match a variable.
-   If the match succeeds, the variable will be placed in VAR.
-   Returns true if successful */
-static bool
-lex_match_variable (struct lexer *lexer, 
-                    const struct dictionary *dict, const struct variable **var)
-{
-  if (lex_token (lexer) !=  T_ID)
-
-    return false;
-
-  *var = parse_variable_const  (lexer, dict);
-
-  if ( *var == NULL)
-    return false;
-  return true;
-}
-
 /* Attempt to parse an interaction from LEXER */
 static struct interaction *
 parse_interaction (struct lexer *lexer, struct examine *ex)
@@ -1483,7 +1528,7 @@ update_n (const void *aux1, void *aux2 UNUSED, void *user_data,
       const struct variable *var = examine->dep_vars[v];
       const double x = case_data (c, var)->f;
       
-      if (var_is_value_missing (var, case_data (c, var), examine->exclude))
+      if (var_is_value_missing (var, case_data (c, var), examine->dep_excl))
         {
           es[v].missing += weight;
           continue;
@@ -1532,7 +1577,6 @@ calculate_n (const void *aux1, void *aux2 UNUSED, void *user_data)
       double imax = es[v].cc;
       struct casereader *reader;
       struct ccase *c;
-      casenumber total_cases;
 
       if (examine->histogram)
         {
@@ -1541,14 +1585,11 @@ calculate_n (const void *aux1, void *aux2 UNUSED, void *user_data)
             / (1 + log2 (es[v].cc))
             ;
 
-          bin_width = chart_rounded_tick (bin_width);
-
           es[v].histogram =
             histogram_create (bin_width, es[v].minimum, es[v].maximum);
         }
 
       es[v].sorted_reader = casewriter_make_reader (es[v].sorted_writer);
-      total_cases = casereader_count_cases (es[v].sorted_reader);
       es[v].sorted_writer = NULL;
 
       es[v].maxima = pool_calloc (examine->pool, examine->calc_extremes, sizeof (*es[v].maxima));
@@ -1741,13 +1782,14 @@ run_examine (struct examine *cmd, struct casereader *input)
   struct payload payload;
   payload.create = create_n;
   payload.update = update_n;
-  payload.destroy = calculate_n;
+  payload.calculate = calculate_n;
+  payload.destroy = NULL;
   
   cmd->wv = dict_get_weight (cmd->dict);
 
   cmd->cats
     = categoricals_create (cmd->iacts, cmd->n_iacts,  
-                           cmd->wv, cmd->exclude);
+                           cmd->wv, cmd->dep_excl, cmd->fctr_excl);
 
   categoricals_set_payload (cmd->cats, &payload, cmd, NULL);
 
@@ -1763,14 +1805,12 @@ run_examine (struct examine *cmd, struct casereader *input)
       case_unref (c);
     }
 
-  /* FIXME: Filter out missing factor variables */
-
   /* Remove cases on a listwise basis if requested */
   if ( cmd->missing_pw == false)
     input = casereader_create_filter_missing (input,
                                               cmd->dep_vars,
                                               cmd->n_dep_vars,
-                                              cmd->exclude,
+                                              cmd->dep_excl,
                                               NULL,
                                               NULL);
 
@@ -1814,6 +1854,9 @@ run_examine (struct examine *cmd, struct casereader *input)
       if (cmd->npplot)
         show_npplot (cmd, i);
 
+      if (cmd->spreadlevel)
+        show_spreadlevel (cmd, i);
+
       if (cmd->descriptives)
         descriptives_report (cmd, i);
     }
@@ -1860,10 +1903,13 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
   examine.iacts = iacts_mem = pool_zalloc (examine.pool, sizeof (struct interaction *));
   examine.iacts[0] = interaction_create (NULL);
 
-  examine.exclude = MV_ANY;
+  examine.dep_excl = MV_ANY;
+  examine.fctr_excl = MV_ANY;
   examine.histogram = false;
   examine.npplot = false;
   examine.boxplot = false;
+  examine.spreadlevel = false;
+  examine.sl_power = 0;
   
   examine.dict = dataset_dict (ds);
 
@@ -2042,11 +2088,19 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
                 }
               else if (lex_match_id (lexer, "EXCLUDE"))
                 {
-                  examine.exclude = MV_ANY;
+                  examine.dep_excl = MV_ANY;
                 }
               else if (lex_match_id (lexer, "INCLUDE"))
                 {
-                  examine.exclude = MV_SYSTEM;
+                  examine.dep_excl = MV_SYSTEM;
+                }
+              else if (lex_match_id (lexer, "REPORT"))
+                {
+                  examine.fctr_excl = MV_NEVER;
+                }
+              else if (lex_match_id (lexer, "NOREPORT"))
+                {
+                  examine.fctr_excl = MV_ANY;
                 }
               else
                 {
@@ -2091,6 +2145,19 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
                 {
                   examine.histogram = true;
                 }
+              else if (lex_match_id (lexer, "SPREADLEVEL"))
+                {
+                 examine.spreadlevel = true;
+                 examine.sl_power = 0;
+                 if (lex_match (lexer, T_LPAREN))
+                   {
+                      examine.sl_power = lex_integer (lexer);
+
+                      lex_get (lexer);
+                      if (! lex_force_match (lexer, T_RPAREN))
+                        goto error;
+                   }
+                }
               else if (lex_match_id (lexer, "NONE"))
                 {
                   examine.histogram = false;
@@ -2149,6 +2216,7 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
     {
       examine.n_iacts--;
       examine.iacts = &iacts_mem[1];
+      interaction_destroy (iacts_mem[0]);
     }
 
 
@@ -2204,9 +2272,6 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
 
   caseproto_unref (examine.ex_proto);
 
-  for (i = 0; i < examine.n_iacts; ++i)
-    interaction_destroy (examine.iacts[i]);
-
   free (examine.ptiles);
   free (examine.dep_vars);
   pool_destroy (examine.pool);