Paqueteria necesaria

library(tableone)
library(Amelia)
library(naniar)
library(tidyr)
library(ggplot2)
library(mice)
library(readxl)

PASO I: Carga de Datos

library("dplyr")
data <- read_excel("/Users/angelaluquegarcia/Documents/Diseño_Bioinfo/Base_de_datos_IAM.xlsx")
summary(data)
##        Id         NivelSocieconomico      Age             Sex         Admission_Date                  
##  Min.   :  1.00   Min.   :  0.00     Min.   :34.00   Min.   :0.0000   Min.   :2010-01-24 00:00:00.00  
##  1st Qu.: 80.75   1st Qu.: 78.75     1st Qu.:48.00   1st Qu.:0.0000   1st Qu.:2012-05-11 12:00:00.00  
##  Median :160.50   Median :157.50     Median :56.00   Median :1.0000   Median :2015-07-26 00:00:00.00  
##  Mean   :160.50   Mean   :158.31     Mean   :54.98   Mean   :0.6773   Mean   :2015-07-09 12:07:07.72  
##  3rd Qu.:240.25   3rd Qu.:238.25     3rd Qu.:62.00   3rd Qu.:1.0000   3rd Qu.:2018-08-21 00:00:00.00  
##  Max.   :320.00   Max.   :319.00     Max.   :79.00   Max.   :1.0000   Max.   :2020-12-16 00:00:00.00  
##  NA's   :1        NA's   :13         NA's   :13      NA's   :8        NA's   :18                      
##  Chest pain type      HTA               FA               BP         Cholesterol     FBS over 120   
##  Min.   :1.000   Min.   :0.0000   Min.   :0.0000   Min.   : 94.0   Min.   :126.0   Min.   :0.0000  
##  1st Qu.:3.000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:120.0   1st Qu.:214.0   1st Qu.:0.0000  
##  Median :3.000   Median :0.0000   Median :0.0000   Median :130.0   Median :244.5   Median :0.0000  
##  Mean   :3.173   Mean   :0.2437   Mean   :0.2461   Mean   :130.8   Mean   :249.2   Mean   :0.1373  
##  3rd Qu.:4.000   3rd Qu.:0.0000   3rd Qu.:0.0000   3rd Qu.:140.0   3rd Qu.:277.0   3rd Qu.:0.0000  
##  Max.   :4.000   Max.   :1.0000   Max.   :1.0000   Max.   :192.0   Max.   :417.0   Max.   :1.0000  
##  NA's   :20      NA's   :1                         NA's   :18      NA's   :19      NA's   :15      
##   EKG results       Max HR      Exercise angina  ST depression    Slope of ST    Number of vessels fluro
##  Min.   :0.00   Min.   : 71.0   Min.   :0.0000   Min.   :0.000   Min.   :1.000   Min.   :0.0000         
##  1st Qu.:0.00   1st Qu.:137.8   1st Qu.:0.0000   1st Qu.:0.000   1st Qu.:1.000   1st Qu.:0.0000         
##  Median :1.50   Median :153.0   Median :0.0000   Median :0.800   Median :2.000   Median :0.0000         
##  Mean   :1.01   Mean   :149.6   Mean   :0.3322   Mean   :1.091   Mean   :1.594   Mean   :0.6964         
##  3rd Qu.:2.00   3rd Qu.:165.0   3rd Qu.:1.0000   3rd Qu.:1.800   3rd Qu.:2.000   3rd Qu.:1.0000         
##  Max.   :2.00   Max.   :202.0   Max.   :1.0000   Max.   :6.200   Max.   :3.000   Max.   :3.0000         
##  NA's   :23     NA's   :17      NA's   :20       NA's   :13      NA's   :18      NA's   :18             
##     Thallium     BiomarcadorBasal_A BiomarcadorBasal_B Heart Disease    Cholesterol_Level Glucose_Level   
##  Min.   :3.000   Min.   :  1.636    Min.   :  1.565    Min.   :0.0000   Min.   : 69.01    Min.   : 65.47  
##  1st Qu.:3.000   1st Qu.:  7.537    1st Qu.:  5.499    1st Qu.:0.0000   1st Qu.:170.50    1st Qu.: 87.14  
##  Median :3.000   Median :  9.650    Median :  6.889    Median :0.0000   Median :205.26    Median : 99.34  
##  Mean   :4.757   Mean   : 11.096    Mean   :  7.976    Mean   :0.4488   Mean   :204.40    Mean   : 99.02  
##  3rd Qu.:7.000   3rd Qu.: 11.750    3rd Qu.:  8.251    3rd Qu.:1.0000   3rd Qu.:234.01    3rd Qu.:108.45  
##  Max.   :7.000   Max.   :435.045    Max.   :350.024    Max.   :1.0000   Max.   :392.64    Max.   :146.18  
##  NA's   :17      NA's   :12         NA's   :14         NA's   :18       NA's   :185       NA's   :185     
##  Neutrophils_Count Platelets_Count  LDL_Colesterol   BiomarcadorAgudo_C Symptom_Scale_Admission
##  Min.   :1.890     Min.   : 45.62   Min.   :  0.00   Min.   : 23.92     Min.   : 0.00          
##  1st Qu.:4.199     1st Qu.:209.48   1st Qu.: 88.64   1st Qu.: 46.37     1st Qu.: 8.00          
##  Median :5.033     Median :247.68   Median :143.02   Median : 60.97     Median :19.00          
##  Mean   :5.118     Mean   :251.91   Mean   :144.78   Mean   : 67.53     Mean   :20.53          
##  3rd Qu.:6.015     3rd Qu.:292.82   3rd Qu.:205.73   3rd Qu.: 79.83     3rd Qu.:34.00          
##  Max.   :8.949     Max.   :404.78   Max.   :306.48   Max.   :218.84     Max.   :48.00          
##  NA's   :185       NA's   :185      NA's   :192      NA's   :164        NA's   :184            
##  Prognosis_Scale_24h Killip_Class_24H   Recovery_Discharge Treatment_Response Treatment_Type    
##  Min.   : 0.00       Length:321         Length:321         Length:321         Length:321        
##  1st Qu.:16.00       Class :character   Class :character   Class :character   Class :character  
##  Median :26.00       Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :26.31                                                                                  
##  3rd Qu.:40.00                                                                                  
##  Max.   :50.00                                                                                  
##  NA's   :192                                                                                    
##  3M_Prognosis         Death_Date                    
##  Length:321         Min.   :2010-11-23 00:00:00.00  
##  Class :character   1st Qu.:2015-04-27 00:00:00.00  
##  Mode  :character   Median :2019-06-24 00:00:00.00  
##                     Mean   :2018-01-19 01:48:35.41  
##                     3rd Qu.:2019-12-30 00:00:00.00  
##                     Max.   :2023-11-27 00:00:00.00  
##                     NA's   :16

se realiza un vector con el título de las columnas de interés, estas serán las mostradas en nuestro diccionario como datos antes.

selected_columns <- c("Id",
  "NivelSocieconomico",
  "Age",
  "Sex",
  "Admission_Date",
  "Chest pain type",
  "BP",
  "Cholesterol",
  "FBS over 120",
  "EKG results",
  "Max HR",
  "Exercise angina",
  "ST depression",
  "Slope of ST",
  "Number of vessels fluro",
  "Thallium",
  "BiomarcadorBasal_A",
  "BiomarcadorBasal_B",
  "Heart Disease")
data_antes <- data[, selected_columns]

Paso II:

Observamos los datatos cuyo porcentaje de misvalues es menor al 20% pueden ser imputados. La columna ID parece tener solo una valor nan cuyas variables tambien lo son, por lo que eliminamos esa la fila completa Damos un repaso a nuestra base de datos para revisar que loa media de hoy esta en orden y

data_antes <- data_antes %>% filter(!is.na(Id)) # Eliminacion de fila
summary(data_antes) #verificacion de la dat
##        Id         NivelSocieconomico      Age             Sex         Admission_Date                  
##  Min.   :  1.00   Min.   :  0.00     Min.   :34.00   Min.   :0.0000   Min.   :2010-01-24 00:00:00.00  
##  1st Qu.: 80.75   1st Qu.: 78.75     1st Qu.:48.00   1st Qu.:0.0000   1st Qu.:2012-05-11 12:00:00.00  
##  Median :160.50   Median :157.50     Median :56.00   Median :1.0000   Median :2015-07-26 00:00:00.00  
##  Mean   :160.50   Mean   :158.31     Mean   :54.98   Mean   :0.6773   Mean   :2015-07-09 12:07:07.72  
##  3rd Qu.:240.25   3rd Qu.:238.25     3rd Qu.:62.00   3rd Qu.:1.0000   3rd Qu.:2018-08-21 00:00:00.00  
##  Max.   :320.00   Max.   :319.00     Max.   :79.00   Max.   :1.0000   Max.   :2020-12-16 00:00:00.00  
##                   NA's   :12         NA's   :12      NA's   :7        NA's   :17                      
##  Chest pain type       BP         Cholesterol     FBS over 120     EKG results       Max HR     
##  Min.   :1.000   Min.   : 94.0   Min.   :126.0   Min.   :0.0000   Min.   :0.00   Min.   : 71.0  
##  1st Qu.:3.000   1st Qu.:120.0   1st Qu.:214.0   1st Qu.:0.0000   1st Qu.:0.00   1st Qu.:137.8  
##  Median :3.000   Median :130.0   Median :244.5   Median :0.0000   Median :1.50   Median :153.0  
##  Mean   :3.173   Mean   :130.8   Mean   :249.2   Mean   :0.1373   Mean   :1.01   Mean   :149.6  
##  3rd Qu.:4.000   3rd Qu.:140.0   3rd Qu.:277.0   3rd Qu.:0.0000   3rd Qu.:2.00   3rd Qu.:165.0  
##  Max.   :4.000   Max.   :192.0   Max.   :417.0   Max.   :1.0000   Max.   :2.00   Max.   :202.0  
##  NA's   :19      NA's   :17      NA's   :18      NA's   :14       NA's   :22     NA's   :16     
##  Exercise angina  ST depression    Slope of ST    Number of vessels fluro    Thallium     BiomarcadorBasal_A
##  Min.   :0.0000   Min.   :0.000   Min.   :1.000   Min.   :0.0000          Min.   :3.000   Min.   :  1.636   
##  1st Qu.:0.0000   1st Qu.:0.000   1st Qu.:1.000   1st Qu.:0.0000          1st Qu.:3.000   1st Qu.:  7.537   
##  Median :0.0000   Median :0.800   Median :2.000   Median :0.0000          Median :3.000   Median :  9.650   
##  Mean   :0.3322   Mean   :1.091   Mean   :1.594   Mean   :0.6964          Mean   :4.757   Mean   : 11.096   
##  3rd Qu.:1.0000   3rd Qu.:1.800   3rd Qu.:2.000   3rd Qu.:1.0000          3rd Qu.:7.000   3rd Qu.: 11.750   
##  Max.   :1.0000   Max.   :6.200   Max.   :3.000   Max.   :3.0000          Max.   :7.000   Max.   :435.045   
##  NA's   :19       NA's   :12      NA's   :17      NA's   :17              NA's   :16      NA's   :11        
##  BiomarcadorBasal_B Heart Disease   
##  Min.   :  1.565    Min.   :0.0000  
##  1st Qu.:  5.499    1st Qu.:0.0000  
##  Median :  6.889    Median :0.0000  
##  Mean   :  7.976    Mean   :0.4488  
##  3rd Qu.:  8.251    3rd Qu.:1.0000  
##  Max.   :350.024    Max.   :1.0000  
##  NA's   :13         NA's   :17

Se hace un conteo de miss valué en la base de datos para sacar su porcentaje y saber qué tipo de imputación es la más adecuada. Al no haber porcentajes mayores al 20% estos pueden ser imputados por media, moda o predicción.

#get percentage of missing value of the attributes - Approach 2 (Function)
sapply(data_antes, function(df)
{
  sum(is.na(df)==T)/length(df)*100
})
##                      Id      NivelSocieconomico                     Age                     Sex 
##                  0.0000                  3.7500                  3.7500                  2.1875 
##          Admission_Date         Chest pain type                      BP             Cholesterol 
##                  5.3125                  5.9375                  5.3125                  5.6250 
##            FBS over 120             EKG results                  Max HR         Exercise angina 
##                  4.3750                  6.8750                  5.0000                  5.9375 
##           ST depression             Slope of ST Number of vessels fluro                Thallium 
##                  3.7500                  5.3125                  5.3125                  5.0000 
##      BiomarcadorBasal_A      BiomarcadorBasal_B           Heart Disease 
##                  3.4375                  4.0625                  5.3125
missmap(data_antes, main = "Missing Map", col = c("yellow", "black"), legend = TRUE)
## Warning: Unknown or uninitialised column: `arguments`.
## Unknown or uninitialised column: `arguments`.
## Warning: Unknown or uninitialised column: `imputations`.

# Visualización de valores faltantes con naniar
vis_miss(data_antes) +
  labs(title = "Mapa de valores faltantes")

# Porcentaje de valores faltantes
missing_summary <- data_antes %>%
  summarise(across(everything(), ~ mean(is.na(.)) * 100)) %>%
  pivot_longer(cols = everything(), names_to = "Variable", values_to = "Missing_Percentage")

# Visualización del porcentaje de valores faltantes
ggplot(missing_summary, aes(x = reorder(Variable, -Missing_Percentage), y = Missing_Percentage)) +
  geom_col(fill = "red") +
  coord_flip() +
  labs(title = "Porcentaje de valores faltantes por variable", x = "Variable", y = "Porcentaje")

### *** Paso III: Procesamiento de los datos he imputacion.****

Para tener mejores resultados en la imputación nos aseguramos que las variables categóricas estén clasificadas como un factor.

# Convertir la columna 'Gender' a factor con los niveles 'Masculino' y 'Femenino'
data_antes$Sex <- as.factor(data_antes$Sex)
levels(data_antes$Sex) <- c("Femenino", "Masculino")

data_antes <- data_antes %>% mutate(across(where(~ all(. %in% c(0, 1))), ~ ifelse(. == 1, "SI", "NO")))


# Convertir columnas no numéricas a factores para imputación
factor_columns <- sapply(data_antes, is.character) | sapply(data_antes, is.logical)
data_antes[factor_columns] <- lapply(data_antes[factor_columns], as.factor)


# 4. Configurar la matriz de predicción (opcional)
pred_matrix <- quickpred(data_antes, mincor = 0.1, minpuc = 0.5)

# categorizar columanas segun el diccionario.

# Convertir columnas específicas en factores si existen
columns_to_factor <- c("Chest pain type", "FBS over 120", "EKG results", "Exercise angina", 
                       "Slope of ST", "Thallium", "Heart Disease", "Killip_Class_24H", 
                       "Recovery_Discharge", "Treatment_Response", "Treatment_Type", 
                       "3M_Prognosis")
existing_columns <- columns_to_factor[columns_to_factor %in% names(data_antes)]
data_antes[existing_columns] <- lapply(data_antes[existing_columns], as.factor)

data_antes$`FBS over 120` <- as.factor(ifelse(data_antes$`FBS over 120` == 1, "Si", "No"))
data_antes$`Exercise angina` <- as.factor(ifelse(data_antes$`Exercise angina` == 1, "Si", "No"))
data_antes$`Heart Disease` <- as.factor(ifelse(data_antes$`Heart Disease` == 1, "Presence", "Absence"))

lapply(data_antes, class)
## $Id
## [1] "numeric"
## 
## $NivelSocieconomico
## [1] "numeric"
## 
## $Age
## [1] "numeric"
## 
## $Sex
## [1] "factor"
## 
## $Admission_Date
## [1] "POSIXct" "POSIXt" 
## 
## $`Chest pain type`
## [1] "factor"
## 
## $BP
## [1] "numeric"
## 
## $Cholesterol
## [1] "numeric"
## 
## $`FBS over 120`
## [1] "factor"
## 
## $`EKG results`
## [1] "factor"
## 
## $`Max HR`
## [1] "numeric"
## 
## $`Exercise angina`
## [1] "factor"
## 
## $`ST depression`
## [1] "numeric"
## 
## $`Slope of ST`
## [1] "factor"
## 
## $`Number of vessels fluro`
## [1] "numeric"
## 
## $Thallium
## [1] "factor"
## 
## $BiomarcadorBasal_A
## [1] "numeric"
## 
## $BiomarcadorBasal_B
## [1] "numeric"
## 
## $`Heart Disease`
## [1] "factor"
summary(data_antes)
##        Id         NivelSocieconomico      Age               Sex      Admission_Date                  
##  Min.   :  1.00   Min.   :  0.00     Min.   :34.00   Femenino :101   Min.   :2010-01-24 00:00:00.00  
##  1st Qu.: 80.75   1st Qu.: 78.75     1st Qu.:48.00   Masculino:212   1st Qu.:2012-05-11 12:00:00.00  
##  Median :160.50   Median :157.50     Median :56.00   NA's     :  7   Median :2015-07-26 00:00:00.00  
##  Mean   :160.50   Mean   :158.31     Mean   :54.98                   Mean   :2015-07-09 12:07:07.72  
##  3rd Qu.:240.25   3rd Qu.:238.25     3rd Qu.:62.00                   3rd Qu.:2018-08-21 00:00:00.00  
##  Max.   :320.00   Max.   :319.00     Max.   :79.00                   Max.   :2020-12-16 00:00:00.00  
##                   NA's   :12         NA's   :12                      NA's   :17                      
##  Chest pain type       BP         Cholesterol    FBS over 120 EKG results     Max HR      Exercise angina
##  1   : 21        Min.   : 94.0   Min.   :126.0   No  :264     0   :146    Min.   : 71.0   No  :201       
##  2   : 46        1st Qu.:120.0   1st Qu.:214.0   Si  : 42     1   :  3    1st Qu.:137.8   Si  :100       
##  3   : 94        Median :130.0   Median :244.5   NA's: 14     2   :149    Median :153.0   NA's: 19       
##  4   :140        Mean   :130.8   Mean   :249.2                NA's: 22    Mean   :149.6                  
##  NA's: 19        3rd Qu.:140.0   3rd Qu.:277.0                            3rd Qu.:165.0                  
##                  Max.   :192.0   Max.   :417.0                            Max.   :202.0                  
##                  NA's   :17      NA's   :18                               NA's   :16                     
##  ST depression   Slope of ST Number of vessels fluro Thallium   BiomarcadorBasal_A BiomarcadorBasal_B
##  Min.   :0.000   1   :143    Min.   :0.0000          3   :166   Min.   :  1.636    Min.   :  1.565   
##  1st Qu.:0.000   2   :140    1st Qu.:0.0000          6   : 18   1st Qu.:  7.537    1st Qu.:  5.499   
##  Median :0.800   3   : 20    Median :0.0000          7   :120   Median :  9.650    Median :  6.889   
##  Mean   :1.091   NA's: 17    Mean   :0.6964          NA's: 16   Mean   : 11.096    Mean   :  7.976   
##  3rd Qu.:1.800               3rd Qu.:1.0000                     3rd Qu.: 11.750    3rd Qu.:  8.251   
##  Max.   :6.200               Max.   :3.0000                     Max.   :435.045    Max.   :350.024   
##  NA's   :12                  NA's   :17                         NA's   :11         NA's   :13        
##   Heart Disease
##  Absence :167  
##  Presence:136  
##  NA's    : 17  
##                
##                
##                
## 

Para la imputación de variables numéricas se usa la paquetería “mice” con el método “PMM”

numeric_columns <- sapply(data_antes, is.numeric)
numeric_data <- data_antes[, numeric_columns]

# Realizar imputación con PMM (Predictive Mean Matching)
imputed_numeric <- mice(numeric_data, method = "pmm", m = 5, maxit = 50, seed = 123)
## 
##  iter imp variable
##   1   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   1   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   1   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   1   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   1   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   2   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   2   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   2   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   2   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   2   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   3   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   3   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   3   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   3   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   3   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   4   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   4   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   4   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   4   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   4   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   5   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   5   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   5   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   5   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   5   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   6   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   6   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   6   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   6   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   6   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   7   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   7   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   7   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   7   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   7   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   8   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   8   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   8   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   8   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   8   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   9   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   9   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   9   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   9   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   9   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   10   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   10   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   10   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   10   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   10   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   11   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   11   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   11   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   11   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   11   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   12   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   12   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   12   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   12   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   12   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   13   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   13   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   13   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   13   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   13   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   14   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   14   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   14   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   14   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   14   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   15   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   15   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   15   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   15   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   15   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   16   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   16   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   16   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   16   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   16   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   17   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   17   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   17   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   17   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   17   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   18   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   18   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   18   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   18   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   18   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   19   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   19   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   19   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   19   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   19   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   20   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   20   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   20   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   20   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   20   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   21   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   21   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   21   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   21   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   21   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   22   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   22   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   22   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   22   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   22   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   23   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   23   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   23   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   23   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   23   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   24   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   24   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   24   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   24   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   24   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   25   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   25   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   25   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   25   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   25   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   26   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   26   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   26   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   26   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   26   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   27   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   27   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   27   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   27   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   27   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   28   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   28   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   28   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   28   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   28   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   29   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   29   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   29   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   29   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   29   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   30   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   30   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   30   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   30   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   30   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   31   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   31   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   31   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   31   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   31   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   32   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   32   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   32   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   32   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   32   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   33   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   33   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   33   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   33   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   33   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   34   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   34   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   34   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   34   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   34   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   35   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   35   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   35   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   35   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   35   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   36   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   36   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   36   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   36   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   36   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   37   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   37   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   37   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   37   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   37   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   38   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   38   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   38   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   38   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   38   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   39   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   39   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   39   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   39   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   39   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   40   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   40   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   40   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   40   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   40   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   41   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   41   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   41   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   41   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   41   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   42   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   42   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   42   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   42   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   42   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   43   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   43   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   43   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   43   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   43   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   44   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   44   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   44   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   44   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   44   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   45   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   45   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   45   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   45   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   45   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   46   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   46   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   46   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   46   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   46   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   47   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   47   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   47   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   47   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   47   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   48   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   48   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   48   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   48   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   48   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   49   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   49   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   49   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   49   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   49   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   50   1  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   50   2  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   50   3  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   50   4  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
##   50   5  NivelSocieconomico  Age  BP  Cholesterol  Max HR  ST depression  Number of vessels fluro  BiomarcadorBasal_A  BiomarcadorBasal_B
numeric_data <- complete(imputed_numeric)
data_antes[, numeric_columns] <- numeric_data
summary(data_antes)
##        Id         NivelSocieconomico      Age               Sex      Admission_Date                  
##  Min.   :  1.00   Min.   :  0.00     Min.   :34.00   Femenino :101   Min.   :2010-01-24 00:00:00.00  
##  1st Qu.: 80.75   1st Qu.: 78.75     1st Qu.:48.00   Masculino:212   1st Qu.:2012-05-11 12:00:00.00  
##  Median :160.50   Median :156.50     Median :56.00   NA's     :  7   Median :2015-07-26 00:00:00.00  
##  Mean   :160.50   Mean   :157.85     Mean   :54.99                   Mean   :2015-07-09 12:07:07.72  
##  3rd Qu.:240.25   3rd Qu.:237.25     3rd Qu.:62.00                   3rd Qu.:2018-08-21 00:00:00.00  
##  Max.   :320.00   Max.   :319.00     Max.   :79.00                   Max.   :2020-12-16 00:00:00.00  
##                                                                      NA's   :17                      
##  Chest pain type       BP         Cholesterol    FBS over 120 EKG results     Max HR      Exercise angina
##  1   : 21        Min.   : 94.0   Min.   :126.0   No  :264     0   :146    Min.   : 71.0   No  :201       
##  2   : 46        1st Qu.:120.0   1st Qu.:212.8   Si  : 42     1   :  3    1st Qu.:135.5   Si  :100       
##  3   : 94        Median :130.0   Median :244.5   NA's: 14     2   :149    Median :152.0   NA's: 19       
##  4   :140        Mean   :131.3   Mean   :248.8                NA's: 22    Mean   :149.0                  
##  NA's: 19        3rd Qu.:140.0   3rd Qu.:281.2                            3rd Qu.:164.2                  
##                  Max.   :192.0   Max.   :417.0                            Max.   :202.0                  
##                                                                                                          
##  ST depression Slope of ST Number of vessels fluro Thallium   BiomarcadorBasal_A BiomarcadorBasal_B
##  Min.   :0.0   1   :143    Min.   :0.0000          3   :166   Min.   :  1.636    Min.   :  1.565   
##  1st Qu.:0.0   2   :140    1st Qu.:0.0000          6   : 18   1st Qu.:  7.508    1st Qu.:  5.494   
##  Median :0.8   3   : 20    Median :0.0000          7   :120   Median :  9.660    Median :  6.918   
##  Mean   :1.1   NA's: 17    Mean   :0.7031          NA's: 16   Mean   : 11.054    Mean   :  7.931   
##  3rd Qu.:1.8               3rd Qu.:1.0000                     3rd Qu.: 11.751    3rd Qu.:  8.247   
##  Max.   :6.2               Max.   :3.0000                     Max.   :435.045    Max.   :350.024   
##                                                                                                    
##   Heart Disease
##  Absence :167  
##  Presence:136  
##  NA's    : 17  
##                
##                
##                
## 

Imputación para variables categóricas ordinales con regrecion logistica “logrer”

# Seleccionar columnas categóricas
categorical_columns <- sapply(data_antes, is.factor) | sapply(data_antes, is.character)
categorical_data <- data_antes[, categorical_columns]

# Convertir columnas de texto a factores (si es necesario)
categorical_data <- lapply(categorical_data, as.factor)
categorical_data <- as.data.frame(categorical_data)

# Realizar imputación con regresión logística (logreg)
imputed_categorical <- mice(categorical_data, method = "cart", m = 5, maxit = 50, seed = 123)
## 
##  iter imp variable
##   1   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   1   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   1   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   1   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   1   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   2   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   2   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   2   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   2   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   2   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   3   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   3   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   3   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   3   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   3   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   4   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   4   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   4   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   4   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   4   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   5   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   5   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   5   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   5   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   5   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   6   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   6   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   6   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   6   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   6   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   7   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   7   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   7   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   7   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   7   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   8   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   8   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   8   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   8   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   8   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   9   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   9   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   9   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   9   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   9   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   10   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   10   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   10   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   10   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   10   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   11   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   11   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   11   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   11   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   11   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   12   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   12   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   12   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   12   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   12   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   13   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   13   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   13   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   13   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   13   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   14   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   14   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   14   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   14   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   14   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   15   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   15   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   15   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   15   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   15   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   16   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   16   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   16   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   16   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   16   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   17   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   17   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   17   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   17   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   17   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   18   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   18   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   18   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   18   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   18   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   19   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   19   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   19   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   19   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   19   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   20   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   20   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   20   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   20   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   20   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   21   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   21   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   21   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   21   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   21   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   22   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   22   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   22   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   22   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   22   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   23   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   23   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   23   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   23   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   23   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   24   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   24   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   24   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   24   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   24   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   25   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   25   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   25   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   25   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   25   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   26   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   26   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   26   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   26   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   26   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   27   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   27   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   27   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   27   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   27   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   28   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   28   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   28   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   28   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   28   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   29   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   29   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   29   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   29   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   29   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   30   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   30   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   30   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   30   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   30   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   31   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   31   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   31   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   31   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   31   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   32   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   32   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   32   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   32   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   32   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   33   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   33   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   33   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   33   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   33   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   34   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   34   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   34   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   34   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   34   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   35   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   35   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   35   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   35   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   35   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   36   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   36   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   36   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   36   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   36   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   37   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   37   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   37   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   37   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   37   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   38   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   38   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   38   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   38   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   38   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   39   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   39   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   39   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   39   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   39   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   40   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   40   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   40   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   40   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   40   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   41   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   41   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   41   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   41   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   41   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   42   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   42   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   42   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   42   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   42   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   43   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   43   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   43   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   43   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   43   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   44   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   44   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   44   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   44   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   44   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   45   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   45   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   45   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   45   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   45   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   46   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   46   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   46   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   46   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   46   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   47   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   47   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   47   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   47   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   47   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   48   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   48   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   48   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   48   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   48   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   49   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   49   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   49   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   49   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   49   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   50   1  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   50   2  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   50   3  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   50   4  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
##   50   5  Sex  Chest.pain.type  FBS.over.120  EKG.results  Exercise.angina  Slope.of.ST  Thallium  Heart.Disease
categorical_data <- complete(imputed_categorical)

# Reemplazar las columnas imputadas en el conjunto de datos original
data_antes[, categorical_columns] <- categorical_data
summary(data_antes)
##        Id         NivelSocieconomico      Age               Sex      Admission_Date                  
##  Min.   :  1.00   Min.   :  0.00     Min.   :34.00   Femenino :104   Min.   :2010-01-24 00:00:00.00  
##  1st Qu.: 80.75   1st Qu.: 78.75     1st Qu.:48.00   Masculino:216   1st Qu.:2012-05-11 12:00:00.00  
##  Median :160.50   Median :156.50     Median :56.00                   Median :2015-07-26 00:00:00.00  
##  Mean   :160.50   Mean   :157.85     Mean   :54.99                   Mean   :2015-07-09 12:07:07.72  
##  3rd Qu.:240.25   3rd Qu.:237.25     3rd Qu.:62.00                   3rd Qu.:2018-08-21 00:00:00.00  
##  Max.   :320.00   Max.   :319.00     Max.   :79.00                   Max.   :2020-12-16 00:00:00.00  
##                                                                      NA's   :17                      
##  Chest pain type       BP         Cholesterol    FBS over 120 EKG results     Max HR      Exercise angina
##  1: 22           Min.   : 94.0   Min.   :126.0   No:273       0:156       Min.   : 71.0   No:213         
##  2: 50           1st Qu.:120.0   1st Qu.:212.8   Si: 47       1:  4       1st Qu.:135.5   Si:107         
##  3: 98           Median :130.0   Median :244.5                2:160       Median :152.0                  
##  4:150           Mean   :131.3   Mean   :248.8                            Mean   :149.0                  
##                  3rd Qu.:140.0   3rd Qu.:281.2                            3rd Qu.:164.2                  
##                  Max.   :192.0   Max.   :417.0                            Max.   :202.0                  
##                                                                                                          
##  ST depression Slope of ST Number of vessels fluro Thallium BiomarcadorBasal_A BiomarcadorBasal_B
##  Min.   :0.0   1:151       Min.   :0.0000          3:181    Min.   :  1.636    Min.   :  1.565   
##  1st Qu.:0.0   2:148       1st Qu.:0.0000          6: 18    1st Qu.:  7.508    1st Qu.:  5.494   
##  Median :0.8   3: 21       Median :0.0000          7:121    Median :  9.660    Median :  6.918   
##  Mean   :1.1               Mean   :0.7031                   Mean   : 11.054    Mean   :  7.931   
##  3rd Qu.:1.8               3rd Qu.:1.0000                   3rd Qu.: 11.751    3rd Qu.:  8.247   
##  Max.   :6.2               Max.   :3.0000                   Max.   :435.045    Max.   :350.024   
##                                                                                                  
##   Heart Disease
##  Absence :173  
##  Presence:147  
##                
##                
##                
##                
## 

Debido a que las fechas no son consideradas variables categóricas ni numéricas, esta debe ser clasificada como Date, se usa un modelo preliminar con variables posiblemente relacionadas con las fechas de admisión y se predicen los datos faltantes.

data_antes$Admission_Date <- as.Date(data_antes$Admission_Date)
class(data_antes$Admission_Date)
## [1] "Date"
# Ajustar un modelo de regresión para predecir Admission_Date
model <- lm(as.numeric(Admission_Date) ~ . , data = data_antes, na.action = na.exclude)

# Predecir valores faltantes
predicted_dates <- predict(model, newdata = data_antes[is.na(data_antes$Admission_Date), ])
data_antes$Admission_Date[is.na(data_antes$Admission_Date)] <- as.Date(predicted_dates, origin = "1970-01-01")

PASO IV: Analisis de colinealidad de los datos.

Para analizar la colinealidad de los datos se analizó la paquetería Tableone que muestra la colinealidad entra las variables y nos muestra el p-value entre ellas, se seleccionaran las más representativas. La variable de estratificación será “Heart Desease” debido a que esa es la que se quiere analizar

# Obtener todos los nombres de las columnas de data_antes, excepto la variable de estratificación
variables <- setdiff(names(data_antes), "Heart Disease")

# Crear la tabla estratificada por "Heart Disease"
table_one <- CreateTableOne(vars = variables, 
                            strata = "Heart Disease", 
                            data = data_antes, 
                            test = TRUE)
## Warning in CreateTableOne(vars = variables, strata = "Heart Disease", data = data_antes, : Dropping variable(s) Admission_Date  due to unsupported class.
# Mostrar la tabla
print(table_one)
##                                      Stratified by Heart Disease
##                                       Absence        Presence       p      test
##   n                                      173            147                    
##   Id (mean (SD))                      151.09 (89.98) 171.57 (94.53)  0.048     
##   NivelSocieconomico (mean (SD))      157.92 (92.22) 157.77 (91.51)  0.988     
##   Age (mean (SD))                      53.77 (9.93)   56.43 (8.42)   0.011     
##   Sex = Masculino (%)                     92 (53.2)     124 (84.4)  <0.001     
##   Chest pain type (%)                                               <0.001     
##      1                                    17 ( 9.8)       5 ( 3.4)             
##      2                                    43 (24.9)       7 ( 4.8)             
##      3                                    70 (40.5)      28 (19.0)             
##      4                                    43 (24.9)     107 (72.8)             
##   BP (mean (SD))                      129.20 (16.50) 133.80 (17.54)  0.016     
##   Cholesterol (mean (SD))             242.81 (46.45) 255.84 (50.13)  0.016     
##   FBS over 120 = Si (%)                   24 (13.9)      23 (15.6)   0.773     
##   EKG results (%)                                                    0.004     
##      0                                    99 (57.2)      57 (38.8)             
##      1                                     2 ( 1.2)       2 ( 1.4)             
##      2                                    72 (41.6)      88 (59.9)             
##   Max HR (mean (SD))                  156.84 (19.08) 139.88 (22.51) <0.001     
##   Exercise angina = Si (%)                28 (16.2)      79 (53.7)  <0.001     
##   ST depression (mean (SD))             0.67 (0.81)    1.60 (1.30)  <0.001     
##   Slope of ST (%)                                                   <0.001     
##      1                                   113 (65.3)      38 (25.9)             
##      2                                    52 (30.1)      96 (65.3)             
##      3                                     8 ( 4.6)      13 ( 8.8)             
##   Number of vessels fluro (mean (SD))   0.29 (0.65)    1.18 (1.04)  <0.001     
##   Thallium (%)                                                      <0.001     
##      3                                   140 (80.9)      41 (27.9)             
##      6                                     7 ( 4.0)      11 ( 7.5)             
##      7                                    26 (15.0)      95 (64.6)             
##   BiomarcadorBasal_A (mean (SD))        9.93 (3.08)   12.37 (35.27)  0.366     
##   BiomarcadorBasal_B (mean (SD))        8.89 (26.16)   6.80 (2.03)   0.333
# Versión más detallada con niveles y sin comillas
print(table_one, showAllLevels = TRUE, quote = FALSE, nospaces = TRUE)
##                                      Stratified by Heart Disease
##                                       level     Absence        Presence       p      test
##   n                                                173            147                    
##   Id (mean (SD))                                151.09 (89.98) 171.57 (94.53)  0.048     
##   NivelSocieconomico (mean (SD))                157.92 (92.22) 157.77 (91.51)  0.988     
##   Age (mean (SD))                                53.77 (9.93)   56.43 (8.42)   0.011     
##   Sex (%)                             Femenino      81 (46.8)      23 (15.6)  <0.001     
##                                       Masculino     92 (53.2)     124 (84.4)             
##   Chest pain type (%)                 1             17 ( 9.8)       5 ( 3.4)  <0.001     
##                                       2             43 (24.9)       7 ( 4.8)             
##                                       3             70 (40.5)      28 (19.0)             
##                                       4             43 (24.9)     107 (72.8)             
##   BP (mean (SD))                                129.20 (16.50) 133.80 (17.54)  0.016     
##   Cholesterol (mean (SD))                       242.81 (46.45) 255.84 (50.13)  0.016     
##   FBS over 120 (%)                    No           149 (86.1)     124 (84.4)   0.773     
##                                       Si            24 (13.9)      23 (15.6)             
##   EKG results (%)                     0             99 (57.2)      57 (38.8)   0.004     
##                                       1              2 ( 1.2)       2 ( 1.4)             
##                                       2             72 (41.6)      88 (59.9)             
##   Max HR (mean (SD))                            156.84 (19.08) 139.88 (22.51) <0.001     
##   Exercise angina (%)                 No           145 (83.8)      68 (46.3)  <0.001     
##                                       Si            28 (16.2)      79 (53.7)             
##   ST depression (mean (SD))                       0.67 (0.81)    1.60 (1.30)  <0.001     
##   Slope of ST (%)                     1            113 (65.3)      38 (25.9)  <0.001     
##                                       2             52 (30.1)      96 (65.3)             
##                                       3              8 ( 4.6)      13 ( 8.8)             
##   Number of vessels fluro (mean (SD))             0.29 (0.65)    1.18 (1.04)  <0.001     
##   Thallium (%)                        3            140 (80.9)      41 (27.9)  <0.001     
##                                       6              7 ( 4.0)      11 ( 7.5)             
##                                       7             26 (15.0)      95 (64.6)             
##   BiomarcadorBasal_A (mean (SD))                  9.93 (3.08)   12.37 (35.27)  0.366     
##   BiomarcadorBasal_B (mean (SD))                  8.89 (26.16)   6.80 (2.03)   0.333

Se hace un análisis de Stepwise las variables significativas identificadas en el Tableone y se analiza con “forwart”, “both” y “backward”, al final seleccionamos el modelo con AIC menor y tomamos las variables usadas para entrenar nuestro modelo final por regresión logística.

repre = c( "Age", "Sex", "`Chest pain type`", 
               "BP", "Cholesterol",  "`EKG results`", 
               "`Max HR`", "`Exercise angina`", "`ST depression`", 
               "`Slope of ST`", "`Number of vessels fluro`", "Thallium" )


# Crear la fórmula para las variables
formula <- as.formula(paste("`Heart Disease` ~ ", paste(repre, collapse = " + ")))

# Ejecutar el modelo paso a paso con selección hacia adelante
stepwise_model_F <- step(
  glm(formula, data = data_antes, family = binomial), 
  direction = "forward", 
  trace = FALSE
)

stepwise_model_B <- step(
  glm(formula, data = data_antes, family = binomial), 
  direction = "backward", 
  trace = FALSE
)
stepwise_model_BO <- step(
  glm(formula, data = data_antes, family = binomial), 
  direction = "both", 
  trace = FALSE
)


# Obtener los AICs de los modelos
aic_values <- c(
  Forward = AIC(stepwise_model_F),
  Backward = AIC(stepwise_model_B),
  Both = AIC(stepwise_model_BO)
)

# Encontrar el modelo con el menor AIC
mejor_direccion <- names(which.min(aic_values))
mejor_aic <- min(aic_values)

# Mostrar resultados
cat("El modelo con el menor AIC es:", mejor_direccion, "\n")
## El modelo con el menor AIC es: Backward
cat("Valor del AIC:", mejor_aic, "\n")
## Valor del AIC: 234.789
# Resumen del mejor modelo
if (mejor_direccion == "Forward") {
  resumen_mejor_modelo <- summary(stepwise_model_F)
} else if (mejor_direccion == "Backward") {
  resumen_mejor_modelo <- summary(stepwise_model_B)
} else {
  resumen_mejor_modelo <- summary(stepwise_model_BO)
}

print(resumen_mejor_modelo)
## 
## Call:
## glm(formula = `Heart Disease` ~ Sex + `Chest pain type` + BP + 
##     `Exercise angina` + `Slope of ST` + `Number of vessels fluro` + 
##     Thallium, family = binomial, data = data_antes)
## 
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)               -9.31643    1.82564  -5.103 3.34e-07 ***
## SexMasculino               2.17152    0.51576   4.210 2.55e-05 ***
## `Chest pain type`2         1.10403    0.79650   1.386  0.16571    
## `Chest pain type`3         0.86497    0.66121   1.308  0.19082    
## `Chest pain type`4         2.83473    0.68191   4.157 3.22e-05 ***
## BP                         0.02625    0.01012   2.593  0.00950 ** 
## `Exercise angina`Si        0.84285    0.40921   2.060  0.03943 *  
## `Slope of ST`2             1.65041    0.40140   4.112 3.93e-05 ***
## `Slope of ST`3             1.70443    0.74246   2.296  0.02170 *  
## `Number of vessels fluro`  1.17369    0.22722   5.165 2.40e-07 ***
## Thallium6                  0.23348    0.68646   0.340  0.73376    
## Thallium7                  1.13477    0.39954   2.840  0.00451 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 441.50  on 319  degrees of freedom
## Residual deviance: 210.79  on 308  degrees of freedom
## AIC: 234.79
## 
## Number of Fisher Scoring iterations: 6

Parte VI: Prediccion de Heard desease seguun el modelo resultante.

set.seed(123)  # Para reproducibilidad
sample_index <- sample(1:nrow(data_antes), size = 0.7 * nrow(data_antes))
train_data <- data_antes[sample_index, ]
test_data <- data_antes[-sample_index, ]

# Asegurarte de que la variable dependiente sea categórica
train_data$Heart_Disease <- as.factor(train_data$`Heart Disease`)
test_data$Heart_Disease <- as.factor(test_data$`Heart Disease`)

# Ajustar el modelo de regresión logística
logistic_model <- glm(`Heart Disease` ~ Sex + `Chest pain type` + BP + 
    `Exercise angina` + `Slope of ST` + `Number of vessels fluro` + 
    Thallium,
                      data = train_data, family = binomial)

# Resumen del modelo
summary(logistic_model)
## 
## Call:
## glm(formula = `Heart Disease` ~ Sex + `Chest pain type` + BP + 
##     `Exercise angina` + `Slope of ST` + `Number of vessels fluro` + 
##     Thallium, family = binomial, data = train_data)
## 
## Coefficients:
##                            Estimate Std. Error z value Pr(>|z|)    
## (Intercept)               -11.46201    2.60839  -4.394 1.11e-05 ***
## SexMasculino                2.88708    0.78581   3.674 0.000239 ***
## `Chest pain type`2          1.31460    1.15773   1.135 0.256166    
## `Chest pain type`3          1.14683    0.89712   1.278 0.201127    
## `Chest pain type`4          4.19790    1.01294   4.144 3.41e-05 ***
## BP                          0.02819    0.01325   2.128 0.033343 *  
## `Exercise angina`Si         0.87941    0.61103   1.439 0.150090    
## `Slope of ST`2              2.37502    0.60596   3.919 8.88e-05 ***
## `Slope of ST`3              1.79679    1.02742   1.749 0.080319 .  
## `Number of vessels fluro`   1.54646    0.35708   4.331 1.49e-05 ***
## Thallium6                   0.53503    1.06388   0.503 0.615029    
## Thallium7                   1.39992    0.56786   2.465 0.013691 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 310.37  on 223  degrees of freedom
## Residual deviance: 110.40  on 212  degrees of freedom
## AIC: 134.4
## 
## Number of Fisher Scoring iterations: 7
# Predicciones en el conjunto de prueba
predicciones_prob <- predict(logistic_model, newdata = test_data, type = "response")

# Convertir probabilidades a clases (umbral de 0.5)
predicciones_clases <- ifelse(predicciones_prob >= 0.5, 1, 0)

# Matriz de confusión
matriz_confusion <- table(Predicho = predicciones_clases, Real = test_data$Heart_Disease)

# Extraer los valores de la matriz de confusión
TN <- matriz_confusion[1,1]
FP <- matriz_confusion[1,2]
FN <- matriz_confusion[2,1]
TP <- matriz_confusion[2,2]

# Sensibilidad (Recall o TPR)
sensitivity <- TP / (TP + FN)

# Especificidad (TNR)
specificity <- TN / (TN + FP)

# PPV (Valor Predictivo Positivo)
ppv <- TP / (TP + FP)

# NPV (Valor Predictivo Negativo)
npv <- TN / (TN + FN)

# Mostrar los resultados
cat("Sensibilidad: ", sensitivity, "\n")
## Sensibilidad:  0.7435897
cat("Especificidad: ", specificity, "\n")
## Especificidad:  0.8421053
cat("PPV: ", ppv, "\n")
## PPV:  0.7631579
cat("NPV: ", npv, "\n")
## NPV:  0.8275862
# Calcular accuracy
accuracy <- sum(diag(matriz_confusion)) / sum(matriz_confusion)
cat("Accuracy del modelo:", accuracy, "\n")
## Accuracy del modelo: 0.8020833
# Gráfico ROC y AUC
library(pROC)
roc_curve <- roc(test_data$Heart_Disease, predicciones_prob)
## Setting levels: control = Absence, case = Presence
## Setting direction: controls < cases
plot(roc_curve, main = "Curva ROC", col = "blue")

auc <- auc(roc_curve)
cat("Área bajo la curva (AUC):", auc, "\n")
## Área bajo la curva (AUC): 0.8477768
matriz_confusion <- table(Predicho = predicciones_clases, Real = test_data$Heart_Disease)


# Convertir la matriz de confusión en un data frame para ggplot2
conf_df <- as.data.frame(as.table(matriz_confusion))

# Graficar la matriz de confusión
ggplot(conf_df, aes(x = Predicho, y = Real, fill = Freq)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "lightblue", high = "blue") +
  geom_text(aes(label = Freq), vjust = 1, color = "white", size = 5) +
  labs(
    title = "Matriz de Confusión - Modelo Logístico",
    x = "Clase Predicha",
    y = "Clase Real"
  ) +
  theme_minimal() +
  theme(plot)

metrics_summary <- data.frame(
  Metric = c("Sensibilidad", "Especificidad", "PPV", "NPV", "Accuracy", "AUC"),
  Value = c(sensitivity, specificity, ppv, npv, accuracy, auc)
)

# Mostrar la tabla resumen
print(metrics_summary)

PARTE 2:Analizar, entre los pacientes que han sufrido un IAM y pronóstico a corto y medio plazo, asi como en la respuesta a tratamiento.

Parte I: Seleccion de datos.

Seleccionamos las otra Parte de la base de datos guardadno las variables d einteres en un vector

selected_columns <- c("BiomarcadorAgudo_C", "Symptom_Scale_Admission", 
                     "Prognosis_Scale_24h", "Killip_Class_24H", "Recovery_Discharge", 
                     "Treatment_Response", "Treatment_Type", "3M_Prognosis", 
                     "Death_Date", "Cholesterol_Level", "Glucose_Level", 
                     "Neutrophils_Count", "Platelets_Count")

# Select only the desired columns
data_despues <- data[, selected_columns] 

# Eliminar la última fila de data_imputed
data_despues <- data_despues[-nrow(data_despues), ]


# Verificar las dimensiones después de eliminar la fila
summary(data_despues)
##  BiomarcadorAgudo_C Symptom_Scale_Admission Prognosis_Scale_24h Killip_Class_24H   Recovery_Discharge
##  Min.   : 23.92     Min.   : 0.00           Min.   : 0.00       Length:320         Length:320        
##  1st Qu.: 46.37     1st Qu.: 8.00           1st Qu.:16.00       Class :character   Class :character  
##  Median : 60.97     Median :19.00           Median :26.00       Mode  :character   Mode  :character  
##  Mean   : 67.53     Mean   :20.53           Mean   :26.31                                            
##  3rd Qu.: 79.83     3rd Qu.:34.00           3rd Qu.:40.00                                            
##  Max.   :218.84     Max.   :48.00           Max.   :50.00                                            
##  NA's   :163        NA's   :183             NA's   :191                                              
##  Treatment_Response Treatment_Type     3M_Prognosis         Death_Date                     Cholesterol_Level
##  Length:320         Length:320         Length:320         Min.   :2010-11-23 00:00:00.00   Min.   : 69.01   
##  Class :character   Class :character   Class :character   1st Qu.:2015-04-27 00:00:00.00   1st Qu.:170.50   
##  Mode  :character   Mode  :character   Mode  :character   Median :2019-06-24 00:00:00.00   Median :205.26   
##                                                           Mean   :2018-01-19 01:48:35.41   Mean   :204.40   
##                                                           3rd Qu.:2019-12-30 00:00:00.00   3rd Qu.:234.01   
##                                                           Max.   :2023-11-27 00:00:00.00   Max.   :392.64   
##                                                           NA's   :15                       NA's   :184      
##  Glucose_Level    Neutrophils_Count Platelets_Count 
##  Min.   : 65.47   Min.   :1.890     Min.   : 45.62  
##  1st Qu.: 87.14   1st Qu.:4.199     1st Qu.:209.48  
##  Median : 99.34   Median :5.033     Median :247.68  
##  Mean   : 99.02   Mean   :5.118     Mean   :251.91  
##  3rd Qu.:108.45   3rd Qu.:6.015     3rd Qu.:292.82  
##  Max.   :146.18   Max.   :8.949     Max.   :404.78  
##  NA's   :184      NA's   :184       NA's   :184

*** Paso III: Procesamiento de los datos he imputacion.****

Para tener mejores resultados en la imputación nos aseguramos que las variables categóricas estén clasificadas como un factor.

columns_to_factor <- c("Killip_Class_24H", "Recovery_Discharge", "Treatment_Response", "Treatment_Type", "3M_Prognosis")
existing_columns <- columns_to_factor[columns_to_factor %in% names(data_despues)]
data_despues[existing_columns] <- lapply(data_despues[existing_columns], as.factor)

data_despues$Death_Date <- as.Date(data_despues$Death_Date)
class(data_despues$Death_Date)
## [1] "Date"
# Ajustar un modelo de regresión para predecir Admission_Date
model <- lm(as.numeric(Death_Date) ~ Treatment_Response + Treatment_Type, data = data_despues, na.action = na.exclude)

# Predecir valores faltantes
predicted_dates <- predict(model, newdata = data_despues[is.na(data_despues$Death_Date), ])
data_despues$Death_Date[is.na(data_despues$Death_Date)] <- as.Date(predicted_dates, origin = "1970-01-01")
lapply(data_despues, class)
## $BiomarcadorAgudo_C
## [1] "numeric"
## 
## $Symptom_Scale_Admission
## [1] "numeric"
## 
## $Prognosis_Scale_24h
## [1] "numeric"
## 
## $Killip_Class_24H
## [1] "factor"
## 
## $Recovery_Discharge
## [1] "factor"
## 
## $Treatment_Response
## [1] "factor"
## 
## $Treatment_Type
## [1] "factor"
## 
## $`3M_Prognosis`
## [1] "factor"
## 
## $Death_Date
## [1] "Date"
## 
## $Cholesterol_Level
## [1] "numeric"
## 
## $Glucose_Level
## [1] "numeric"
## 
## $Neutrophils_Count
## [1] "numeric"
## 
## $Platelets_Count
## [1] "numeric"

En este caso se imputaros los datos por media y moda, las fechas seran iimputadas con la paqueteria mice

# Procesamiento de datos.
# Función para calcular la moda
calcular_moda <- function(x) {
  ux <- unique(x[!is.na(x)]) # Valores únicos excluyendo NA
  ux[which.max(tabulate(match(x, ux)))]
}

# Imputación por media y moda
imputar_datos <- function(data_depues) {
  for (col in colnames(data_despues)) {
    if (is.numeric(data_despues[[col]])) {
      # Imputar con la media para variables numéricas
      media <- mean(data_despues[[col]], na.rm = TRUE)
      data_despues[[col]][is.na(data_despues[[col]])] <- media
    } else if (is.factor(data_despues[[col]]) || is.character(data_despues[[col]])) {
      # Imputar con la moda para variables categóricas
      moda <- calcular_moda(data_despues[[col]])
      data_despues[[col]][is.na(data_despues[[col]])] <- moda
    }
  }
  return(data_despues)
}

# Aplicar imputación a los datos
datos_imputados <- imputar_datos(data_despues)

# Verificar la imputación
summary(datos_imputados)
##  BiomarcadorAgudo_C Symptom_Scale_Admission Prognosis_Scale_24h Killip_Class_24H Recovery_Discharge
##  Min.   : 23.92     Min.   : 0.00           Min.   : 0.00       I  :259          Good    : 39      
##  1st Qu.: 61.21     1st Qu.:20.53           1st Qu.:26.31       II : 25          Moderate: 29      
##  Median : 67.53     Median :20.53           Median :26.31       III: 26          Poor    :252      
##  Mean   : 67.53     Mean   :20.53           Mean   :26.31       IV : 10                            
##  3rd Qu.: 67.53     3rd Qu.:20.53           3rd Qu.:26.31                                          
##  Max.   :218.84     Max.   :48.00           Max.   :50.00                                          
##                                                                                                    
##  Treatment_Response                   Treatment_Type   3M_Prognosis   Death_Date         Cholesterol_Level
##  Good   : 29        Intervention + Medication: 46    Mild    :259   Min.   :2010-11-23   Min.   : 69.01   
##  Poor   :242        Medication Only          :268    Moderate: 26   1st Qu.:2015-05-28   1st Qu.:204.40   
##  Regular: 49        No Treatment             :  6    Severe  : 35   Median :2019-05-14   Median :204.40   
##                                                                     Mean   :2018-01-20   Mean   :204.40   
##                                                                     3rd Qu.:2019-12-30   3rd Qu.:204.40   
##                                                                     Max.   :2023-11-27   Max.   :392.64   
##                                                                     NA's   :8                             
##  Glucose_Level    Neutrophils_Count Platelets_Count 
##  Min.   : 65.47   Min.   :1.890     Min.   : 45.62  
##  1st Qu.: 99.02   1st Qu.:5.118     1st Qu.:251.91  
##  Median : 99.02   Median :5.118     Median :251.91  
##  Mean   : 99.02   Mean   :5.118     Mean   :251.91  
##  3rd Qu.: 99.02   3rd Qu.:5.118     3rd Qu.:251.91  
##  Max.   :146.18   Max.   :8.949     Max.   :404.78  
## 

Imputación de fechas (en este caso Death_Date)

data_despues\(Death_Date <- as.numeric(as.Date(data_despues\)Death_Date))

Usar mice para la imputación

library(mice) meth <- rep(“rf”, ncol(data_despues)) # Usa Random Forest para la imputación imp <- mice(data_despues, method = meth)

Recuperar los datos imputados

data_imputed <- complete(imp)

Unimos las bases de datos en la arte 1 y 2 para tener encuentra todas las variables y filtramos para tener solo a los pacientes con presencia de enfermedades cardiacas

# Unir ambas bases de datos 
data_total <- cbind(data_antes, data_imputed)

summary(data_total)
##        Id         NivelSocieconomico      Age               Sex      Admission_Date       Chest pain type
##  Min.   :  1.00   Min.   :  0.00     Min.   :34.00   Femenino :104   Min.   :2010-01-24   1: 22          
##  1st Qu.: 80.75   1st Qu.: 78.75     1st Qu.:48.00   Masculino:216   1st Qu.:2012-06-27   2: 50          
##  Median :160.50   Median :156.50     Median :56.00                   Median :2016-01-20   3: 98          
##  Mean   :160.50   Mean   :157.85     Mean   :54.99                   Mean   :2015-11-06   4:150          
##  3rd Qu.:240.25   3rd Qu.:237.25     3rd Qu.:62.00                   3rd Qu.:2019-01-13                  
##  Max.   :320.00   Max.   :319.00     Max.   :79.00                   Max.   :2022-05-07                  
##        BP         Cholesterol    FBS over 120 EKG results     Max HR      Exercise angina ST depression
##  Min.   : 94.0   Min.   :126.0   No:273       0:156       Min.   : 71.0   No:213          Min.   :0.0  
##  1st Qu.:120.0   1st Qu.:212.8   Si: 47       1:  4       1st Qu.:135.5   Si:107          1st Qu.:0.0  
##  Median :130.0   Median :244.5                2:160       Median :152.0                   Median :0.8  
##  Mean   :131.3   Mean   :248.8                            Mean   :149.0                   Mean   :1.1  
##  3rd Qu.:140.0   3rd Qu.:281.2                            3rd Qu.:164.2                   3rd Qu.:1.8  
##  Max.   :192.0   Max.   :417.0                            Max.   :202.0                   Max.   :6.2  
##  Slope of ST Number of vessels fluro Thallium BiomarcadorBasal_A BiomarcadorBasal_B  Heart Disease
##  1:151       Min.   :0.0000          3:181    Min.   :  1.636    Min.   :  1.565    Absence :173  
##  2:148       1st Qu.:0.0000          6: 18    1st Qu.:  7.508    1st Qu.:  5.494    Presence:147  
##  3: 21       Median :0.0000          7:121    Median :  9.660    Median :  6.918                  
##              Mean   :0.7031                   Mean   : 11.054    Mean   :  7.931                  
##              3rd Qu.:1.0000                   3rd Qu.: 11.751    3rd Qu.:  8.247                  
##              Max.   :3.0000                   Max.   :435.045    Max.   :350.024                  
##  BiomarcadorAgudo_C Symptom_Scale_Admission Prognosis_Scale_24h Killip_Class_24H Recovery_Discharge
##  Min.   : 23.92     Min.   : 0.00           Min.   : 0.00       I  :227          Good    : 97      
##  1st Qu.: 46.88     1st Qu.: 8.00           1st Qu.:16.00       II : 38          Moderate: 53      
##  Median : 59.92     Median :21.00           Median :26.00       III: 44          Poor    :170      
##  Mean   : 66.29     Mean   :20.85           Mean   :26.40       IV : 11                            
##  3rd Qu.: 78.39     3rd Qu.:34.00           3rd Qu.:39.25                                          
##  Max.   :218.84     Max.   :48.00           Max.   :50.00                                          
##  Treatment_Response                   Treatment_Type   3M_Prognosis   Death_Date    Cholesterol_Level
##  Good   : 58        Intervention + Medication:100    Mild    :227   Min.   :14936   Min.   : 69.01   
##  Poor   :138        Medication Only          :208    Moderate: 37   1st Qu.:16628   1st Qu.:167.04   
##  Regular:124        No Treatment             : 12    Severe  : 56   Median :18058   Median :204.34   
##                                                                     Mean   :17563   Mean   :202.82   
##                                                                     3rd Qu.:18260   3rd Qu.:233.98   
##                                                                     Max.   :19688   Max.   :392.64   
##  Glucose_Level    Neutrophils_Count Platelets_Count 
##  Min.   : 65.47   Min.   :1.890     Min.   : 45.62  
##  1st Qu.: 87.62   1st Qu.:4.213     1st Qu.:211.66  
##  Median : 99.17   Median :5.057     Median :247.36  
##  Mean   : 98.59   Mean   :5.164     Mean   :254.58  
##  3rd Qu.:107.38   3rd Qu.:6.092     3rd Qu.:292.54  
##  Max.   :146.18   Max.   :8.949     Max.   :404.78
data_total <- data_total %>% filter(`Heart Disease` == "Presence")

Nos aseguramos que las variables categoricas esten como factores y si es posible unificamos variables teniendo encuenta su modleo teorico y la estadistica de sus datos

# Función para convertir números romanos a arábigos (definida anteriormente)

data_total$Killip_Class_24H <- as.numeric(data_total$Killip_Class_24H)
class(data_total$Killip_Class_24H)
## [1] "numeric"
# Unificar los niveles "leve" y "mild"
data_total$`3M_Prognosis`
##   [1] Moderate Mild     Mild     Severe   Severe   Moderate Mild     Mild     Severe   Mild     Mild    
##  [12] Mild     Mild     Moderate Mild     Mild     Mild     Severe   Mild     Moderate Severe   Moderate
##  [23] Moderate Moderate Moderate Mild     Moderate Mild     Moderate Moderate Mild     Severe   Severe  
##  [34] Moderate Moderate Mild     Mild     Mild     Severe   Mild     Moderate Severe   Mild     Mild    
##  [45] Moderate Mild     Mild     Mild     Severe   Mild     Mild     Mild     Mild     Mild     Mild    
##  [56] Mild     Mild     Mild     Moderate Severe   Severe   Mild     Mild     Moderate Mild     Mild    
##  [67] Mild     Mild     Severe   Severe   Mild     Severe   Mild     Severe   Mild     Mild     Mild    
##  [78] Mild     Mild     Mild     Severe   Mild     Severe   Mild     Moderate Mild     Mild     Mild    
##  [89] Moderate Moderate Mild     Mild     Moderate Mild     Mild     Mild     Severe   Mild     Mild    
## [100] Mild     Mild     Mild     Mild     Severe   Moderate Moderate Severe   Severe   Mild     Severe  
## [111] Severe   Severe   Severe   Mild     Mild     Mild     Severe   Mild     Moderate Mild     Mild    
## [122] Mild     Moderate Mild     Severe   Moderate Mild     Mild     Severe   Severe   Severe   Severe  
## [133] Mild     Mild     Severe   Mild     Mild     Severe   Severe   Mild     Mild     Severe   Severe  
## [144] Mild     Severe   Mild     Mild    
## Levels: Mild Moderate Severe
data_total$`3M_Prognosis` <- factor(data_total$`3M_Prognosis`, 
                                      levels = c("Mild", "Leve", "Severe"), 
                                      labels = c("Leve_mild", "Leve_mild", "Severe"))
levels(data_total$data_total$`3M_Prognosis`)
## NULL
# Contar las observaciones por nivel
table(data_total$data_total$`3M_Prognosis`)
## < table of extent 0 >
data_total$data_total$`3M_Prognosis` <- trimws(data_total$`3M_Prognosis`)
data_total <- data_total %>%
  mutate(`Chest pain type` = case_when(
    `Chest pain type` %in% c(1, 2) ~ "leve",
    `Chest pain type` %in% c(3, 4) ~ "agudo"
  ))
# Instalar el paquete forcats si no lo tienes
# install.packages("forcats")

library(forcats)

# Recodificar la variable 'recobre_discharch'
data_total$Recovery_Discharge <- fct_recode(data_total$Recovery_Discharge,
                                            "good_moderate" = "Good",
                                            "good_moderate" = "Moderate")

# Ver los niveles de la variable después de la recodificación
levels(data_total$Recovery_Discharge)
## [1] "good_moderate" "Poor"
factores <- c("Chest pain type","Recovery_Discharge")

existing_columns <- factores[factores %in% names(data_total)]
data_total[existing_columns] <- lapply(data_total[existing_columns], as.factor)

summary(data_total)
##        Id        NivelSocieconomico      Age               Sex      Admission_Date       Chest pain type
##  Min.   :  1.0   Min.   :  0.0      Min.   :35.00   Femenino : 23   Min.   :2010-01-24   agudo:135      
##  1st Qu.: 85.0   1st Qu.: 85.5      1st Qu.:51.00   Masculino:124   1st Qu.:2012-10-14   leve : 12      
##  Median :177.0   Median :148.0      Median :58.00                   Median :2016-08-24                  
##  Mean   :171.6   Mean   :157.8      Mean   :56.43                   Mean   :2016-04-06                  
##  3rd Qu.:258.0   3rd Qu.:238.5      3rd Qu.:62.00                   3rd Qu.:2019-06-02                  
##  Max.   :319.0   Max.   :319.0      Max.   :79.00                   Max.   :2021-11-10                  
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##        BP         Cholesterol    FBS over 120 EKG results     Max HR      Exercise angina ST depression  
##  Min.   :100.0   Min.   :141.0   No:124       0:57        Min.   : 71.0   No:68           Min.   :0.000  
##  1st Qu.:120.0   1st Qu.:226.5   Si: 23       1: 2        1st Qu.:125.0   Si:79           1st Qu.:0.600  
##  Median :132.0   Median :255.0                2:88        Median :142.0                   Median :1.400  
##  Mean   :133.8   Mean   :255.8                            Mean   :139.9                   Mean   :1.602  
##  3rd Qu.:141.0   3rd Qu.:288.0                            3rd Qu.:156.5                   3rd Qu.:2.400  
##  Max.   :192.0   Max.   :409.0                            Max.   :195.0                   Max.   :6.200  
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##  Slope of ST Number of vessels fluro Thallium BiomarcadorBasal_A BiomarcadorBasal_B  Heart Disease
##  1:38        Min.   :0.000           3:41     Min.   :  1.636    Min.   : 1.835     Absence :  0  
##  2:96        1st Qu.:0.000           6:11     1st Qu.:  6.948    1st Qu.: 5.499     Presence:147  
##  3:13        Median :1.000           7:95     Median :  9.425    Median : 6.836                   
##              Mean   :1.184                    Mean   : 12.371    Mean   : 6.797                   
##              3rd Qu.:2.000                    3rd Qu.: 11.624    3rd Qu.: 8.352                   
##              Max.   :3.000                    Max.   :435.045    Max.   :11.662                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##  BiomarcadorAgudo_C Symptom_Scale_Admission Prognosis_Scale_24h Killip_Class_24H     Recovery_Discharge
##  Min.   : 23.92     Min.   : 0.00           Min.   : 0.00       Min.   :1.000    good_moderate:72      
##  1st Qu.: 46.16     1st Qu.: 8.00           1st Qu.:14.50       1st Qu.:1.000    Poor         :75      
##  Median : 62.65     Median :19.00           Median :26.00       Median :1.000                          
##  Mean   : 66.73     Mean   :20.37           Mean   :25.94       Mean   :1.748                          
##  3rd Qu.: 79.12     3rd Qu.:34.00           3rd Qu.:40.00       3rd Qu.:2.500                          
##  Max.   :218.84     Max.   :48.00           Max.   :50.00       Max.   :4.000                          
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##  Treatment_Response                   Treatment_Type    3M_Prognosis   Death_Date    Cholesterol_Level
##  Good   :31         Intervention + Medication:47     Leve_mild:83    Min.   :15049   Min.   : 69.01   
##  Poor   :62         Medication Only          :93     Severe   :38    1st Qu.:16773   1st Qu.:170.48   
##  Regular:54         No Treatment             : 7     NA's     :26    Median :18173   Median :205.55   
##                                                                      Mean   :17673   Mean   :204.27   
##                                                                      3rd Qu.:18260   3rd Qu.:233.54   
##                                                                      Max.   :19688   Max.   :392.64   
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##  Glucose_Level    Neutrophils_Count Platelets_Count  data_total.Length  data_total.Class  data_total.Mode
##  Min.   : 65.47   Min.   :1.890     Min.   : 45.62   147        -none-     character                     
##  1st Qu.: 87.21   1st Qu.:4.208     1st Qu.:209.59   147        -none-     character                     
##  Median : 98.28   Median :5.042     Median :247.77   147        -none-     character                     
##  Mean   : 98.50   Mean   :5.149     Mean   :253.25   147        -none-     character                     
##  3rd Qu.:107.94   3rd Qu.:6.023     3rd Qu.:293.10   147        -none-     character                     
##  Max.   :146.18   Max.   :8.949     Max.   :404.78   147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##  [ reached getOption("max.print") -- omitted 117 rows ]

Analisamos la correlacion de los datos

library(corrplot)
cor_data <- data_total %>%
  select_if(is.numeric) %>%
  cor(use = "complete.obs")
corrplot(cor_data, method = "circle")

PASO IV: Analisis de colinealidad de los datos.

Para analizar la colinealidad de los datos se analizó la paquetería Tableone que muestra la colinealidad entra las variables y nos muestra el p-value entre ellas, se seleccionaran las más representativas. La variable de estratificación será “3M_Prognosis” debido a que esa es la que se quiere analizar.

# Variables seleccionadas para la tabla
selected_vars <- c(
  "Age", "Sex", "`Chest pain type`", "BP", "Cholesterol", 
  "`EKG results`", "`Max HR`", "`Exercise angina`", 
  "`ST depression`", "`Slope of ST`", "`Number of vessels fluro`", 
  "Thallium", "BiomarcadorAgudo_C", "Symptom_Scale_Admission", 
  "Killip_Class_24H", "Recovery_Discharge", "Treatment_Response", 
  "Treatment_Type", "Cholesterol_Level", "Glucose_Level", 
  "Neutrophils_Count", "Platelets_Count","BiomarcadorBasal_A","BiomarcadorBasal_B"
)

# Verificar que las variables existen en el conjunto de datos
selected_vars <- intersect(selected_vars, names(data_total))

# Crear la Table One estratificada por 3M_Prognosis
table_one <- CreateTableOne(
  vars = selected_vars,
  strata = "3M_Prognosis", # Variable de estratificación
  data = data_total,
  factorVars = selected_vars[sapply(data_total[selected_vars], is.factor)]
)

# Mostrar la tabla en consola
print(table_one, showAllLevels = TRUE)
##                                      Stratified by 3M_Prognosis
##                                       level                     Leve_mild      Severe         p      test
##   n                                                                 83             38                    
##   Age (mean (SD))                                                56.84 (8.48)   56.16 (9.09)   0.687     
##   Sex (%)                             Femenino                       8 ( 9.6)       9 (23.7)   0.075     
##                                       Masculino                     75 (90.4)      29 (76.3)             
##   BP (mean (SD))                                                133.52 (16.35) 138.84 (18.75)  0.115     
##   Cholesterol (mean (SD))                                       252.06 (48.89) 263.68 (59.70)  0.260     
##   Thallium (%)                        3                             22 (26.5)       7 (18.4)   0.420     
##                                       6                              6 ( 7.2)       5 (13.2)             
##                                       7                             55 (66.3)      26 (68.4)             
##   BiomarcadorAgudo_C (mean (SD))                                 65.55 (30.36)  79.69 (28.72)  0.017     
##   Symptom_Scale_Admission (mean (SD))                            22.72 (12.80)  13.32 (13.29) <0.001     
##   Killip_Class_24H (mean (SD))                                    1.00 (0.00)    3.21 (0.58)  <0.001     
##   Recovery_Discharge (%)              good_moderate                 33 (39.8)      26 (68.4)   0.006     
##                                       Poor                          50 (60.2)      12 (31.6)             
##   Treatment_Response (%)              Good                          13 (15.7)       9 (23.7)   0.528     
##                                       Poor                          38 (45.8)      17 (44.7)             
##                                       Regular                       32 (38.6)      12 (31.6)             
##   Treatment_Type (%)                  Intervention + Medication     30 (36.1)       6 (15.8)   0.003     
##                                       Medication Only               52 (62.7)      27 (71.1)             
##                                       No Treatment                   1 ( 1.2)       5 (13.2)             
##   Cholesterol_Level (mean (SD))                                 203.52 (49.97) 200.20 (58.56)  0.749     
##   Glucose_Level (mean (SD))                                      98.37 (16.03) 100.40 (13.59)  0.499     
##   Neutrophils_Count (mean (SD))                                   5.08 (1.48)    5.15 (1.14)   0.819     
##   Platelets_Count (mean (SD))                                   248.37 (56.23) 252.59 (61.72)  0.711     
##   BiomarcadorBasal_A (mean (SD))                                 14.50 (46.86)   9.62 (3.05)   0.523     
##   BiomarcadorBasal_B (mean (SD))                                  7.03 (2.05)    6.39 (1.96)   0.108

Se hace un análisis de Stepwise las variables significativas identificadas en el Tableone y se analiza con “forwart”, “both” y “backward”, al final seleccionamos el modelo con AIC menor y tomamos las variables usadas para entrenar nuestro modelo final por regresión logística.

# Variables seleccionadas
selected_vars <- c(
   "Sex", "`Chest pain type`", "BP",
  "`EKG results`", "`Max HR`", "`Exercise angina`", 
  "`ST depression`", "`Slope of ST`", "`Number of vessels fluro`", 
  "Thallium", "BiomarcadorAgudo_C", "Symptom_Scale_Admission", 
  "Recovery_Discharge", 
  "Treatment_Type", "Death_Date",  
  "Glucose_Level", "Platelets_Count","BiomarcadorBasal_B"
)

# Filtrar variables existentes en el conjunto de datos
repre <- intersect(selected_vars, names(data_total))

# Remover la variable objetivo del conjunto `repre`
repre <- repre[repre != "3M_Prognosis"]

# Crear la fórmula con las variables seleccionadas
formula <- as.formula(paste("`3M_Prognosis` ~", paste(repre, collapse = " + ")))

# Remover NAs del conjunto de datos
data_total <- na.omit(data_total)

# Ejecutar los modelos stepwise
stepwise_model_F <- step(
  glm(formula, data = data_total, family = binomial), 
  direction = "forward", 
  trace = FALSE
)

stepwise_model_B <- step(
  glm(formula, data = data_total, family = binomial), 
  direction = "backward", 
  trace = FALSE
)

stepwise_model_BO <- step(
  glm(formula, data = data_total, family = binomial), 
  direction = "both", 
  trace = FALSE
)

# Obtener los AICs de los modelos
aic_values <- c(
  Forward = AIC(stepwise_model_F),
  Backward = AIC(stepwise_model_B),
  Both = AIC(stepwise_model_BO)
)

# Encontrar el modelo con el menor AIC
mejor_direccion <- names(which.min(aic_values))
mejor_aic <- min(aic_values)

# Mostrar resultados
cat("El modelo con el menor AIC es:", mejor_direccion, "\n")
## El modelo con el menor AIC es: Backward
cat("Valor del AIC:", mejor_aic, "\n")
## Valor del AIC: 121.4268
# Resumen del mejor modelo
if (mejor_direccion == "Forward") {
  resumen_mejor_modelo <- summary(stepwise_model_F)
} else if (mejor_direccion == "Backward") {
  resumen_mejor_modelo <- summary(stepwise_model_B)
} else {
  resumen_mejor_modelo <- summary(stepwise_model_BO)
}

print(resumen_mejor_modelo)
## 
## Call:
## glm(formula = `3M_Prognosis` ~ Sex + BiomarcadorAgudo_C + Symptom_Scale_Admission + 
##     Treatment_Type + Death_Date + BiomarcadorBasal_B, family = binomial, 
##     data = data_total)
## 
## Coefficients:
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                   -1.489e+01  6.236e+00  -2.388 0.016933 *  
## SexMasculino                  -1.407e+00  6.935e-01  -2.029 0.042415 *  
## BiomarcadorAgudo_C             1.748e-02  7.722e-03   2.264 0.023600 *  
## Symptom_Scale_Admission       -7.073e-02  1.977e-02  -3.577 0.000348 ***
## Treatment_TypeMedication Only  1.748e+00  6.381e-01   2.740 0.006145 ** 
## Treatment_TypeNo Treatment     3.398e+00  1.530e+00   2.220 0.026398 *  
## Death_Date                     8.388e-04  3.367e-04   2.491 0.012735 *  
## BiomarcadorBasal_B            -1.822e-01  1.245e-01  -1.464 0.143254    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 150.60  on 120  degrees of freedom
## Residual deviance: 105.43  on 113  degrees of freedom
## AIC: 121.43
## 
## Number of Fisher Scoring iterations: 5

Parte VI: Prediccion de Heard desease seguun el modelo resultante.

set.seed(123)  # Para reproducibilidad
sample_index <- sample(1:nrow(data_antes), size = 0.7 * nrow(data_antes))
train_data <- data_total[sample_index, ]
test_data <- data_total[-sample_index, ]

# Asegurarte de que la variable dependiente sea categórica
train_data$`3M_Prognosis`<- as.factor(train_data$`3M_Prognosis`)
test_data$`3M_Prognosis` <- as.factor(test_data$`3M_Prognosis`)

# Ajustar el modelo de regresión logística
logistic_model <- glm(`3M_Prognosis` ~ Sex + BiomarcadorAgudo_C + Symptom_Scale_Admission + 
    Treatment_Type + Death_Date + BiomarcadorBasal_B,
                      data = train_data, family = binomial)

# Resumen del modelo
summary(logistic_model)
## 
## Call:
## glm(formula = `3M_Prognosis` ~ Sex + BiomarcadorAgudo_C + Symptom_Scale_Admission + 
##     Treatment_Type + Death_Date + BiomarcadorBasal_B, family = binomial, 
##     data = train_data)
## 
## Coefficients:
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                   -1.488e+01  7.248e+00  -2.053 0.040048 *  
## SexMasculino                  -2.042e+00  8.809e-01  -2.318 0.020476 *  
## BiomarcadorAgudo_C             2.814e-02  1.209e-02   2.327 0.019942 *  
## Symptom_Scale_Admission       -1.013e-01  2.721e-02  -3.721 0.000198 ***
## Treatment_TypeMedication Only  1.509e+00  7.819e-01   1.929 0.053680 .  
## Treatment_TypeNo Treatment     2.750e+00  1.955e+00   1.406 0.159680    
## Death_Date                     8.652e-04  3.905e-04   2.216 0.026720 *  
## BiomarcadorBasal_B            -1.864e-01  1.660e-01  -1.123 0.261285    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 107.771  on 86  degrees of freedom
## Residual deviance:  66.192  on 79  degrees of freedom
##   (137 observations deleted due to missingness)
## AIC: 82.192
## 
## Number of Fisher Scoring iterations: 5
# Predicciones en el conjunto de prueba
predicciones_prob <- predict(logistic_model, newdata = test_data, type = "response")

# Convertir probabilidades a clases (umbral de 0.5)
predicciones_clases <- ifelse(predicciones_prob >= 0.5, 1, 0)

# Matriz de confusión
matriz_confusion <- table(Predicho = predicciones_clases, Real = test_data$`3M_Prognosis`)
print(matriz_confusion)
##         Real
## Predicho Leve_mild Severe
##        0        17      6
##        1         6      5
# Extraer los valores de la matriz de confusión
TN <- matriz_confusion[1,1]
FP <- matriz_confusion[1,2]
FN <- matriz_confusion[2,1]
TP <- matriz_confusion[2,2]

# Sensibilidad (Recall o TPR)
sensitivity <- TP / (TP + FN)

# Especificidad (TNR)
specificity <- TN / (TN + FP)

# PPV (Valor Predictivo Positivo)
ppv <- TP / (TP + FP)

# NPV (Valor Predictivo Negativo)
npv <- TN / (TN + FN)

# Mostrar los resultados
cat("Sensibilidad: ", sensitivity, "\n")
## Sensibilidad:  0.4545455
cat("Especificidad: ", specificity, "\n")
## Especificidad:  0.7391304
cat("PPV: ", ppv, "\n")
## PPV:  0.4545455
cat("NPV: ", npv, "\n")
## NPV:  0.7391304
# Calcular accuracy
accuracy <- sum(diag(matriz_confusion)) / sum(matriz_confusion)
cat("Accuracy del modelo:", accuracy, "\n")
## Accuracy del modelo: 0.6470588
# Gráfico ROC y AUC
library(pROC)
roc_curve <- roc(test_data$`3M_Prognosis`, predicciones_prob)
## Setting levels: control = Leve_mild, case = Severe
## Setting direction: controls < cases
plot(roc_curve, main = "Curva ROC", col = "blue")

auc <- auc(roc_curve)
cat("Área bajo la curva (AUC):", auc, "\n")
## Área bajo la curva (AUC): 0.7193676
library(ggplot2)


# Crear matriz de confusión
matriz_confusion <- table(Predicho = predicciones_clases, Real = test_data$`3M_Prognosis`)

# Verificar matriz de confusión
print(matriz_confusion)
##         Real
## Predicho Leve_mild Severe
##        0        17      6
##        1         6      5
# Convertir la matriz de confusión en un data frame para ggplot2
conf_df <- as.data.frame(as.table(matriz_confusion))

# Graficar la matriz de confusión
ggplot(conf_df, aes(x = Predicho, y = Real, fill = Freq)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "lightpink", high = "red") +
  geom_text(aes(label = Freq), vjust = 1, color = "white", size = 5) +
  labs(
    title = "Matriz de Confusión - Modelo Logístico",
    x = "Clase Predicha",
    y = "Clase Real"
  ) +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 14))

metrics_summary <- data.frame(
  Metric = c("Sensibilidad", "Especificidad", "PPV", "NPV", "Accuracy", "AUC"),
  Value = c(sensitivity, specificity, ppv, npv, accuracy, auc)
)

# Mostrar la tabla resumen
print(metrics_summary)

PARTE 2,2:Analizar, entre los pacientes que han sufrido un IAM y pronóstico a corto y medio plazo, asi como en la respuesta a tratamiento.

# Variables seleccionadas para la tabla
selected_vars <- c(
  "Age", "Sex", "`Chest pain type`", "BP", "Cholesterol", 
  "`EKG results`", "`Max HR`", "`Exercise angina`", 
  "`ST depression`", "`Slope of ST`", "`Number of vessels fluro`", 
  "Thallium", "BiomarcadorAgudo_C", "Symptom_Scale_Admission", 
  "Killip_Class_24H", "Recovery_Discharge", "Treatment_Response", 
  "Treatment_Type", "Cholesterol_Level", "Glucose_Level", 
  "Neutrophils_Count", "Platelets_Count","BiomarcadorBasal_A","BiomarcadorBasal_B"
)

# Verificar que las variables existen en el conjunto de datos
selected_vars <- intersect(selected_vars, names(data_total))

# Crear la Table One estratificada por 3M_Prognosis
table_one <- CreateTableOne(
  vars = selected_vars,
  strata = "3M_Prognosis", # Variable de estratificación
  data = data_total,
  factorVars = selected_vars[sapply(data_total[selected_vars], is.factor)]
)

# Mostrar la tabla en consola
print(table_one, showAllLevels = TRUE)
##                                      Stratified by 3M_Prognosis
##                                       level                     Leve_mild      Severe         p      test
##   n                                                                 83             38                    
##   Age (mean (SD))                                                56.84 (8.48)   56.16 (9.09)   0.687     
##   Sex (%)                             Femenino                       8 ( 9.6)       9 (23.7)   0.075     
##                                       Masculino                     75 (90.4)      29 (76.3)             
##   BP (mean (SD))                                                133.52 (16.35) 138.84 (18.75)  0.115     
##   Cholesterol (mean (SD))                                       252.06 (48.89) 263.68 (59.70)  0.260     
##   Thallium (%)                        3                             22 (26.5)       7 (18.4)   0.420     
##                                       6                              6 ( 7.2)       5 (13.2)             
##                                       7                             55 (66.3)      26 (68.4)             
##   BiomarcadorAgudo_C (mean (SD))                                 65.55 (30.36)  79.69 (28.72)  0.017     
##   Symptom_Scale_Admission (mean (SD))                            22.72 (12.80)  13.32 (13.29) <0.001     
##   Killip_Class_24H (mean (SD))                                    1.00 (0.00)    3.21 (0.58)  <0.001     
##   Recovery_Discharge (%)              good_moderate                 33 (39.8)      26 (68.4)   0.006     
##                                       Poor                          50 (60.2)      12 (31.6)             
##   Treatment_Response (%)              Good                          13 (15.7)       9 (23.7)   0.528     
##                                       Poor                          38 (45.8)      17 (44.7)             
##                                       Regular                       32 (38.6)      12 (31.6)             
##   Treatment_Type (%)                  Intervention + Medication     30 (36.1)       6 (15.8)   0.003     
##                                       Medication Only               52 (62.7)      27 (71.1)             
##                                       No Treatment                   1 ( 1.2)       5 (13.2)             
##   Cholesterol_Level (mean (SD))                                 203.52 (49.97) 200.20 (58.56)  0.749     
##   Glucose_Level (mean (SD))                                      98.37 (16.03) 100.40 (13.59)  0.499     
##   Neutrophils_Count (mean (SD))                                   5.08 (1.48)    5.15 (1.14)   0.819     
##   Platelets_Count (mean (SD))                                   248.37 (56.23) 252.59 (61.72)  0.711     
##   BiomarcadorBasal_A (mean (SD))                                 14.50 (46.86)   9.62 (3.05)   0.523     
##   BiomarcadorBasal_B (mean (SD))                                  7.03 (2.05)    6.39 (1.96)   0.108
summary(data_total)
##        Id        NivelSocieconomico      Age               Sex      Admission_Date       Chest pain type
##  Min.   :  4.0   Min.   :  0.0      Min.   :35.00   Femenino : 17   Min.   :2010-03-21   agudo:111      
##  1st Qu.:112.0   1st Qu.: 82.0      1st Qu.:53.00   Masculino:104   1st Qu.:2014-01-16   leve : 10      
##  Median :190.0   Median :147.0      Median :58.00                   Median :2017-04-21                  
##  Mean   :180.5   Mean   :157.7      Mean   :56.63                   Mean   :2016-08-13                  
##  3rd Qu.:264.0   3rd Qu.:245.0      3rd Qu.:61.00                   3rd Qu.:2019-09-04                  
##  Max.   :319.0   Max.   :319.0      Max.   :79.00                   Max.   :2021-11-10                  
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##                                                                                                         
##        BP         Cholesterol    FBS over 120 EKG results     Max HR      Exercise angina ST depression  
##  Min.   :100.0   Min.   :149.0   No:103       0:43        Min.   : 71.0   No:52           Min.   :0.000  
##  1st Qu.:122.0   1st Qu.:219.0   Si: 18       1: 2        1st Qu.:125.0   Si:69           1st Qu.:0.600  
##  Median :134.0   Median :256.0                2:76        Median :140.0                   Median :1.500  
##  Mean   :135.2   Mean   :255.7                            Mean   :137.8                   Mean   :1.645  
##  3rd Qu.:145.0   3rd Qu.:288.0                            3rd Qu.:154.0                   3rd Qu.:2.400  
##  Max.   :192.0   Max.   :409.0                            Max.   :195.0                   Max.   :6.200  
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##  Slope of ST Number of vessels fluro Thallium BiomarcadorBasal_A BiomarcadorBasal_B  Heart Disease
##  1:31        Min.   :0.000           3:29     Min.   :  1.636    Min.   : 1.835     Absence :  0  
##  2:79        1st Qu.:0.000           6:11     1st Qu.:  7.041    1st Qu.: 5.550     Presence:121  
##  3:11        Median :1.000           7:81     Median :  9.425    Median : 6.815                   
##              Mean   :1.165                    Mean   : 12.972    Mean   : 6.828                   
##              3rd Qu.:2.000                    3rd Qu.: 11.705    3rd Qu.: 8.382                   
##              Max.   :3.000                    Max.   :435.045    Max.   :11.662                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##                                                                                                   
##  BiomarcadorAgudo_C Symptom_Scale_Admission Prognosis_Scale_24h Killip_Class_24H     Recovery_Discharge
##  Min.   : 25.04     Min.   : 0.00           Min.   : 0.00       Min.   :1.000    good_moderate:59      
##  1st Qu.: 49.13     1st Qu.: 8.00           1st Qu.:16.00       1st Qu.:1.000    Poor         :62      
##  Median : 65.93     Median :17.00           Median :28.00       Median :1.000                          
##  Mean   : 69.99     Mean   :19.77           Mean   :27.14       Mean   :1.694                          
##  3rd Qu.: 83.73     3rd Qu.:32.00           3rd Qu.:40.00       3rd Qu.:3.000                          
##  Max.   :218.84     Max.   :48.00           Max.   :50.00       Max.   :4.000                          
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##                                                                                                        
##  Treatment_Response                   Treatment_Type    3M_Prognosis   Death_Date    Cholesterol_Level
##  Good   :22         Intervention + Medication:36     Leve_mild:83    Min.   :15049   Min.   : 69.01   
##  Poor   :55         Medication Only          :79     Severe   :38    1st Qu.:17593   1st Qu.:167.33   
##  Regular:44         No Treatment             : 6                     Median :18260   Median :203.43   
##                                                                      Mean   :17858   Mean   :202.48   
##                                                                      3rd Qu.:18260   3rd Qu.:233.11   
##                                                                      Max.   :19688   Max.   :392.64   
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##                                                                                                       
##  Glucose_Level    Neutrophils_Count Platelets_Count  data_total.Length  data_total.Class  data_total.Mode
##  Min.   : 66.83   Min.   :1.890     Min.   : 76.22   147        -none-     character                     
##  1st Qu.: 87.54   1st Qu.:4.183     1st Qu.:209.59   147        -none-     character                     
##  Median : 98.37   Median :5.021     Median :246.54   147        -none-     character                     
##  Mean   : 99.01   Mean   :5.104     Mean   :249.70   147        -none-     character                     
##  3rd Qu.:108.15   3rd Qu.:6.022     3rd Qu.:288.83   147        -none-     character                     
##  Max.   :146.18   Max.   :8.949     Max.   :389.16   147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##                                                      147        -none-     character                     
##  [ reached getOption("max.print") -- omitted 91 rows ]
# Variables seleccionadas para la tabla
selected_vars <- c(
  "Age", "Sex", "`Chest pain type`", "BP", "Cholesterol", 
  "`EKG results`", "`Max HR`", "`Exercise angina`", 
  "`ST depression`", "`Slope of ST`", "`Number of vessels fluro`", 
  "Thallium", "BiomarcadorAgudo_C", "Symptom_Scale_Admission", 
  "Killip_Class_24H", "Recovery_Discharge", "3M_Prognosis", 
  "Treatment_Type", "Cholesterol_Level", "Glucose_Level", 
  "Neutrophils_Count", "Platelets_Count","BiomarcadorBasal_A","BiomarcadorBasal_B"
)

# Verificar que las variables existen en el conjunto de datos
selected_vars <- intersect(selected_vars, names(data_total))

# Crear la Table One estratificada por "Treatment_Response"
table_one <- CreateTableOne(
  vars = selected_vars,
  strata = "Treatment_Response", # Variable de estratificación
  data = data_total,
  factorVars = selected_vars[sapply(data_total[selected_vars], is.factor)]
)

# Mostrar la tabla en consola
print(table_one, showAllLevels = TRUE)
##                                      Stratified by Treatment_Response
##                                       level                     Good           Poor           Regular       
##   n                                                                 22             55             44        
##   Age (mean (SD))                                                58.64 (9.60)   56.56 (8.48)   55.70 (8.40) 
##   Sex (%)                             Femenino                       4 (18.2)       6 (10.9)       7 (15.9) 
##                                       Masculino                     18 (81.8)      49 (89.1)      37 (84.1) 
##   BP (mean (SD))                                                137.86 (19.31) 133.22 (14.39) 136.32 (19.43)
##   Cholesterol (mean (SD))                                       247.82 (49.24) 257.87 (50.41) 256.95 (57.38)
##   Thallium (%)                        3                              6 (27.3)      13 (23.6)      10 (22.7) 
##                                       6                              3 (13.6)       6 (10.9)       2 ( 4.5) 
##                                       7                             13 (59.1)      36 (65.5)      32 (72.7) 
##   BiomarcadorAgudo_C (mean (SD))                                 55.56 (31.43)  79.44 (25.60)  65.39 (32.25)
##   Symptom_Scale_Admission (mean (SD))                            22.23 (11.86)  21.35 (14.52)  16.57 (12.96)
##   Killip_Class_24H (mean (SD))                                    1.91 (1.15)    1.73 (1.13)    1.55 (0.98) 
##   Recovery_Discharge (%)              good_moderate                  9 (40.9)      22 (40.0)      28 (63.6) 
##                                       Poor                          13 (59.1)      33 (60.0)      16 (36.4) 
##   3M_Prognosis (%)                    Leve_mild                     13 (59.1)      38 (69.1)      32 (72.7) 
##                                       Severe                         9 (40.9)      17 (30.9)      12 (27.3) 
##   Treatment_Type (%)                  Intervention + Medication      2 ( 9.1)      17 (30.9)      17 (38.6) 
##                                       Medication Only               19 (86.4)      35 (63.6)      25 (56.8) 
##                                       No Treatment                   1 ( 4.5)       3 ( 5.5)       2 ( 4.5) 
##   Cholesterol_Level (mean (SD))                                 198.16 (62.48) 206.09 (50.06) 200.12 (51.32)
##   Glucose_Level (mean (SD))                                     104.06 (15.45)  96.47 (15.92)  99.65 (13.95)
##   Neutrophils_Count (mean (SD))                                   4.90 (1.24)    5.12 (1.40)    5.18 (1.43) 
##   Platelets_Count (mean (SD))                                   248.81 (54.55) 247.55 (58.64) 252.82 (59.42)
##   BiomarcadorBasal_A (mean (SD))                                  8.72 (3.63)    9.12 (3.45)   19.91 (64.11)
##   BiomarcadorBasal_B (mean (SD))                                  7.25 (2.08)    6.45 (2.06)    7.09 (1.94) 
##                                      Stratified by Treatment_Response
##                                       p      test
##   n                                              
##   Age (mean (SD))                      0.433     
##   Sex (%)                              0.642     
##                                                  
##   BP (mean (SD))                       0.491     
##   Cholesterol (mean (SD))              0.739     
##   Thallium (%)                         0.686     
##                                                  
##                                                  
##   BiomarcadorAgudo_C (mean (SD))       0.003     
##   Symptom_Scale_Admission (mean (SD))  0.144     
##   Killip_Class_24H (mean (SD))         0.418     
##   Recovery_Discharge (%)               0.047     
##                                                  
##   3M_Prognosis (%)                     0.528     
##                                                  
##   Treatment_Type (%)                   0.171     
##                                                  
##                                                  
##   Cholesterol_Level (mean (SD))        0.783     
##   Glucose_Level (mean (SD))            0.135     
##   Neutrophils_Count (mean (SD))        0.732     
##   Platelets_Count (mean (SD))          0.902     
##   BiomarcadorBasal_A (mean (SD))       0.334     
##   BiomarcadorBasal_B (mean (SD))       0.166
# Variables seleccionadas
selected_vars <-  c(
  "Age", "Sex", "`Chest pain type`", "BP", "Cholesterol", 
  "`EKG results`", "`Max HR`", "`Exercise angina`", 
  "`ST depression`", "`Slope of ST`", "`Number of vessels fluro`", 
  "Thallium", "BiomarcadorAgudo_C", "Symptom_Scale_Admission", 
  "Killip_Class_24H", "Recovery_Discharge", "`3M_Prognosis`", 
  "Treatment_Type", "Cholesterol_Level", "Glucose_Level", 
  "Neutrophils_Count", "Platelets_Count","BiomarcadorBasal_A","BiomarcadorBasal_B")

# Filtrar variables existentes en el conjunto de datos
repre <- intersect(selected_vars, names(data_total))

# Remover la variable objetivo del conjunto `repre`
repre <- repre[repre != "Treatment_Response"]

# Crear la fórmula con las variables seleccionadas
formula <- as.formula(paste("Treatment_Response ~", paste(repre, collapse = " + ")))



# Ejecutar los modelos stepwise
stepwise_model_F <- step(
  glm(formula, data = data_total, family = binomial), 
  direction = "forward", 
  trace = FALSE
)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
stepwise_model_B <- step(
  glm(formula, data = data_total, family = binomial), 
  direction = "backward", 
  trace = FALSE
)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
stepwise_model_BO <- step(
  glm(formula, data = data_total, family = binomial), 
  direction = "both", 
  trace = FALSE
)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
# Obtener los AICs de los modelos
aic_values <- c(
  Forward = AIC(stepwise_model_F),
  Backward = AIC(stepwise_model_B),
  Both = AIC(stepwise_model_BO)
)

# Encontrar el modelo con el menor AIC
mejor_direccion <- names(which.min(aic_values))
mejor_aic <- min(aic_values)

# Mostrar resultados
cat("El modelo con el menor AIC es:", mejor_direccion, "\n")
## El modelo con el menor AIC es: Backward
cat("Valor del AIC:", mejor_aic, "\n")
## Valor del AIC: 102.1959
# Resumen del mejor modelo
if (mejor_direccion == "Forward") {
  resumen_mejor_modelo <- summary(stepwise_model_F)
} else if (mejor_direccion == "Backward") {
  resumen_mejor_modelo <- summary(stepwise_model_B)
} else {
  resumen_mejor_modelo <- summary(stepwise_model_BO)
}

print(resumen_mejor_modelo)
## 
## Call:
## glm(formula = Treatment_Response ~ Age + Cholesterol + BiomarcadorAgudo_C + 
##     Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type + 
##     Glucose_Level + BiomarcadorBasal_A, family = binomial, data = data_total)
## 
## Coefficients:
##                                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                    7.761628   3.409247   2.277 0.022808 *  
## Age                           -0.063286   0.037258  -1.699 0.089399 .  
## Cholesterol                    0.007890   0.005194   1.519 0.128695    
## BiomarcadorAgudo_C             0.046963   0.014221   3.302 0.000959 ***
## Symptom_Scale_Admission       -0.059049   0.025192  -2.344 0.019080 *  
## Killip_Class_24H              -0.774185   0.310719  -2.492 0.012717 *  
## Treatment_TypeMedication Only -1.764648   0.860843  -2.050 0.040374 *  
## Treatment_TypeNo Treatment    -2.045430   1.593997  -1.283 0.199419    
## Glucose_Level                 -0.045618   0.020842  -2.189 0.028617 *  
## BiomarcadorBasal_A             0.114551   0.087279   1.312 0.189366    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 114.742  on 120  degrees of freedom
## Residual deviance:  82.196  on 111  degrees of freedom
## AIC: 102.2
## 
## Number of Fisher Scoring iterations: 7
set.seed(123)  # Para reproducibilidad

# Dividir datos en conjunto de entrenamiento (70%) y prueba (30%)
sample_index <- sample(1:nrow(data_total), size = 0.8 * nrow(data_total))
train_data <- data_total[sample_index, ]
test_data <- data_total[-sample_index, ]

# Asegurarte de que la variable dependiente sea categórica
train_data$`3M_Prognosis` <- as.factor(train_data$Treatment_Response)
test_data$`3M_Prognosis` <- as.factor(test_data$Treatment_Response)

# Cargar librería RandomForest
library(randomForest)

# Entrenar el modelo Random Forest
rf_model <- randomForest(
  Treatment_Response ~ Age +  BiomarcadorAgudo_C + 
    Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type + 
    Glucose_Level,
  data = train_data, 
  ntree = 500,       # Número de árboles
  mtry = 3,          # Variables consideradas en cada split
  importance = TRUE  # Calcular importancia de las variables
)

# Resumen del modelo
print(rf_model)
## 
## Call:
##  randomForest(formula = Treatment_Response ~ Age + BiomarcadorAgudo_C +      Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type +      Glucose_Level, data = train_data, ntree = 500, mtry = 3,      importance = TRUE) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 3
## 
##         OOB estimate of  error rate: 45.83%
## Confusion matrix:
##         Good Poor Regular class.error
## Good       3    7       9   0.8421053
## Poor       3   36       6   0.2000000
## Regular    5   14      13   0.5937500
# Predicciones en el conjunto de prueba
predicciones_rf <- predict(rf_model, newdata = test_data, type = "response")

# Matriz de confusión
matriz_confusion <- table(Predicho = predicciones_rf, Real = test_data$Treatment_Response)
print(matriz_confusion)
##          Real
## Predicho  Good Poor Regular
##   Good       2    1       1
##   Poor       0    8       3
##   Regular    1    1       8
# Extraer valores de la matriz de confusión
TN <- matriz_confusion[1,1]
FP <- matriz_confusion[1,2]
FN <- matriz_confusion[2,1]
TP <- matriz_confusion[2,2]

# Sensibilidad (Recall o TPR)
sensitivity <- TP / (TP + FN)

# Especificidad (TNR)
specificity <- TN / (TN + FP)

# PPV (Valor Predictivo Positivo)
ppv <- TP / (TP + FP)

# NPV (Valor Predictivo Negativo)
npv <- TN / (TN + FN)

# Mostrar métricas
cat("Sensibilidad: ", sensitivity, "\n")
## Sensibilidad:  1
cat("Especificidad: ", specificity, "\n")
## Especificidad:  0.6666667
cat("PPV: ", ppv, "\n")
## PPV:  0.8888889
cat("NPV: ", npv, "\n")
## NPV:  1
# Calcular Accuracy
accuracy <- sum(diag(matriz_confusion)) / sum(matriz_confusion)
cat("Accuracy del modelo:", accuracy, "\n")
## Accuracy del modelo: 0.72
# Gráfico de importancia de variables
importance <- importance(rf_model)
varImpPlot(rf_model)

# Curva ROC y AUC
library(pROC)
roc_curve <- roc(as.numeric(test_data$`3M_Prognosis`), as.numeric(predicciones_rf))
## Warning in roc.default(as.numeric(test_data$`3M_Prognosis`), as.numeric(predicciones_rf)): 'response' has more
## than two levels. Consider setting 'levels' explicitly or using 'multiclass.roc' instead
## Setting levels: control = 1, case = 2
## Setting direction: controls < cases
plot(roc_curve, main = "Curva ROC para Random Forest", col = "blue")

auc <- auc(roc_curve)
cat("Área bajo la curva (AUC):", auc, "\n")
## Área bajo la curva (AUC): 0.65
test_data$Treatment_Response <- factor(test_data$Treatment_Response)
predicciones_rf <- factor(predicciones_rf, levels = levels(test_data$Treatment_Response))

# Crear matriz de confusión
conf_matrix <- table(Predicho = predicciones_rf, Real = test_data$Treatment_Response)

# Verificar si la matriz de confusión tiene sentido
print(conf_matrix)
##          Real
## Predicho  Good Poor Regular
##   Good       2    1       1
##   Poor       0    8       3
##   Regular    1    1       8
# Convertir la matriz en un data frame para ggplot2
conf_df <- as.data.frame(as.table(conf_matrix))

# Graficar usando ggplot2
ggplot(conf_df, aes(x = Predicho, y = Real, fill = Freq)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  geom_text(aes(label = Freq), vjust = 1, color = "white", size = 5) +
  labs(title = "Matriz de Confusión",
       x = "Predicho", y = "Real") +
  theme_minimal()

metrics_summary <- data.frame(
  Metric = c("Sensibilidad", "Especificidad", "PPV", "NPV", "Accuracy", "AUC"),
  Value = c(sensitivity, specificity, ppv, npv, accuracy, auc)
)

# Mostrar la tabla resumen
print(metrics_summary)
# Predicciones en probabilidades
pred_prob <- predict(rf_model, newdata = test_data, type = "prob")

# Ajustar el umbral para la clase positiva (puedes probar diferentes umbrales)
threshold <- 0.7  # Prueba con 0.6, 0.7 o un valor que te convenga
pred_class <- ifelse(pred_prob[,2] > threshold, 1, 0)

# Calcular la frecuencia de las clases
class_weights <- table(train_data$Treatment_Response)
class_weights <- 1 / class_weights
names(class_weights) <- levels(train_data$Treatment_Response)

# Ajustar el modelo Random Forest con pesos de clases
rf_model <- randomForest(
  Treatment_Response ~ Age + BiomarcadorAgudo_C + 
    Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type + 
    Glucose_Level, 
  data = train_data, 
  ntree = 500,       
  mtry = 4,          
  importance = TRUE, 
  classwt = class_weights  # Pesos ajustados
)

# Resumen del modelo
print(rf_model)
## 
## Call:
##  randomForest(formula = Treatment_Response ~ Age + BiomarcadorAgudo_C +      Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type +      Glucose_Level, data = train_data, ntree = 500, mtry = 4,      importance = TRUE, classwt = class_weights) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 45.83%
## Confusion matrix:
##         Good Poor Regular class.error
## Good       4    8       7   0.7894737
## Poor       3   35       7   0.2222222
## Regular    5   14      13   0.5937500
# Calcular la frecuencia de las clases
class_weights <- table(train_data$Treatment_Response)
class_weights <- 1 / class_weights
names(class_weights) <- levels(train_data$Treatment_Response)

# Ajustar el modelo Random Forest con pesos de clases
rf_model <- randomForest(
  Treatment_Response ~ Age + BiomarcadorAgudo_C + 
    Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type + 
    Glucose_Level, 
  data = train_data, 
  ntree = 500,       
  mtry = 4,          
  importance = TRUE, 
  classwt = class_weights  # Pesos ajustados
)

# Resumen del modelo
print(rf_model)
## 
## Call:
##  randomForest(formula = Treatment_Response ~ Age + BiomarcadorAgudo_C +      Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type +      Glucose_Level, data = train_data, ntree = 500, mtry = 4,      importance = TRUE, classwt = class_weights) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 45.83%
## Confusion matrix:
##         Good Poor Regular class.error
## Good       4    8       7   0.7894737
## Poor       2   35       8   0.2222222
## Regular    5   14      13   0.5937500
# Ajustar el parámetro nodesize
rf_model <- randomForest(
  Treatment_Response ~ Age + BiomarcadorAgudo_C + 
    Symptom_Scale_Admission + Killip_Class_24H + Treatment_Type + 
    Glucose_Level, 
  data = train_data, 
  ntree = 500,       
  mtry = 4,          
  importance = TRUE, 
  nodesize = 10       # Cambiar este valor según sea necesario
)
conf_matrix <- table(Predicted = pred_class, Actual = test_data$Treatment_Response)

# Calcular especificidad
TN <- conf_matrix[1, 1]
FP <- conf_matrix[1, 2]
specificity <- TN / (TN + FP)
cat("Especificidad:", specificity, "\n")
## Especificidad: 0.375
# Calcular AUC y otras métricas (si es necesario)
library(pROC)
roc_curve <- roc(test_data$Treatment_Response, pred_prob[, 2])
## Warning in roc.default(test_data$Treatment_Response, pred_prob[, 2]): 'response' has more than two levels.
## Consider setting 'levels' explicitly or using 'multiclass.roc' instead
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
auc_value <- auc(roc_curve)
cat("AUC:", auc_value, "\n")
## AUC: 1

PARTE 2,2: analisis de supervivencia engun la respuesta al tratamiento.

library(survival)
library(survminer)

# Asegúrate de que las fechas están en formato Date
data_total$Admission_Date <- as.Date(data_total$Admission_Date)
data_total$Death_Date <- as.Date(data_total$Death_Date)

# Crear el objeto de supervivencia
# Usamos la diferencia entre 'Admission_Date' y 'Death_Date' como tiempo de seguimiento
# Si Death_Date es NA, el evento no ocurrió (censura)
surv_object <- Surv(time = as.numeric(difftime(data_total$Death_Date, data_total$Admission_Date, units = "days")), 
                    event = ifelse(!is.na(data_total$Death_Date), 1, 0))

# Ajustar el modelo Kaplan-Meier estratificado por respuesta al tratamiento
km_fit <- survfit(surv_object ~ Treatment_Response, data = data_total)

# Graficar las curvas de supervivencia
ggsurvplot(
  km_fit,
  data = data_total,
  conf.int = TRUE,                 # Mostrar intervalos de confianza
  pval = TRUE,                     # Mostrar valor p
  risk.table = TRUE,               # Agregar tabla de riesgos
  title = "Curvas de Supervivencia según Respuesta al Tratamiento",
  xlab = "Días",
  ylab = "Probabilidad de Supervivencia",
  legend.title = "Respuesta al Tratamiento",
  palette = c("#1b9e77", "#d95f02", "#7570b3") # Colores para las categorías
)