This function converts an oblique split condition (linear combination) from a tree-based model into a valid R expression. Oblique splits use a weighted sum of multiple variables compared to a threshold.
Arguments
- split
A named list with four required elements:
columns: character vector - variable names for the linear combination.values: numeric vector - coefficients for each variable (same length as.columns)operator: character string - one of:<,<=,>,>=,==.threshold: numeric scalar - the threshold value for comparison.
Value
An R expression object that can be evaluated. The expression
represents: values[1]*columns[1] + .. + values[n]*columns[n] {operator} threshold.
Examples
# Simple oblique split with two variables
obliq_split_to_expr(list(
columns = c("x", "y"),
values = c(2, 3),
operator = ">",
threshold = 10
))
#> 2 * x + 3 * y > 10
# Oblique split with negative coefficients
obliq_split_to_expr(list(
columns = c("age", "income"),
values = c(1.5, -0.001),
operator = "<=",
threshold = 50
))
#> 1.5 * age - 0.001 * income <= 50
# Three-variable oblique split
obliq_split_to_expr(list(
columns = c("x", "y", "z"),
values = c(1, 2, -1),
operator = ">=",
threshold = 0
))
#> 1 * x + 2 * y - 1 * z >= 0
# Evaluate the expression
expr <- obliq_split_to_expr(list(
columns = c("x", "y"),
values = c(1, 1),
operator = ">",
threshold = 5
))
test_data <- data.frame(x = c(2, 3, 4), y = c(2, 3, 4))
test_data[eval(expr, test_data), ]
#> x y
#> 2 3 3
#> 3 4 4