1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include <output/charts/roc-chart.h>
21 #include <output/chart-provider.h>
22 #include <output/charts/cartesian.h>
23 #include <output/charts/plot-chart.h>
24 #include <data/casereader.h>
25 #include <language/stats/roc.h>
30 #define _(msgid) gettext (msgid)
35 struct casereader *cutpoint_reader;
44 size_t allocated_vars;
47 static const struct chart_class roc_chart_class;
50 roc_chart_create (bool reference)
52 struct roc_chart *rc = xmalloc (sizeof *rc);
53 chart_init (&rc->chart, &roc_chart_class);
54 rc->reference = reference;
57 rc->allocated_vars = 0;
62 roc_chart_add_var (struct roc_chart *rc, const char *var_name,
63 const struct casereader *cutpoint_reader)
67 if (rc->n_vars >= rc->allocated_vars)
68 rc->vars = x2nrealloc (rc->vars, &rc->allocated_vars, sizeof *rc->vars);
70 rv = &rc->vars[rc->n_vars++];
71 rv->name = xstrdup (var_name);
72 rv->cutpoint_reader = casereader_clone (cutpoint_reader);
76 roc_chart_get_chart (struct roc_chart *rc)
82 roc_chart_draw (const struct chart *chart, cairo_t *cr,
83 struct chart_geometry *geom)
85 const struct roc_chart *rc = UP_CAST (chart, struct roc_chart, chart);
88 chart_write_title (cr, geom, _("ROC Curve"));
89 chart_write_xlabel (cr, geom, _("1 - Specificity"));
90 chart_write_ylabel (cr, geom, _("Sensitivity"));
92 chart_write_xscale (cr, geom, 0, 1, 5);
93 chart_write_yscale (cr, geom, 0, 1, 5);
97 chart_line (cr, geom, 1.0, 0,
102 for (i = 0; i < rc->n_vars; ++i)
104 const struct roc_var *rv = &rc->vars[i];
105 struct casereader *r = casereader_clone (rv->cutpoint_reader);
108 chart_vector_start (cr, geom, rv->name);
109 for (; (cc = casereader_read (r)) != NULL; case_unref (cc))
111 double se = case_data_idx (cc, ROC_TP)->f;
112 double sp = case_data_idx (cc, ROC_TN)->f;
114 se /= case_data_idx (cc, ROC_FN)->f + case_data_idx (cc, ROC_TP)->f ;
115 sp /= case_data_idx (cc, ROC_TN)->f + case_data_idx (cc, ROC_FP)->f ;
117 chart_vector (cr, geom, 1 - sp, se);
119 chart_vector_end (cr, geom);
120 casereader_destroy (r);
123 chart_write_legend (cr, geom);
127 roc_chart_destroy (struct chart *chart)
129 struct roc_chart *rc = UP_CAST (chart, struct roc_chart, chart);
132 for (i = 0; i < rc->n_vars; i++)
134 struct roc_var *rv = &rc->vars[i];
136 casereader_destroy (rv->cutpoint_reader);
142 static const struct chart_class roc_chart_class =