Carga y preparación de los datos
library(dplyr)
library(ggplot2)
library(DescTools)
library(rpart)
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.cleveland.data"
datos <- read.csv(url, header = FALSE, na.strings = "?")
names(datos) <- c("edad", "sexo", "tipo_dolor", "presion_reposo", "colesterol",
"glucosa_ayunas", "electro_reposo", "fc_maxima",
"angina_ejercicio", "depresion_st", "pendiente_st",
"vasos_coloreados", "talasemia", "diagnostico")
datos$enfermedad <- factor(ifelse(datos$diagnostico > 0,
"Con enfermedad", "Sin enfermedad"))
str(datos)
## 'data.frame': 303 obs. of 15 variables:
## $ edad : num 63 67 67 37 41 56 62 57 63 53 ...
## $ sexo : num 1 1 1 1 0 1 0 0 1 1 ...
## $ tipo_dolor : num 1 4 4 3 2 2 4 4 4 4 ...
## $ presion_reposo : num 145 160 120 130 130 120 140 120 130 140 ...
## $ colesterol : num 233 286 229 250 204 236 268 354 254 203 ...
## $ glucosa_ayunas : num 1 0 0 0 0 0 0 0 0 1 ...
## $ electro_reposo : num 2 2 2 0 2 0 2 0 2 2 ...
## $ fc_maxima : num 150 108 129 187 172 178 160 163 147 155 ...
## $ angina_ejercicio: num 0 1 1 0 0 0 0 1 0 1 ...
## $ depresion_st : num 2.3 1.5 2.6 3.5 1.4 0.8 3.6 0.6 1.4 3.1 ...
## $ pendiente_st : num 3 2 2 3 1 1 3 1 2 3 ...
## $ vasos_coloreados: num 0 3 2 0 0 0 2 0 1 0 ...
## $ talasemia : num 6 3 7 3 3 3 3 3 7 7 ...
## $ diagnostico : int 0 2 1 0 0 0 3 0 2 1 ...
## $ enfermedad : Factor w/ 2 levels "Con enfermedad",..: 2 1 1 2 2 2 1 2 1 1 ...
Ejemplos de sintaxis básica
# Ejemplo 1: objetos y funciones
round(c(media = mean(datos$colesterol),
mediana = median(datos$colesterol),
desv_est = sd(datos$colesterol)), 2)
## media mediana desv_est
## 246.69 241.00 51.78
# Ejemplo 2: vectores y operadores lógicos
mayores_55 <- datos$edad > 55
sum(mayores_55)
## [1] 152
sum(datos$edad > 55 & datos$presion_reposo > 140)
## [1] 50
# Ejemplo 3: factores y tablas
table(datos$enfermedad)
##
## Con enfermedad Sin enfermedad
## 139 164
tapply(datos$edad, datos$enfermedad, mean)
## Con enfermedad Sin enfermedad
## 56.62590 52.58537
Paquetes
# dplyr
datos %>%
group_by(enfermedad) %>%
summarise(pacientes = n(), edad_prom = round(mean(edad), 1),
colesterol_prom = round(mean(colesterol), 1),
fc_max_prom = round(mean(fc_maxima), 1))
## # A tibble: 2 × 5
## enfermedad pacientes edad_prom colesterol_prom fc_max_prom
## <fct> <int> <dbl> <dbl> <dbl>
## 1 Con enfermedad 139 56.6 252. 139.
## 2 Sin enfermedad 164 52.6 243. 158.
# ggplot2
ggplot(datos, aes(x = enfermedad, y = edad, fill = enfermedad)) +
geom_boxplot() +
labs(title = "Edad según presencia de enfermedad cardíaca",
x = "Diagnóstico", y = "Edad (años)") +
theme_minimal()

# DescTools
Mode(datos$edad)
## [1] 58
## attr(,"freq")
## [1] 19
Skew(datos$colesterol)
## [1] 1.124285
MeanCI(datos$colesterol, conf.level = 0.95)
## mean lwr.ci upr.ci
## 246.6931 240.8397 252.5465
# rpart
arbol <- rpart(enfermedad ~ edad + sexo + tipo_dolor + presion_reposo +
colesterol + fc_maxima + angina_ejercicio + depresion_st,
data = datos, method = "class")
plot(arbol, uniform = TRUE, margin = 0.15); text(arbol, cex = 0.7)
