koalaverse / homlr

Supplementary material for Hands-On Machine Learning with R, an applied book covering the fundamentals of machine learning with R.
https://koalaverse.github.io/homlr
Creative Commons Attribution Share Alike 4.0 International
229 stars 88 forks source link

Figure 5.7 #64

Open rubrobledo opened 1 year ago

rubrobledo commented 1 year ago

The code proposed to reproduce Figure 5.7 (pag.118) does not seem to work correctly.

pred.fun <- function(object, newdata) { Yes <- mean(predict(object, newdata, type = "prob")$Yes) as.data.frame(Yes)} p1 <- pdp::partial(cv_model3, pred.var = "OverTime", pred.fun=pred.fun) %>% autoplot(rug = TRUE) + ylim(c(0, 1)) Error in ggplot_two_predictor_pdp(object = object, rug = rug, smooth = smooth, : The training data must be supplied for rug display.

It is possible that the proposed prediction function does not generate an acceptable data frame for the "partial" function, since when executing:

pdp::partial(cv_model3, pred.var = "OverTime", pred.fun=pred.fun) you get the following: OverTime yhat.Yes yhat.Yes 1 No 0.1044187 0.31364 2 Yes 0.1044187 0.31364

What can I be doing wrong or what should be corrected in the code? Thank you