El paquete CARET (Classification And Regression Training) es un paquete integral con una amplia variedad de algoritmos para el aprendizaje automático.
# install.packages("caret") # Algoritmos de aprendizaje automático
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
# install.packages("ggplot2") # Gráficas
library(ggplot2)
# install.packages("lattice") # Crear gráficos
library(lattice)
# install.packages("datasets") # Usar bases de datos precargadas
library(datasets)
# install.packages("DataExplorer") # Análisis Descriptivo
library(DataExplorer)
# install.packages("kernlab")
library(kernlab)
##
## Attaching package: 'kernlab'
## The following object is masked from 'package:ggplot2':
##
## alpha
# install.packages("readxl")
library(readxl)
# file.choose()
df <- read_excel("/Users/elisarivas/Desktop/IA concentración/M2/heart.xlsx")
summary(df)
## age sex cp trestbps
## Min. :29.00 Min. :0.0000 Min. :0.0000 Min. : 94.0
## 1st Qu.:48.00 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:120.0
## Median :56.00 Median :1.0000 Median :1.0000 Median :130.0
## Mean :54.43 Mean :0.6956 Mean :0.9424 Mean :131.6
## 3rd Qu.:61.00 3rd Qu.:1.0000 3rd Qu.:2.0000 3rd Qu.:140.0
## Max. :77.00 Max. :1.0000 Max. :3.0000 Max. :200.0
## chol fbs restecg thalach
## Min. :126 Min. :0.0000 Min. :0.0000 Min. : 71.0
## 1st Qu.:211 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:132.0
## Median :240 Median :0.0000 Median :1.0000 Median :152.0
## Mean :246 Mean :0.1493 Mean :0.5298 Mean :149.1
## 3rd Qu.:275 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:166.0
## Max. :564 Max. :1.0000 Max. :2.0000 Max. :202.0
## exang oldpeak slope ca
## Min. :0.0000 Min. :0.000 Min. :0.000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:0.000 1st Qu.:1.000 1st Qu.:0.0000
## Median :0.0000 Median :0.800 Median :1.000 Median :0.0000
## Mean :0.3366 Mean :1.072 Mean :1.385 Mean :0.7541
## 3rd Qu.:1.0000 3rd Qu.:1.800 3rd Qu.:2.000 3rd Qu.:1.0000
## Max. :1.0000 Max. :6.200 Max. :2.000 Max. :4.0000
## thal target
## Min. :0.000 Min. :0.0000
## 1st Qu.:2.000 1st Qu.:0.0000
## Median :2.000 Median :1.0000
## Mean :2.324 Mean :0.5132
## 3rd Qu.:3.000 3rd Qu.:1.0000
## Max. :3.000 Max. :1.0000
str(df)
## tibble [1,025 × 14] (S3: tbl_df/tbl/data.frame)
## $ age : num [1:1025] 52 53 70 61 62 58 58 55 46 54 ...
## $ sex : num [1:1025] 1 1 1 1 0 0 1 1 1 1 ...
## $ cp : num [1:1025] 0 0 0 0 0 0 0 0 0 0 ...
## $ trestbps: num [1:1025] 125 140 145 148 138 100 114 160 120 122 ...
## $ chol : num [1:1025] 212 203 174 203 294 248 318 289 249 286 ...
## $ fbs : num [1:1025] 0 1 0 0 1 0 0 0 0 0 ...
## $ restecg : num [1:1025] 1 0 1 1 1 0 2 0 0 0 ...
## $ thalach : num [1:1025] 168 155 125 161 106 122 140 145 144 116 ...
## $ exang : num [1:1025] 0 1 1 0 0 0 0 1 0 1 ...
## $ oldpeak : num [1:1025] 1 3.1 2.6 0 1.9 1 4.4 0.8 0.8 3.2 ...
## $ slope : num [1:1025] 2 0 0 2 1 1 0 1 2 1 ...
## $ ca : num [1:1025] 2 0 0 1 3 0 3 1 0 2 ...
## $ thal : num [1:1025] 3 3 3 3 2 2 1 3 3 2 ...
## $ target : num [1:1025] 0 0 0 0 0 1 0 0 0 0 ...
# create_report(df)
plot_missing(df)
plot_histogram(df)
plot_correlation(df)
# Normalmente 80-20 o 70-30
set.seed(123)
renglones_entrenamiento <- createDataPartition(df$target, p=0.8, list=FALSE)
entrenamiento <- df[renglones_entrenamiento, ]
prueba <- df[-renglones_entrenamiento, ]
Los métodos más utilizados para modelar aprendizaje automático son:
entrenamiento$target <- as.factor(entrenamiento$target)
prueba$target <- as.factor(prueba$target)
modelo1 <- train(target~., data=entrenamiento,
method="svmLinear", # Cambiar
preProcess = c("scale","center"),
trControl = trainControl(method="cv", number=10),
tuneGrid = data.frame(C=1) # Cambiar
)
resultado_entrenamiento1 <- predict(modelo1,entrenamiento)
resultado_prueba1 <- predict(modelo1,prueba)
# Matriz de Confusión
# Es una tabla de evaluación que desglosa el rendimiento del modelo de clasificación
# Matriz de Confusión del Resultado de Entrenamiento
mcre1 <- confusionMatrix(resultado_entrenamiento1,entrenamiento$target)
mcre1
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 315 40
## 1 89 376
##
## Accuracy : 0.8427
## 95% CI : (0.8159, 0.8669)
## No Information Rate : 0.5073
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.6847
##
## Mcnemar's Test P-Value : 2.377e-05
##
## Sensitivity : 0.7797
## Specificity : 0.9038
## Pos Pred Value : 0.8873
## Neg Pred Value : 0.8086
## Prevalence : 0.4927
## Detection Rate : 0.3841
## Detection Prevalence : 0.4329
## Balanced Accuracy : 0.8418
##
## 'Positive' Class : 0
##
# Matriz de Confusión del Resultado de la Pruebav
mcrp1 <- confusionMatrix(resultado_prueba1, prueba$target)
mcrp1
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 74 7
## 1 21 103
##
## Accuracy : 0.8634
## 95% CI : (0.8087, 0.9073)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : < 2e-16
##
## Kappa : 0.7226
##
## Mcnemar's Test P-Value : 0.01402
##
## Sensitivity : 0.7789
## Specificity : 0.9364
## Pos Pred Value : 0.9136
## Neg Pred Value : 0.8306
## Prevalence : 0.4634
## Detection Rate : 0.3610
## Detection Prevalence : 0.3951
## Balanced Accuracy : 0.8577
##
## 'Positive' Class : 0
##
library(caret)
library(kernlab)
modelo2 <- train(target~., data=entrenamiento,
method = "svmRadial", # Cambiar
preProcess = c("center", "scale"),
trControl = trainControl(method = "cv", number = 10),
tuneGrid = expand.grid(sigma = 0.01, C=1)
)
resultado_entrenamiento2 <- predict(modelo2, entrenamiento)
resultado_prueba2 <- predict(modelo2, prueba)
# Matriz de confusión
# Es una tabla de evaluación que desglosa el rendimiento del modelo de clasificación
# Matriz de confusión del resultado de entrenamiento
mcre2 <- confusionMatrix(resultado_entrenamiento2, entrenamiento$target)
mcre2
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 318 33
## 1 86 383
##
## Accuracy : 0.8549
## 95% CI : (0.8289, 0.8783)
## No Information Rate : 0.5073
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.7091
##
## Mcnemar's Test P-Value : 1.871e-06
##
## Sensitivity : 0.7871
## Specificity : 0.9207
## Pos Pred Value : 0.9060
## Neg Pred Value : 0.8166
## Prevalence : 0.4927
## Detection Rate : 0.3878
## Detection Prevalence : 0.4280
## Balanced Accuracy : 0.8539
##
## 'Positive' Class : 0
##
# Matriz de confusión del resultado de la prueba
mcrp2 <- confusionMatrix(resultado_prueba2, prueba$target)
mcrp2
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 79 4
## 1 16 106
##
## Accuracy : 0.9024
## 95% CI : (0.8533, 0.9394)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : < 2e-16
##
## Kappa : 0.8021
##
## Mcnemar's Test P-Value : 0.01391
##
## Sensitivity : 0.8316
## Specificity : 0.9636
## Pos Pred Value : 0.9518
## Neg Pred Value : 0.8689
## Prevalence : 0.4634
## Detection Rate : 0.3854
## Detection Prevalence : 0.4049
## Balanced Accuracy : 0.8976
##
## 'Positive' Class : 0
##
modelo3 <- train(target~., data = entrenamiento,
method = "svmPoly", # Cambiar
preProcess = c("scale", "center"),
trControl = trainControl(method = "cv", number = 10),
tuneGrid = data.frame(degree = 2, scale = 1, C=1)
)
resultado_entrenamiento3 <- predict(modelo3, entrenamiento)
resultado_prueba3 <- predict(modelo3, prueba)
# Matriz de Confusión
# Es una tabla de evaluación que desglosa el rendimiento del modelo de clasificación
# Matriz de Confusión del Resultado de Entrenamiento
mcre3 <- confusionMatrix(resultado_entrenamiento3, entrenamiento$target)
mcre3
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 402 3
## 1 2 413
##
## Accuracy : 0.9939
## 95% CI : (0.9858, 0.998)
## No Information Rate : 0.5073
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.9878
##
## Mcnemar's Test P-Value : 1
##
## Sensitivity : 0.9950
## Specificity : 0.9928
## Pos Pred Value : 0.9926
## Neg Pred Value : 0.9952
## Prevalence : 0.4927
## Detection Rate : 0.4902
## Detection Prevalence : 0.4939
## Balanced Accuracy : 0.9939
##
## 'Positive' Class : 0
##
# Matriz de Confusión del Resultado de la Prueba
mcrp3 <- confusionMatrix(resultado_prueba3, prueba$target)
mcrp3
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 93 4
## 1 2 106
##
## Accuracy : 0.9707
## 95% CI : (0.9374, 0.9892)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.9412
##
## Mcnemar's Test P-Value : 0.6831
##
## Sensitivity : 0.9789
## Specificity : 0.9636
## Pos Pred Value : 0.9588
## Neg Pred Value : 0.9815
## Prevalence : 0.4634
## Detection Rate : 0.4537
## Detection Prevalence : 0.4732
## Balanced Accuracy : 0.9713
##
## 'Positive' Class : 0
##
modelo4 <- train(target~., data = entrenamiento,
method = "rpart", # Cambiar
preProcess = c("scale", "center"),
trControl = trainControl(method = "cv", number = 10),
tuneLength = 10
)
resultado_entrenamiento4 <- predict(modelo4, entrenamiento)
resultado_prueba4 <- predict(modelo4, prueba)
# Matriz de Confusión
# Es una tabla de evaluación que desglosa el rendimiento del modelo de clasificación
# Matriz de Confusión del Resultado de Entrenamiento
mcre4 <- confusionMatrix(resultado_entrenamiento4, entrenamiento$target)
mcre4
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 356 33
## 1 48 383
##
## Accuracy : 0.9012
## 95% CI : (0.8787, 0.9208)
## No Information Rate : 0.5073
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.8023
##
## Mcnemar's Test P-Value : 0.1198
##
## Sensitivity : 0.8812
## Specificity : 0.9207
## Pos Pred Value : 0.9152
## Neg Pred Value : 0.8886
## Prevalence : 0.4927
## Detection Rate : 0.4341
## Detection Prevalence : 0.4744
## Balanced Accuracy : 0.9009
##
## 'Positive' Class : 0
##
mcrp4 <- confusionMatrix(resultado_prueba4, prueba$target)
mcrp4
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 85 18
## 1 10 92
##
## Accuracy : 0.8634
## 95% CI : (0.8087, 0.9073)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.7269
##
## Mcnemar's Test P-Value : 0.1859
##
## Sensitivity : 0.8947
## Specificity : 0.8364
## Pos Pred Value : 0.8252
## Neg Pred Value : 0.9020
## Prevalence : 0.4634
## Detection Rate : 0.4146
## Detection Prevalence : 0.5024
## Balanced Accuracy : 0.8656
##
## 'Positive' Class : 0
##
modelo5 <- train(target~., data = entrenamiento,
method = "rf", # Cambiar
preProcess = c("scale", "center"),
trControl = trainControl(method = "cv", number = 10),
tuneGrid = expand.grid(mtry = c(2,4,6,8))
)
resultado_entrenamiento5 <- predict(modelo5, entrenamiento)
resultado_prueba5 <- predict(modelo5, prueba)
# Matriz de Confusión
# Es una tabla de evaluación que desglosa el rendimiento del modelo de clasificación
mcre5 <- confusionMatrix(resultado_entrenamiento5, entrenamiento$target)
mcre5
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 404 0
## 1 0 416
##
## Accuracy : 1
## 95% CI : (0.9955, 1)
## No Information Rate : 0.5073
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
##
## Mcnemar's Test P-Value : NA
##
## Sensitivity : 1.0000
## Specificity : 1.0000
## Pos Pred Value : 1.0000
## Neg Pred Value : 1.0000
## Prevalence : 0.4927
## Detection Rate : 0.4927
## Detection Prevalence : 0.4927
## Balanced Accuracy : 1.0000
##
## 'Positive' Class : 0
##
mcrp5 <- confusionMatrix(resultado_prueba5, prueba$target)
mcrp5
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 95 3
## 1 0 107
##
## Accuracy : 0.9854
## 95% CI : (0.9578, 0.997)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.9706
##
## Mcnemar's Test P-Value : 0.2482
##
## Sensitivity : 1.0000
## Specificity : 0.9727
## Pos Pred Value : 0.9694
## Neg Pred Value : 1.0000
## Prevalence : 0.4634
## Detection Rate : 0.4634
## Detection Prevalence : 0.4780
## Balanced Accuracy : 0.9864
##
## 'Positive' Class : 0
##
modelo6 <- train(target~., data = entrenamiento,
method = "nnet", # Cambiar
preProcess = c("scale", "center"),
trControl = trainControl(method = "cv", number = 10),
trace = FALSE
)
resultado_entrenamiento6 <- predict(modelo6, entrenamiento)
resultado_prueba6 <- predict(modelo6, prueba)
# Matriz de Confusión
# Es una tabla de evaluación que desglosa el rendimiento del modelo de clasificación
mcre6 <- confusionMatrix(resultado_entrenamiento6, entrenamiento$target)
mcre6
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 398 3
## 1 6 413
##
## Accuracy : 0.989
## 95% CI : (0.9793, 0.995)
## No Information Rate : 0.5073
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.978
##
## Mcnemar's Test P-Value : 0.505
##
## Sensitivity : 0.9851
## Specificity : 0.9928
## Pos Pred Value : 0.9925
## Neg Pred Value : 0.9857
## Prevalence : 0.4927
## Detection Rate : 0.4854
## Detection Prevalence : 0.4890
## Balanced Accuracy : 0.9890
##
## 'Positive' Class : 0
##
mcrp6 <- confusionMatrix(resultado_prueba6, prueba$target)
mcrp6
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 93 0
## 1 2 110
##
## Accuracy : 0.9902
## 95% CI : (0.9652, 0.9988)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.9804
##
## Mcnemar's Test P-Value : 0.4795
##
## Sensitivity : 0.9789
## Specificity : 1.0000
## Pos Pred Value : 1.0000
## Neg Pred Value : 0.9821
## Prevalence : 0.4634
## Detection Rate : 0.4537
## Detection Prevalence : 0.4537
## Balanced Accuracy : 0.9895
##
## 'Positive' Class : 0
##
resultados <- data.frame(
"svmLinear" = c(mcre1$overall["Accuracy"], mcrp1$overall["Accuracy"]),
"svmRadial" = c(mcre2$overall["Accuracy"], mcrp2$overall["Accuracy"]),
"svmPolly" = c(mcre3$overall["Accuracy"], mcrp3$overall["Accuracy"]),
"rpart" = c(mcre4$overall["Accuracy"], mcrp4$overall["Accuracy"]),
"rf" = c(mcre5$overall["Accuracy"], mcrp5$overall["Accuracy"]),
"nnet" = c(mcre6$overall["Accuracy"], mcrp6$overall["Accuracy"])
)
rownames(resultados) <- c("Exactitud del Entrenamiento", "Exactitud de la Prueba")
resultados
## svmLinear svmRadial svmPolly rpart rf
## Exactitud del Entrenamiento 0.8426829 0.854878 0.9939024 0.9012195 1.0000000
## Exactitud de la Prueba 0.8634146 0.902439 0.9707317 0.8634146 0.9853659
## nnet
## Exactitud del Entrenamiento 0.9890244
## Exactitud de la Prueba 0.9902439
En conclusión, el modelo de Bosques Aleatorios o Random Forest es el recomendado para la clasificación de problemas del corazón.