Predicción de presencia de enfermedad cardíaca con SVM

Paula Henríquez

Analizo la estructura de los datos

## 'data.frame':    1025 obs. of  14 variables:
##  $ age     : int  52 53 70 61 62 58 58 55 46 54 ...
##  $ sex     : int  1 1 1 1 0 0 1 1 1 1 ...
##  $ cp      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ trestbps: int  125 140 145 148 138 100 114 160 120 122 ...
##  $ chol    : int  212 203 174 203 294 248 318 289 249 286 ...
##  $ fbs     : int  0 1 0 0 1 0 0 0 0 0 ...
##  $ restecg : int  1 0 1 1 1 0 2 0 0 0 ...
##  $ thalach : int  168 155 125 161 106 122 140 145 144 116 ...
##  $ exang   : int  0 1 1 0 0 0 0 1 0 1 ...
##  $ oldpeak : num  1 3.1 2.6 0 1.9 1 4.4 0.8 0.8 3.2 ...
##  $ slope   : int  2 0 0 2 1 1 0 1 2 1 ...
##  $ ca      : int  2 0 0 1 3 0 3 1 0 2 ...
##  $ thal    : int  3 3 3 3 2 2 1 3 3 2 ...
##  $ target  : int  0 0 0 0 0 1 0 0 0 0 ...

Transformo todas las columnas a numéricas y compruebo si se efectuo el cambio

## 'data.frame':    1025 obs. of  14 variables:
##  $ age     : num  52 53 70 61 62 58 58 55 46 54 ...
##  $ sex     : num  1 1 1 1 0 0 1 1 1 1 ...
##  $ cp      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ trestbps: num  125 140 145 148 138 100 114 160 120 122 ...
##  $ chol    : num  212 203 174 203 294 248 318 289 249 286 ...
##  $ fbs     : num  0 1 0 0 1 0 0 0 0 0 ...
##  $ restecg : num  1 0 1 1 1 0 2 0 0 0 ...
##  $ thalach : num  168 155 125 161 106 122 140 145 144 116 ...
##  $ exang   : num  0 1 1 0 0 0 0 1 0 1 ...
##  $ oldpeak : num  1 3.1 2.6 0 1.9 1 4.4 0.8 0.8 3.2 ...
##  $ slope   : num  2 0 0 2 1 1 0 1 2 1 ...
##  $ ca      : num  2 0 0 1 3 0 3 1 0 2 ...
##  $ thal    : num  3 3 3 3 2 2 1 3 3 2 ...
##  $ target  : num  0 0 0 0 0 1 0 0 0 0 ...

Divido los datos en conjuntos de entrenamiento y prueba. Utilizo createDataPartition para dividir los datos en un 70% para entrenamiento y un 30% para prueba.

## [1] 718  14
## [1] 307  14

Verifico la presencia de valores faltantes en los datos

## [1] FALSE

Convierto la variable objetivo a factor en el conjunto de entrenamiento ; donde 0 indica ausencia de enfermedad cardíaca y 1 presencia de enfermedad cardíaca

## 'data.frame':    718 obs. of  14 variables:
##  $ age     : num  70 62 58 58 55 54 71 43 34 51 ...
##  $ sex     : num  1 0 0 1 1 1 0 0 0 1 ...
##  $ cp      : num  0 0 0 0 0 0 0 0 1 0 ...
##  $ trestbps: num  145 138 100 114 160 122 112 132 118 140 ...
##  $ chol    : num  174 294 248 318 289 286 149 341 210 298 ...
##  $ fbs     : num  0 1 0 0 0 0 0 1 0 0 ...
##  $ restecg : num  1 1 0 2 0 0 1 0 1 1 ...
##  $ thalach : num  125 106 122 140 145 116 125 136 192 122 ...
##  $ exang   : num  1 0 0 0 1 1 0 1 0 1 ...
##  $ oldpeak : num  2.6 1.9 1 4.4 0.8 3.2 1.6 3 0.7 4.2 ...
##  $ slope   : num  0 1 1 0 1 1 1 1 2 1 ...
##  $ ca      : num  0 3 0 3 1 2 0 0 0 3 ...
##  $ thal    : num  3 2 2 1 3 2 2 3 2 3 ...
##  $ target  : Factor w/ 2 levels "0","1": 1 1 2 1 1 1 2 1 2 1 ...

Establezco el control del entrenamiento especificando el método de validación cruzada y otros parámetros.

Entreno un modelo SVM utilizando train() con el conjunto de entrenamiento, especificando el método svmLinear,preprocesamiento, y otros ajustes.

## Support Vector Machines with Linear Kernel 
## 
## 718 samples
##  13 predictor
##   2 classes: '0', '1' 
## 
## Pre-processing: centered (13), scaled (13) 
## Resampling: Cross-Validated (10 fold, repeated 3 times) 
## Summary of sample sizes: 646, 646, 645, 647, 647, 646, ... 
## Resampling results:
## 
##   Accuracy   Kappa    
##   0.8279316  0.6547078
## 
## Tuning parameter 'C' was held constant at a value of 1

una precisión del 82.79% sugiere que el modelo es capaz de predecir correctamente la clase de las muestras en el conjunto de prueba. Kappa de 0.6547 muestra una concordancia significativa entre las predicciones del modelo y los valores reales, teniendo en cuenta el azar.

Realizo predicciones en el conjunto de prueba

##   [1] 0 0 0 0 0 1 0 1 1 0 1 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 1
##  [38] 0 0 1 1 1 1 0 0 1 1 0 0 1 0 1 0 0 1 0 0 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0 1 0
##  [75] 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 0 1 0
## [112] 1 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1
## [149] 1 1 1 1 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 0
## [186] 0 0 0 1 1 0 1 0 1 1 1 0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 1 0 1 0 1 0 1 1 0
## [223] 1 1 1 0 0 0 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 0 1 0 1 0 1 1 0 1 1 0 0 1 1
## [260] 1 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0
## [297] 1 0 1 0 0 0 0 1 0 1 0
## Levels: 0 1

Genero una matriz de confusión para comparar las predicciones del modelo con los valores reales en el conjunto de prueba.

## Confusion Matrix and Statistics
## 
##          
## test_pred   0   1
##         0 120  20
##         1  26 141
##                                           
##                Accuracy : 0.8502          
##                  95% CI : (0.8052, 0.8882)
##     No Information Rate : 0.5244          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.699           
##                                           
##  Mcnemar's Test P-Value : 0.461           
##                                           
##             Sensitivity : 0.8219          
##             Specificity : 0.8758          
##          Pos Pred Value : 0.8571          
##          Neg Pred Value : 0.8443          
##              Prevalence : 0.4756          
##          Detection Rate : 0.3909          
##    Detection Prevalence : 0.4560          
##       Balanced Accuracy : 0.8488          
##                                           
##        'Positive' Class : 0               
## 

El modelo predijo correctamente 120 muestras de la clase 0 y 141 muestras de la clase 1. Cometió 20 errores al predecir la clase 0 como 1 y 26 errores al predecir la clase 1 como 0.