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/>. */
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>
25 #include <data/casegrouper.h>
26 #include <data/casereader.h>
27 #include <data/casewriter.h>
28 #include <data/dictionary.h>
29 #include <math/sort.h>
31 #include <libpspp/misc.h>
33 #include <gsl/gsl_cdf.h>
34 #include <output/table.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
43 const struct variable **vars;
45 struct variable *state_var ;
46 union value state_value;
48 /* Plot the roc curve */
50 /* Plot the reference line */
57 bool bi_neg_exp; /* True iff the bi-negative exponential critieria
59 enum mv_class exclude;
61 bool invert ; /* True iff a smaller test result variable indicates
65 static int run_roc (struct dataset *ds, struct cmd_roc *roc);
68 cmd_roc (struct lexer *lexer, struct dataset *ds)
71 const struct dictionary *dict = dataset_dict (ds);
76 roc.print_coords = false;
79 roc.reference = false;
81 roc.bi_neg_exp = false;
84 if (!parse_variables_const (lexer, dict, &roc.vars, &roc.n_vars,
85 PV_APPEND | PV_NO_DUPLICATE | PV_NUMERIC))
88 if ( ! lex_force_match (lexer, T_BY))
93 roc.state_var = parse_variable (lexer, dict);
95 if ( !lex_force_match (lexer, '('))
100 parse_value (lexer, &roc.state_value, var_get_width (roc.state_var));
103 if ( !lex_force_match (lexer, ')'))
109 while (lex_token (lexer) != '.')
111 lex_match (lexer, '/');
112 if (lex_match_id (lexer, "MISSING"))
114 lex_match (lexer, '=');
115 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
117 if (lex_match_id (lexer, "INCLUDE"))
119 roc.exclude = MV_SYSTEM;
121 else if (lex_match_id (lexer, "EXCLUDE"))
123 roc.exclude = MV_ANY;
127 lex_error (lexer, NULL);
132 else if (lex_match_id (lexer, "PLOT"))
134 lex_match (lexer, '=');
135 if (lex_match_id (lexer, "CURVE"))
138 if (lex_match (lexer, '('))
140 roc.reference = true;
141 lex_force_match_id (lexer, "REFERENCE");
142 lex_force_match (lexer, ')');
145 else if (lex_match_id (lexer, "NONE"))
151 lex_error (lexer, NULL);
155 else if (lex_match_id (lexer, "PRINT"))
157 lex_match (lexer, '=');
158 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
160 if (lex_match_id (lexer, "SE"))
164 else if (lex_match_id (lexer, "COORDINATES"))
166 roc.print_coords = true;
170 lex_error (lexer, NULL);
175 else if (lex_match_id (lexer, "CRITERIA"))
177 lex_match (lexer, '=');
178 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
180 if (lex_match_id (lexer, "CUTOFF"))
182 lex_force_match (lexer, '(');
183 if (lex_match_id (lexer, "INCLUDE"))
185 roc.exclude = MV_SYSTEM;
187 else if (lex_match_id (lexer, "EXCLUDE"))
189 roc.exclude = MV_USER | MV_SYSTEM;
193 lex_error (lexer, NULL);
196 lex_force_match (lexer, ')');
198 else if (lex_match_id (lexer, "TESTPOS"))
200 lex_force_match (lexer, '(');
201 if (lex_match_id (lexer, "LARGE"))
205 else if (lex_match_id (lexer, "SMALL"))
211 lex_error (lexer, NULL);
214 lex_force_match (lexer, ')');
216 else if (lex_match_id (lexer, "CI"))
218 lex_force_match (lexer, '(');
219 lex_force_num (lexer);
220 roc.ci = lex_number (lexer);
222 lex_force_match (lexer, ')');
224 else if (lex_match_id (lexer, "DISTRIBUTION"))
226 lex_force_match (lexer, '(');
227 if (lex_match_id (lexer, "FREE"))
229 roc.bi_neg_exp = false;
231 else if (lex_match_id (lexer, "NEGEXPO"))
233 roc.bi_neg_exp = true;
237 lex_error (lexer, NULL);
240 lex_force_match (lexer, ')');
244 lex_error (lexer, NULL);
251 lex_error (lexer, NULL);
265 do_roc (struct cmd_roc *roc, struct casereader *group, struct dictionary *dict);
269 run_roc (struct dataset *ds, struct cmd_roc *roc)
271 struct dictionary *dict = dataset_dict (ds);
273 struct casereader *group;
275 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
276 while (casegrouper_get_next_group (grouper, &group))
278 do_roc (roc, group, dataset_dict (ds));
280 ok = casegrouper_destroy (grouper);
281 ok = proc_commit (ds) && ok;
288 dump_casereader (struct casereader *reader)
291 struct casereader *r = casereader_clone (reader);
293 for ( ; (c = casereader_read (r) ); case_unref (c))
296 for (i = 0 ; i < case_get_value_cnt (c); ++i)
298 printf ("%g ", case_data_idx (c, i)->f);
303 casereader_destroy (r);
307 match_positives (const struct ccase *c, void *aux)
309 struct cmd_roc *roc = aux;
311 return 0 == value_compare_3way (case_data (c, roc->state_var),
313 var_get_width (roc->state_var));
333 static void output_roc (struct roc_state *rs, const struct cmd_roc *roc);
337 static struct casereader *
338 process_group (const struct variable *var, struct casereader *reader,
339 bool (*pred) (double, double),
340 const struct dictionary *dict,
343 const struct variable *w = dict_get_weight (dict);
344 const int weight_idx = w ? var_get_case_index (w) :
345 caseproto_get_n_widths (casereader_get_proto (reader)) - 1;
347 struct casereader *r1 =
348 casereader_create_distinct (sort_execute_1var (reader, var), var, w);
352 struct casereader *rclone = casereader_clone (r1);
353 struct casewriter *wtr;
354 struct caseproto *proto = caseproto_create ();
356 proto = caseproto_add_width (proto, 0);
357 proto = caseproto_add_width (proto, 0);
358 proto = caseproto_add_width (proto, 0);
360 wtr = autopaging_writer_create (proto);
364 for ( ; (c1 = casereader_read (r1) ); case_unref (c1))
367 struct casereader *r2 = casereader_clone (rclone);
369 const double weight1 = case_data_idx (c1, weight_idx)->f;
370 const double d1 = case_data (c1, var)->f;
375 struct ccase *new_case = case_create (proto);
379 for ( ; (c2 = casereader_read (r2) ); case_unref (c2))
381 const double d2 = case_data (c2, var)->f;
382 const double weight2 = case_data_idx (c2, weight_idx)->f;
389 else if ( pred (d2, d1))
395 case_data_rw_idx (new_case, VALUE)->f = d1;
396 case_data_rw_idx (new_case, N_EQ)->f = n_eq;
397 case_data_rw_idx (new_case, N_PRED)->f = n_pred;
399 casewriter_write (wtr, new_case);
401 casereader_destroy (r2);
404 casereader_destroy (r1);
405 casereader_destroy (rclone);
407 return casewriter_make_reader (wtr);
411 gt (double d1, double d2)
417 lt (double d1, double d2)
424 do_roc (struct cmd_roc *roc, struct casereader *input, struct dictionary *dict)
428 struct roc_state *rs = xcalloc (roc->n_vars, sizeof *rs);
430 const struct caseproto *proto = casereader_get_proto (input);
432 struct casewriter *neg_wtr = autopaging_writer_create (proto);
434 struct casereader *negatives = NULL;
436 struct casereader *positives =
437 casereader_create_filter_func (input,
444 for (i = 0 ; i < roc->n_vars; ++i)
447 struct casereader *n_neg ;
448 const struct variable *var = roc->vars[i];
450 struct casereader *neg ;
451 struct casereader *pos = casereader_clone (positives);
453 struct casereader *n_pos = process_group (var, pos, gt, dict, &rs[i].n1);
455 if ( negatives == NULL)
457 negatives = casewriter_make_reader (neg_wtr);
460 neg = casereader_clone (negatives);
462 n_neg = process_group (var, neg, lt, dict, &rs[i].n2);
464 /* Simple join on VALUE */
465 for ( ; (cpos = casereader_read (n_pos) ); case_unref (cpos))
467 struct ccase *cneg = NULL;
468 double dneg = -DBL_MAX;
469 const double dpos = case_data_idx (cpos, VALUE)->f;
475 cneg = casereader_read (n_neg);
476 dneg = case_data_idx (cneg, VALUE)->f;
481 double n_pos_eq = case_data_idx (cpos, N_EQ)->f;
482 double n_neg_eq = case_data_idx (cneg, N_EQ)->f;
483 double n_pos_gt = case_data_idx (cpos, N_PRED)->f;
484 double n_neg_lt = case_data_idx (cneg, N_PRED)->f;
486 rs[i].auc += n_pos_gt * n_neg_eq + (n_pos_eq * n_neg_eq) / 2.0;
488 n_neg_eq * ( pow2 (n_pos_gt) + n_pos_gt * n_pos_eq + pow2 (n_pos_eq) / 3.0);
490 n_pos_eq * ( pow2 (n_neg_lt) + n_neg_lt * n_neg_eq + pow2 (n_neg_eq) / 3.0);
497 rs[i].auc /= rs[i].n1 * rs[i].n2;
499 rs[i].auc = 1 - rs[i].auc;
501 if ( roc->bi_neg_exp )
503 rs[i].q1hat = rs[i].auc / ( 2 - rs[i].auc);
504 rs[i].q2hat = 2 * pow2 (rs[i].auc) / ( 1 + rs[i].auc);
508 rs[i].q1hat /= rs[i].n2 * pow2 (rs[i].n1);
509 rs[i].q2hat /= rs[i].n1 * pow2 (rs[i].n2);
513 casereader_destroy (positives);
514 casereader_destroy (negatives);
516 output_roc (rs, roc);
525 show_auc (struct roc_state *rs, const struct cmd_roc *roc)
528 const int n_fields = roc->print_se ? 5 : 1;
529 const int n_cols = roc->n_vars > 1 ? n_fields + 1: n_fields;
530 const int n_rows = 2 + roc->n_vars;
531 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
533 if ( roc->n_vars > 1)
534 tab_title (tbl, _("Area Under the Curve"));
536 tab_title (tbl, _("Area Under the Curve (%s)"), var_to_string (roc->vars[0]));
538 tab_headers (tbl, n_cols - n_fields, 0, 1, 0);
540 tab_dim (tbl, tab_natural_dimensions, NULL);
542 tab_text (tbl, n_cols - n_fields, 1, TAT_TITLE, _("Area"));
544 tab_hline (tbl, TAL_2, 0, n_cols - 1, 2);
555 tab_text (tbl, n_cols - 4, 1, TAT_TITLE, _("Std. Error"));
556 tab_text (tbl, n_cols - 3, 1, TAT_TITLE, _("Asymptotic Sig."));
558 tab_text (tbl, n_cols - 2, 1, TAT_TITLE, _("Lower Bound"));
559 tab_text (tbl, n_cols - 1, 1, TAT_TITLE, _("Upper Bound"));
561 tab_joint_text (tbl, n_cols - 2, 0, 4, 0,
562 TAT_TITLE | TAB_CENTER | TAT_PRINTF,
563 _("Asymp. %g%% Confidence Interval"), roc->ci);
564 tab_vline (tbl, 0, n_cols - 1, 0, 0);
565 tab_hline (tbl, TAL_1, n_cols - 2, n_cols - 1, 1);
568 if ( roc->n_vars > 1)
569 tab_text (tbl, 0, 1, TAT_TITLE, _("Variable under test"));
571 if ( roc->n_vars > 1)
572 tab_vline (tbl, TAL_2, 1, 0, n_rows - 1);
575 for ( i = 0 ; i < roc->n_vars ; ++i )
577 tab_text (tbl, 0, 2 + i, TAT_TITLE, var_to_string (roc->vars[i]));
579 tab_double (tbl, n_cols - n_fields, 2 + i, 0, rs[i].auc, NULL);
585 const double sd_0_5 = sqrt ((rs[i].n1 + rs[i].n2 + 1) /
586 (12 * rs[i].n1 * rs[i].n2));
590 se = rs[i].auc * (1 - rs[i].auc) + (rs[i].n1 - 1) * (rs[i].q1hat - pow2 (rs[i].auc)) +
591 (rs[i].n2 - 1) * (rs[i].q2hat - pow2 (rs[i].auc));
593 se /= rs[i].n1 * rs[i].n2;
597 tab_double (tbl, n_cols - 4, 2 + i, 0,
601 ci = 1 - roc->ci / 100.0;
602 yy = gsl_cdf_gaussian_Qinv (ci, se) ;
604 tab_double (tbl, n_cols - 2, 2 + i, 0,
608 tab_double (tbl, n_cols - 1, 2 + i, 0,
612 tab_double (tbl, n_cols - 3, 2 + i, 0,
613 2.0 * gsl_cdf_ugaussian_Q (fabs ((rs[i].auc - 0.5 ) / sd_0_5)),
625 output_roc (struct roc_state *rs, const struct cmd_roc *roc)
637 if ( roc->print_coords )
638 show_coords (rs, roc);