# The Use of Machine Learning Methods in Road Safety Research in Poland
## 4 Mathematical Modeling of Road Safety in Poland
### K-Nearest Neighbors (KNN)  
K-Nearest Neighbors (KNN) is one of the simplest machine learning algorithms, primarily used for classification tasks. The parameter \(k\) (the number of neighbors) significantly influences the model's performance. The KNN model was evaluated for different values of the parameter \(k\). The results are presented in Table 5.  
The accuracy was used to select the optimal model. The proportion of correct predictions relative to all the samples reached the highest accuracy of 64.82% for \(k=9\). The Kappa value, a metric that accounts for chance agreement, was also the highest. The value Kappa = 0.4261 indicates moderate agreement between the predicted and actual classes. Thus, for the KNN model, \(k=9\) was assumed. The confusion matrix for this model is presented in Table 6.  
For the KNN model, a very low \(p\)-value for the McNemar test was obtained (\(\chi^{2}<2.2\times 10^{-16}\)). This result shows that the difference between the number of misclassifications for one class and the misclassifications for the other class is statistically significant. This means that the model makes classification errors in an asymmetric manner. The model demonstrates strong performance in identifying observations for the \"no injuries\" class, achieving a high sensitivity of 88.9% and a relatively high specificity of 73.62%. The model performs well with this class and is less likely to assign other classes to \"no injuries\"-the balanced accuracy is 81.26%. For the \"minor injuries\" class, the sensitivity is 62.72%, and the specificity is 74.49%. The positive predictive value (PPV) of 57.38% indicates that the model frequently misclassifies other classes as \"minor injuries\", leading to the balanced accuracy of 68.61%. The model struggles significantly with the \"severe injuries\" class. The sensitivity is notably low at 14.83%, although the specificity is high  
\begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Actual Class** & **No Injuries** & **Minor Injuries** & **Severe Injuries** & **Deaths** \\ \hline No injuries & 3130 & 798 & 271 & 100 \\ Minor injuries & 347 & 1765 & 795 & 169 \\ Severe injuries & 41 & 229 & 190 & 47 \\ Deaths & 3 & 22 & 25 & 21 \\ \hline \hline \end{tabular}
\end{table}
Table 6: Confusion matrix of the KNN model for \(k=9\).  
\begin{table}
\begin{tabular}{c c c} \hline \hline
**k** & **Accuracy** & **Kappa** \\ \hline
5 & 0.6393 & 0.4179 \\
7 & 0.6448 & 0.4232 \\
9 & 0.6482 & 0.4261 \\ \hline \hline \end{tabular}
\end{table}
Table 5: KNN results for different values of the parameter \(k\).  
at 95.25%, meaning the model rarely misclassifies other classes as \"severe injuries\". The balanced accuracy of 55.04% indicates poor performance in this class. The most critical issue lies in the \"deaths\" class, where the model essentially fails to detect it. The sensitivity is extremely low at 6.23%, although the specificity is very high at 99.34%, indicating that the model almost never misclassifies other classes as \"deaths\". The balanced accuracy of 52.79% further underscores the model's challenges in accurately identifying this class.