88f954044f2122c6ae64be9f20093719b522de13
[pspp-builds.git] / src / language / stats / roc.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "roc.h"
20 #include <data/procedure.h>
21 #include <language/lexer/variable-parser.h>
22 #include <language/lexer/value-parser.h>
23 #include <language/lexer/lexer.h>
24
25 #include <data/casegrouper.h>
26 #include <data/casereader.h>
27 #include <data/casewriter.h>
28 #include <data/dictionary.h>
29 #include <data/format.h>
30 #include <math/sort.h>
31 #include <data/subcase.h>
32
33
34 #include <libpspp/misc.h>
35
36 #include <gsl/gsl_cdf.h>
37 #include <output/table.h>
38
39 #include <output/charts/plot-chart.h>
40 #include <output/charts/cartesian.h>
41
42 #include "gettext.h"
43 #define _(msgid) gettext (msgid)
44 #define N_(msgid) msgid
45
46 struct cmd_roc
47 {
48   size_t n_vars;
49   const struct variable **vars;
50
51   struct variable *state_var ;
52   union value state_value;
53
54   /* Plot the roc curve */
55   bool curve;
56   /* Plot the reference line */
57   bool reference;
58
59   double ci;
60
61   bool print_coords;
62   bool print_se;
63   bool bi_neg_exp; /* True iff the bi-negative exponential critieria
64                       should be used */
65   enum mv_class exclude;
66
67   bool invert ; /* True iff a smaller test result variable indicates
68                    a positive result */
69 };
70
71 static int run_roc (struct dataset *ds, struct cmd_roc *roc);
72
73 int
74 cmd_roc (struct lexer *lexer, struct dataset *ds)
75 {
76   struct cmd_roc roc ;
77   const struct dictionary *dict = dataset_dict (ds);
78
79   roc.vars = NULL;
80   roc.n_vars = 0;
81   roc.print_se = false;
82   roc.print_coords = false;
83   roc.exclude = MV_ANY;
84   roc.curve = true;
85   roc.reference = false;
86   roc.ci = 95;
87   roc.bi_neg_exp = false;
88   roc.invert = false;
89
90   if (!parse_variables_const (lexer, dict, &roc.vars, &roc.n_vars,
91                               PV_APPEND | PV_NO_DUPLICATE | PV_NUMERIC))
92     return 2;
93
94   if ( ! lex_force_match (lexer, T_BY))
95     {
96       return 2;
97     }
98
99   roc.state_var = parse_variable (lexer, dict);
100
101   if ( !lex_force_match (lexer, '('))
102     {
103       return 2;
104     }
105
106   parse_value (lexer, &roc.state_value, var_get_width (roc.state_var));
107
108
109   if ( !lex_force_match (lexer, ')'))
110     {
111       return 2;
112     }
113
114
115   while (lex_token (lexer) != '.')
116     {
117       lex_match (lexer, '/');
118       if (lex_match_id (lexer, "MISSING"))
119         {
120           lex_match (lexer, '=');
121           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
122             {
123               if (lex_match_id (lexer, "INCLUDE"))
124                 {
125                   roc.exclude = MV_SYSTEM;
126                 }
127               else if (lex_match_id (lexer, "EXCLUDE"))
128                 {
129                   roc.exclude = MV_ANY;
130                 }
131               else
132                 {
133                   lex_error (lexer, NULL);
134                   return 2;
135                 }
136             }
137         }
138       else if (lex_match_id (lexer, "PLOT"))
139         {
140           lex_match (lexer, '=');
141           if (lex_match_id (lexer, "CURVE"))
142             {
143               roc.curve = true;
144               if (lex_match (lexer, '('))
145                 {
146                   roc.reference = true;
147                   lex_force_match_id (lexer, "REFERENCE");
148                   lex_force_match (lexer, ')');
149                 }
150             }
151           else if (lex_match_id (lexer, "NONE"))
152             {
153               roc.curve = false;
154             }
155           else
156             {
157               lex_error (lexer, NULL);
158               return 2;
159             }
160         }
161       else if (lex_match_id (lexer, "PRINT"))
162         {
163           lex_match (lexer, '=');
164           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
165             {
166               if (lex_match_id (lexer, "SE"))
167                 {
168                   roc.print_se = true;
169                 }
170               else if (lex_match_id (lexer, "COORDINATES"))
171                 {
172                   roc.print_coords = true;
173                 }
174               else
175                 {
176                   lex_error (lexer, NULL);
177                   return 2;
178                 }
179             }
180         }
181       else if (lex_match_id (lexer, "CRITERIA"))
182         {
183           lex_match (lexer, '=');
184           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
185             {
186               if (lex_match_id (lexer, "CUTOFF"))
187                 {
188                   lex_force_match (lexer, '(');
189                   if (lex_match_id (lexer, "INCLUDE"))
190                     {
191                       roc.exclude = MV_SYSTEM;
192                     }
193                   else if (lex_match_id (lexer, "EXCLUDE"))
194                     {
195                       roc.exclude = MV_USER | MV_SYSTEM;
196                     }
197                   else
198                     {
199                       lex_error (lexer, NULL);
200                       return 2;
201                     }
202                   lex_force_match (lexer, ')');
203                 }
204               else if (lex_match_id (lexer, "TESTPOS"))
205                 {
206                   lex_force_match (lexer, '(');
207                   if (lex_match_id (lexer, "LARGE"))
208                     {
209                       roc.invert = false;
210                     }
211                   else if (lex_match_id (lexer, "SMALL"))
212                     {
213                       roc.invert = true;
214                     }
215                   else
216                     {
217                       lex_error (lexer, NULL);
218                       return 2;
219                     }
220                   lex_force_match (lexer, ')');
221                 }
222               else if (lex_match_id (lexer, "CI"))
223                 {
224                   lex_force_match (lexer, '(');
225                   lex_force_num (lexer);
226                   roc.ci = lex_number (lexer);
227                   lex_get (lexer);
228                   lex_force_match (lexer, ')');
229                 }
230               else if (lex_match_id (lexer, "DISTRIBUTION"))
231                 {
232                   lex_force_match (lexer, '(');
233                   if (lex_match_id (lexer, "FREE"))
234                     {
235                       roc.bi_neg_exp = false;
236                     }
237                   else if (lex_match_id (lexer, "NEGEXPO"))
238                     {
239                       roc.bi_neg_exp = true;
240                     }
241                   else
242                     {
243                       lex_error (lexer, NULL);
244                       return 2;
245                     }
246                   lex_force_match (lexer, ')');
247                 }
248               else
249                 {
250                   lex_error (lexer, NULL);
251                   return 2;
252                 }
253             }
254         }
255       else
256         {
257           lex_error (lexer, NULL);
258           break;
259         }
260     }
261
262   run_roc (ds, &roc);
263
264   return 1;
265 }
266
267
268
269
270 static void
271 do_roc (struct cmd_roc *roc, struct casereader *group, struct dictionary *dict);
272
273
274 static int
275 run_roc (struct dataset *ds, struct cmd_roc *roc)
276 {
277   struct dictionary *dict = dataset_dict (ds);
278   bool ok;
279   struct casereader *group;
280
281   struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
282   while (casegrouper_get_next_group (grouper, &group))
283     {
284       do_roc (roc, group, dataset_dict (ds));
285     }
286   ok = casegrouper_destroy (grouper);
287   ok = proc_commit (ds) && ok;
288
289   return ok;
290 }
291
292
293 static void
294 dump_casereader (struct casereader *reader)
295 {
296   struct ccase *c;
297   struct casereader *r = casereader_clone (reader);
298
299   for ( ; (c = casereader_read (r) ); case_unref (c))
300     {
301       int i;
302       for (i = 0 ; i < case_get_value_cnt (c); ++i)
303         {
304           printf ("%g ", case_data_idx (c, i)->f);
305         }
306       printf ("\n");
307     }
308
309   casereader_destroy (r);
310 }
311
312 static bool
313 match_positives (const struct ccase *c, void *aux)
314 {
315   struct cmd_roc *roc = aux;
316
317   return 0 == value_compare_3way (case_data (c, roc->state_var),
318                                  &roc->state_value,
319                                  var_get_width (roc->state_var));
320 }
321
322
323 #define VALUE  0
324 #define N_EQ   1
325 #define N_PRED 2
326
327 struct roc_state
328 {
329   double auc;
330
331   double n1;
332   double n2;
333
334   double q1hat;
335   double q2hat;
336
337   struct casewriter *cutpoint_wtr;
338   struct casereader *cutpoint_rdr;
339   double prev_result;
340   double min;
341   double max;
342 };
343
344
345
346 #define CUTPOINT 0
347 #define TP 1
348 #define FN 2
349 #define TN 3
350 #define FP 4
351
352
353 static struct casereader *
354 accumulate_counts (struct casereader *cutpoint_rdr, 
355                    double result, double weight, 
356                    bool (*pos_cond) (double, double),
357                    int true_index, int false_index)
358 {
359   const struct caseproto *proto = casereader_get_proto (cutpoint_rdr);
360   struct casewriter *w =
361     autopaging_writer_create (proto);
362   struct casereader *r = casereader_clone (cutpoint_rdr);
363   struct ccase *cpc;
364   double prev_cp = SYSMIS;
365
366
367   for ( ; (cpc = casereader_read (r) ); case_unref (cpc))
368     {
369       struct ccase *new_case;
370       const double cp = case_data_idx (cpc, CUTPOINT)->f;
371
372       /* We don't want duplicates here */
373       if ( cp == prev_cp )
374         continue;
375
376       new_case = case_clone (cpc);
377
378       if ( pos_cond (result, cp))
379         {
380           case_data_rw_idx (new_case, true_index)->f += weight;
381         }
382       else
383         {
384           case_data_rw_idx (new_case, false_index)->f += weight;
385         }
386
387       prev_cp = cp;
388
389       casewriter_write (w, new_case);
390     }
391   casereader_destroy (r);
392
393   return casewriter_make_reader (w);
394 }
395
396
397
398 static void output_roc (struct roc_state *rs, const struct cmd_roc *roc);
399
400
401 static struct casereader *
402 process_group (const struct variable *var, struct casereader *reader,
403                bool (*pred) (double, double),
404                const struct dictionary *dict,
405                double *cc,
406                struct casereader **cutpoint_rdr, 
407                bool (*pos_cond) (double, double),
408                int true_index,
409                int false_index
410                )
411 {
412   const struct variable *w = dict_get_weight (dict);
413   struct casereader *r1 =
414     casereader_create_distinct (sort_execute_1var (reader, var), var, w);
415
416   const int weight_idx  = w ? var_get_case_index (w) :
417     caseproto_get_n_widths (casereader_get_proto (r1)) - 1;
418   
419   struct ccase *c1;
420
421   struct casereader *rclone = casereader_clone (r1);
422   struct casewriter *wtr;
423   struct caseproto *proto = caseproto_create ();
424
425   proto = caseproto_add_width (proto, 0);
426   proto = caseproto_add_width (proto, 0);
427   proto = caseproto_add_width (proto, 0);
428
429   wtr = autopaging_writer_create (proto);  
430
431   *cc = 0;
432
433   for ( ; (c1 = casereader_read (r1) ); case_unref (c1))
434     {
435       struct ccase *c2;
436       struct casereader *r2 = casereader_clone (rclone);
437
438       const double weight1 = case_data_idx (c1, weight_idx)->f;
439       const double d1 = case_data (c1, var)->f;
440       double n_eq = 0.0;
441       double n_pred = 0.0;
442
443       *cutpoint_rdr = accumulate_counts (*cutpoint_rdr, d1, weight1,
444                                         pos_cond,
445                                         true_index, false_index);
446
447       struct ccase *new_case = case_create (proto);
448
449       *cc += weight1;
450
451       for ( ; (c2 = casereader_read (r2) ); case_unref (c2))
452         {
453           const double d2 = case_data (c2, var)->f;
454           const double weight2 = case_data_idx (c2, weight_idx)->f;
455
456           if ( d1 == d2 )
457             {
458               n_eq += weight2;
459               continue;
460             }
461           else  if ( pred (d2, d1))
462             {
463               n_pred += weight2;
464             }
465         }
466
467       case_data_rw_idx (new_case, VALUE)->f = d1;
468       case_data_rw_idx (new_case, N_EQ)->f = n_eq;
469       case_data_rw_idx (new_case, N_PRED)->f = n_pred;
470
471       casewriter_write (wtr, new_case);
472
473       casereader_destroy (r2);
474     }
475
476   casereader_destroy (r1);
477   casereader_destroy (rclone);
478
479   return casewriter_make_reader (wtr);
480 }
481
482 static bool
483 gt (double d1, double d2)
484 {
485   return d1 > d2;
486 }
487
488
489 static bool
490 ge (double d1, double d2)
491 {
492   return d1 > d2;
493 }
494
495 static bool
496 lt (double d1, double d2)
497 {
498   return d1 < d2;
499 }
500
501 static struct casereader *
502 process_positive_group (const struct variable *var, struct casereader *reader,
503                         const struct dictionary *dict,
504                         struct roc_state *rs)
505 {
506   return process_group (var, reader, gt, dict, &rs->n1,
507                         &rs->cutpoint_rdr,
508                         ge,
509                         TP, FN);
510 }
511
512
513 static struct casereader *
514 process_negative_group (const struct variable *var, struct casereader *reader,
515                         const struct dictionary *dict,
516                         struct roc_state *rs)
517 {
518   return process_group (var, reader, lt, dict, &rs->n2,
519                         &rs->cutpoint_rdr,
520                         lt,
521                         TN, FP);
522 }
523
524
525
526
527 static void
528 append_cutpoint (struct casewriter *writer, double cutpoint)
529 {
530   struct ccase *cc = case_create (casewriter_get_proto (writer));
531
532   case_data_rw_idx (cc, CUTPOINT)->f = cutpoint;
533   case_data_rw_idx (cc, TP)->f = 0;
534   case_data_rw_idx (cc, FN)->f = 0;
535   case_data_rw_idx (cc, TN)->f = 0;
536   case_data_rw_idx (cc, FP)->f = 0;
537
538
539   casewriter_write (writer, cc);
540 }
541
542
543 static void
544 do_roc (struct cmd_roc *roc, struct casereader *input, struct dictionary *dict)
545 {
546   int i;
547
548   struct roc_state *rs = xcalloc (roc->n_vars, sizeof *rs);
549
550   struct casewriter *neg_wtr = autopaging_writer_create (casereader_get_proto (input));
551
552   struct casereader *negatives = NULL;
553   struct casereader *positives = NULL;
554
555
556   /* Prepare the cutpoints */
557   {
558     struct casereader *r = casereader_clone (input);
559     struct ccase *c;
560     struct caseproto *proto = caseproto_create ();
561
562     struct subcase ordering;
563     subcase_init (&ordering, CUTPOINT, 0, SC_ASCEND);
564
565
566     proto = caseproto_add_width (proto, 0); /* cutpoint */
567     proto = caseproto_add_width (proto, 0); /* TP */
568     proto = caseproto_add_width (proto, 0); /* FN */
569     proto = caseproto_add_width (proto, 0); /* TN */
570     proto = caseproto_add_width (proto, 0); /* FP */
571
572
573     for (i = 0 ; i < roc->n_vars; ++i)
574       {
575         rs[i].cutpoint_wtr = sort_create_writer (&ordering, proto);
576         rs[i].prev_result = SYSMIS;
577         rs[i].max = -DBL_MAX;
578         rs[i].min = DBL_MAX;
579       }
580
581     for (; (c = casereader_read (r)) != NULL; case_unref (c))
582       {
583         const double weight = dict_get_case_weight (dict, c, NULL);
584         for (i = 0 ; i < roc->n_vars; ++i)
585           {
586             const double result = case_data (c, roc->vars[i])->f;
587
588             minimize (&rs[i].min, result);
589             maximize (&rs[i].max, result);
590
591             if ( rs[i].prev_result != SYSMIS && rs[i].prev_result != result )
592               {
593                 const double mean = (result + rs[i].prev_result ) / 2.0;
594                 append_cutpoint (rs[i].cutpoint_wtr, mean);
595               }
596
597             rs[i].prev_result = result;
598           }
599       }
600     casereader_destroy (r);
601
602
603     /* Append the min and max cutpoints */
604     for (i = 0 ; i < roc->n_vars; ++i)
605       {
606         append_cutpoint (rs[i].cutpoint_wtr, rs[i].min - 1);
607         append_cutpoint (rs[i].cutpoint_wtr, rs[i].max + 1);
608
609         rs[i].cutpoint_rdr = casewriter_make_reader (rs[i].cutpoint_wtr);
610       }
611   }
612
613  positives = 
614     casereader_create_filter_func (input,
615                                    match_positives,
616                                    NULL,
617                                    roc,
618                                    neg_wtr);
619
620
621   for (i = 0 ; i < roc->n_vars; ++i)
622     {
623       struct ccase *cpos;
624       struct casereader *n_neg ;
625       const struct variable *var = roc->vars[i];
626
627       struct casereader *neg ;
628       struct casereader *pos = casereader_clone (positives);
629
630       struct casereader *n_pos =
631         process_positive_group (var, pos, dict, &rs[i]);
632
633       if ( negatives == NULL)
634         {
635           negatives = casewriter_make_reader (neg_wtr);
636         }
637
638       neg = casereader_clone (negatives);
639
640       n_neg = process_negative_group (var, neg, dict, &rs[i]);
641
642
643       printf ("Positives:\n");
644       dump_casereader (n_pos);
645
646       printf ("Negatives:\n");
647       dump_casereader (n_neg);
648
649 #if 0
650       /* Simple join on VALUE */
651       for ( ; (cpos = casereader_read (n_pos) ); case_unref (cpos))
652         {
653           struct ccase *cneg = NULL;
654           double dneg = -DBL_MAX;
655           const double dpos = case_data_idx (cpos, VALUE)->f;
656           while (dneg < dpos)
657             {
658               if ( cneg )
659                 case_unref (cneg);
660
661               cneg = casereader_read (n_neg);
662               if ( ! cneg )
663                 break;
664               dneg = case_data_idx (cneg, VALUE)->f;
665             }
666         
667           if ( dpos == dneg )
668             {
669               double n_pos_eq = case_data_idx (cpos, N_EQ)->f;
670               double n_neg_eq = case_data_idx (cneg, N_EQ)->f;
671               double n_pos_gt = case_data_idx (cpos, N_PRED)->f;
672               double n_neg_lt = case_data_idx (cneg, N_PRED)->f;
673
674               rs[i].auc += n_pos_gt * n_neg_eq + (n_pos_eq * n_neg_eq) / 2.0;
675               rs[i].q1hat +=
676                 n_neg_eq * ( pow2 (n_pos_gt) + n_pos_gt * n_pos_eq + pow2 (n_pos_eq) / 3.0);
677               rs[i].q2hat +=
678                 n_pos_eq * ( pow2 (n_neg_lt) + n_neg_lt * n_neg_eq + pow2 (n_neg_eq) / 3.0);
679             }
680
681           if ( cneg )
682             case_unref (cneg);
683         }
684
685       rs[i].auc /=  rs[i].n1 * rs[i].n2; 
686       if ( roc->invert ) 
687         rs[i].auc = 1 - rs[i].auc;
688
689       if ( roc->bi_neg_exp )
690         {
691           rs[i].q1hat = rs[i].auc / ( 2 - rs[i].auc);
692           rs[i].q2hat = 2 * pow2 (rs[i].auc) / ( 1 + rs[i].auc);
693         }
694       else
695         {
696           rs[i].q1hat /= rs[i].n2 * pow2 (rs[i].n1);
697           rs[i].q2hat /= rs[i].n1 * pow2 (rs[i].n2);
698         }
699     }
700 #endif
701
702   casereader_destroy (positives);
703   casereader_destroy (negatives);
704
705   output_roc (rs, roc);
706
707   free (rs);
708 }
709
710
711
712
713 static void
714 show_auc  (struct roc_state *rs, const struct cmd_roc *roc)
715 {
716   int i;
717   const int n_fields = roc->print_se ? 5 : 1;
718   const int n_cols = roc->n_vars > 1 ? n_fields + 1: n_fields;
719   const int n_rows = 2 + roc->n_vars;
720   struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
721
722   if ( roc->n_vars > 1)
723     tab_title (tbl, _("Area Under the Curve"));
724   else
725     tab_title (tbl, _("Area Under the Curve (%s)"), var_to_string (roc->vars[0]));
726
727   tab_headers (tbl, n_cols - n_fields, 0, 1, 0);
728
729   tab_dim (tbl, tab_natural_dimensions, NULL);
730
731   tab_text (tbl, n_cols - n_fields, 1, TAT_TITLE, _("Area"));
732
733   tab_hline (tbl, TAL_2, 0, n_cols - 1, 2);
734
735   tab_box (tbl,
736            TAL_2, TAL_2,
737            -1, TAL_1,
738            0, 0,
739            n_cols - 1,
740            n_rows - 1);
741
742   if ( roc->print_se )
743     {
744       tab_text (tbl, n_cols - 4, 1, TAT_TITLE, _("Std. Error"));
745       tab_text (tbl, n_cols - 3, 1, TAT_TITLE, _("Asymptotic Sig."));
746
747       tab_text (tbl, n_cols - 2, 1, TAT_TITLE, _("Lower Bound"));
748       tab_text (tbl, n_cols - 1, 1, TAT_TITLE, _("Upper Bound"));
749
750       tab_joint_text (tbl, n_cols - 2, 0, 4, 0,
751                       TAT_TITLE | TAB_CENTER | TAT_PRINTF,
752                       _("Asymp. %g%% Confidence Interval"), roc->ci);
753       tab_vline (tbl, 0, n_cols - 1, 0, 0);
754       tab_hline (tbl, TAL_1, n_cols - 2, n_cols - 1, 1);
755     }
756
757   if ( roc->n_vars > 1)
758     tab_text (tbl, 0, 1, TAT_TITLE, _("Variable under test"));
759
760   if ( roc->n_vars > 1)
761     tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
762
763
764   for ( i = 0 ; i < roc->n_vars ; ++i )
765     {
766       tab_text (tbl, 0, 2 + i, TAT_TITLE, var_to_string (roc->vars[i]));
767
768       tab_double (tbl, n_cols - n_fields, 2 + i, 0, rs[i].auc, NULL);
769
770       if ( roc->print_se )
771         {
772           double se ;
773           const double sd_0_5 = sqrt ((rs[i].n1 + rs[i].n2 + 1) /
774                                       (12 * rs[i].n1 * rs[i].n2));
775           double ci ;
776           double yy ;
777
778           se = rs[i].auc * (1 - rs[i].auc) + (rs[i].n1 - 1) * (rs[i].q1hat - pow2 (rs[i].auc)) +
779             (rs[i].n2 - 1) * (rs[i].q2hat - pow2 (rs[i].auc));
780
781           se /= rs[i].n1 * rs[i].n2;
782
783           se = sqrt (se);
784
785           tab_double (tbl, n_cols - 4, 2 + i, 0,
786                       se,
787                       NULL);
788
789           ci = 1 - roc->ci / 100.0;
790           yy = gsl_cdf_gaussian_Qinv (ci, se) ;
791
792           tab_double (tbl, n_cols - 2, 2 + i, 0,
793                       rs[i].auc - yy,
794                       NULL);
795
796           tab_double (tbl, n_cols - 1, 2 + i, 0,
797                       rs[i].auc + yy,
798                       NULL);
799
800           tab_double (tbl, n_cols - 3, 2 + i, 0,
801                       2.0 * gsl_cdf_ugaussian_Q (fabs ((rs[i].auc - 0.5 ) / sd_0_5)),
802                       NULL);
803         }
804     }
805
806   tab_submit (tbl);
807 }
808
809
810 static void
811 show_summary (const struct cmd_roc *roc)
812 {
813   const int n_cols = 3;
814   const int n_rows = 4;
815   struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
816
817   tab_title (tbl, _("Case Summary"));
818
819   tab_headers (tbl, 1, 0, 2, 0);
820
821   tab_dim (tbl, tab_natural_dimensions, NULL);
822
823   tab_box (tbl,
824            TAL_2, TAL_2,
825            -1, -1,
826            0, 0,
827            n_cols - 1,
828            n_rows - 1);
829
830   tab_hline (tbl, TAL_2, 0, n_cols - 1, 2);
831   tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
832
833
834   tab_hline (tbl, TAL_2, 1, n_cols - 1, 1);
835   tab_vline (tbl, TAL_1, 2, 1, n_rows - 1);
836
837
838   tab_text (tbl, 0, 1, TAT_TITLE | TAB_LEFT, var_to_string (roc->state_var));
839   tab_text (tbl, 1, 1, TAT_TITLE, _("Unweighted"));
840   tab_text (tbl, 2, 1, TAT_TITLE, _("Weighted"));
841
842   tab_joint_text (tbl, 1, 0, 2, 0,
843                   TAT_TITLE | TAB_CENTER,
844                   _("Valid N (listwise)"));
845
846
847   tab_text (tbl, 0, 2, TAB_LEFT, _("Positive"));
848   tab_text (tbl, 0, 3, TAB_LEFT, _("Negative"));
849
850
851 #if 0
852   tab_double (tbl, 1, 2, 0, roc->pos, &F_8_0);
853   tab_double (tbl, 1, 3, 0, roc->neg, &F_8_0);
854
855   tab_double (tbl, 2, 2, 0, roc->pos_weighted, 0);
856   tab_double (tbl, 2, 3, 0, roc->neg_weighted, 0);
857 #endif
858
859   tab_submit (tbl);
860 }
861
862
863 static void
864 show_coords (struct roc_state *rs, const struct cmd_roc *roc)
865 {
866   int x = 1;
867   int i;
868   const int n_cols = roc->n_vars > 1 ? 4 : 3;
869   int n_rows = 1;
870   struct tab_table *tbl ;
871
872   for (i = 0; i < roc->n_vars; ++i)
873     n_rows += casereader_count_cases (rs[i].cutpoint_rdr);
874
875   tbl = tab_create (n_cols, n_rows, 0);
876
877   if ( roc->n_vars > 1)
878     tab_title (tbl, _("Coordinates of the Curve"));
879   else
880     tab_title (tbl, _("Coordinates of the Curve (%s)"), var_to_string (roc->vars[0]));
881
882
883   tab_headers (tbl, 1, 0, 1, 0);
884
885   tab_dim (tbl, tab_natural_dimensions, NULL);
886
887   tab_hline (tbl, TAL_2, 0, n_cols - 1, 1);
888
889   if ( roc->n_vars > 1)
890     tab_text (tbl, 0, 0, TAT_TITLE, _("Test variable"));
891
892   tab_text (tbl, n_cols - 3, 0, TAT_TITLE, _("Positive if greater than or equal to"));
893   tab_text (tbl, n_cols - 2, 0, TAT_TITLE, _("Sensitivity"));
894   tab_text (tbl, n_cols - 1, 0, TAT_TITLE, _("1 - Specificity"));
895
896   tab_box (tbl,
897            TAL_2, TAL_2,
898            -1, TAL_1,
899            0, 0,
900            n_cols - 1,
901            n_rows - 1);
902
903   if ( roc->n_vars > 1)
904     tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
905
906   for (i = 0; i < roc->n_vars; ++i)
907     {
908       struct ccase *cc;
909       struct casereader *r = casereader_clone (rs[i].cutpoint_rdr);
910
911       if ( roc->n_vars > 1)
912         tab_text (tbl, 0, x, TAT_TITLE, var_to_string (roc->vars[i]));
913
914       if ( i > 0)
915         tab_hline (tbl, TAL_1, 0, n_cols - 1, x);
916
917
918       for (; (cc = casereader_read (r)) != NULL;
919            case_unref (cc), x++)
920         {
921           const double se = case_data_idx (cc, TP)->f /
922             (
923              case_data_idx (cc, TP)->f
924              +
925              case_data_idx (cc, FN)->f
926              );
927
928           const double sp = case_data_idx (cc, TN)->f /
929             (
930              case_data_idx (cc, TN)->f
931              +
932              case_data_idx (cc, FP)->f
933              );
934
935           tab_double (tbl, n_cols - 3, x, 0, case_data_idx (cc, CUTPOINT)->f,
936                       var_get_print_format (roc->vars[i]));
937
938           tab_double (tbl, n_cols - 2, x, 0, se, NULL);
939           tab_double (tbl, n_cols - 1, x, 0, 1 - sp, NULL);
940         }
941
942       casereader_destroy (r);
943     }
944
945   tab_submit (tbl);
946 }
947
948
949 static void
950 draw_roc (struct roc_state *rs, const struct cmd_roc *roc)
951 {
952   int i;
953
954   struct chart *roc_chart = chart_create ();
955
956   chart_write_title (roc_chart, _("ROC Curve"));
957   chart_write_xlabel (roc_chart, _("1 - Specificity"));
958   chart_write_ylabel (roc_chart, _("Sensitivity"));
959
960   chart_write_xscale (roc_chart, 0, 1, 5);
961   chart_write_yscale (roc_chart, 0, 1, 5);
962
963   if ( roc->reference )
964     {
965       chart_line (roc_chart, 1.0, 0,
966                   0.0, 1.0,
967                   CHART_DIM_X);
968     }
969
970   for (i = 0; i < roc->n_vars; ++i)
971     {
972       struct ccase *cc;
973       struct casereader *r = casereader_clone (rs[i].cutpoint_rdr);
974
975       chart_vector_start (roc_chart, var_get_name (roc->vars[i]));
976       for (; (cc = casereader_read (r)) != NULL;
977            case_unref (cc))
978         {
979           double se = case_data_idx (cc, TP)->f;
980           double sp = case_data_idx (cc, TN)->f;
981
982           se /= case_data_idx (cc, FN)->f +
983             case_data_idx (cc, TP)->f ;
984
985           sp /= case_data_idx (cc, TN)->f +
986             case_data_idx (cc, FP)->f ;
987
988           chart_vector (roc_chart, 1 - sp, se);
989         }
990       chart_vector_end (roc_chart);
991       casereader_destroy (r);
992     }
993
994   chart_write_legend (roc_chart);
995
996   chart_submit (roc_chart);
997 }
998
999
1000 static void
1001 output_roc (struct roc_state *rs, const struct cmd_roc *roc)
1002 {
1003   show_summary (roc);
1004
1005   if ( roc->curve )
1006     draw_roc (rs, roc);
1007
1008   show_auc (rs, roc);
1009
1010
1011   if ( roc->print_coords )
1012     show_coords (rs, roc);
1013 }
1014