Code Monkey home page Code Monkey logo

Comments (3)

bradleyboehmke avatar bradleyboehmke commented on May 19, 2024

Hmmmm...when I run the following I reproduce the left plot in Figure 4.12. If you run this code in a fresh R session do you still get that error?

# packages required
library(ranger)
library(rsample)
library(pdp)

# data
attrition <- rsample::attrition %>% 
  mutate_if(is.ordered, factor, ordered = FALSE) %>%
  mutate(Attrition = relevel(Attrition, ref = "Yes"))

# Create training and testing sets
set.seed(123)
split <- initial_split(attrition, prop = .8, strata = "Attrition")
attrit_train <- training(split)
attrit_test  <- testing(split)

# probability model
m3_ranger_prob <- ranger(
  formula         = Attrition ~ ., 
  data            = attrit_train, 
  num.trees       = 250,
  mtry            = 25,
  respect.unordered.factors = 'order',
  verbose         = FALSE,
  seed            = 123,
  min.node.size   = 1,
  sample.fraction = .80,
  probability     = TRUE,
  importance      = 'impurity'
  )

# custom ICE prediction function
custom_pred <- function(object, newdata) {
  pred <- predict(object, newdata)
  avg <- pred$predictions[, 1]
  return(avg)
}

# compute and plot ICE curves
m3_ranger_prob %>%
  partial(pred.var = "OverTime", ice = TRUE, center = TRUE, pred.fun = custom_pred, train = attrit_train) %>%
  autoplot(rug = TRUE, train = attrit_train, alpha = 0.2)

from homlr.

bragks avatar bragks commented on May 19, 2024

My bad, sorry! Although, I suggest changing the # description and maybe function names..;)

I just assumed it was the same function since both the description and function names were the same.

# custom prediction function
custom_pred <- function(object, newdata) {
  pred <- predict(object, newdata)
  avg <- mean(pred$predictions[, 1]) 
  return(avg)
}
# custom prediction function
custom_pred <- function(object, newdata) {
  pred <- predict(object, newdata)
  avg <- pred$predictions[, 1]
  return(avg)
}

from homlr.

bradleyboehmke avatar bradleyboehmke commented on May 19, 2024

Good call out; I can see how that will likely confuse most readers. I'll update the functions to be more unique. Thanks.

from homlr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.