Skip to content

Commit 7bc39e3

Browse files
committed
Enhance gg_partial function to normalize factor levels and handle empty categorical lists
1 parent 584a476 commit 7bc39e3

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

R/gg_partial.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ gg_partial <- function(part_dta,
7575
## ---- Categorical variable: few unique x values -------------------
7676
## VarPro works with logical or continuous only; factors are
7777
## one-hot encoded internally in the varPro call.
78+
## Normalize factors so ordered/regular factors bind cleanly later.
79+
x_factor <- if (is.factor(x_vals)) {
80+
factor(as.character(x_vals), levels = levels(x_vals))
81+
} else {
82+
factor(x_vals)
83+
}
84+
7885
plt.df <- dplyr::bind_cols(
79-
x = factor(x_vals),
86+
x = x_factor,
8087
yhat = part_dta$plotthis[[feature]]$yhat
8188
)
8289
plt.df$name <- names(part_dta$plotthis)[[feature]]
@@ -87,8 +94,11 @@ gg_partial <- function(part_dta,
8794

8895
# Combine per-variable lists into single data frames (NULL entries dropped)
8996
continuous <- dplyr::bind_rows(cont_list)
90-
categorical <- dplyr::bind_rows(cat_list)
91-
97+
if(length(cat_list) == 0) {
98+
categorical <- NA
99+
} else {
100+
categorical <- dplyr::bind_rows(cat_list)
101+
}
92102
## Optionally attach a model label (useful when overlaying multiple forests)
93103
if (!is.null(model)) {
94104
continuous$model <- categorical$model <- model

0 commit comments

Comments
 (0)