# Store binnin models in R after fitting a dataset

I'm looking for algorithms to create bins of variables in order to reduce the noise. I have found several libraries for that, one if the chi2 library:

https://www.rdocumentation.org/packages/discretization/versions/1.0-1/topics/chi2

The documentation has the following example:

data(iris)
#---cut-points
chi2(iris,0.5,0.05)$cutp #--discretized dataset using Chi2 algorithm chi2(iris,0.5,0.05)$Disc.data


This works for this data, but if I train a model after transforming this data in order to make prediction over new records I will have to use the same cuts that were used here. My question is, is there any method or library that stored the cuts of the bins in a way that can be easily applied to new data similarly to a predict method? without any custom function

It provides the cuts and you have to apply it back. Reading the code in this package function it assumes the last column is the dependent variable and the other columns can be discretized. My point, not much documentation or warnings, I am not very sure about how to best use this library.

For example if you look at how the data is cut:

disc = chi2(iris,0.5,0.05)

tapply(iris[,1],disc$$Disc.data[,1],range)$$1
 4.3 5.4

$2  5.5 5.7$3
 5.8 7.0

$4  7.1 7.9  You can see the range of the discrete labels don't quite agree with cutp, making it very difficult to map back: > disc$cutp
[]
 3.5 4.5 6.5


One option if you are tied to this package is do the discretization over all your data, split them into train and test, and do the training, for example, we make the data into train and test, like you normally get in kaggle:

idx = sample(nrow(iris),100)
traindf = iris[idx,]
testdf = iris[-idx,]


You perform the discretization over the combined dataset:

disc = chi2(rbind(traindf,testdf),0.5,0.05)


And you fit over the train subset, not clear how you want to use the discrete data, because the result is given as an integer, so below I just treat it as continuous, i would consider using it as catgeorical:

library(nnet)
fit = nnet(Species ~ .,data=disc$Disc.data[1:nrow(traindf),],size=2)  Then predict over the other subset: predict(fit,data=disc$Disc.data[-(1:nrow(traindf)),])


You can also try using a better documented library arules.