Skip to content

plot.gg_partial_rfsrc: categorical partial dependence stacks per-observation predictions instead of averaging #299

Description

@ehrlinger

Observed

For a factor or 0/1 predictor, plot(gg_partial_rfsrc(...)) draws bars whose height is the sum of every observation's prediction, so a class probability reads in the hundreds.

Reproduce (ggRandomForests 4.0.0, randomForestSRC 3.9.0)

library(randomForestSRC); library(ggRandomForests)
set.seed(1); n <- 400
d <- data.frame(x = rnorm(n), g = rbinom(n, 1, .4))
d$y <- factor(rbinom(n, 1, plogis(-1 + d$x + d$g)))
p <- gg_partial_rfsrc(rfsrc(y ~ x + g, d, ntree = 100), xvar.names = c("x", "g"))
nrow(p$continuous)    # 25: averaged over the grid
nrow(p$categorical)   # 800: one prediction per observation per level, not averaged
tapply(p$categorical$yhat, p$categorical$x, mean)  # 0.626, 0.482
b <- ggplot2::ggplot_build(plot(p))  # categorical panel is geom_bar; y accumulates 1.00, 1.84, 2.81, ...

The continuous component is a partial-dependence curve (averaged); the categorical component is individual conditional expectations, and geom_bar stacks them.

Expected

The categorical panel shows the mean prediction per level (0.63 and 0.48 here), as the continuous panel does, or a boxplot of the per-observation predictions if the spread is the point. Either way the y-axis should be on the response scale.

Seen in hvtiRtemplates' rfs-explain and rfc-explain reports: bars near 1800 (survival) and 600 (classification) for 0/1 predictors.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions