Plotting gain chart

October 29, 2011

(This article was first published on R snippets, and kindly contributed to R-bloggers)

Gain chart is a popular method to visually inspect model performance in binary prediction. It presents the percentage of captured positive responses as a function of selected percentage of a sample. It is easy to obtain it using ROCR package plotting “tpr” against “rpp”. However, it is worth to note that gain chart can be equivalently interpreted as empirical cumulative distribution function of random variable representing rank of randomly selected positive response divided by sample size. This equivalence is presented in the following code:

gain.chart <- function(n) {
    score <- runif(n)
    y <- (runif(n) < score)
    plot(performance(prediction(score, y)“tpr”“rpp”),
         lwd = 7, main = paste(“N =”, n))
    lines(ecdf((rank(-score)[y == T]) / n),
          verticals = T, do.points = F, col = “red”, lwd = 3)

par(mfrow = c(12))

The code plots the following gain charts:

For small samples the two methods do not produce identical plots as ecdf returns step function and ROCR plot provides linear interpolation at jumps.

To leave a comment for the author, please follow the link and comment on their blog: R snippets. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)