Add ptile support (untested).
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 30 Jan 2022 00:59:31 +0000 (16:59 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 30 Jan 2022 01:00:17 +0000 (17:00 -0800)
src/language/stats/ctables.c

index 17032c8c8e90db6e77a4038cb00354a040cbb72f..2edd40d5d39d78cfc46a86e1dd6bdfad9df0b0ff 100644 (file)
@@ -1686,7 +1686,7 @@ union ctables_summary
     /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
     struct moments1 *moments;
 
-    /* MEDIAN, MODE. */
+    /* MEDIAN, MODE, PTILE. */
     struct
       {
         struct casewriter *writer;
@@ -1694,7 +1694,7 @@ union ctables_summary
         double ovalue;
       };
 
-    /* XXX percentiles, mode, multiple response */
+    /* XXX multiple response */
   };
 
 static void
@@ -1757,6 +1757,7 @@ ctables_summary_init (union ctables_summary *s,
 
     case CTSF_MEDIAN:
     case CTSF_MODE:
+    case CTSF_PTILE:
       {
         struct caseproto *proto = caseproto_create ();
         proto = caseproto_add_width (proto, 0);
@@ -1773,9 +1774,6 @@ ctables_summary_init (union ctables_summary *s,
       }
       break;
 
-    case CTSF_PTILE:
-      NOT_REACHED ();
-
     case CTSF_RESPONSES:
     case CTSF_ROWPCT_RESPONSES:
     case CTSF_COLPCT_RESPONSES:
@@ -1860,12 +1858,10 @@ ctables_summary_uninit (union ctables_summary *s,
 
     case CTSF_MEDIAN:
     case CTSF_MODE:
+    case CTSF_PTILE:
       casewriter_destroy (s->writer);
       break;
 
-    case CTSF_PTILE:
-      NOT_REACHED ();
-
     case CTSF_RESPONSES:
     case CTSF_ROWPCT_RESPONSES:
     case CTSF_COLPCT_RESPONSES:
@@ -1965,6 +1961,7 @@ ctables_summary_add (union ctables_summary *s,
 
     case CTSF_MEDIAN:
     case CTSF_MODE:
+    case CTSF_PTILE:
       if (var_is_value_missing (var, value))
         {
           s->ovalid += weight;
@@ -1976,9 +1973,6 @@ ctables_summary_add (union ctables_summary *s,
         }
       break;
 
-    case CTSF_PTILE:
-      NOT_REACHED ();
-
     case CTSF_RESPONSES:
     case CTSF_ROWPCT_RESPONSES:
     case CTSF_COLPCT_RESPONSES:
@@ -2118,16 +2112,18 @@ ctables_summary_value (const struct ctables_cell *cell,
       NOT_REACHED ();
 
     case CTSF_MEDIAN:
+    case CTSF_PTILE:
       if (s->writer)
         {
           struct casereader *reader = casewriter_make_reader (s->writer);
           s->writer = NULL;
 
-          struct percentile *median = percentile_create (0.5, s->ovalid);
-          struct order_stats *os = &median->parent;
+          struct percentile *ptile = percentile_create (
+            ss->function == CTSF_PTILE ? ss->percentile : 0.5, s->ovalid);
+          struct order_stats *os = &ptile->parent;
           order_stats_accumulate_idx (&os, 1, reader, 1, 0);
-          s->ovalue = percentile_calculate (median, PC_HAVERAGE);
-          statistic_destroy (&median->parent.parent);
+          s->ovalue = percentile_calculate (ptile, PC_HAVERAGE);
+          statistic_destroy (&ptile->parent.parent);
         }
       return s->ovalue;
 
@@ -2145,9 +2141,6 @@ ctables_summary_value (const struct ctables_cell *cell,
         }
       return s->ovalue;
 
-    case CTSF_PTILE:
-      NOT_REACHED ();
-
     case CTSF_RESPONSES:
     case CTSF_ROWPCT_RESPONSES:
     case CTSF_COLPCT_RESPONSES: