Below example shows the step by step implementation of nonet_ensemble and nonet_plot functions in the context of classification. We have used Bank Note authentication data set to predict the output class variable using random forest and neural network models. Predictions from random forest model and neural network model are being used as inputs to the nonet_ensemble in the list form.
Let’s start:
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
library(ggplot2)
library(nonet)
dataframe <- data.frame(banknote_authentication)
head(dataframe)
## variance skewness curtosis entropy class
## 1 3.62160 8.6661 -2.8073 -0.44699 0
## 2 4.54590 8.1674 -2.4586 -1.46210 0
## 3 3.86600 -2.6383 1.9242 0.10645 0
## 4 3.45660 9.5228 -4.0112 -3.59440 0
## 5 0.32924 -4.4552 4.5718 -0.98880 0
## 6 4.36840 9.6718 -3.9606 -3.16250 0
We can see above that class variable has int datatype, we need to convert it into factor so that classification models can be trained on that.
dataframe$class <- as.factor(ifelse(dataframe$class >= 1, 'Yes', 'No'))
dataframe <- data.frame(dataframe)
head(dataframe)
## variance skewness curtosis entropy class
## 1 3.62160 8.6661 -2.8073 -0.44699 No
## 2 4.54590 8.1674 -2.4586 -1.46210 No
## 3 3.86600 -2.6383 1.9242 0.10645 No
## 4 3.45660 9.5228 -4.0112 -3.59440 No
## 5 0.32924 -4.4552 4.5718 -0.98880 No
## 6 4.36840 9.6718 -3.9606 -3.16250 No
index <- createDataPartition(dataframe$class, p=0.75, list=FALSE)
trainSet <- dataframe[ index,]
testSet <- dataframe[-index,]
control <- rfeControl(functions = rfFuncs,
method = "repeatedcv",
repeats = 3,
verbose = FALSE)
outcomeName <- 'class'
predictors <- c("variance", "skewness", "curtosis", "entropy")
banknote_rf <- train(trainSet[,predictors],trainSet[,outcomeName],method='rf')
banknote_nnet <- train(trainSet[,predictors],trainSet[,outcomeName],method='nnet')
Now we need to predict the outcome on testSet using the trained models
predictions_rf <- predict.train(object=banknote_rf,testSet[,predictors],type="prob")
predictions_nnet <- predict.train(object=banknote_nnet,testSet[,predictors],type="prob")
predictions_rf_raw <- predict.train(object=banknote_rf,testSet[,predictors],type="raw")
predictions_nnet_raw <- predict.train(object=banknote_nnet,testSet[,predictors],type="raw")
Stack_object <- list(predictions_rf$Yes, predictions_nnet$Yes)
names(Stack_object) <- c("model_rf", "model_nnet")
Stack_object_df <- data.frame(Stack_object)
Now we need to apply the nonet_ensemble method by supplying list object and best model name as input. Note that We have not provided training or test outcome labels to compute the weights in the weighted average ensemble method, which is being used inside the none_ensemble. Thus it uses best models prediction to compute the weights in the weighted average ensemble.
prediction_nonet_raw <- nonet_ensemble(Stack_object, "model_nnet")
prediction_nonet <- as.factor(ifelse(prediction_nonet_raw >= "0.5", "Yes", "No"))
Here Confusion matrix is being used to evaluate the performance of nonet, rf and nnet.
nonet_eval <- confusionMatrix(prediction_nonet, testSet[,outcomeName])
nonet_eval_rf <- confusionMatrix(predictions_rf_raw,testSet[,outcomeName])
nonet_eval_nnet <- confusionMatrix(predictions_nnet_raw,testSet[,outcomeName])
nonet_eval_df <- data.frame(nonet_eval$table)
nonet_eval_rf_df <- data.frame(nonet_eval_rf$table)
nonet_eval_nnet_df <- data.frame(nonet_eval_nnet$table)
Results can be plotted using the nonet_plot function. nonet_plot is being designed to provided different plot_type options to the user so that one can plot different visualization based on their needs.
plot_first <- nonet_plot(nonet_eval_df$Prediction, nonet_eval_df$Reference, nonet_eval_df, plot_type = "point")
plot_first
plot_second <- nonet_plot(nonet_eval_rf_df$Prediction, nonet_eval_rf_df$Reference, nonet_eval_rf_df, plot_type = "boxplot")
plot_second
plot_third <- nonet_plot(nonet_eval_nnet_df$Prediction, nonet_eval_nnet_df$Reference, nonet_eval_nnet_df, plot_type = "density")
plot_third
Above it can be seen that nonet_ensemble and nonet_plot can serve in a way that one do not need to worry about the outcome variables labels to compute the weights of weighted average ensemble solution.