[This article was first published on R snippets, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
library(ROCR)
gain.chart <- function(n) {
gain.chart <- function(n) {
score <- runif(n)< o:p>
y <- (runif(n) < score)< o:p>
plot(performance(prediction(score, y), “tpr”, “rpp”),
lwd = 7, main = paste(“N =”, n))
lines(ecdf((rank(-score)[y == T]) / n),< o:p>
verticals = T, do.points = F, col = “red”, lwd = 3)< o:p>
}< o:p>
set.seed(1)< o:p>
par(mfrow = c(1, 2))< o:p>
gain.chart(10)
gain.chart(10000)
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.