FFTrees icon indicating copy to clipboard operation
FFTrees copied to clipboard

Add support (either functionality and/or documentation) on integrating with tidymodels

Open ndphillips opened this issue 8 years ago • 4 comments

ndphillips avatar Oct 24 '17 19:10 ndphillips

Hello,

I am playing a bit with your package and tried to incorporate your FFTrees function into mlr. Here is the piece of code I came up with.

# step 1 - set up properties of the learner
makeRLearner.classif.fftree = function() {
  makeRLearnerClassif(
    cl = "classif.fftree",
    package = "FFTrees",
    par.set = makeParamSet(
      makeNumericLearnerParam(id = "max.levels", default = 4, lower = 1),
      makeNumericLearnerParam(id = "sens.w", default = .5, lower = 0, upper = 1),
      makeDiscreteLearnerParam(id = "stopping.rule", default = "exemplars", values = c("exemplars", "levels", "statdelta")),
      makeNumericLearnerParam(id = "stopping.par", default = .1, lower = 0),
      makeDiscreteLearnerParam(id = "goal", default = "wacc", values = c("acc", "wacc", "bacc")),
      makeDiscreteLearnerParam(id = "goal.chase", default = "bacc", values = c("acc", "wacc", "bacc", "cost")),
      makeDiscreteLearnerParam(id = "goal.threshold", default = "bacc", values = c("acc", "wacc", "bacc")),
      makeDiscreteLearnerParam(id = "numthresh.method", default = "o", values = c("o", "m")),
      makeLogicalLearnerParam(id = "repeat.cues", default = TRUE),
      makeLogicalLearnerParam(id = "do.comp", default = FALSE, tunable = FALSE)
    ),
    properties = c("twoclass", "numerics", "factors", "prob"),
    name = "Fast and Frugal decision tree",
    short.name = "fftree"
  )
}

# step 2 - create the train function
trainLearner.classif.fftree = function(.learner, .task, .subset, .weights = NULL, ...) {
  f = getTaskFormula(.task)
  d = getTaskData(.task, .subset)
  FFTrees::FFTrees(f, data = d, ...)
}

# step 3 - create the predict function
predictLearner.classif.fftree = function(.learner, .model, .newdata, ...) {
  type = switch(.learner$predict.type, prob = "prob", "class")
  p = predict(.model$learner.model, newdata = .newdata, type = type, ...)
  return(p)
}

# step 4 - register the functions
registerS3method("makeRLearner", "classif.fftree", makeRLearner.classif.fftree)
registerS3method("trainLearner", "classif.fftree", trainLearner.classif.fftree)
registerS3method("predictLearner", "classif.fftree", predictLearner.classif.fftree)

I hope this will help.

Cheers, Mathieu

MathieuMarauri avatar Oct 15 '19 14:10 MathieuMarauri

This is awesome, thanks so much @MathieuMarauri !!!! I tried doing something similar a while ago with mixed success. Your solution looks very clean. Thanks!!

ndphillips avatar Oct 31 '19 13:10 ndphillips

Caret is maintained but will not develop much further. making this work with parsnip would be really cool though!

RMHogervorst avatar Jul 25 '20 20:07 RMHogervorst

Changing title to integration with tidymodels as that seems to be the most active ecosystem for ML in R these days

ndphillips avatar May 19 '24 13:05 ndphillips