# Leer el archivo CSV
wine_data <- read.csv("/Users/luisangel/Library/CloudStorage/OneDrive-InstitutoTecnologicoydeEstudiosSuperioresdeMonterrey/7th Season/M2/wine/wine.data", header = FALSE)

# Asignar nombres a las columnas
colnames(wine_data) <- c("Class", "Alcohol", "Malic_acid", "Ash", "Alcalinity_of_ash",
                         "Magnesium", "Total_phenols", "Flavanoids", "Nonflavanoid_phenols",
                         "Proanthocyanins", "Color_intensity", "Hue",
                         "OD280_OD315", "Proline")

# Ver las primeras filas del dataset
head(wine_data)
##   Class Alcohol Malic_acid  Ash Alcalinity_of_ash Magnesium Total_phenols
## 1     1   14.23       1.71 2.43              15.6       127          2.80
## 2     1   13.20       1.78 2.14              11.2       100          2.65
## 3     1   13.16       2.36 2.67              18.6       101          2.80
## 4     1   14.37       1.95 2.50              16.8       113          3.85
## 5     1   13.24       2.59 2.87              21.0       118          2.80
## 6     1   14.20       1.76 2.45              15.2       112          3.27
##   Flavanoids Nonflavanoid_phenols Proanthocyanins Color_intensity  Hue
## 1       3.06                 0.28            2.29            5.64 1.04
## 2       2.76                 0.26            1.28            4.38 1.05
## 3       3.24                 0.30            2.81            5.68 1.03
## 4       3.49                 0.24            2.18            7.80 0.86
## 5       2.69                 0.39            1.82            4.32 1.04
## 6       3.39                 0.34            1.97            6.75 1.05
##   OD280_OD315 Proline
## 1        3.92    1065
## 2        3.40    1050
## 3        3.17    1185
## 4        3.45    1480
## 5        2.93     735
## 6        2.85    1450

Contexto

Estos datos son el resultado de un análisis químico de vinos cultivados en la misma región de Italia pero derivados de tres cultivares diferentes.

El análisis determinó las cantidades de 13 componentes que se encuentran en cada uno de los tres tipos de vinos.

Fuente:
Wine dataset

Paso 1. Instalar paquetes y llamar librerías

# install.packages("cluster")
library(cluster)
# install.packages("ggplot2")
library(ggplot2)
# install.packages("data.table")
library(data.table)
# install.packages("factoextra")
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa

Paso 2. Obtener los datos

datos <- wine_data

Paso 3. Entender la base de datos

summary(datos)
##      Class          Alcohol        Malic_acid         Ash       
##  Min.   :1.000   Min.   :11.03   Min.   :0.740   Min.   :1.360  
##  1st Qu.:1.000   1st Qu.:12.36   1st Qu.:1.603   1st Qu.:2.210  
##  Median :2.000   Median :13.05   Median :1.865   Median :2.360  
##  Mean   :1.938   Mean   :13.00   Mean   :2.336   Mean   :2.367  
##  3rd Qu.:3.000   3rd Qu.:13.68   3rd Qu.:3.083   3rd Qu.:2.558  
##  Max.   :3.000   Max.   :14.83   Max.   :5.800   Max.   :3.230  
##  Alcalinity_of_ash   Magnesium      Total_phenols     Flavanoids   
##  Min.   :10.60     Min.   : 70.00   Min.   :0.980   Min.   :0.340  
##  1st Qu.:17.20     1st Qu.: 88.00   1st Qu.:1.742   1st Qu.:1.205  
##  Median :19.50     Median : 98.00   Median :2.355   Median :2.135  
##  Mean   :19.49     Mean   : 99.74   Mean   :2.295   Mean   :2.029  
##  3rd Qu.:21.50     3rd Qu.:107.00   3rd Qu.:2.800   3rd Qu.:2.875  
##  Max.   :30.00     Max.   :162.00   Max.   :3.880   Max.   :5.080  
##  Nonflavanoid_phenols Proanthocyanins Color_intensity       Hue        
##  Min.   :0.1300       Min.   :0.410   Min.   : 1.280   Min.   :0.4800  
##  1st Qu.:0.2700       1st Qu.:1.250   1st Qu.: 3.220   1st Qu.:0.7825  
##  Median :0.3400       Median :1.555   Median : 4.690   Median :0.9650  
##  Mean   :0.3619       Mean   :1.591   Mean   : 5.058   Mean   :0.9574  
##  3rd Qu.:0.4375       3rd Qu.:1.950   3rd Qu.: 6.200   3rd Qu.:1.1200  
##  Max.   :0.6600       Max.   :3.580   Max.   :13.000   Max.   :1.7100  
##   OD280_OD315       Proline      
##  Min.   :1.270   Min.   : 278.0  
##  1st Qu.:1.938   1st Qu.: 500.5  
##  Median :2.780   Median : 673.5  
##  Mean   :2.612   Mean   : 746.9  
##  3rd Qu.:3.170   3rd Qu.: 985.0  
##  Max.   :4.000   Max.   :1680.0

Paso 4. Escalar la base de datos

df <- scale(datos)

Paso 5. Cantidad de grupos

grupos <- 3

Paso 6. Generar los segmentos

segmentos <- kmeans(df,grupos)
segmentos
## K-means clustering with 3 clusters of sizes 68, 61, 49
## 
## Cluster means:
##         Class    Alcohol Malic_acid        Ash Alcalinity_of_ash   Magnesium
## 1  0.07973544 -0.9195318 -0.3778231 -0.4643776         0.1750133 -0.46892793
## 2 -1.16822514  0.8756272 -0.3037196  0.3180446        -0.6626544  0.56329925
## 3  1.34366784  0.1860184  0.9024258  0.2485092         0.5820616 -0.05049296
##   Total_phenols  Flavanoids Nonflavanoid_phenols Proanthocyanins
## 1   -0.07372644  0.04416309          0.008736157      0.01821349
## 2    0.87403990  0.94098462         -0.583942581      0.58014642
## 3   -0.98577624 -1.23271740          0.714825281     -0.74749896
##   Color_intensity        Hue OD280_OD315    Proline
## 1      -0.8598525  0.4233092   0.2490794 -0.7630972
## 2       0.1667181  0.4823674   0.7648958  1.1550888
## 3       0.9857177 -1.1879477  -1.2978785 -0.3789756
## 
## 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 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
##  [75] 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [112] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [149] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 
## Within cluster sum of squares by cluster:
## [1] 623.1702 350.5475 304.6223
##  (between_SS / total_SS =  48.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Paso 7. Asignar el grupo al que pertenece cada observación

asignacion <- cbind(datos, cluster = segmentos$cluster)
asignacion
##     Class Alcohol Malic_acid  Ash Alcalinity_of_ash Magnesium Total_phenols
## 1       1   14.23       1.71 2.43              15.6       127          2.80
## 2       1   13.20       1.78 2.14              11.2       100          2.65
## 3       1   13.16       2.36 2.67              18.6       101          2.80
## 4       1   14.37       1.95 2.50              16.8       113          3.85
## 5       1   13.24       2.59 2.87              21.0       118          2.80
## 6       1   14.20       1.76 2.45              15.2       112          3.27
## 7       1   14.39       1.87 2.45              14.6        96          2.50
## 8       1   14.06       2.15 2.61              17.6       121          2.60
## 9       1   14.83       1.64 2.17              14.0        97          2.80
## 10      1   13.86       1.35 2.27              16.0        98          2.98
## 11      1   14.10       2.16 2.30              18.0       105          2.95
## 12      1   14.12       1.48 2.32              16.8        95          2.20
## 13      1   13.75       1.73 2.41              16.0        89          2.60
## 14      1   14.75       1.73 2.39              11.4        91          3.10
## 15      1   14.38       1.87 2.38              12.0       102          3.30
## 16      1   13.63       1.81 2.70              17.2       112          2.85
## 17      1   14.30       1.92 2.72              20.0       120          2.80
## 18      1   13.83       1.57 2.62              20.0       115          2.95
## 19      1   14.19       1.59 2.48              16.5       108          3.30
## 20      1   13.64       3.10 2.56              15.2       116          2.70
## 21      1   14.06       1.63 2.28              16.0       126          3.00
## 22      1   12.93       3.80 2.65              18.6       102          2.41
## 23      1   13.71       1.86 2.36              16.6       101          2.61
## 24      1   12.85       1.60 2.52              17.8        95          2.48
## 25      1   13.50       1.81 2.61              20.0        96          2.53
## 26      1   13.05       2.05 3.22              25.0       124          2.63
## 27      1   13.39       1.77 2.62              16.1        93          2.85
## 28      1   13.30       1.72 2.14              17.0        94          2.40
## 29      1   13.87       1.90 2.80              19.4       107          2.95
## 30      1   14.02       1.68 2.21              16.0        96          2.65
## 31      1   13.73       1.50 2.70              22.5       101          3.00
## 32      1   13.58       1.66 2.36              19.1       106          2.86
## 33      1   13.68       1.83 2.36              17.2       104          2.42
## 34      1   13.76       1.53 2.70              19.5       132          2.95
## 35      1   13.51       1.80 2.65              19.0       110          2.35
## 36      1   13.48       1.81 2.41              20.5       100          2.70
## 37      1   13.28       1.64 2.84              15.5       110          2.60
## 38      1   13.05       1.65 2.55              18.0        98          2.45
## 39      1   13.07       1.50 2.10              15.5        98          2.40
## 40      1   14.22       3.99 2.51              13.2       128          3.00
## 41      1   13.56       1.71 2.31              16.2       117          3.15
## 42      1   13.41       3.84 2.12              18.8        90          2.45
## 43      1   13.88       1.89 2.59              15.0       101          3.25
## 44      1   13.24       3.98 2.29              17.5       103          2.64
## 45      1   13.05       1.77 2.10              17.0       107          3.00
## 46      1   14.21       4.04 2.44              18.9       111          2.85
## 47      1   14.38       3.59 2.28              16.0       102          3.25
## 48      1   13.90       1.68 2.12              16.0       101          3.10
## 49      1   14.10       2.02 2.40              18.8       103          2.75
## 50      1   13.94       1.73 2.27              17.4       108          2.88
## 51      1   13.05       1.73 2.04              12.4        92          2.72
## 52      1   13.83       1.65 2.60              17.2        94          2.45
## 53      1   13.82       1.75 2.42              14.0       111          3.88
## 54      1   13.77       1.90 2.68              17.1       115          3.00
## 55      1   13.74       1.67 2.25              16.4       118          2.60
## 56      1   13.56       1.73 2.46              20.5       116          2.96
## 57      1   14.22       1.70 2.30              16.3       118          3.20
## 58      1   13.29       1.97 2.68              16.8       102          3.00
## 59      1   13.72       1.43 2.50              16.7       108          3.40
## 60      2   12.37       0.94 1.36              10.6        88          1.98
## 61      2   12.33       1.10 2.28              16.0       101          2.05
## 62      2   12.64       1.36 2.02              16.8       100          2.02
## 63      2   13.67       1.25 1.92              18.0        94          2.10
## 64      2   12.37       1.13 2.16              19.0        87          3.50
## 65      2   12.17       1.45 2.53              19.0       104          1.89
## 66      2   12.37       1.21 2.56              18.1        98          2.42
## 67      2   13.11       1.01 1.70              15.0        78          2.98
## 68      2   12.37       1.17 1.92              19.6        78          2.11
## 69      2   13.34       0.94 2.36              17.0       110          2.53
## 70      2   12.21       1.19 1.75              16.8       151          1.85
## 71      2   12.29       1.61 2.21              20.4       103          1.10
## 72      2   13.86       1.51 2.67              25.0        86          2.95
## 73      2   13.49       1.66 2.24              24.0        87          1.88
## 74      2   12.99       1.67 2.60              30.0       139          3.30
## 75      2   11.96       1.09 2.30              21.0       101          3.38
## 76      2   11.66       1.88 1.92              16.0        97          1.61
## 77      2   13.03       0.90 1.71              16.0        86          1.95
## 78      2   11.84       2.89 2.23              18.0       112          1.72
## 79      2   12.33       0.99 1.95              14.8       136          1.90
## 80      2   12.70       3.87 2.40              23.0       101          2.83
## 81      2   12.00       0.92 2.00              19.0        86          2.42
## 82      2   12.72       1.81 2.20              18.8        86          2.20
## 83      2   12.08       1.13 2.51              24.0        78          2.00
## 84      2   13.05       3.86 2.32              22.5        85          1.65
## 85      2   11.84       0.89 2.58              18.0        94          2.20
## 86      2   12.67       0.98 2.24              18.0        99          2.20
## 87      2   12.16       1.61 2.31              22.8        90          1.78
## 88      2   11.65       1.67 2.62              26.0        88          1.92
## 89      2   11.64       2.06 2.46              21.6        84          1.95
## 90      2   12.08       1.33 2.30              23.6        70          2.20
## 91      2   12.08       1.83 2.32              18.5        81          1.60
## 92      2   12.00       1.51 2.42              22.0        86          1.45
## 93      2   12.69       1.53 2.26              20.7        80          1.38
## 94      2   12.29       2.83 2.22              18.0        88          2.45
## 95      2   11.62       1.99 2.28              18.0        98          3.02
## 96      2   12.47       1.52 2.20              19.0       162          2.50
## 97      2   11.81       2.12 2.74              21.5       134          1.60
## 98      2   12.29       1.41 1.98              16.0        85          2.55
## 99      2   12.37       1.07 2.10              18.5        88          3.52
## 100     2   12.29       3.17 2.21              18.0        88          2.85
## 101     2   12.08       2.08 1.70              17.5        97          2.23
## 102     2   12.60       1.34 1.90              18.5        88          1.45
## 103     2   12.34       2.45 2.46              21.0        98          2.56
## 104     2   11.82       1.72 1.88              19.5        86          2.50
## 105     2   12.51       1.73 1.98              20.5        85          2.20
## 106     2   12.42       2.55 2.27              22.0        90          1.68
## 107     2   12.25       1.73 2.12              19.0        80          1.65
## 108     2   12.72       1.75 2.28              22.5        84          1.38
## 109     2   12.22       1.29 1.94              19.0        92          2.36
## 110     2   11.61       1.35 2.70              20.0        94          2.74
## 111     2   11.46       3.74 1.82              19.5       107          3.18
## 112     2   12.52       2.43 2.17              21.0        88          2.55
## 113     2   11.76       2.68 2.92              20.0       103          1.75
## 114     2   11.41       0.74 2.50              21.0        88          2.48
## 115     2   12.08       1.39 2.50              22.5        84          2.56
## 116     2   11.03       1.51 2.20              21.5        85          2.46
## 117     2   11.82       1.47 1.99              20.8        86          1.98
## 118     2   12.42       1.61 2.19              22.5       108          2.00
## 119     2   12.77       3.43 1.98              16.0        80          1.63
## 120     2   12.00       3.43 2.00              19.0        87          2.00
## 121     2   11.45       2.40 2.42              20.0        96          2.90
## 122     2   11.56       2.05 3.23              28.5       119          3.18
## 123     2   12.42       4.43 2.73              26.5       102          2.20
## 124     2   13.05       5.80 2.13              21.5        86          2.62
## 125     2   11.87       4.31 2.39              21.0        82          2.86
## 126     2   12.07       2.16 2.17              21.0        85          2.60
## 127     2   12.43       1.53 2.29              21.5        86          2.74
## 128     2   11.79       2.13 2.78              28.5        92          2.13
## 129     2   12.37       1.63 2.30              24.5        88          2.22
## 130     2   12.04       4.30 2.38              22.0        80          2.10
## 131     3   12.86       1.35 2.32              18.0       122          1.51
## 132     3   12.88       2.99 2.40              20.0       104          1.30
## 133     3   12.81       2.31 2.40              24.0        98          1.15
## 134     3   12.70       3.55 2.36              21.5       106          1.70
## 135     3   12.51       1.24 2.25              17.5        85          2.00
## 136     3   12.60       2.46 2.20              18.5        94          1.62
## 137     3   12.25       4.72 2.54              21.0        89          1.38
## 138     3   12.53       5.51 2.64              25.0        96          1.79
## 139     3   13.49       3.59 2.19              19.5        88          1.62
## 140     3   12.84       2.96 2.61              24.0       101          2.32
## 141     3   12.93       2.81 2.70              21.0        96          1.54
## 142     3   13.36       2.56 2.35              20.0        89          1.40
## 143     3   13.52       3.17 2.72              23.5        97          1.55
## 144     3   13.62       4.95 2.35              20.0        92          2.00
## 145     3   12.25       3.88 2.20              18.5       112          1.38
## 146     3   13.16       3.57 2.15              21.0       102          1.50
## 147     3   13.88       5.04 2.23              20.0        80          0.98
## 148     3   12.87       4.61 2.48              21.5        86          1.70
## 149     3   13.32       3.24 2.38              21.5        92          1.93
## 150     3   13.08       3.90 2.36              21.5       113          1.41
## 151     3   13.50       3.12 2.62              24.0       123          1.40
## 152     3   12.79       2.67 2.48              22.0       112          1.48
## 153     3   13.11       1.90 2.75              25.5       116          2.20
## 154     3   13.23       3.30 2.28              18.5        98          1.80
## 155     3   12.58       1.29 2.10              20.0       103          1.48
## 156     3   13.17       5.19 2.32              22.0        93          1.74
## 157     3   13.84       4.12 2.38              19.5        89          1.80
## 158     3   12.45       3.03 2.64              27.0        97          1.90
## 159     3   14.34       1.68 2.70              25.0        98          2.80
## 160     3   13.48       1.67 2.64              22.5        89          2.60
## 161     3   12.36       3.83 2.38              21.0        88          2.30
## 162     3   13.69       3.26 2.54              20.0       107          1.83
## 163     3   12.85       3.27 2.58              22.0       106          1.65
## 164     3   12.96       3.45 2.35              18.5       106          1.39
## 165     3   13.78       2.76 2.30              22.0        90          1.35
## 166     3   13.73       4.36 2.26              22.5        88          1.28
## 167     3   13.45       3.70 2.60              23.0       111          1.70
## 168     3   12.82       3.37 2.30              19.5        88          1.48
## 169     3   13.58       2.58 2.69              24.5       105          1.55
## 170     3   13.40       4.60 2.86              25.0       112          1.98
## 171     3   12.20       3.03 2.32              19.0        96          1.25
## 172     3   12.77       2.39 2.28              19.5        86          1.39
## 173     3   14.16       2.51 2.48              20.0        91          1.68
## 174     3   13.71       5.65 2.45              20.5        95          1.68
## 175     3   13.40       3.91 2.48              23.0       102          1.80
## 176     3   13.27       4.28 2.26              20.0       120          1.59
## 177     3   13.17       2.59 2.37              20.0       120          1.65
## 178     3   14.13       4.10 2.74              24.5        96          2.05
##     Flavanoids Nonflavanoid_phenols Proanthocyanins Color_intensity   Hue
## 1         3.06                 0.28            2.29        5.640000 1.040
## 2         2.76                 0.26            1.28        4.380000 1.050
## 3         3.24                 0.30            2.81        5.680000 1.030
## 4         3.49                 0.24            2.18        7.800000 0.860
## 5         2.69                 0.39            1.82        4.320000 1.040
## 6         3.39                 0.34            1.97        6.750000 1.050
## 7         2.52                 0.30            1.98        5.250000 1.020
## 8         2.51                 0.31            1.25        5.050000 1.060
## 9         2.98                 0.29            1.98        5.200000 1.080
## 10        3.15                 0.22            1.85        7.220000 1.010
## 11        3.32                 0.22            2.38        5.750000 1.250
## 12        2.43                 0.26            1.57        5.000000 1.170
## 13        2.76                 0.29            1.81        5.600000 1.150
## 14        3.69                 0.43            2.81        5.400000 1.250
## 15        3.64                 0.29            2.96        7.500000 1.200
## 16        2.91                 0.30            1.46        7.300000 1.280
## 17        3.14                 0.33            1.97        6.200000 1.070
## 18        3.40                 0.40            1.72        6.600000 1.130
## 19        3.93                 0.32            1.86        8.700000 1.230
## 20        3.03                 0.17            1.66        5.100000 0.960
## 21        3.17                 0.24            2.10        5.650000 1.090
## 22        2.41                 0.25            1.98        4.500000 1.030
## 23        2.88                 0.27            1.69        3.800000 1.110
## 24        2.37                 0.26            1.46        3.930000 1.090
## 25        2.61                 0.28            1.66        3.520000 1.120
## 26        2.68                 0.47            1.92        3.580000 1.130
## 27        2.94                 0.34            1.45        4.800000 0.920
## 28        2.19                 0.27            1.35        3.950000 1.020
## 29        2.97                 0.37            1.76        4.500000 1.250
## 30        2.33                 0.26            1.98        4.700000 1.040
## 31        3.25                 0.29            2.38        5.700000 1.190
## 32        3.19                 0.22            1.95        6.900000 1.090
## 33        2.69                 0.42            1.97        3.840000 1.230
## 34        2.74                 0.50            1.35        5.400000 1.250
## 35        2.53                 0.29            1.54        4.200000 1.100
## 36        2.98                 0.26            1.86        5.100000 1.040
## 37        2.68                 0.34            1.36        4.600000 1.090
## 38        2.43                 0.29            1.44        4.250000 1.120
## 39        2.64                 0.28            1.37        3.700000 1.180
## 40        3.04                 0.20            2.08        5.100000 0.890
## 41        3.29                 0.34            2.34        6.130000 0.950
## 42        2.68                 0.27            1.48        4.280000 0.910
## 43        3.56                 0.17            1.70        5.430000 0.880
## 44        2.63                 0.32            1.66        4.360000 0.820
## 45        3.00                 0.28            2.03        5.040000 0.880
## 46        2.65                 0.30            1.25        5.240000 0.870
## 47        3.17                 0.27            2.19        4.900000 1.040
## 48        3.39                 0.21            2.14        6.100000 0.910
## 49        2.92                 0.32            2.38        6.200000 1.070
## 50        3.54                 0.32            2.08        8.900000 1.120
## 51        3.27                 0.17            2.91        7.200000 1.120
## 52        2.99                 0.22            2.29        5.600000 1.240
## 53        3.74                 0.32            1.87        7.050000 1.010
## 54        2.79                 0.39            1.68        6.300000 1.130
## 55        2.90                 0.21            1.62        5.850000 0.920
## 56        2.78                 0.20            2.45        6.250000 0.980
## 57        3.00                 0.26            2.03        6.380000 0.940
## 58        3.23                 0.31            1.66        6.000000 1.070
## 59        3.67                 0.19            2.04        6.800000 0.890
## 60        0.57                 0.28            0.42        1.950000 1.050
## 61        1.09                 0.63            0.41        3.270000 1.250
## 62        1.41                 0.53            0.62        5.750000 0.980
## 63        1.79                 0.32            0.73        3.800000 1.230
## 64        3.10                 0.19            1.87        4.450000 1.220
## 65        1.75                 0.45            1.03        2.950000 1.450
## 66        2.65                 0.37            2.08        4.600000 1.190
## 67        3.18                 0.26            2.28        5.300000 1.120
## 68        2.00                 0.27            1.04        4.680000 1.120
## 69        1.30                 0.55            0.42        3.170000 1.020
## 70        1.28                 0.14            2.50        2.850000 1.280
## 71        1.02                 0.37            1.46        3.050000 0.906
## 72        2.86                 0.21            1.87        3.380000 1.360
## 73        1.84                 0.27            1.03        3.740000 0.980
## 74        2.89                 0.21            1.96        3.350000 1.310
## 75        2.14                 0.13            1.65        3.210000 0.990
## 76        1.57                 0.34            1.15        3.800000 1.230
## 77        2.03                 0.24            1.46        4.600000 1.190
## 78        1.32                 0.43            0.95        2.650000 0.960
## 79        1.85                 0.35            2.76        3.400000 1.060
## 80        2.55                 0.43            1.95        2.570000 1.190
## 81        2.26                 0.30            1.43        2.500000 1.380
## 82        2.53                 0.26            1.77        3.900000 1.160
## 83        1.58                 0.40            1.40        2.200000 1.310
## 84        1.59                 0.61            1.62        4.800000 0.840
## 85        2.21                 0.22            2.35        3.050000 0.790
## 86        1.94                 0.30            1.46        2.620000 1.230
## 87        1.69                 0.43            1.56        2.450000 1.330
## 88        1.61                 0.40            1.34        2.600000 1.360
## 89        1.69                 0.48            1.35        2.800000 1.000
## 90        1.59                 0.42            1.38        1.740000 1.070
## 91        1.50                 0.52            1.64        2.400000 1.080
## 92        1.25                 0.50            1.63        3.600000 1.050
## 93        1.46                 0.58            1.62        3.050000 0.960
## 94        2.25                 0.25            1.99        2.150000 1.150
## 95        2.26                 0.17            1.35        3.250000 1.160
## 96        2.27                 0.32            3.28        2.600000 1.160
## 97        0.99                 0.14            1.56        2.500000 0.950
## 98        2.50                 0.29            1.77        2.900000 1.230
## 99        3.75                 0.24            1.95        4.500000 1.040
## 100       2.99                 0.45            2.81        2.300000 1.420
## 101       2.17                 0.26            1.40        3.300000 1.270
## 102       1.36                 0.29            1.35        2.450000 1.040
## 103       2.11                 0.34            1.31        2.800000 0.800
## 104       1.64                 0.37            1.42        2.060000 0.940
## 105       1.92                 0.32            1.48        2.940000 1.040
## 106       1.84                 0.66            1.42        2.700000 0.860
## 107       2.03                 0.37            1.63        3.400000 1.000
## 108       1.76                 0.48            1.63        3.300000 0.880
## 109       2.04                 0.39            2.08        2.700000 0.860
## 110       2.92                 0.29            2.49        2.650000 0.960
## 111       2.58                 0.24            3.58        2.900000 0.750
## 112       2.27                 0.26            1.22        2.000000 0.900
## 113       2.03                 0.60            1.05        3.800000 1.230
## 114       2.01                 0.42            1.44        3.080000 1.100
## 115       2.29                 0.43            1.04        2.900000 0.930
## 116       2.17                 0.52            2.01        1.900000 1.710
## 117       1.60                 0.30            1.53        1.950000 0.950
## 118       2.09                 0.34            1.61        2.060000 1.060
## 119       1.25                 0.43            0.83        3.400000 0.700
## 120       1.64                 0.37            1.87        1.280000 0.930
## 121       2.79                 0.32            1.83        3.250000 0.800
## 122       5.08                 0.47            1.87        6.000000 0.930
## 123       2.13                 0.43            1.71        2.080000 0.920
## 124       2.65                 0.30            2.01        2.600000 0.730
## 125       3.03                 0.21            2.91        2.800000 0.750
## 126       2.65                 0.37            1.35        2.760000 0.860
## 127       3.15                 0.39            1.77        3.940000 0.690
## 128       2.24                 0.58            1.76        3.000000 0.970
## 129       2.45                 0.40            1.90        2.120000 0.890
## 130       1.75                 0.42            1.35        2.600000 0.790
## 131       1.25                 0.21            0.94        4.100000 0.760
## 132       1.22                 0.24            0.83        5.400000 0.740
## 133       1.09                 0.27            0.83        5.700000 0.660
## 134       1.20                 0.17            0.84        5.000000 0.780
## 135       0.58                 0.60            1.25        5.450000 0.750
## 136       0.66                 0.63            0.94        7.100000 0.730
## 137       0.47                 0.53            0.80        3.850000 0.750
## 138       0.60                 0.63            1.10        5.000000 0.820
## 139       0.48                 0.58            0.88        5.700000 0.810
## 140       0.60                 0.53            0.81        4.920000 0.890
## 141       0.50                 0.53            0.75        4.600000 0.770
## 142       0.50                 0.37            0.64        5.600000 0.700
## 143       0.52                 0.50            0.55        4.350000 0.890
## 144       0.80                 0.47            1.02        4.400000 0.910
## 145       0.78                 0.29            1.14        8.210000 0.650
## 146       0.55                 0.43            1.30        4.000000 0.600
## 147       0.34                 0.40            0.68        4.900000 0.580
## 148       0.65                 0.47            0.86        7.650000 0.540
## 149       0.76                 0.45            1.25        8.420000 0.550
## 150       1.39                 0.34            1.14        9.400000 0.570
## 151       1.57                 0.22            1.25        8.600000 0.590
## 152       1.36                 0.24            1.26       10.800000 0.480
## 153       1.28                 0.26            1.56        7.100000 0.610
## 154       0.83                 0.61            1.87       10.520000 0.560
## 155       0.58                 0.53            1.40        7.600000 0.580
## 156       0.63                 0.61            1.55        7.900000 0.600
## 157       0.83                 0.48            1.56        9.010000 0.570
## 158       0.58                 0.63            1.14        7.500000 0.670
## 159       1.31                 0.53            2.70       13.000000 0.570
## 160       1.10                 0.52            2.29       11.750000 0.570
## 161       0.92                 0.50            1.04        7.650000 0.560
## 162       0.56                 0.50            0.80        5.880000 0.960
## 163       0.60                 0.60            0.96        5.580000 0.870
## 164       0.70                 0.40            0.94        5.280000 0.680
## 165       0.68                 0.41            1.03        9.580000 0.700
## 166       0.47                 0.52            1.15        6.620000 0.780
## 167       0.92                 0.43            1.46       10.680000 0.850
## 168       0.66                 0.40            0.97       10.260000 0.720
## 169       0.84                 0.39            1.54        8.660000 0.740
## 170       0.96                 0.27            1.11        8.500000 0.670
## 171       0.49                 0.40            0.73        5.500000 0.660
## 172       0.51                 0.48            0.64        9.899999 0.570
## 173       0.70                 0.44            1.24        9.700000 0.620
## 174       0.61                 0.52            1.06        7.700000 0.640
## 175       0.75                 0.43            1.41        7.300000 0.700
## 176       0.69                 0.43            1.35       10.200000 0.590
## 177       0.68                 0.53            1.46        9.300000 0.600
## 178       0.76                 0.56            1.35        9.200000 0.610
##     OD280_OD315 Proline cluster
## 1          3.92    1065       2
## 2          3.40    1050       2
## 3          3.17    1185       2
## 4          3.45    1480       2
## 5          2.93     735       2
## 6          2.85    1450       2
## 7          3.58    1290       2
## 8          3.58    1295       2
## 9          2.85    1045       2
## 10         3.55    1045       2
## 11         3.17    1510       2
## 12         2.82    1280       2
## 13         2.90    1320       2
## 14         2.73    1150       2
## 15         3.00    1547       2
## 16         2.88    1310       2
## 17         2.65    1280       2
## 18         2.57    1130       2
## 19         2.82    1680       2
## 20         3.36     845       2
## 21         3.71     780       2
## 22         3.52     770       2
## 23         4.00    1035       2
## 24         3.63    1015       2
## 25         3.82     845       2
## 26         3.20     830       2
## 27         3.22    1195       2
## 28         2.77    1285       2
## 29         3.40     915       2
## 30         3.59    1035       2
## 31         2.71    1285       2
## 32         2.88    1515       2
## 33         2.87     990       2
## 34         3.00    1235       2
## 35         2.87    1095       2
## 36         3.47     920       2
## 37         2.78     880       2
## 38         2.51    1105       2
## 39         2.69    1020       2
## 40         3.53     760       2
## 41         3.38     795       2
## 42         3.00    1035       2
## 43         3.56    1095       2
## 44         3.00     680       2
## 45         3.35     885       2
## 46         3.33    1080       2
## 47         3.44    1065       2
## 48         3.33     985       2
## 49         2.75    1060       2
## 50         3.10    1260       2
## 51         2.91    1150       2
## 52         3.37    1265       2
## 53         3.26    1190       2
## 54         2.93    1375       2
## 55         3.20    1060       2
## 56         3.03    1120       2
## 57         3.31     970       2
## 58         2.84    1270       2
## 59         2.87    1285       2
## 60         1.82     520       1
## 61         1.67     680       1
## 62         1.59     450       1
## 63         2.46     630       1
## 64         2.87     420       1
## 65         2.23     355       1
## 66         2.30     678       1
## 67         3.18     502       1
## 68         3.48     510       1
## 69         1.93     750       1
## 70         3.07     718       1
## 71         1.82     870       1
## 72         3.16     410       1
## 73         2.78     472       1
## 74         3.50     985       2
## 75         3.13     886       1
## 76         2.14     428       1
## 77         2.48     392       1
## 78         2.52     500       1
## 79         2.31     750       1
## 80         3.13     463       1
## 81         3.12     278       1
## 82         3.14     714       1
## 83         2.72     630       1
## 84         2.01     515       3
## 85         3.08     520       1
## 86         3.16     450       1
## 87         2.26     495       1
## 88         3.21     562       1
## 89         2.75     680       1
## 90         3.21     625       1
## 91         2.27     480       1
## 92         2.65     450       1
## 93         2.06     495       1
## 94         3.30     290       1
## 95         2.96     345       1
## 96         2.63     937       2
## 97         2.26     625       1
## 98         2.74     428       1
## 99         2.77     660       1
## 100        2.83     406       1
## 101        2.96     710       1
## 102        2.77     562       1
## 103        3.38     438       1
## 104        2.44     415       1
## 105        3.57     672       1
## 106        3.30     315       1
## 107        3.17     510       1
## 108        2.42     488       1
## 109        3.02     312       1
## 110        3.26     680       1
## 111        2.81     562       1
## 112        2.78     325       1
## 113        2.50     607       1
## 114        2.31     434       1
## 115        3.19     385       1
## 116        2.87     407       1
## 117        3.33     495       1
## 118        2.96     345       1
## 119        2.12     372       1
## 120        3.05     564       1
## 121        3.39     625       1
## 122        3.69     465       1
## 123        3.12     365       1
## 124        3.10     380       1
## 125        3.64     380       1
## 126        3.28     378       1
## 127        2.84     352       1
## 128        2.44     466       1
## 129        2.78     342       1
## 130        2.57     580       1
## 131        1.29     630       3
## 132        1.42     530       3
## 133        1.36     560       3
## 134        1.29     600       3
## 135        1.51     650       3
## 136        1.58     695       3
## 137        1.27     720       3
## 138        1.69     515       3
## 139        1.82     580       3
## 140        2.15     590       3
## 141        2.31     600       3
## 142        2.47     780       3
## 143        2.06     520       3
## 144        2.05     550       3
## 145        2.00     855       3
## 146        1.68     830       3
## 147        1.33     415       3
## 148        1.86     625       3
## 149        1.62     650       3
## 150        1.33     550       3
## 151        1.30     500       3
## 152        1.47     480       3
## 153        1.33     425       3
## 154        1.51     675       3
## 155        1.55     640       3
## 156        1.48     725       3
## 157        1.64     480       3
## 158        1.73     880       3
## 159        1.96     660       3
## 160        1.78     620       3
## 161        1.58     520       3
## 162        1.82     680       3
## 163        2.11     570       3
## 164        1.75     675       3
## 165        1.68     615       3
## 166        1.75     520       3
## 167        1.56     695       3
## 168        1.75     685       3
## 169        1.80     750       3
## 170        1.92     630       3
## 171        1.83     510       3
## 172        1.63     470       3
## 173        1.71     660       3
## 174        1.74     740       3
## 175        1.56     750       3
## 176        1.56     835       3
## 177        1.62     840       3
## 178        1.60     560       3

Paso 8. Graficar los clusters

fviz_cluster(segmentos, data=df)

Paso 9. Optimizar la cantidad de grupos

La cantidad óptima de grupos corresponde al punto más alto de la siguiente gráfica.

set.seed(123)
optimizacion <- clusGap(df, FUN=kmeans, nstart=1, K.max =10)
plot(optimizacion, xlab="Número de clusters k")

Paso 10. Comparar segmentos

promedio <- aggregate(asignacion, by=list(asignacion$cluster), FUN=mean)
promedio
##   Group.1    Class  Alcohol Malic_acid      Ash Alcalinity_of_ash Magnesium
## 1       1 2.000000 12.25412   1.914265 2.239118          20.07941  93.04412
## 2       2 1.032787 13.71148   1.997049 2.453770          17.28197 107.78689
## 3       3 2.979592 13.15163   3.344490 2.434694          21.43878  99.02041
##   Total_phenols Flavanoids Nonflavanoid_phenols Proanthocyanins Color_intensity
## 1      2.248971  2.0733824            0.3629412        1.601324        3.064706
## 2      2.842131  2.9691803            0.2891803        1.922951        5.444590
## 3      1.678163  0.7979592            0.4508163        1.163061        7.343265
##         Hue OD280_OD315   Proline cluster
## 1 1.0542059    2.788529  506.5882       1
## 2 1.0677049    3.154754 1110.6393       2
## 3 0.6859184    1.690204  627.5510       3

Conclusión

La segmentación o clusters es un algoritmo útil para identificar el cultivar correspondiente a cada vino.

LS0tCnRpdGxlOiAiU2VnbWVudGFjacOzbiBkZSBWaW5vcyIKYXV0aG9yOiAiTHVpcyBBbmdlbCBEaWF6IEEwMTY2MjAzNCIKZGF0ZTogIjIwMjQtMDItMTkiCm91dHB1dDogCiAgaHRtbF9kb2N1bWVudDoKICAgIHRvYzogVFJVRQogICAgdG9jX2Zsb2F0OiBUUlVFCiAgICBjb2RlX2Rvd25sb2FkOiBUUlVFCiAgICB0aGVtZTogY29zbW8KLS0tCgpgYGB7ciBzZXR1cCwgaW5jbHVkZT1GQUxTRX0Ka25pdHI6Om9wdHNfY2h1bmskc2V0KGVjaG8gPSBUUlVFKQpgYGAKIVtdKC9Vc2Vycy9sdWlzYW5nZWwvTGlicmFyeS9DbG91ZFN0b3JhZ2UvT25lRHJpdmUtSW5zdGl0dXRvVGVjbm9sb2dpY295ZGVFc3R1ZGlvc1N1cGVyaW9yZXNkZU1vbnRlcnJleS83dGggU2Vhc29uL00yL3dpbmUuZ2lmKQoKYGBge3J9CiMgTGVlciBlbCBhcmNoaXZvIENTVgp3aW5lX2RhdGEgPC0gcmVhZC5jc3YoIi9Vc2Vycy9sdWlzYW5nZWwvTGlicmFyeS9DbG91ZFN0b3JhZ2UvT25lRHJpdmUtSW5zdGl0dXRvVGVjbm9sb2dpY295ZGVFc3R1ZGlvc1N1cGVyaW9yZXNkZU1vbnRlcnJ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