Skip to contents

This function calculates how much each feature contributes to the anomaly score of specific samples using path-based analysis or permutation importance.

Usage

feature_contribution(
  object,
  sample_ids = NULL,
  data,
  method = "path",
  contamination = 0.05,
  n_permutations = 15,
  max_trees = 50
)

Arguments

object

An isoForest model object

sample_ids

Vector of sample IDs to analyze (if NULL, analyzes all anomalies)

data

The original training data

method

Method for calculating contributions: "path" (default) or "permutation"

contamination

Contamination rate for identifying anomalies (default 0.05)

n_permutations

Number of permutations for permutation importance (default 15). Lower values (10-15) are usually sufficient and much faster. Values above 20 show diminishing returns.

max_trees

Maximum number of trees to analyze for path method (default 50)

Value

A list containing feature contributions for each analyzed sample

Examples

# Train model
model <- isoForest(iris[1:4])

# Analyze feature contributions for anomalous samples
contributions <- feature_contribution(model, data = iris[1:4])
print(contributions)
#> Feature Contribution Analysis
#> =============================
#> 
#> Sample 14 | Score: 0.643 
#>   Petal.Length: 31.2%
#>   Sepal.Width: 27.1%
#>   Sepal.Length: 21.8%
#>   Petal.Width: 19.9%
#> 
#> Sample 16 | Score: 0.67 
#>   Sepal.Width: 34.8%
#>   Sepal.Length: 26.6%
#>   Petal.Length: 20.6%
#>   Petal.Width: 18.0%
#> 
#> Sample 23 | Score: 0.619 
#>   Petal.Length: 31.0%
#>   Sepal.Width: 27.6%
#>   Sepal.Length: 22.0%
#>   Petal.Width: 19.5%
#> 
#> Sample 42 | Score: 0.622 
#>   Sepal.Width: 29.9%
#>   Petal.Length: 26.8%
#>   Sepal.Length: 23.2%
#>   Petal.Width: 20.1%
#> 
#> Sample 61 | Score: 0.638 
#>   Sepal.Width: 33.2%
#>   Petal.Length: 27.3%
#>   Sepal.Length: 21.3%
#>   Petal.Width: 18.2%
#> 
#> Sample 118 | Score: 0.646 
#>   Petal.Length: 26.0%
#>   Sepal.Width: 25.7%
#>   Sepal.Length: 25.3%
#>   Petal.Width: 23.0%
#> 
#> Sample 119 | Score: 0.652 
#>   Petal.Length: 29.3%
#>   Sepal.Width: 26.7%
#>   Petal.Width: 24.4%
#>   Sepal.Length: 19.6%
#> 
#> Sample 132 | Score: 0.649 
#>   Sepal.Length: 27.2%
#>   Petal.Length: 26.2%
#>   Sepal.Width: 23.9%
#>   Petal.Width: 22.6%
#> 
#> Summary (Top 4 features):
#>   Sepal.Width: 28.6%
#>   Petal.Length: 27.3%
#>   Sepal.Length: 23.4%
#>   Petal.Width: 20.7%

# Analyze specific samples with permutation method (fast)
contributions <- feature_contribution(model, sample_ids = c(1, 50), 
                                    data = iris[1:4], method = "permutation",
                                    n_permutations = 10)  # 10-15 recommended