Teoría

El árbol de decisión es un diagrama de aprendizaje automático que modela las opciones, eventos y posibles resultados de una decisión de forma visual.

Sus partes principales son:

  • Nodo raiz: Inicio del diagrama
  • Ramas: Opciones disponibles
  • Nodos internos: Nuevas decisiones
  • Hojas: resultado final de cada cambio

Importar la base de datos

cancer = read.csv(file.choose())

Entender la base de datos

summary(cancer)
##      diagnosis    radius_mean      texture_mean   perimeter_mean  
##  Length   :569   Min.   : 6.981   Min.   : 9.71   Min.   : 43.79  
##  N.unique :  2   1st Qu.:11.700   1st Qu.:16.17   1st Qu.: 75.17  
##  N.blank  :  0   Median :13.370   Median :18.84   Median : 86.24  
##  Min.nchar:  1   Mean   :14.127   Mean   :19.29   Mean   : 91.97  
##  Max.nchar:  1   3rd Qu.:15.780   3rd Qu.:21.80   3rd Qu.:104.10  
##                  Max.   :28.110   Max.   :39.28   Max.   :188.50  
##    area_mean      smoothness_mean   compactness_mean  concavity_mean   
##  Min.   : 143.5   Min.   :0.05263   Min.   :0.01938   Min.   :0.00000  
##  1st Qu.: 420.3   1st Qu.:0.08637   1st Qu.:0.06492   1st Qu.:0.02956  
##  Median : 551.1   Median :0.09587   Median :0.09263   Median :0.06154  
##  Mean   : 654.9   Mean   :0.09636   Mean   :0.10434   Mean   :0.08880  
##  3rd Qu.: 782.7   3rd Qu.:0.10530   3rd Qu.:0.13040   3rd Qu.:0.13070  
##  Max.   :2501.0   Max.   :0.16340   Max.   :0.34540   Max.   :0.42680  
##  concave.points_mean symmetry_mean    fractal_dimension_mean   radius_se     
##  Min.   :0.00000     Min.   :0.1060   Min.   :0.04996        Min.   :0.1115  
##  1st Qu.:0.02031     1st Qu.:0.1619   1st Qu.:0.05770        1st Qu.:0.2324  
##  Median :0.03350     Median :0.1792   Median :0.06154        Median :0.3242  
##  Mean   :0.04892     Mean   :0.1812   Mean   :0.06280        Mean   :0.4052  
##  3rd Qu.:0.07400     3rd Qu.:0.1957   3rd Qu.:0.06612        3rd Qu.:0.4789  
##  Max.   :0.20120     Max.   :0.3040   Max.   :0.09744        Max.   :2.8730  
##    texture_se      perimeter_se       area_se        smoothness_se     
##  Min.   :0.3602   Min.   : 0.757   Min.   :  6.802   Min.   :0.001713  
##  1st Qu.:0.8339   1st Qu.: 1.606   1st Qu.: 17.850   1st Qu.:0.005169  
##  Median :1.1080   Median : 2.287   Median : 24.530   Median :0.006380  
##  Mean   :1.2169   Mean   : 2.866   Mean   : 40.337   Mean   :0.007041  
##  3rd Qu.:1.4740   3rd Qu.: 3.357   3rd Qu.: 45.190   3rd Qu.:0.008146  
##  Max.   :4.8850   Max.   :21.980   Max.   :542.200   Max.   :0.031130  
##  compactness_se      concavity_se     concave.points_se   symmetry_se      
##  Min.   :0.002252   Min.   :0.00000   Min.   :0.000000   Min.   :0.007882  
##  1st Qu.:0.013080   1st Qu.:0.01509   1st Qu.:0.007638   1st Qu.:0.015160  
##  Median :0.020450   Median :0.02589   Median :0.010930   Median :0.018730  
##  Mean   :0.025478   Mean   :0.03189   Mean   :0.011796   Mean   :0.020542  
##  3rd Qu.:0.032450   3rd Qu.:0.04205   3rd Qu.:0.014710   3rd Qu.:0.023480  
##  Max.   :0.135400   Max.   :0.39600   Max.   :0.052790   Max.   :0.078950  
##  fractal_dimension_se  radius_worst   texture_worst   perimeter_worst 
##  Min.   :0.0008948    Min.   : 7.93   Min.   :12.02   Min.   : 50.41  
##  1st Qu.:0.0022480    1st Qu.:13.01   1st Qu.:21.08   1st Qu.: 84.11  
##  Median :0.0031870    Median :14.97   Median :25.41   Median : 97.66  
##  Mean   :0.0037949    Mean   :16.27   Mean   :25.68   Mean   :107.26  
##  3rd Qu.:0.0045580    3rd Qu.:18.79   3rd Qu.:29.72   3rd Qu.:125.40  
##  Max.   :0.0298400    Max.   :36.04   Max.   :49.54   Max.   :251.20  
##    area_worst     smoothness_worst  compactness_worst concavity_worst 
##  Min.   : 185.2   Min.   :0.07117   Min.   :0.02729   Min.   :0.0000  
##  1st Qu.: 515.3   1st Qu.:0.11660   1st Qu.:0.14720   1st Qu.:0.1145  
##  Median : 686.5   Median :0.13130   Median :0.21190   Median :0.2267  
##  Mean   : 880.6   Mean   :0.13237   Mean   :0.25427   Mean   :0.2722  
##  3rd Qu.:1084.0   3rd Qu.:0.14600   3rd Qu.:0.33910   3rd Qu.:0.3829  
##  Max.   :4254.0   Max.   :0.22260   Max.   :1.05800   Max.   :1.2520  
##  concave.points_worst symmetry_worst   fractal_dimension_worst
##  Min.   :0.00000      Min.   :0.1565   Min.   :0.05504        
##  1st Qu.:0.06493      1st Qu.:0.2504   1st Qu.:0.07146        
##  Median :0.09993      Median :0.2822   Median :0.08004        
##  Mean   :0.11461      Mean   :0.2901   Mean   :0.08395        
##  3rd Qu.:0.16140      3rd Qu.:0.3179   3rd Qu.:0.09208        
##  Max.   :0.29100      Max.   :0.6638   Max.   :0.20750
str(cancer)
## 'data.frame':    569 obs. of  31 variables:
##  $ diagnosis              : chr  "M" "M" "M" "M" ...
##  $ radius_mean            : num  18 20.6 19.7 11.4 20.3 ...
##  $ texture_mean           : num  10.4 17.8 21.2 20.4 14.3 ...
##  $ perimeter_mean         : num  122.8 132.9 130 77.6 135.1 ...
##  $ area_mean              : num  1001 1326 1203 386 1297 ...
##  $ smoothness_mean        : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...
##  $ compactness_mean       : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...
##  $ concavity_mean         : num  0.3001 0.0869 0.1974 0.2414 0.198 ...
##  $ concave.points_mean    : num  0.1471 0.0702 0.1279 0.1052 0.1043 ...
##  $ symmetry_mean          : num  0.242 0.181 0.207 0.26 0.181 ...
##  $ fractal_dimension_mean : num  0.0787 0.0567 0.06 0.0974 0.0588 ...
##  $ radius_se              : num  1.095 0.543 0.746 0.496 0.757 ...
##  $ texture_se             : num  0.905 0.734 0.787 1.156 0.781 ...
##  $ perimeter_se           : num  8.59 3.4 4.58 3.44 5.44 ...
##  $ area_se                : num  153.4 74.1 94 27.2 94.4 ...
##  $ smoothness_se          : num  0.0064 0.00522 0.00615 0.00911 0.01149 ...
##  $ compactness_se         : num  0.049 0.0131 0.0401 0.0746 0.0246 ...
##  $ concavity_se           : num  0.0537 0.0186 0.0383 0.0566 0.0569 ...
##  $ concave.points_se      : num  0.0159 0.0134 0.0206 0.0187 0.0188 ...
##  $ symmetry_se            : num  0.03 0.0139 0.0225 0.0596 0.0176 ...
##  $ fractal_dimension_se   : num  0.00619 0.00353 0.00457 0.00921 0.00511 ...
##  $ radius_worst           : num  25.4 25 23.6 14.9 22.5 ...
##  $ texture_worst          : num  17.3 23.4 25.5 26.5 16.7 ...
##  $ perimeter_worst        : num  184.6 158.8 152.5 98.9 152.2 ...
##  $ area_worst             : num  2019 1956 1709 568 1575 ...
##  $ smoothness_worst       : num  0.162 0.124 0.144 0.21 0.137 ...
##  $ compactness_worst      : num  0.666 0.187 0.424 0.866 0.205 ...
##  $ concavity_worst        : num  0.712 0.242 0.45 0.687 0.4 ...
##  $ concave.points_worst   : num  0.265 0.186 0.243 0.258 0.163 ...
##  $ symmetry_worst         : num  0.46 0.275 0.361 0.664 0.236 ...
##  $ fractal_dimension_worst: num  0.1189 0.089 0.0876 0.173 0.0768 ...

Filtrar base de datos

Cancer = cancer[,c("diagnosis","radius_mean","texture_mean","perimeter_mean",
"area_mean","smoothness_mean","compactness_mean","concavity_mean",
"concave.points_mean","symmetry_mean","fractal_dimension_mean")]
Cancer$diagnosis = as.factor(ifelse(Cancer$diagnosis=="M", "Maligno",
"Benigno"))
str(Cancer)
## 'data.frame':    569 obs. of  11 variables:
##  $ diagnosis             : Factor w/ 2 levels "Benigno","Maligno": 2 2 2 2 2 2 2 2 2 2 ...
##  $ radius_mean           : num  18 20.6 19.7 11.4 20.3 ...
##  $ texture_mean          : num  10.4 17.8 21.2 20.4 14.3 ...
##  $ perimeter_mean        : num  122.8 132.9 130 77.6 135.1 ...
##  $ area_mean             : num  1001 1326 1203 386 1297 ...
##  $ smoothness_mean       : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...
##  $ compactness_mean      : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...
##  $ concavity_mean        : num  0.3001 0.0869 0.1974 0.2414 0.198 ...
##  $ concave.points_mean   : num  0.1471 0.0702 0.1279 0.1052 0.1043 ...
##  $ symmetry_mean         : num  0.242 0.181 0.207 0.26 0.181 ...
##  $ fractal_dimension_mean: num  0.0787 0.0567 0.06 0.0974 0.0588 ...
sum(is.na(Cancer))
## [1] 0
sapply(Cancer, function(x) sum(is.na(x)))
##              diagnosis            radius_mean           texture_mean 
##                      0                      0                      0 
##         perimeter_mean              area_mean        smoothness_mean 
##                      0                      0                      0 
##       compactness_mean         concavity_mean    concave.points_mean 
##                      0                      0                      0 
##          symmetry_mean fractal_dimension_mean 
##                      0                      0
Cancer = na.omit(Cancer)

Crear arbol de decisión

# install.packages("rpart")
library(rpart)
arbol = rpart(formula=diagnosis ~ ., data = Cancer)
arbol
## n= 569 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 569 212 Benigno (0.62741652 0.37258348)  
##    2) concave.points_mean< 0.05142 349  20 Benigno (0.94269341 0.05730659)  
##      4) area_mean< 696.25 331   9 Benigno (0.97280967 0.02719033) *
##      5) area_mean>=696.25 18   7 Maligno (0.38888889 0.61111111) *
##    3) concave.points_mean>=0.05142 220  28 Maligno (0.12727273 0.87272727)  
##      6) texture_mean< 16.395 28  11 Benigno (0.60714286 0.39285714)  
##       12) concave.points_mean< 0.07905 17   1 Benigno (0.94117647 0.05882353) *
##       13) concave.points_mean>=0.07905 11   1 Maligno (0.09090909 0.90909091) *
##      7) texture_mean>=16.395 192  11 Maligno (0.05729167 0.94270833) *
# install.packages("rpart.plot")
library(rpart.plot)
rpart.plot(arbol)

prp(arbol,extra = 7,prefix = "fraccion")

Notas de los Árboles de Decisión

Nodo raíz (arriba de todo): concave.points_mean < 0.05 Divide de que a todos los tumores según qué tan marcadas son las concavidades del contorno celular, porque esta variable es la que más influye en el diagnóstico.

Rama izquierda → concave.points_mean < 0.05: Se divide otra vez por radius_mean * Tumores con radius_mean < 15 → hoja final: 97% Benigno * Tumores con radius_mean >= 15 → hoja final: 55% Maligno

Rama derecha → concave.points_mean >= 0.05: * Se divide por texture_mean >= 16.4 * Tumores con texture_mean < 16.4 → hoja final: 61% Benigno * Tumores con texture_mean >= 16.4 → hoja final: 94% Maligno

Conclusiones

  1. Los tumores con menor probabilidad de ser malignos son aquellos con concave.points_mean bajo y radius_mean menor a 15 (97% Benigno).
  2. Los tumores con mayor probabilidad de ser malignos son aquellos con concave.points_mean alto y texture_mean mayor a 16.4 (94% Maligno), seguidos de los tumores con radius_mean mayor a 15 (55% Maligno).
LS0tDQp0aXRsZTogIsOBcmJvbCBkZSBEZWNpc2nDs24gLSBDw6FuY2VyIGRlIE1hbWEiDQphdXRob3I6ICJNYXLDrWEgRmVybmFuZGEgU2FuIFJvbcOhbiBPcm96Y28gLSBBMDE0MjQ2OTEiDQpkYXRlOiAiMjAvMDgvMjAyNiINCm91dHB1dDogDQogIGh0bWxfZG9jdW1lbnQ6DQogICAgdG9jOiBUUlVFDQogICAgdG9jX2Zsb2F0OiBUUlVFDQogICAgY29kZV9kb3dubG9hZDogVFJVRQ0KICAgIHRoZW1lOiBzaW1wbGV4DQotLS0NCg0KIVtdKGh0dHBzOi8va296b3Byb3llY3Rvcy5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTkvMDUvMDEtSU0tR0lGLmdpZikNCg0KIyA8c3BhbiBzdHlsZSA9ImNvbG9yOnBpbmsiPlRlb3LDrWE8L3NwYW4+DQpFbCAqKsOhcmJvbCBkZSBkZWNpc2nDs24qKiBlcyB1biBkaWFncmFtYSBkZSBhcHJlbmRpemFqZSBhdXRvbcOhdGljbyBxdWUgbW9kZWxhIGxhcyBvcGNpb25lcywgZXZlbnRvcyB5IHBvc2libGVzIHJlc3VsdGFkb3MgZGUgdW5hIGRlY2lzacOzbiBkZSBmb3JtYSB2aXN1YWwuDQoNClN1cyBwYXJ0ZXMgc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