# Predicting Road Traffic Collisions Using a Two-Layer Ensemble Machine Learning Algorithm
## 2 Materials and Methods
### Building the Two-Layer Ensemble Model  
We evaluated the performance of machine learning approaches by splitting the dataset in the ratio of 70% training dataset and 30% testing dataset. In our research, we employed four well-known classification algorithms (previously used to predict road traffic collisions) and the stacking ensemble method to predict road traffic collisions. Stacking is an ensemble method for integrating numerous models with a meta-classifier. Following the development of the base models, the four base models (level-0)--k-NN, AdaBoost, DT, and Naive Bayes--were integrated using a stacking framework for road collision prediction. We selected the four base models because of their proven diversity in predicting road collisions. In the second layer, logistic regression was employed as a meta-classifier to classify road collisions from the outputs of the base models. A 10-fold cross-validation technique was used to evaluate how well the models predicted traffic collisions [25]. The proposed two-layer ensemble model is shown in Figure 3. The following section expounds on the four supervised machine learning techniques and the stacking method employed in our study. (i) Naive Bayesian Classifier (NBC): This algorithm employs the theorem of [PERSON]. It works by estimating the probability of various classes based on a variety of features and allocates the new class to the class with the highest probability [26]. In our study, Gaussian NB was chosen because the feature set contained continuous variables. The NB is represented by the following formula:  
\[P(H|E)=\frac{P(E|H)*P(H)}{P(E)} \tag{1}\]  
where \(P(H|E)\) is the posterior probability of the hypothesis given that the evidence is true, \(P(E|H)\) is the likelihood of the evidence given that the hypothesis is true, \(P(H)\) is the prior probability of the hypothesis, and \(P(E)\) is the prior probability that the evidence is true. The posterior probability is mainly the probability of '\(H\)' being true given that '\(E\)' is true. (ii) k-Nearest Neighbors (k-NN): This method can be considered a voting system in which the majority class determines the class label of a new data point among its nearest neighbors [27]. It then analyzes datasets, calculates the distance function and similarities between them, and groups them based on k values. In our study, the k value was obtained by performing several tests with values ranging from 1 to 50, and the prediction performance was compared to the k value. We plotted the accuracies for both training and test datasets, as shown in Figure 4. The performance of k-NN showed a drop in both the test and training datasets after adding neighbors; the drop continued for both until the point at which they converged. The test dataset improved with an increase in the number of neighbors from iteration 33 until they converged with the training dataset at neighbor 42. In the proposed model, we set the k value at 42 because this yielded the best results, and Euclidean distance was selected as the distance function [28]. \begin{table}
\begin{tabular}{c c c c} \hline
**Univariate Feature Selection** & **Recursive Elimination Method** & **Feature Importance** & **Particle Swarm Optimization (PSO)** \\ \hline Lane gap & Lane gap & Lane gap & Lane gap \\ \hline Speed & Speed & Speed & Speed \\ \hline Brake & Brake & Brake & Brake \\ \hline Education level & Education level & Education level & Driver Experience \\ \hline Driver Experience & Driver Experience & Driver Experience & Surface condition \\ \hline Driver Age & Driver Age & Driver Age & Gender \\ \hline \end{tabular}
\end{table} 
Table 1: Features having a strong relationship with road collisions. Figure 4: Line plot illustrating k-NN accuracy on training and test datasets for different neighbors. Figure 3: The proposed two-layer ensemble model. The distance between the clusters is used to classify the new input data, and the closest cluster is allocated.