Skip to content

Commit b13dfd0

Browse files
hidekojicursoragent
andcommitted
fix(chaid): show readable edge_label conditions in reports
tree_nodes used "var = (a, b]" / "var = <= x", which made the characteristic-groups Condition column hard to read vs CART. Rewrite edge_label via chaid_readable_one_condition; keep cond_value as collapsed bin labels for DTreeGenerator filters. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ac1b626 commit b13dfd0

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

R/build_chaid.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,13 +847,17 @@ build_chaid_tree_nodes <- function(x) {
847847
original_categories <- strsplit(edges$original_categories[edge_row], " \\| ")[[1]]
848848
# tam #37177: a branch built from a run of contiguous numeric bins reads
849849
# as the range it covers ("<= 2317.6, (2317.6, 2695.8]" -> "<= 2695.8").
850-
# cond_value is collapsed IN LOCKSTEP because DTreeGenerator rebuilds the
851-
# edge label from cond_value, and its Show Detail filter
852-
# (binLabelsToRangeConditions, min-lo/max-hi) parses the collapsed labels
853-
# to the same range -- CHAID merges only ADJACENT ordered bins, so no gap
854-
# can make the two differ. Categorical members pass through untouched.
850+
# cond_value stays the collapsed bin/category labels so DTreeGenerator's
851+
# Show Detail filter (binLabelsToRangeConditions) can parse them. The
852+
# displayed edge_label is rewritten to the same readable inequalities
853+
# used by node_summary / rules ("給料 = <= 2695.8" -> "給料 <= 2695.8",
854+
# "給料 = (2695.8, 4228.8]" -> "2695.8 < 給料 <= 4228.8") so the
855+
# characteristic-groups Condition column and tree chart match CART.
855856
display_categories <- chaid_collapse_intervals(original_categories)
856-
edge_label <- paste0(cond_column, " = ", paste(display_categories, collapse = ", "))
857+
edge_label <- chaid_readable_one_condition(
858+
paste0(cond_column, " in {",
859+
paste(display_categories, collapse = CHAID_GROUP_SEPARATOR), "}")
860+
)
857861
cond_value <- as.character(jsonlite::toJSON(as.character(display_categories)))
858862
} else {
859863
cond_column <- NA_character_

tests/testthat/test_build_chaid.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,13 @@ test_that("tree_nodes edge labels collapse contiguous numeric bins (tam #37177)"
478478
# Every numeric branch collapses its contiguous bin run to ONE range label.
479479
expect_equal(length(values), 1)
480480
expect_true(grepl("^(<=|>|\\()", values))
481-
# edge_label mirrors cond_value (DTreeGenerator rebuilds from cond_value).
482-
expect_equal(edges$edge_label[i], paste0("salary = ", values))
481+
# cond_value stays machine-parseable bin labels; edge_label is readable
482+
# (CHAID report Condition / tree chart — not "salary = <= x").
483+
expect_equal(
484+
edges$edge_label[i],
485+
chaid_readable_one_condition(paste0("salary in {", values, "}"))
486+
)
487+
expect_false(grepl(" = <=| = \\(| = >", edges$edge_label[i]))
483488
}
484489
# A categorical branch keeps its member enumeration untouched.
485490
cat_edges <- nodes[!is.na(nodes$parent_id) & nodes$cond_column == "dept", ]

0 commit comments

Comments
 (0)