R Function : Gain and Lift Table

This tutorial demonstrates how to calculate gain and lift chart with R. Gain and Lift charts are used to measure the performance of a predictive classification model. They measure how much better results one can expect with the predictive classification model comparing without a model. The lift metrics is a very popular metrics to evaluate the customer and marketing analytics model. The 'lift' term comes from marketing analytics domain.

In the first parameter of the lift function, you need to define a column wherein binary target variable (dependent variable) is stored. In this case, churn$target : churn is the data frame and target is the dependent variable column.

In the second parameter, you need to specify the predicted probability variable.

The Cumulative Lift of 3.4 for top two deciles, means that when selecting 20% of the records based on the model, one can expect 3.4 times the total number of targets (events) found by randomly selecting 20%-of-records without a model. In terms of customer attrition (churn) model, we can say we can cover 3.4 times the number of attritors by selecting only 20% of the customers based on the model as compared to 20% customer selection randomly.

Output

The output of gain and lift table is shown in the image below -

Gain and Lift Table

Interpretation of Gain

% of targets (events) covered at a given decile level. For example, 67.9% of events covered in top 20% of data based on model.

Export gain and lift table

You can export gain table in CSV format by submitting the following code -

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Thanks for the code. However, after running it, the gain table that I am getting, its not dividing the data into 10 equal groups, each group is having different observation number, ideally which should not be the case. Need help on this.

It is because some of predicted probability scores are equal so one or two groups are not of same size. I am assuming the difference of number of observations in each group is very minimal. For example, c(0.1,0.2,0.3,0.3,0.4,0.5) - you cannot assign different rank to 0.3 in this vector.

To have same observations in each group, you can add ties.method = "first" after groups = 10 in the code. Revised code would be -gaint = gains(actual= valgain[,1], predicted=pred ,groups=10, ties.method = "first")