3dff0407f8a301219cbbcee8ecaf6bc3d9ee1088
[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     struct variable *iv = var_create_internal (CUTPOINT);
564     subcase_init_var (&ordering, iv, SC_ASCEND);
565
566
567     proto = caseproto_add_width (proto, 0); /* cutpoint */
568     proto = caseproto_add_width (proto, 0); /* TP */
569     proto = caseproto_add_width (proto, 0); /* FN */
570     proto = caseproto_add_width (proto, 0); /* TN */
571     proto = caseproto_add_width (proto, 0); /* FP */
572
573
574     for (i = 0 ; i < roc->n_vars; ++i)
575       {
576         rs[i].cutpoint_wtr = sort_create_writer (&ordering, proto);
577         rs[i].prev_result = SYSMIS;
578         rs[i].max = -DBL_MAX;
579         rs[i].min = DBL_MAX;
580       }
581
582     for (; (c = casereader_read (r)) != NULL; case_unref (c))
583       {
584         const double weight = dict_get_case_weight (dict, c, NULL);
585         for (i = 0 ; i < roc->n_vars; ++i)
586           {
587             const double result = case_data (c, roc->vars[i])->f;
588
589             minimize (&rs[i].min, result);
590             maximize (&rs[i].max, result);
591
592             if ( rs[i].prev_result != SYSMIS && rs[i].prev_result != result )
593               {
594                 const double mean = (result + rs[i].prev_result ) / 2.0;
595                 append_cutpoint (rs[i].cutpoint_wtr, mean);
596               }
597
598             rs[i].prev_result = result;
599           }
600       }
601     casereader_destroy (r);
602
603
604     /* Append the min and max cutpoints */
605     for (i = 0 ; i < roc->n_vars; ++i)
606       {
607         append_cutpoint (rs[i].cutpoint_wtr, rs[i].min - 1);
608         append_cutpoint (rs[i].cutpoint_wtr, rs[i].max + 1);
609
610         rs[i].cutpoint_rdr = casewriter_make_reader (rs[i].cutpoint_wtr);
611       }
612   }
613
614  positives = 
615     casereader_create_filter_func (input,
616                                    match_positives,
617                                    NULL,
618                                    roc,
619                                    neg_wtr);
620
621
622   for (i = 0 ; i < roc->n_vars; ++i)
623     {
624       struct ccase *cpos;
625       struct casereader *n_neg ;
626       const struct variable *var = roc->vars[i];
627
628       struct casereader *neg ;
629       struct casereader *pos = casereader_clone (positives);
630
631       struct casereader *n_pos =
632         process_positive_group (var, pos, dict, &rs[i]);
633
634       if ( negatives == NULL)
635         {
636           negatives = casewriter_make_reader (neg_wtr);
637         }
638
639       neg = casereader_clone (negatives);
640
641       n_neg = process_negative_group (var, neg, dict, &rs[i]);
642
643
644       /* Simple join on VALUE */
645       for ( ; (cpos = casereader_read (n_pos) ); case_unref (cpos))
646         {
647           struct ccase *cneg = NULL;
648           double dneg = -DBL_MAX;
649           const double dpos = case_data_idx (cpos, VALUE)->f;
650           while (dneg < dpos)
651             {
652               if ( cneg )
653                 case_unref (cneg);
654
655               cneg = casereader_read (n_neg);
656               if ( ! cneg )
657                 break;
658               dneg = case_data_idx (cneg, VALUE)->f;
659             }
660         
661           if ( dpos == dneg )
662             {
663               double n_pos_eq = case_data_idx (cpos, N_EQ)->f;
664               double n_neg_eq = case_data_idx (cneg, N_EQ)->f;
665               double n_pos_gt = case_data_idx (cpos, N_PRED)->f;
666               double n_neg_lt = case_data_idx (cneg, N_PRED)->f;
667
668               rs[i].auc += n_pos_gt * n_neg_eq + (n_pos_eq * n_neg_eq) / 2.0;
669               rs[i].q1hat +=
670                 n_neg_eq * ( pow2 (n_pos_gt) + n_pos_gt * n_pos_eq + pow2 (n_pos_eq) / 3.0);
671               rs[i].q2hat +=
672                 n_pos_eq * ( pow2 (n_neg_lt) + n_neg_lt * n_neg_eq + pow2 (n_neg_eq) / 3.0);
673             }
674
675           if ( cneg )
676             case_unref (cneg);
677         }
678
679       rs[i].auc /=  rs[i].n1 * rs[i].n2; 
680       if ( roc->invert ) 
681         rs[i].auc = 1 - rs[i].auc;
682
683       if ( roc->bi_neg_exp )
684         {
685           rs[i].q1hat = rs[i].auc / ( 2 - rs[i].auc);
686           rs[i].q2hat = 2 * pow2 (rs[i].auc) / ( 1 + rs[i].auc);
687         }
688       else
689         {
690           rs[i].q1hat /= rs[i].n2 * pow2 (rs[i].n1);
691           rs[i].q2hat /= rs[i].n1 * pow2 (rs[i].n2);
692         }
693     }
694
695   casereader_destroy (positives);
696   casereader_destroy (negatives);
697
698   output_roc (rs, roc);
699
700   free (rs);
701 }
702
703
704
705
706 static void
707 show_auc  (struct roc_state *rs, const struct cmd_roc *roc)
708 {
709   int i;
710   const int n_fields = roc->print_se ? 5 : 1;
711   const int n_cols = roc->n_vars > 1 ? n_fields + 1: n_fields;
712   const int n_rows = 2 + roc->n_vars;
713   struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
714
715   if ( roc->n_vars > 1)
716     tab_title (tbl, _("Area Under the Curve"));
717   else
718     tab_title (tbl, _("Area Under the Curve (%s)"), var_to_string (roc->vars[0]));
719
720   tab_headers (tbl, n_cols - n_fields, 0, 1, 0);
721
722   tab_dim (tbl, tab_natural_dimensions, NULL);
723
724   tab_text (tbl, n_cols - n_fields, 1, TAT_TITLE, _("Area"));
725
726   tab_hline (tbl, TAL_2, 0, n_cols - 1, 2);
727
728   tab_box (tbl,
729            TAL_2, TAL_2,
730            -1, TAL_1,
731            0, 0,
732            n_cols - 1,
733            n_rows - 1);
734
735   if ( roc->print_se )
736     {
737       tab_text (tbl, n_cols - 4, 1, TAT_TITLE, _("Std. Error"));
738       tab_text (tbl, n_cols - 3, 1, TAT_TITLE, _("Asymptotic Sig."));
739
740       tab_text (tbl, n_cols - 2, 1, TAT_TITLE, _("Lower Bound"));
741       tab_text (tbl, n_cols - 1, 1, TAT_TITLE, _("Upper Bound"));
742
743       tab_joint_text (tbl, n_cols - 2, 0, 4, 0,
744                       TAT_TITLE | TAB_CENTER | TAT_PRINTF,
745                       _("Asymp. %g%% Confidence Interval"), roc->ci);
746       tab_vline (tbl, 0, n_cols - 1, 0, 0);
747       tab_hline (tbl, TAL_1, n_cols - 2, n_cols - 1, 1);
748     }
749
750   if ( roc->n_vars > 1)
751     tab_text (tbl, 0, 1, TAT_TITLE, _("Variable under test"));
752
753   if ( roc->n_vars > 1)
754     tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
755
756
757   for ( i = 0 ; i < roc->n_vars ; ++i )
758     {
759       tab_text (tbl, 0, 2 + i, TAT_TITLE, var_to_string (roc->vars[i]));
760
761       tab_double (tbl, n_cols - n_fields, 2 + i, 0, rs[i].auc, NULL);
762
763       if ( roc->print_se )
764         {
765           double se ;
766           const double sd_0_5 = sqrt ((rs[i].n1 + rs[i].n2 + 1) /
767                                       (12 * rs[i].n1 * rs[i].n2));
768           double ci ;
769           double yy ;
770
771           se = rs[i].auc * (1 - rs[i].auc) + (rs[i].n1 - 1) * (rs[i].q1hat - pow2 (rs[i].auc)) +
772             (rs[i].n2 - 1) * (rs[i].q2hat - pow2 (rs[i].auc));
773
774           se /= rs[i].n1 * rs[i].n2;
775
776           se = sqrt (se);
777
778           tab_double (tbl, n_cols - 4, 2 + i, 0,
779                       se,
780                       NULL);
781
782           ci = 1 - roc->ci / 100.0;
783           yy = gsl_cdf_gaussian_Qinv (ci, se) ;
784
785           tab_double (tbl, n_cols - 2, 2 + i, 0,
786                       rs[i].auc - yy,
787                       NULL);
788
789           tab_double (tbl, n_cols - 1, 2 + i, 0,
790                       rs[i].auc + yy,
791                       NULL);
792
793           tab_double (tbl, n_cols - 3, 2 + i, 0,
794                       2.0 * gsl_cdf_ugaussian_Q (fabs ((rs[i].auc - 0.5 ) / sd_0_5)),
795                       NULL);
796         }
797     }
798
799   tab_submit (tbl);
800 }
801
802
803 static void
804 show_summary (const struct cmd_roc *roc)
805 {
806   const int n_cols = 3;
807   const int n_rows = 4;
808   struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
809
810   tab_title (tbl, _("Case Summary"));
811
812   tab_headers (tbl, 1, 0, 2, 0);
813
814   tab_dim (tbl, tab_natural_dimensions, NULL);
815
816   tab_box (tbl,
817            TAL_2, TAL_2,
818            -1, -1,
819            0, 0,
820            n_cols - 1,
821            n_rows - 1);
822
823   tab_hline (tbl, TAL_2, 0, n_cols - 1, 2);
824   tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
825
826
827   tab_hline (tbl, TAL_2, 1, n_cols - 1, 1);
828   tab_vline (tbl, TAL_1, 2, 1, n_rows - 1);
829
830
831   tab_text (tbl, 0, 1, TAT_TITLE | TAB_LEFT, var_to_string (roc->state_var));
832   tab_text (tbl, 1, 1, TAT_TITLE, _("Unweighted"));
833   tab_text (tbl, 2, 1, TAT_TITLE, _("Weighted"));
834
835   tab_joint_text (tbl, 1, 0, 2, 0,
836                   TAT_TITLE | TAB_CENTER,
837                   _("Valid N (listwise)"));
838
839
840   tab_text (tbl, 0, 2, TAB_LEFT, _("Positive"));
841   tab_text (tbl, 0, 3, TAB_LEFT, _("Negative"));
842
843
844 #if 0
845   tab_double (tbl, 1, 2, 0, roc->pos, &F_8_0);
846   tab_double (tbl, 1, 3, 0, roc->neg, &F_8_0);
847
848   tab_double (tbl, 2, 2, 0, roc->pos_weighted, 0);
849   tab_double (tbl, 2, 3, 0, roc->neg_weighted, 0);
850 #endif
851
852   tab_submit (tbl);
853 }
854
855
856 static void
857 show_coords (struct roc_state *rs, const struct cmd_roc *roc)
858 {
859   int x = 1;
860   int i;
861   const int n_cols = roc->n_vars > 1 ? 4 : 3;
862   int n_rows = 1;
863   struct tab_table *tbl ;
864
865   for (i = 0; i < roc->n_vars; ++i)
866     n_rows += casereader_count_cases (rs[i].cutpoint_rdr);
867
868   tbl = tab_create (n_cols, n_rows, 0);
869
870   if ( roc->n_vars > 1)
871     tab_title (tbl, _("Coordinates of the Curve"));
872   else
873     tab_title (tbl, _("Coordinates of the Curve (%s)"), var_to_string (roc->vars[0]));
874
875
876   tab_headers (tbl, 1, 0, 1, 0);
877
878   tab_dim (tbl, tab_natural_dimensions, NULL);
879
880   tab_hline (tbl, TAL_2, 0, n_cols - 1, 1);
881
882   if ( roc->n_vars > 1)
883     tab_text (tbl, 0, 0, TAT_TITLE, _("Test variable"));
884
885   tab_text (tbl, n_cols - 3, 0, TAT_TITLE, _("Positive if greater than or equal to"));
886   tab_text (tbl, n_cols - 2, 0, TAT_TITLE, _("Sensitivity"));
887   tab_text (tbl, n_cols - 1, 0, TAT_TITLE, _("1 - Specificity"));
888
889   tab_box (tbl,
890            TAL_2, TAL_2,
891            -1, TAL_1,
892            0, 0,
893            n_cols - 1,
894            n_rows - 1);
895
896   if ( roc->n_vars > 1)
897     tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
898
899   for (i = 0; i < roc->n_vars; ++i)
900     {
901       struct ccase *cc;
902       struct casereader *r = casereader_clone (rs[i].cutpoint_rdr);
903
904       if ( roc->n_vars > 1)
905         tab_text (tbl, 0, x, TAT_TITLE, var_to_string (roc->vars[i]));
906
907       if ( i > 0)
908         tab_hline (tbl, TAL_1, 0, n_cols - 1, x);
909
910
911       for (; (cc = casereader_read (r)) != NULL;
912            case_unref (cc), x++)
913         {
914           const double se = case_data_idx (cc, TP)->f /
915             (
916              case_data_idx (cc, TP)->f
917              +
918              case_data_idx (cc, FN)->f
919              );
920
921           const double sp = case_data_idx (cc, TN)->f /
922             (
923              case_data_idx (cc, TN)->f
924              +
925              case_data_idx (cc, FP)->f
926              );
927
928           tab_double (tbl, n_cols - 3, x, 0, case_data_idx (cc, CUTPOINT)->f,
929                       var_get_print_format (roc->vars[i]));
930
931           tab_double (tbl, n_cols - 2, x, 0, se, NULL);
932           tab_double (tbl, n_cols - 1, x, 0, 1 - sp, NULL);
933         }
934
935       casereader_destroy (r);
936     }
937
938   tab_submit (tbl);
939 }
940
941
942 static void
943 draw_roc (struct roc_state *rs, const struct cmd_roc *roc)
944 {
945   int i;
946
947   struct chart *roc_chart = chart_create ();
948
949   chart_write_title (roc_chart, _("ROC Curve"));
950   chart_write_xlabel (roc_chart, _("1 - Specificity"));
951   chart_write_ylabel (roc_chart, _("Sensitivity"));
952
953   chart_write_xscale (roc_chart, 0, 1, 5);
954   chart_write_yscale (roc_chart, 0, 1, 5);
955
956   if ( roc->reference )
957     {
958       chart_line (roc_chart, 1.0, 0,
959                   0.0, 1.0,
960                   CHART_DIM_X);
961     }
962
963   for (i = 0; i < roc->n_vars; ++i)
964     {
965       struct ccase *cc;
966       struct casereader *r = casereader_clone (rs[i].cutpoint_rdr);
967
968       chart_vector_start (roc_chart, var_get_name (roc->vars[i]));
969       for (; (cc = casereader_read (r)) != NULL;
970            case_unref (cc))
971         {
972           double se = case_data_idx (cc, TP)->f;
973           double sp = case_data_idx (cc, TN)->f;
974
975           se /= case_data_idx (cc, FN)->f +
976             case_data_idx (cc, TP)->f ;
977
978           sp /= case_data_idx (cc, TN)->f +
979             case_data_idx (cc, FP)->f ;
980
981           chart_vector (roc_chart, 1 - sp, se);
982         }
983       chart_vector_end (roc_chart);
984       casereader_destroy (r);
985     }
986
987   chart_write_legend (roc_chart);
988
989   chart_submit (roc_chart);
990 }
991
992
993 static void
994 output_roc (struct roc_state *rs, const struct cmd_roc *roc)
995 {
996   show_summary (roc);
997
998   if ( roc->curve )
999     draw_roc (rs, roc);
1000
1001   show_auc (rs, roc);
1002
1003
1004   if ( roc->print_coords )
1005     show_coords (rs, roc);
1006 }
1007