# Predicting Road Traffic Collisions Using a Two-Layer Ensemble Machine Learning Algorithm
## 2 Materials and Methods
### Data Oversampling  
There are limitations associated with working with a binary classification when dealing with imbalanced datasets [36]. Oversampling was chosen to mitigate the effect of any underlying samples with underrepresentation. Across most of the datasets considered to be imbalanced, sampling strategies have been implemented to improve the overall model's accuracy [37,38]. One of the most important aspects to note is that oversampling is not considered to create any new data instances, as this can result in overfitting; conversely, undersampling may exclude important samples from the learning process, meaning that the most useful data instances may be overlooked by the model [39]. In this study, our dataset was imbalanced, and we therefore performed a synthetic minority oversampling technique (SMOTE) resampling strategy to handle the data imbalance [40]. The SMOTE algorithm develops synthetic positive cases to enhance the proportion of the minority class [41]. In our scenario, the data had 76% instances of no collision and 24% instances of collision, as shown in Figure 6.  
The dataset before SMOTE is illustrated in Figure 7 as a scatter plot with many points spread for the majority class and a small number of points scattered for the minority class. Majority class 0 represents no collisions, and 1 represents collisions. The transformed dataset was balanced after SMOTE, as shown in the scatter plot in Figure 8, in the ratio of 1:1. The crash prediction model's performance was evaluated using a classification report that included computed values of accuracy, precision, recall, and the F1 score of the algorithms. Our model suffered from underfitting because the outputs of the base layer model were used in the second layer, and to overcome the problem of underfitting in our model, some input features from Table 1 that were used in the base layer models were reduced and used together with the output of the base layer models. The reason for this approach was to improve the model. Logistic regression was used to train the level-1 input features as a meta-classifier. The test data set was then used to evaluate the two-layer  
Figure 6: SMOTE methodology diagram. ensemble model. The model with the highest values of the metrics was considered the best prediction model. The data generated by the confusion matrix were used to test each model's performance metric. The outcomes of the initial and predicted classifications generated by a classification model comprise the confusion matrix (CM) [42]. Table 2 shows a representation of a confusion matrix. The confusion matrix layout shown above displays the actual classes in the rows and the predicted class observations in the columns. The following defines each entity in CM:  
In _TN_, the entities that are originally negative are appropriately classified as negative. In _FN_, the entities that are originally positive are wrongly classified as negative. In _TP_, the entities that are originally positive are appropriately classified as positive. In _FP_, the entities that are originally negative are incorrectly classified as positive. \begin{table}
\begin{tabular}{c c c c} \hline \hline \multicolumn{2}{c}{**Total Instances**} & \multicolumn{2}{c}{**Predicted**} \\  & \multicolumn{2}{c}{**Negative**} & **Positive** \\ \hline \multirow{2}{*}{Actual} & Negative & True Negative (TN) & False Positive (FP) \\ \cline{2-4}  & Positive & False Negative (TN) & True Positive (TP) \\ \hline \hline \end{tabular}
\end{table} 
Table 2: The architecture of the confusion matrix. Figure 8: Scatter plot of the balanced dataset after SMOTE. Figure 7: Scatter plot of imbalanced dataset before SMOTE.