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

partialResponse constant output for categorical variables in GBM #16

Closed
AMBarbosa opened this issue Oct 31, 2023 · 1 comment
Closed

Comments

@AMBarbosa
Copy link
Contributor

It's awesome that predicts::partialResponse() works for different types of models, but there are still some glitches. For categorial variables in a GBM, the result is a constant, unlike with gbm::plot.gbm():

library(gbm)
library(predicts)

N <- 1000
set.seed(1)
X1 <- runif(N)
set.seed(2)
X2 <- runif(N)
set.seed(3)
X3 <- ordered(sample(letters[1:4], N, replace = TRUE), levels = letters[4:1])
mu <- c(-1, 0, 1, 2)[as.numeric(X3)]
Y <- X1 ^ 1.5 + 2 * (X2 ^ 0.5) + mu

data <- data.frame(Y, X1, X2, X3)
head(data)

mod_gbm <- gbm(Y ~ X1 + X2 + X3, data = data)

partialResponse(mod_gbm, var = "X3", data = data)
#   X3        p
# 1  d 2.304339
# 2  c 2.304339
# 3  b 2.304339
# 4  a 2.304339

plot.gbm(mod_gbm, i.var = "X3", return.grid = TRUE)
#   X3         y
# 1  d 0.8268557
# 2  c 1.7450856
# 3  b 2.7247473
# 4  a 3.6364649

Categorical variables seem to be analysed correctly e.g. for GLM:

mod_glm <- glm(Y ~ X1 + X2 + X3, data = data)
partialResponse(mod_glm, var = "X3", data = data)
#   X3         p
# 1  d 0.7212272
# 2  c 1.7217522
# 3  b 2.7182770
# 4  a 3.7356409
@rhijmans
Copy link
Member

rhijmans commented Oct 1, 2024

Thank you. I now get:

partialResponse(mod_gbm, var = "X3", data = data, n.trees=100)
#  X3         p
#1  d 0.8230262
#2  c 1.7412560
#3  b 2.7209178
#4  a 3.6326354

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants