Activando las librerías

library(tree)

library(rpart)

Gráfico de dispersión

plot(iris$Petal.Length,iris$Petal.Width, col = iris$Species, main="Dispersión")
legend("topleft", legend = unique(iris$Species), col = unique(iris$Species), bty = "n", cex = 0.75, pch = 1)

Ejemplo de k vecinos cercanos con iris

variables <- data.frame(iris$Petal.Length,iris$Petal.Width)
kvecinos<-kmeans(variables, 3)
kvecinos
## K-means clustering with 3 clusters of sizes 52, 50, 48
## 
## Cluster means:
##   iris.Petal.Length iris.Petal.Width
## 1          4.269231         1.342308
## 2          1.462000         0.246000
## 3          5.595833         2.037500
## 
## Clustering vector:
##   [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
##  [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 3 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 1 3 3 3 3
## [112] 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3
## [149] 3 3
## 
## Within cluster sum of squares by cluster:
## [1] 13.05769  2.02200 16.29167
##  (between_SS / total_SS =  94.3 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Gráfico de clusters de k vecinos cercanos con iris

plot(variables, col = kvecinos$cluster, main="k vecinos")
legend("topleft", legend = unique(kvecinos$cluster), col = unique(kvecinos$cluster), 
       bty = "n", cex = 0.75, pch = 1)

Ábol de clasificación con iris

modelo1 <- tree(Species ~., iris)
modelo1
## node), split, n, deviance, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 150 329.600 setosa ( 0.33333 0.33333 0.33333 )  
##    2) Petal.Length < 2.45 50   0.000 setosa ( 1.00000 0.00000 0.00000 ) *
##    3) Petal.Length > 2.45 100 138.600 versicolor ( 0.00000 0.50000 0.50000 )  
##      6) Petal.Width < 1.75 54  33.320 versicolor ( 0.00000 0.90741 0.09259 )  
##       12) Petal.Length < 4.95 48   9.721 versicolor ( 0.00000 0.97917 0.02083 )  
##         24) Sepal.Length < 5.15 5   5.004 versicolor ( 0.00000 0.80000 0.20000 ) *
##         25) Sepal.Length > 5.15 43   0.000 versicolor ( 0.00000 1.00000 0.00000 ) *
##       13) Petal.Length > 4.95 6   7.638 virginica ( 0.00000 0.33333 0.66667 ) *
##      7) Petal.Width > 1.75 46   9.635 virginica ( 0.00000 0.02174 0.97826 )  
##       14) Petal.Length < 4.95 6   5.407 virginica ( 0.00000 0.16667 0.83333 ) *
##       15) Petal.Length > 4.95 40   0.000 virginica ( 0.00000 0.00000 1.00000 ) *

Ajustando el modelo

modelo2 <- tree(Species ~Petal.Length+Petal.Width, iris)
modelo2
## node), split, n, deviance, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 150 329.600 setosa ( 0.33333 0.33333 0.33333 )  
##    2) Petal.Length < 2.45 50   0.000 setosa ( 1.00000 0.00000 0.00000 ) *
##    3) Petal.Length > 2.45 100 138.600 versicolor ( 0.00000 0.50000 0.50000 )  
##      6) Petal.Width < 1.75 54  33.320 versicolor ( 0.00000 0.90741 0.09259 )  
##       12) Petal.Length < 4.95 48   9.721 versicolor ( 0.00000 0.97917 0.02083 ) *
##       13) Petal.Length > 4.95 6   7.638 virginica ( 0.00000 0.33333 0.66667 ) *
##      7) Petal.Width > 1.75 46   9.635 virginica ( 0.00000 0.02174 0.97826 )  
##       14) Petal.Length < 4.95 6   5.407 virginica ( 0.00000 0.16667 0.83333 ) *
##       15) Petal.Length > 4.95 40   0.000 virginica ( 0.00000 0.00000 1.00000 ) *

Partición bidimensional

plot(iris$Petal.Length,iris$Petal.Width, col = iris$Species, main="Árbol de clasificación")
partition.tree(modelo2, label = "Species", add = T)
legend("topleft", legend = unique(iris$Species), col = unique(iris$Species), 
       bty = "n", cex = 0.75, pch = 1)

Comparación de gráficos

par(mfrow=c(1,3))
plot(iris$Petal.Length,iris$Petal.Width, col = iris$Species, main="Dispersión")
legend("topleft", legend = unique(iris$Species), col = unique(iris$Species), 
       bty = "n", cex = 0.75, pch = 1)
plot(variables, col = kvecinos$cluster, main="k vecinos")
legend("topleft", legend = unique(kvecinos$cluster), col = unique(kvecinos$cluster), 
       bty = "n", cex = 0.75, pch = 1)
plot(iris$Petal.Length,iris$Petal.Width, col = iris$Species, main="Árbol de clasificación")
partition.tree(modelo2, label = "Species", add = T)
legend("topleft", legend = unique(iris$Species), col = unique(iris$Species), 
       bty = "n", cex = 0.75, pch = 1)

Este R Markdown fue generado mediante R en October 03, 2020 y forma parte de las actividades realizadas en las materias de Estadística y Taller IV. Facultad de Economía, UNAM, Ciudad Universitaria. Créditos: Cesar Hernández. Esta obra está bajo una licencia de Creative Commons Reconocimiento-NoComercial-CompartirIgual 4.0 Internacional. Creative Commons (CC).Licencia de Creative Commons