Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update partykit_tree_info() to handle classification outputs #118

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ S3method(tidypredict_test,party)
S3method(tidypredict_test,randomForest)
S3method(tidypredict_test,ranger)
S3method(tidypredict_test,xgb.Booster)
export(.extract_partykit_classprob)
export(.extract_xgb_trees)
export(acceptable_formula)
export(as_parsed_model)
Expand Down
80 changes: 77 additions & 3 deletions R/model-partykit.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
partykit_tree_info <- function(model) {
model_nodes <- map(seq_along(model), ~ model[[.x]])
is_split <- map_lgl(model_nodes, ~ class(.x$node[1]) == "partynode")
# non-cat model
mean_resp <- map_dbl(model_nodes, ~ mean(.x$fitted[, "(response)"]))
prediction <- ifelse(!is_split, mean_resp, NA)
if (is.numeric(model_nodes[[1]]$fitted[["(response)"]])) {
mean_resp <- map_dbl(model_nodes, ~ mean(.x$fitted[, "(response)"]))
prediction <- ifelse(!is_split, mean_resp, NA)
} else {
stat_mode <- function(x) {
counts <- rev(sort(table(x)))
if (counts[[1]] == counts[[2]]) {
ties <- counts[counts[1] == counts]
return(names(rev(ties))[1])
}
names(counts)[1]
}
mode_resp <- map_chr(model_nodes, ~ stat_mode(.x$fitted[, "(response)"]))
prediction <- ifelse(!is_split, mode_resp, NA)

Check warning on line 17 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L8-L17

Added lines #L8 - L17 were not covered by tests
}

party_nodes <- map(seq_along(model), ~ partykit::nodeapply(model, .x))

kids <- map(party_nodes, ~ {
Expand Down Expand Up @@ -88,3 +101,64 @@
parsedmodel <- parse_model(model)
build_fit_formula_rf(parsedmodel)[[1]]
}

# For {orbital}
#' @keywords internal
#' @export
.extract_partykit_classprob <- function(model) {
extract_classprob <- function(model) {
mod <- model$fitted
response <- mod[["(response)"]]
weights <- mod[["(weights)"]]

Check warning on line 112 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L109-L112

Added lines #L109 - L112 were not covered by tests

lvls <- levels(response)
weights_sum <- tapply(weights, response, sum)
weights_sum[is.na(weights_sum)] <- 0
res <- weights_sum / sum(weights)
names(res) <- lvls
res
}

Check warning on line 120 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L114-L120

Added lines #L114 - L120 were not covered by tests

preds <- map(seq_along(model), ~extract_classprob(model[[.x]]))
preds <- matrix(
unlist(preds),
nrow = length(preds),
byrow = TRUE,
dimnames = list(NULL, names(preds[[1]]))
)

Check warning on line 128 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L122-L128

Added lines #L122 - L128 were not covered by tests

generate_one_tree <- function(tree_info) {
paths <- tree_info$nodeID[tree_info[, "terminal"]]
paths <- map(
paths,
~ {
prediction <- tree_info$prediction[tree_info$nodeID == .x]
if (is.null(prediction)) cli::cli_abort("Prediction column not found")
if (is.factor(prediction)) prediction <- as.character(prediction)
list(
prediction = prediction,
path = get_ra_path(.x, tree_info, FALSE)
)
}
)

Check warning on line 143 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L130-L143

Added lines #L130 - L143 were not covered by tests

classes <- attr(model$terms, "dataClasses")
pm <- list()
pm$general$model <- "party"
pm$general$type <- "tree"
pm$general$version <- 2
pm$trees <- list(paths)
parsedmodel <- as_parsed_model(pm)

Check warning on line 151 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L145-L151

Added lines #L145 - L151 were not covered by tests

build_fit_formula_rf(parsedmodel)[[1]]
}

Check warning on line 154 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L153-L154

Added lines #L153 - L154 were not covered by tests

tree_info <- partykit_tree_info(model)

Check warning on line 156 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L156

Added line #L156 was not covered by tests

res <- list()
for (i in seq_len(ncol(preds))) {
tree_info$prediction <- preds[, i]
res[[i]] <- generate_one_tree(tree_info)

Check warning on line 161 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L158-L161

Added lines #L158 - L161 were not covered by tests
}
res

Check warning on line 163 in R/model-partykit.R

View check run for this annotation

Codecov / codecov/patch

R/model-partykit.R#L163

Added line #L163 was not covered by tests
}
Loading