Fraud Detection with Decision Trees in R: Imbalanced Data, Metrics and Interpretation

Learn how to build a fraud-detection decision tree in R, handle class imbalance and evaluate recall, precision, F1, PR-AUC and operational trade-offs.

📤 Submit AssignmentRead the guide

Fraud datasets usually contain far fewer fraudulent transactions than legitimate ones. A model that predicts every transaction as legitimate can therefore report impressive accuracy while detecting no fraud. The analysis must focus on the minority class and the cost of errors.

This guide uses a decision tree as an interpretable starting point and explains the wider workflow needed for a credible fraud-detection analysis.

What you will learn

  • Preserve the natural test distribution.
  • Measure minority-class performance.
  • Treat resampling as a training decision.
  • Tune thresholds using operational costs.
  • Explain the limits of historical labels.

1. Define the target and prediction time

State exactly what counts as fraud and when a prediction would be made. Features created after an investigation or chargeback must not be used for a real-time model because they would not yet exist.

Where transactions belong to the same customer, random row splitting can leak behavioural information across partitions. A time-based or customer-aware split may be more appropriate.

2. Explore imbalance and data quality

  • Calculate fraud prevalence.
  • Check duplicated transactions and missing fields.
  • Inspect amount and time distributions.
  • Identify categorical levels that appear only rarely.
  • Look for variables that reveal the label rather than predict it.

3. Train a decision tree responsibly

In R, packages such as rpart can create a transparent tree. Control depth, minimum node size and complexity pruning so the tree does not memorise noise.

If using under-sampling, over-sampling or SMOTE, apply it only to training data. Keep the test set representative of the environment in which predictions will be used.

4. Evaluate the right metrics

  • Recall or sensitivity: proportion of fraud cases detected.
  • Precision: proportion of fraud alerts that are actually fraud.
  • F1 score: harmonic balance of precision and recall.
  • False-positive rate: legitimate transactions incorrectly flagged.
  • PR-AUC: useful summary when positives are rare.
  • Confusion matrix: shows the exact error counts.

5. Choose a threshold and quantify costs

Predicted probabilities become decisions only after a threshold is chosen. Compare thresholds using the value of prevented fraud, investigation cost, customer friction and missed-fraud loss.

A cost matrix can make the trade-off explicit, but its values should be justified rather than invented solely to favour one model.

6. Monitor drift and feedback

Fraud behaviour changes in response to controls. Monitor feature distributions, alert rates, delayed labels and performance by customer or transaction segment. A model should have a review and retraining process rather than being treated as permanent.

NEED HELP WITH YOUR ASSIGNMENT?

Submit your assignment instructions for private support.

📤 Submit Assignment

Frequently asked questions

Why is 99% accuracy sometimes a bad fraud result?

When fraud is very rare, predicting every case as legitimate can exceed 99% accuracy while identifying zero fraudulent transactions.

Should SMOTE be applied before splitting?

No. Resampling before splitting can place synthetic or related information in both training and test data and inflate performance.

Is a decision tree suitable for fraud detection?

It is a useful interpretable baseline. More complex models may improve performance, but they still require careful validation, thresholds and monitoring.

Which is more important, precision or recall?

The answer depends on the cost of missed fraud and the capacity to investigate false alarms. Both should be reported.

References and further reading

  1. He, H., & Garcia, E. A. (2009). Learning from imbalanced data. IEEE Transactions on Knowledge and Data Engineering, 21(9), 1263–1284.
  2. Therneau, T., Atkinson, B., & Ripley, B. rpart: Recursive Partitioning and Regression Trees.
  3. Saito, T., & Rehmsmeier, M. (2015). Precision-recall plot is more informative than ROC for imbalanced datasets. PLOS ONE, 10(3), e0118432.