FREQUENCIES: Choose number of bins for histogram based on valid cases.
[pspp] / src / language / stats / frequencies.q
index d00c90c9072b6ff25f3bea65553dc534688c0f08..523345be51e3a6955c3c13e9c3424314145f2bb9 100644 (file)
@@ -1374,7 +1374,7 @@ freq_tab_to_hist (const struct freq_tab *ft, const struct variable *var)
   double x_max = -DBL_MAX;
 
   struct histogram *hist;
-  const double bins = 11;
+  int bins;
 
   struct hsh_iterator hi;
   struct hsh_table *fh = ft->data;
@@ -1390,6 +1390,11 @@ freq_tab_to_hist (const struct freq_tab *ft, const struct variable *var)
       if ( frq->value.f > x_max ) x_max = frq->value.f ;
     }
 
+  /* Sturges' formula. */
+  bins = ceil (log (ft->valid_cases) / log (2) + 1);
+  if (bins < 5)
+    bins = 5;
+
   hist = histogram_create (bins, x_min, x_max);
 
   for( i = 0 ; i < ft->n_valid ; ++i )