How do I create a gain chart in R for a decision tree model?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A gain chart shows how well a model ranks positive cases compared with random selection. For a decision tree in R, the workflow is: train the model, get predicted probabilities, sort observations by score, compute cumulative capture, and then plot the model curve against a baseline.
What a Gain Chart Measures
Suppose you are predicting who will respond to a campaign. A gain chart answers a business question like this:
"If we target the top 20 percent of people ranked by the model, what fraction of all responders do we capture?"
That is different from plain accuracy. Gain focuses on ranking quality, which is often what matters when you can only act on a limited fraction of the population.
Train a Decision Tree and Get Probabilities
Here is a small example with rpart:
The probs vector contains the model's estimated probability for the positive class. That is what you use to rank the records.
Build Gain-Chart Data
Now create a table sorted by probability from highest to lowest:
At this point:
- '
pct_populationtells you how much of the ranked list you have targeted' - '
pct_positives_capturedtells you how many real positives you have captured so far'
That is the core gain-chart data.
Plot the Gain Chart
A simple base R plot works well:
The diagonal red line is random targeting. A useful model should produce a blue curve above that line.
Add a Decile Table
Many business users prefer a decile summary because it answers questions like "How many positives are in the top 10 percent, 20 percent, and 30 percent?"
This complements the chart and is often easier to drop into a report or slide deck.
Why Test Data Matters
Do not build the gain chart on the training data unless the goal is only debugging. On training data, the curve is usually too optimistic because the model has already seen those records.
The holdout or test set is where the gain chart becomes useful for realistic model comparison and deployment decisions.
Compare More Than One Model
Gain charts become even more valuable when you overlay multiple models. For example, you can compare a tree, logistic regression, and gradient boosting model on the same test set.
The workflow is the same:
- get probabilities from each model
- compute gain data for each one
- plot all curves on one chart
Then compare the early part of the curve, which is often the most important region for targeting budgets.
Common Pitfalls
The biggest mistake is sorting scores in ascending order instead of descending order. That reverses the meaning of the chart and makes a good model look bad.
Another common issue is using the wrong probability column. For classification models, make sure you are ranking by the probability of the actual positive class.
People also frequently evaluate the gain chart on training data and then overestimate production performance.
Finally, convert factor labels carefully when building the cumulative count. If the positive class is not mapped correctly to 1, the gain chart is misleading from the start.
Summary
- A gain chart measures how effectively the model ranks positives ahead of the rest.
- Train the tree, predict positive-class probabilities, and sort descending by score.
- Compute cumulative positive capture against cumulative population share.
- Plot the model curve against a random baseline to judge lift visually.
- Use holdout data and the correct positive class or the chart will be misleading.

