Stroke affects people all around the world, resulting in numerous deaths and disabilities.[1]. If we are able to detection stroke early for those at increased risk is will be amazing for prevention and prompt intervention because stroke frequently happens quickly and can cause long-term neurological disability. Clinicians and public health experts can measure individual-level risk and target high-risk populations for clinical management and lifestyle counseling by using data-driven risk prediction models.
Logistic Regression (LR) is one of the most widely used approaches for modelling binary outcomes such as disease is present in human or not[2]. It extends linear regression to cases where the outcome is categorical and provides interpretable coefficients and odds ratios that describe how each predictor is associated with the probability of the event. LR has been applied across a wide range of domains, including child undernutrition and anaemia[3], road traffic safety[4–6], health-care utilisation and clinical admission decisions[7], and fraud detection[8]. These applications highlight both the flexibility of LR and its suitability for real-world decision-making problems.
In this project, we analyse a publicly available stroke dataset that includes key demographic, behavioural, and clinical predictors such as age, gender, hypertension status, heart disease, marital status, work type, residence type, smoking status, body mass index (BMI), and average glucose level. These variables are commonly reported in the stroke and cardiovascular literature as important determinants of risk. Using this dataset, we first clean and recode the variables into appropriate numeric formats and then develop a series of supervised learning models for stroke prediction.
Logistic Regression is used as the primary, interpretable baseline model, but its performance is compared against several more complex machine-learning techniques, including Decision Tree, Random Forest, Gradient Boosted Machine, k-Nearest Neighbours, and Support Vector Machine (radial). Model performance is evaluated using accuracy, sensitivity, specificity, ROC curves, AUC, and confusion matrices. The main objectives are to identify the most influential predictors of stroke and to determine whether advanced machine-learning models offer meaningful improvements over Logistic Regression for classification of stroke risk in this dataset.
Methodology
This part explains about our stoke dataset, variables, preprocessing steps, logistic regression formulation, and the machine-learning modelling framework used to compare classifiers.
Our datset contains 5,110 observations and 11 predictors commonly associated with cerebrovascular risk. After cleaning missing and inconsistent entries (e.g., “Unknown”, “N/A”, or rare textual categories such as “children” and “other”), a final dataset of 3,357 individuals remained for analysis. The cleaned dataset is stored in the object strokeclean.
Respose we get is in binary so logestic regression is the best approach to observe whether the patient has had stroke=1 or not stroke=0[hosmer2013applied?,james2021isl?]
Variables
The key predictors are listed below.
Variable
Type
Description
age
Numeric
Age of the individual (years)
gender
Categorical (1=Male, 2=Female)
Biological sex
hypertension
Binary (0/1)
Prior hypertension diagnosis
heart_disease
Binary (0/1)
Presence of heart disease
ever_married
Binary
Marital status
work_type
Categorical (1–4)
Employment category
Residence_type
Binary (1=Urban, 2=Rural)
Place of residence
smoking_status
Categorical
Never/Former/Smokes
bmi
Numeric
Body Mass Index
avg_glucose_level
Numeric
Average glucose level
stroke
Binary outcome (0=No, 1=Yes)
Stroke occurrence
Stroke is a highly unbalanced outcome variable: - Yes (stroke): about 5% - No (no stroke): around 95%
Because it is possible to achieve high overall accuracy by merely forecasting the majority class, this class imbalance directly affects model evaluation. Because of this, in addition to accuracy, we also concentrate on sensitivity, specificity, ROC curves, AUC, and Youden’s J statistic.
Dataset Prepration
To guarantee model validity and stop data leakage, data preprocessing is performed.[9].
Among the steps were:
Elimination of non-predictive identifiers (patient ID)
Transforming categorical variables into dummy numerical representations
Managing uncommon or irregular categories (e.g., “Other” gender values handled as absent)
BMI, glucose, and age conversion to numerical
Rows with unintelligible labels (“Unknown,” “N/A”) are removed.
Valid range and consistency verification
After recoding, missing values might be imputed or removed.
During splitting, stratified sampling is used to maintain the stroke/no-stroke ratio[6].
The outcome stroke was defined as a factor with levels “No” and “Yes” in the cleaned dataset strokeclean.
To check sample performance, the data were split into training and test sets. For the baseline logistic regression model, a simple random 70/30 split was used. For the full machine-learning comparison, a stratified partition (via caret::createDataPartition) was applied to preserve the stroke/no-stroke ratio in both sets.
Logistic regression model
Let \(Y_i\) denote the stroke status for patient \(i\), where
Here, \(\beta_0\) is the intercept, \(\beta_j\) is the change in log-odds of stroke for a one-unit increase in predictor \(x_j\), holding other variables constant.
Exponentiating \(\beta_j\) gives the odds ratio (OR): \[
\text{OR}_j = e^{\beta_j},
\]
which represents the multiplicative change in the odds of stroke for a one-unit increase in \(x_j\).
Model Estimation
Let \(\boldsymbol{\beta} = (\beta_0, \beta_1, \ldots, \beta_p)^\top\) denote the vector of regression coefficients. For independent observations, the likelihood of the data is \[
L(\boldsymbol{\beta})
= \prod_{i=1}^{n}
\pi(\mathbf{x}_i)^{\,y_i}
\left[1 - \pi(\mathbf{x}_i)\right]^{\,1-y_i},
\]
where \(\pi(\mathbf{x}_i) = P(Y_i = 1 \mid \mathbf{x}_i)\).
The maximum likelihood estimate \(\hat{\boldsymbol{\beta}}\) is the value of \(\boldsymbol{\beta}\) that maximizes \(\ell(\boldsymbol{\beta})\). In R, this optimization is carried out automatically using glm(..., family = binomial)
Machine learning models and evaluation
Six supervised models were fitted using the caret framework in order to determine whether more sophisticated methods may significantly enhance stroke classification:
Logistic Regression (LR)
Decision Tree (rpart)
Random Forest (RF)
Gradient Boosted Machine (GBM)
k-Nearest Neighbours (k-NN)
Support Vector Machine with radial kernel (SVM-Radial)
All models used the same 70% training / 30% test split and a consistent cross-validation procedure to ensure fair comparison. To guarantee a fair comparison, all models employed the same cross-validation process and a 70% training/30% test split.
Data Splitting and Model Fitting in R
The cleaned dataset is stored in the object strokeclean, where the outcome variable is stroke (0 = No stroke, 1 = Stroke), and predictors include age, hypertension, heart_disease, avg_glucose_level, bmi, smoking_status, and others.
First, the dataset is randomly divided into a training set (70%) and a test set (30%) to evaluate out-of-sample performance, logistic regression model is then fitted on the training data:
From this model, estimated odds ratios and 95% confidence intervals are computed as:
Model Predictions and Performance Measures
Predicted probabilities on the test set are obtained as:
Using a classification threshold \(c = 0.5\), the predicted class for patient \(i\) is
These metrics are widely used in stroke-risk modeling literature and as per article it is often used to find optimial classidfication threshhold.[6].
Analysis
Before starting to generate predictive models, an exploratory analysis was conducted to understand the distribution, structure, and relationships within the cleaned dataset (N = 3,357). This step is crucial in rare-event medical modeling because data imbalance, skewed predictors, or correlated variables can directly influence model behavior and classification performance.
Distribution of Key Continuous Variables
Histograms were used to assess the spread of the primary numeric predictors (Age, BMI, and Average Glucose Level). These variables demonstrate clinically expected right-skewness, particularly glucose and BMI, consistent with published literature on metabolic and cardiovascular risk distributions.
Clear right-skew with a long tail above 200 mg/dL.
Indicates a small group with poor metabolic problem (likely diabetic).
Highly relevant for heart and stroke-related risk.
BMI
Compact distribution (~22–35) with few outliers.
Less variation → weaker contribution compared to vascular predictors.
Pattern aligns with medical proven BMI as influence in our regression results.
Distribution of Key Categorical Variables
Bar charts help visualize population composition. The dataset shows more females than males, a balanced rural–urban distribution, and substantial variation in work type and smoking behavior.
Figure 1: Sample composition by gender, residence type, and smoking status.
Interpreatation Gender: The dataset has more female patients (61%) than males (39%). This imbalance should be noted because gender-related model effects may partly reflect sample composition.
Residence Type: The population is nearly evenly split between urban (51%) and rural (49%) residents. This indicates no geographic bias and good representation of both environments.
Smoking Status: Most participants never smoked (54%), while 25% are former smokers and 22% are current smokers. This provides enough variation to meaningfully examine smoking as a behavioral risk factor for stroke.
Stroke Risk for Clinical and Behavioral Predictors
Figure 2: Stroke percentages (95% CI) by hypertension, heart disease, and smoking status.
Interpretation
The figure summarises how stroke risk varies across key categorical predictors:
Hypertension
Stroke risk is clearly higher among hypertensive patients.
Confidence intervals show a noticeable separation, supporting a strong association.
Heart Disease
Patients with heart disease show higher stroke percentages than those without.
The pattern is consistent with cardiovascular disease being a major clinical risk factor.
Smoking Status
Former and current smokers have higher stroke percentages than never-smokers.
This reflects the long-term vascular effects of tobacco exposure.
Overall, these categorical predictors show patterns aligned with clinical expectations:
vascular risks (hypertension, heart disease) and behavioural risks (smoking) are all associated with elevated stroke likelihood.
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
ℹ The deprecated feature was likely used in the ggcorrplot package.
Please report the issue at <https://github.com/kassambara/ggcorrplot/issues>.
Interpretation Correlation Heatmap of Key Numeric Predictors
All correlations are weak to moderate (0.00–0.26) → no multicollinearity concerns.
Age shows small but meaningful positive correlations with:
glucose (0.24)
hypertension (0.26)
heart disease (0.26) → consistent with known aging-related cardiovascular risk patterns.
BMI has very weak correlations with all other predictors (0.04–0.16) → behaves independently in this dataset.
Avg glucose moderately correlates with:
hypertension (0.17)
heart disease (0.14) → aligns with metabolic/vascular relationships.
Hypertension and heart disease are weakly correlated (0.11) → related but not redundant.
These correlations confirm that the predictors provide unique, non-overlapping information, and all can be safely included in the logistic regression model without multicollinearity issues.
Call:
glm(formula = stroke ~ age + hypertension + heart_disease + avg_glucose_level +
bmi + smoking_status + gender + ever_married, family = binomial(link = "logit"),
data = stroke_train)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -8.924079 0.992603 -8.991 <2e-16 ***
age 0.072637 0.008089 8.979 <2e-16 ***
hypertension 0.455377 0.229005 1.988 0.0468 *
heart_disease 0.487385 0.270707 1.800 0.0718 .
avg_glucose_level 0.003777 0.001705 2.215 0.0267 *
bmi 0.006536 0.015709 0.416 0.6774
smoking_status 0.234263 0.129934 1.803 0.0714 .
gender 0.230592 0.206934 1.114 0.2651
ever_married 0.118496 0.311030 0.381 0.7032
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 953.42 on 2348 degrees of freedom
Residual deviance: 776.77 on 2340 degrees of freedom
AIC: 794.77
Number of Fisher Scoring iterations: 7
Interpretation — Logistic Regression Coefficients
Age is a strong and highly significant predictor (p < 0.001). Higher age is associated with a substantial increase in the odds of stroke.
Hypertension has a significant positive effect on stroke risk (p = 0.0468), indicating hypertensive individuals are more likely to experience stroke.
Average glucose level is also a important predictor (p = 0.0267). Higher glucose values modestly increase stroke risk.
Heart disease shows a positive association but is only borderline significant (p = 0.0718). This suggests a potential effect, but not statistically explainable in this model.
Smoking has likewise borderline significant (p = 0.0714), indicating a increased risk among smokers, but the evidence is not too much strong.
BMI, gender, and marital status show no meaningful statistical association with stroke in this dataset (all p > 0.26). These variables did not contribute substantially to prediction after accounting for other factors.
Model fit improved substantially from the null model (deviance reduced from 953.4 → 776.8; AIC = 794.8), indicating a reasonable fit and useful predictive value.
Odds ratios and confidence intervals
# Odds ratios and 95% confidence intervalscoef_est <-coef(fit_glm)OR <-exp(coef_est)conf_int <-exp(confint(fit_glm)) # confidence intervals on OR scale
The logistic regression findings demonstrate how each predictor impacts the likelihood of having a stroke, while keeping other variables constant:
Age (OR = 1.075, CI: 1.059–1.093) Age is the strongest continuous predictor. Each additional year of age increases the odds of stroke by about 7.5%, and the confidence interval does not include 1, indicating strong statistical significance.
Hypertension (OR = 1.577, CI: 0.996–2.450) Individuals with hypertension have roughly 58% higher odds of stroke compared to those without hypertension, although the lower CI bound is just below 1. This suggests a borderline significant effect, but clinically important.
Heart disease (OR = 1.628, CI: 0.942–2.733) Heart disease increases stroke odds by about 63%, but the CI includes 1, implying the association is positive but not statistically strong in this dataset.
Average glucose level (OR = 1.004, CI: 1.000–1.007) Higher glucose levels are associated with slightly increased stroke risk. Though the effect is small, the CI indicates marginal significance, aligning with known metabolic risk patterns.
BMI (OR = 1.007, CI: 0.975–1.037) BMI shows almost no meaningful effect on stroke risk, and the CI overlaps 1. This predictor does not significantly influence stroke likelihood in this dataset.
Smoking (Fsmoked OR = 1.263; Smokes OR = 1.598)
Former smokers have 26% higher odds, but CI crosses 1 → weak evidence.
Current smokers have ~60% higher odds, but CI still overlaps 1 → suggests increased risk but not statistically conclusive here.
Gender (Female) (OR = 1.259; CI: 0.842–1.903) Females show slightly higher odds, but this effect is not statistically significant.
Ever married (OR = 1.126; CI: 0.590–2.013) Marital status has no clear effect on stroke odds in this sample.
Model predictions and performance on the test set
library(caret)# 1) Predicted probabilities from logistic regressionstroke_test$pred_prob <-predict( fit_glm,newdata = stroke_test,type ="response")# 2) Make sure the TRUE outcome is a factor with levels No / Yesstroke_test$stroke <-factor(stroke_test$stroke,levels =c("No", "Yes"))# 3) Class predictions at threshold c = 0.5stroke_test$pred_class <-ifelse(stroke_test$pred_prob >=0.5, "Yes", "No")stroke_test$pred_class <-factor(stroke_test$pred_class,levels =c("No", "Yes"))# 4) Confusion matrix: positive = "Yes"cm <-confusionMatrix(data = stroke_test$pred_class,reference = stroke_test$stroke,positive ="Yes")cm
Confusion Matrix and Statistics
Reference
Prediction No Yes
No 949 58
Yes 0 1
Accuracy : 0.9425
95% CI : (0.9262, 0.956)
No Information Rate : 0.9415
P-Value [Acc > NIR] : 0.4811
Kappa : 0.0314
Mcnemar's Test P-Value : 7.184e-14
Sensitivity : 0.0169492
Specificity : 1.0000000
Pos Pred Value : 1.0000000
Neg Pred Value : 0.9424032
Prevalence : 0.0585317
Detection Rate : 0.0009921
Detection Prevalence : 0.0009921
Balanced Accuracy : 0.5084746
'Positive' Class : Yes
From the confusion matrix, the following performance metrics are defined:
Positive Predictive Value (Precision)\[
\text{PPV} =
\frac{TP}{TP + FP}.
\]Negative Predictive Value (NPV)
\[
\text{NPV} =
\frac{TN}{TN + FN}.
\]
Interpretation of Logistic Regression Performance (Test Set)
Accuracy = 94.25% The model correctly classified most cases, mainly because the dataset is highly imbalanced (only ~6% stroke cases). High accuracy here does not mean good stroke detection.
Sensitivity (True Positive Rate) = 0.017 The model correctly identified only 1 out of 59 actual stroke cases (≈1.7%). → This shows the model fails to detect stroke cases, which is common in rare-event medical datasets.
Specificity (True Negative Rate) = 1.00 The model correctly classified all non-stroke cases. → It is extremely good at predicting “No stroke,” which dominates the dataset.
Positive Predictive Value (Precision) = 1.00 When the model predicts “Yes,” it is always correct — but it predicted “Yes” only once. High precision is misleading because the model rarely predicts a positive case.
Negative Predictive Value = 0.942 Most “No” predictions are correct, matching the overall class imbalance.
Kappa = 0.031 Kappa measures agreement beyond chance. A value near zero shows the model performs only slightly better than random when considering class imbalance.
Balanced Accuracy = 0.508 When weighting sensitivity and specificity equally, the model performs at chance level (~50%). → Confirms that stroke detection is weak.
McNemar’s Test p < 0.0001 Strong evidence that the model’s errors are systematically skewed—it overwhelmingly predicts “No stroke.”
The logistic regression model achieves high accuracy only because the negative class dominates.It detects almost no true stroke cases, giving extremely poor sensitivity. It performs well for the majority class (non-stroke), but fails for the minority class (stroke).
These results highlight the challenge of severe class imbalance, which requires additional techniques (e.g., SMOTE, class weights, resampling) to improve medical-event prediction.
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
A higher AUC (closer to 1) indicates better discrimination between stroke and non-stroke cases. Values substantially above 0.5 indicate that the model performs better than random classification.
Interpretation of ROC Curve and AUC (Test Set)
The ROC curve evaluates the model’s ability to distinguish between stroke and non-stroke cases across all possible classification thresholds, not just the default 0.5 cutoff.
The AUC = 0.815, which indicates good discriminative performance.
AUC = 0.5 is no discrimination (random guessing)
AUC = 0.7–0.8 is acceptable
AUC = 0.8–0.9 is good
AUC > 0.9 is excellent
Even though the confusion matrix showed poor sensitivity at threshold 0.5, the AUC reveals that the model can separate the two classes reasonably well if a better threshold is chosen.
The strong AUC compared to weak sensitivity highlights the impact of severe class imbalance and the importance of customizing the probability cutoff for medical prediction tasks.
Overall, the ROC analysis suggests that the logistic model contains useful predictive signal, but performance for detecting stroke can be improved with:
set.seed(123)index <-createDataPartition(model_df$stroke, p =0.70, list =FALSE)train_data <- model_df[index, ]test_data <- model_df[-index, ]train_data$stroke <-factor(train_data$stroke, levels =c("No","Yes"))test_data$stroke <-factor(test_data$stroke, levels =c("No","Yes"))
Train control settings
library(caret) # <- add this line to be safectrl <-trainControl(method ="repeatedcv",number =5,repeats =3,classProbs =TRUE,summaryFunction = twoClassSummary,verboseIter =FALSE)
Across all six models, overall accuracy and specificity are very high, mainly because the dataset is highly imbalanced (only ~6% stroke cases). However, sensitivity is extremely low across every model, meaning that almost none of the models correctly identify stroke cases.
Logistic Regression (AUC = 0.78) and GBM (AUC = 0.76) show the best overall discrimination, indicated by the highest AUC values. These models are better at ranking high-risk vs. low-risk individuals, even though they still fail at detecting positives under the default 0.5 threshold.
Tree-based models (Decision Tree, Random Forest, GBM) achieve slightly higher sensitivity than LR, but only marginally (still around 1–2%). KNN and SVM detect 0 stroke cases at this threshold, despite high accuracy.
All models appear to perform well based on accuracy and specificity, but this is misleading—they are failing at the most important task: detecting stroke cases. This confirms that class imbalance severely affects performance and requires threshold tuning, resampling, or cost-sensitive learning to achieve meaningful sensitivity.
ROC curve comparison across models
library(scales)# 1. Create ROC objects for each modelroc_list <-list(LR =roc(test_data$stroke,predict(model_lr, test_data, type ="prob")[, "Yes"],levels =c("No","Yes"), direction ="<"),Tree =roc(test_data$stroke,predict(model_tree, test_data, type ="prob")[, "Yes"],levels =c("No","Yes"), direction ="<"),RF =roc(test_data$stroke,predict(model_rf, test_data, type ="prob")[, "Yes"],levels =c("No","Yes"), direction ="<"),GBM =roc(test_data$stroke,predict(model_gbm, test_data, type ="prob")[, "Yes"],levels =c("No","Yes"), direction ="<"),KNN =roc(test_data$stroke,predict(model_knn, test_data, type ="prob")[, "Yes"],levels =c("No","Yes"), direction ="<"),SVM =roc(test_data$stroke,predict(model_svm, test_data, type ="prob")[, "Yes"],levels =c("No","Yes"), direction ="<"))# 2. AUC valuesauc_vals <-sapply(roc_list, auc)# 3. Long data frame of ROC coordinatesroc_df <-do.call(rbind, lapply(names(roc_list), function(m) { r <- roc_list[[m]]data.frame(model = m,specificity =rev(r$specificities),sensitivity =rev(r$sensitivities) )}))# Treat model as factor in a consistent orderroc_df$model <-factor(roc_df$model, levels =names(roc_list))# 4. Legend labels with AUClabel_map <-paste0(names(auc_vals), " (AUC = ", sprintf("%.3f", auc_vals), ")")names(label_map) <-names(auc_vals)# 5. Color palette by short model namemodel_cols <-c(LR ="#E69F00",Tree ="#0072B2",RF ="#009E73",GBM ="#CC79A7",KNN ="#F0E442",SVM ="#000000")# 6. Plotggplot(roc_df, aes(x =1- specificity, y = sensitivity,colour = model, group = model)) +geom_abline(intercept =0, slope =1,linetype ="dashed", colour ="grey70", linewidth =0.6) +geom_line(linewidth =1) +scale_color_manual(values = model_cols,breaks =names(label_map),labels = label_map,name ="Model" ) +scale_x_continuous(labels =percent_format(accuracy =1)) +scale_y_continuous(labels =percent_format(accuracy =1)) +labs(x ="1 – Specificity (False Positive Rate)",y ="Sensitivity (True Positive Rate)" ) +theme_bw(base_size =12) +theme(legend.position =c(0.65, 0.25),legend.background =element_rect(fill ="white", colour ="grey80"),legend.title =element_text(face ="bold"),panel.grid.minor =element_blank() )
Interpretation
Interpretation of ROC Comparison Across Models
Logistic Regression (AUC = 0.779) performs the best among all six models, showing the strongest ability to differentiate stroke vs. non-stroke cases.
Random Forest (AUC = 0.725) and GBM (AUC = 0.759) also show good discriminative ability and are close competitors to logistic regression.
KNN (AUC = 0.667) performs moderately, better than random guessing but weaker than the tree-based and regression models.
Decision Tree (AUC = 0.648) and SVM (AUC = 0.639) show the lowest AUC values, indicating weaker predictive performance.
All models perform above 0.5, meaning they all do better than random chance — but with large differences in quality.
The ROC curves demonstrate that tree-based ensemble models (RF, GBM) and logistic regression extract more meaningful patterns from the data compared to simpler (Tree) and distance-based (KNN, SVM) methods.
Overall, logistic regression remains the most stable and best-performing model for this dataset, despite class imbalance challenges.
Odds ratios and risk stratification
library(ggplot2)library(dplyr)library(scales)# Fit logistic regression on the same train_data used in the ML comparisonglm_lr <-glm(stroke ~ age + gender + hypertension + heart_disease + ever_married +work_type + Residence_type + avg_glucose_level + bmi + smoking_status,data = train_data,family = binomial)# Coefficients, CIs, p-valueslr_coef <-summary(glm_lr)$coefficients # estimates + p-valuesci_raw <-suppressMessages(confint(glm_lr)) # CI on log-odds scaleor_df <-data.frame(Predictor =rownames(lr_coef),logOR = lr_coef[, "Estimate"],OR =exp(lr_coef[, "Estimate"]),CI_lower =exp(ci_raw[, 1]),CI_upper =exp(ci_raw[, 2]),p_value = lr_coef[, "Pr(>|z|)"]) %>%# remove interceptfilter(Predictor !="(Intercept)") %>%# nicer labels for the plotmutate(Label = dplyr::recode(Predictor,age ="Age (per year)",gender ="Female vs Male",hypertension ="Hypertension (Yes vs No)",heart_disease ="Heart disease (Yes vs No)",ever_married ="Ever married (Yes vs No)",work_type ="Work type (higher level)",Residence_type ="Residence: Rural vs Urban",avg_glucose_level ="Average glucose level",bmi ="BMI",smoking_status ="Smoking status (higher level)"),# significance flag for colourSig =ifelse(p_value <0.05, "p < 0.05", "NS")) %>%# order from lower to higher OR so the plot reads nicelyarrange(OR) %>%mutate(Label =factor(Label, levels = Label))# Forest plotggplot(or_df, aes(x = Label, y = OR, colour = Sig)) +geom_hline(yintercept =1, linetype ="dashed", colour ="grey40") +geom_errorbar(aes(ymin = CI_lower, ymax = CI_upper),width =0.15, linewidth =0.6) +geom_point(size =3) +coord_flip() +scale_y_log10(breaks =c(0.5, 0.75, 1, 1.5, 2, 3, 4),labels =c("0.5", "0.75", "1", "1.5", "2", "3", "4")) +scale_colour_manual(values =c("p < 0.05"="#D55E00", "NS"="#999999")) +labs(title ="Odds Ratios for Stroke Predictors (Logistic Regression)",x =NULL,y ="Odds Ratio (log scale)",colour =NULL) +theme_minimal(base_size =13) +theme(panel.grid.minor =element_blank(),plot.title =element_text(face ="bold", hjust =0.5, size =15),axis.text.y =element_text(size =11),legend.position ="bottom")
Interpretation
Hypertension (Yes vs No)
This is the strongest predictor. Its OR is clearly > 2, and the whole 95% CI lies above 1 (orange point), meaning hypertensive patients have more than double the odds of stroke, with strong statistical evidence.
Age (per year)
The OR is slightly above 1 with a narrow CI fully above 1 (orange).
Each additional year of age increases stroke odds by a small but consistent amount, making age an important continuous risk factor.
Average glucose level
OR is just above 1 with a tight CI above 1 (orange).
Higher glucose is associated with a modest but statistically reliable increase in stroke risk, consistent with metabolic / diabetes-related vascular risk.
Ever married, heart disease, smoking status, gender, BMI, residence, work type
Their confidence intervals all cross 1 (grey points), so in this multivariable model they do not show statistically significant effects after adjusting for age, hypertension and glucose.
Some (e.g., heart disease, smoking) still have ORs above 1, suggesting possible elevated risk, but the evidence is weaker in this dataset.
Overall message: The forest plot shows that, after adjusting for other variables, hypertension, older age, and higher average glucose level are the clearest independent predictors of stroke, while other factors have smaller or more uncertain effects. This aligns well with established clinical knowledge and supports your logistic regression model as a sensible risk-stratification tool.
Threshold tuning to 0.2 from 0.5
# Threshold tuning: use 0.2 instead of 0.5new_threshold <-0.2stroke_test$pred_class_02 <-ifelse(stroke_test$pred_prob >= new_threshold,"Yes", "No")stroke_test$pred_class_02 <-factor(stroke_test$pred_class_02,levels =c("No", "Yes"))# Confusion matrix for threshold = 0.2cm_02 <-confusionMatrix(data = stroke_test$pred_class_02,reference = stroke_test$stroke,positive ="Yes")cm_02
Confusion Matrix and Statistics
Reference
Prediction No Yes
No 903 46
Yes 46 13
Accuracy : 0.9087
95% CI : (0.8892, 0.9258)
No Information Rate : 0.9415
P-Value [Acc > NIR] : 1
Kappa : 0.1719
Mcnemar's Test P-Value : 1
Sensitivity : 0.22034
Specificity : 0.95153
Pos Pred Value : 0.22034
Neg Pred Value : 0.95153
Prevalence : 0.05853
Detection Rate : 0.01290
Detection Prevalence : 0.05853
Balanced Accuracy : 0.58593
'Positive' Class : Yes
Interpretation (threshold = 0.2)
With a lower decision criterion of 0.2, the model successfully identifies 13 out of 59 stroke cases (sensitivity = 22%), compared to only one case with the default 0.5 threshold.
Specificity remains high at almost 95%, indicating that the majority of non-stroke patients are still properly categorized as “no stroke” (903 out of 949).
While overall accuracy declines from 94% to 91%, balanced accuracy improves (from ≈0.51 to ≈0.59), indicating a greater balance of sensitivity and specificity.
This change indicates a therapeutically reasonable compromise: the model detects more possible stroke patients (fewer missed cases) at the expense of a moderate rise in false positives.
conclusion
This experiment compared a conventional logistic regression model with several machine-learning algorithms and examined whether common demographic, behavioral, and clinical characteristics may be used to predict stroke risk using a stroke dataset. Stroke was a rare outcome (about 5% of cases) in the final sample of 3,357 people that was analyzed after the data was cleaned and inconsistent or missing values were eliminated. In addition to reflecting actual epidemiology, this significant class disparity complicates classification, particularly when it comes to identifying the minority (stroke) class.
Age, hypertension, cardiac disease, and raised average glucose levels are among the best predictors of stroke, according to the baseline logistic regression model. Smoking status substantially increased risk. These variables were identified as significant risk factors by odds ratios significantly greater than 1 and confidence intervals that did not cross 1. These results support the use of logistic regression as an interpretable tool for comprehending the relationship between particular risk variables and the likelihood of stroke and are in line with the clinical literature on cerebrovascular illness.
The logistic regression model performed reasonably well overall in terms of prediction; however, sensitivity for stroke cases was more constrained at the default 0.5 probability threshold, as would be expected with an imbalanced outcome. The model clearly outperformed random guessing, according to the ROC curve and AUC values, but there was still space for improvement in terms of differentiating between stroke and non-stroke patients. Youden’s J statistic offers a method for selecting a different categorization threshold that enhances the ratio of sensitivity to specificity, which may be crucial in a screening setting when it is expensive to miss actual stroke cases.
More sophisticated models, such Random Forest and Gradient Boosted Machine, were able to attain somewhat higher AUC values than logistic regression in the machine-learning comparison, showing superior discrimination across a range of thresholds. However, these increases in AUC came at the expense of decreased interpretability and were not always accompanied by significant increases in sensitivity at fixed cut-offs. Logistic regression, on the other hand, offers precise odds ratios and confidence intervals that are simpler for public health professionals and doctors to understand when discussing risk and developing interventions.
Because of the severe class imbalance, sensitivity for stroke cases was extremely low (around 2%), meaning that the model almost never predicted “stroke = Yes” and therefore missed most true stroke cases.
To address this, the decision threshold was lowered from 0.5 to 0.2. At this cut-off, sensitivity increased from roughly 2% to about 22%, while specificity remained high at around 95%. Overall accuracy dropped slightly to about 91%, but balanced accuracy improved, indicating a more reasonable trade-off between detecting stroke cases and avoiding false positives. This threshold experiment illustrates a key practical point: for rare but serious outcomes such as stroke, it can be preferable to sacrifice some overall accuracy in order to reduce the number of missed high-risk individuals. In this setting, the logistic model is more appropriately viewed as a screening or risk-flagging tool rather than a definitive diagnostic rule.
Overall, the findings show that relatively simple models built from routinely collected health indicators can meaningfully distinguish between individuals with and without stroke, even in the presence of substantial class imbalance. Logistic regression emerges as a strong, interpretable baseline, while tree-based ensemble methods provide incremental performance improvements at the cost of transparency. Future work could focus on external validation, calibration assessment, more sophisticated imbalance-handling techniques, and the inclusion of additional clinical or longitudinal information. These extensions would help move from proof-of-concept modelling toward robust, clinically usable tools for stroke risk stratification and targeted prevention.
3. Asmare, A. A., & Agmas, Y. A. (2024). Determinants of coexistence of undernutrition and anemia among under-five children in rwanda; evidence from 2019/20 demographic health survey: Application of bivariate binary logistic regression model. Plos One, 19(4), e0290111.
4. Rahman, M. H., Zafri, N. M., Akter, T., & Pervaz, S. (2021). Identification of factors influencing severity of motorcycle crashes in dhaka, bangladesh using binary logistic regression model. International Journal of Injury Control and Safety Promotion, 28(2), 141–152.
5. Chen, Y., You, P., & Chang, Z. (2024). Binary logistic regression analysis of factors affecting urban road traffic safety. Advances in Transportation Studies, 3.
6. Chen, M.-M., & Chen, M.-C. (2020). Modeling road accident severity with comparisons of logistic regression, decision tree and random forest. Information, 11(5), 270.
7. Hutchinson, A., Pickering, A., Williams, P., & Johnson, M. (2023). Predictors of hospital admission when presenting with acute-on-chronic breathlessness: Binary logistic regression. PLoS One, 18(8), e0289263.
8. Samara, B. (2024). Using binary logistic regression to detect health insurance fraud. Pakistan Journal of Life & Social Sciences, 22(2).
9. Wang, M. (2014). Generalized estimating equations in longitudinal data analysis: A review and recent developments. Advances in Statistics, 2014.