5.2 Binning

# create a space to store our binning results and spearman
bin_data  <- matrix(rep(NA,nrow(sausage.processed)),nrow =  
                         nrow(sausage.processed), ncol = ncol(sausage.processed)
                         )
colnames(bin_data) <- colnames(sausage.processed)
rownames(bin_data) <- wk0$Product
# we use this loop along with a try catch to skip the varibles that BinQuant cannot bin.
for (i in 1:19) {
    tryCatch({
  var_clust = which(colnames(sausage.processed) == colnames(sausage.processed)[i])
  bin_data[,colnames(sausage.processed)[var_clust]] <- BinQuant(
                     sausage.processed[,var_clust], nClass = 4, stem = '')
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}  
## ERROR : 'breaks' are not unique 
## ERROR : 'breaks' are not unique 
## ERROR : 'breaks' are not unique

Notice that we use a different binning method for the following variables. The process is largely manual by either changing the amount of Bin Class or manually assign bins.

# variable Bitter - works only when nclass = 1
t <- sausage.processed[,6]
a <- as.matrix(ifelse(t == 0, 1, 2))
bin_data[,6] <- a

# variable Fatty - works when nclass = 3
var_clust = which(colnames(sausage.processed) == colnames(sausage.processed)[13])
bin_data[,colnames(sausage.processed)[var_clust]] <- BinQuant(
                     sausage.processed[,var_clust], nClass = 3, stem = '')

# variable HVP - works when nclass = 3
var_clust = which(colnames(sausage.processed) == colnames(sausage.processed)[14])
bin_data[,colnames(sausage.processed)[var_clust]] <- BinQuant(
                     sausage.processed[,var_clust], nClass = 3, stem = '')