2

I'm using R caret package and I want to apply a discretization function to all predictive variables within cross-validation.

For example using this code:

# load the library
library(caret)
# load the iris dataset
data(iris)
# define training control
train_control <- trainControl(method="cv", number=10)
# fix the parameters of the algorithm
grid <- expand.grid(.fL=c(0), .usekernel=c(FALSE))
# train the model
model <- train(Species~., data=iris, trControl=train_control, method="nb", tuneGrid=grid)
# summarize results
print(model)

I want to apply arules discretizeDF.supervised (https://www.rdocumentation.org/packages/arulesCBA/versions/1.1.4/topics/discretizeDF.supervised) using only the training fold to learn the correct groups and then apply it to the test fold. I don't mind using any other library/function for discretization as long as it is supervised.

AFAIK this is the correct way to do discretization so it must be a way to do it. I have been reading about "recipes" but I haven't been able to make it work.

juyt
  • 21
  • 1
  • @topepo Any ideas? – juyt Nov 05 '19 at 10:42
  • @missuse Any ideas? – juyt Nov 05 '19 at 10:42
  • One option is to create a custom recipes step function. [This tutorial](https://cran.rstudio.com/web/packages/recipes/vignettes/Custom_Steps.html) can help with that. Another option is to switch to [mlr](https://mlr.mlr-org.com/) and create a custom [mlrCPO](https://github.com/mlr-org/mlrCPO). Here is a [list of built in CPOs](https://rawgit.com/mlr-org/mlrCPO/master/inst/doc/a_3_all_CPOs.html). To create you own check [this tutorial](https://rawgit.com/mlr-org/mlrCPO/master/inst/doc/a_4_custom_CPOs.html). If you edit the post to add your attempts with recipes it would be easier to help. – missuse Nov 06 '19 at 14:42
  • Btw I was not notified by your @missuse attempt. Looks like it works only if I commented previously. – missuse Nov 06 '19 at 14:43
  • @missuse I have tried creating a custom recipe using the same tutorial that you reference but I have not managed to get it working. I want to apply the discretization function from the library but that same discretization that is learned from the training set must be applied to the test set. I think I don't understand how recipes work. Could you please write an example? Sorry about the @ mention but I have read lots of your previous posts and all of them are great and have helped me understand r-caret. – juyt Nov 06 '19 at 19:25
  • no need to apologize and I am glad they helped at least some one. I will try to find a solution tomorrow and will post it if I manage. – missuse Nov 06 '19 at 19:28
  • @missuse Thank you. I've struggling with it for a whole week. – juyt Nov 06 '19 at 19:31
  • Hey, just wanted to say that I spent some time fiddling with this problem and I could not make i work . I think your best bet would be to post an issue on [recipes github](https://github.com/tidymodels/recipes). Good luck. – missuse Nov 08 '19 at 11:12
  • @missuse I opened the issue https://github.com/tidymodels/recipes/issues/408 It's something basic in my opinion so it should be a trivial way to do it. – juyt Nov 09 '19 at 12:06

0 Answers0