Variables

merged2 <- merged %>% 
           remove_rownames %>% 
           column_to_rownames(var="Nom_Barri") %>% 
           select("n.tot.V1419","n.esp.V1419","n.ue27.V1419",
                  "hotel2019",
                  "alq.num.V1519","alq.pmq.V1519","alq.pm.V1519",
                  "tot_ann.V1519","ent.V1519","priv.V1519","shared.V1519","pmedio.V1519","pm_ent.V1519",
                  "RFD.2017",
                  "tot.comp.V1519","usat.comp.V1519","tot.eur.V1519","usat.eur.V1519","tot.eurm2.V1519",
                  )

Standardizar los datos

df <- scale(merged2)

Optimal K Elbow

#elbow (1 option)
set.seed(123)
wss <- function(k) {
  kmeans(df, k, nstart = 100 )$tot.withinss
}

k.values <- 1:15

wss_values <- map_dbl(k.values, wss)

plot(k.values, wss_values,
       type="b", pch = 19, frame = FALSE, 
       xlab="Number of clusters K",
       ylab="Total within-clusters sum of squares")

#elbow (2 option)
set.seed(123)
fviz_nbclust(df, kmeans, method = "wss")

#Optimal K Average Silhouette Method
avg_sil <- function(k) {
  km.res <- kmeans(df, centers = k, nstart = 100)
  ss <- silhouette(km.res$cluster, dist(df))
  mean(ss[, 3])
}

k.values <- 2:15

avg_sil_values <- map_dbl(k.values, avg_sil)

plot(k.values, avg_sil_values,
       type = "b", pch = 19, frame = FALSE, 
       xlab = "Number of clusters K",
       ylab = "Average Silhouettes")

# Silhouette Method 2
fviz_nbclust(df, kmeans, method = "silhouette")

#Optimal K Gap Statistic Method
set.seed(123)
gap_stat <- clusGap(df, FUN = kmeans, nstart = 100, K.max = 10, B = 50)

print(gap_stat, method = "firstmax")
## Clustering Gap statistic ["clusGap"] from call:
## clusGap(x = df, FUNcluster = kmeans, K.max = 10, B = 50, nstart = 100)
## B=50 simulated reference sets, k = 1..10; spaceH0="scaledPCA"
##  --> Number of clusters (method 'firstmax'): 1
##           logW   E.logW       gap     SE.sim
##  [1,] 4.610539 5.326039 0.7154999 0.02076777
##  [2,] 4.531501 5.203734 0.6722330 0.02009577
##  [3,] 4.454856 5.117652 0.6627967 0.01721417
##  [4,] 4.382964 5.053982 0.6710185 0.01659167
##  [5,] 4.328907 5.001213 0.6723059 0.01498542
##  [6,] 4.256248 4.955472 0.6992240 0.01484569
##  [7,] 4.217645 4.916434 0.6987886 0.01461005
##  [8,] 4.174325 4.880426 0.7061007 0.01480869
##  [9,] 4.130014 4.847048 0.7170347 0.01496196
## [10,] 4.089234 4.814741 0.7255075 0.01494351
#Optimal K Gap Statistic Method 2
fviz_gap_stat(gap_stat)

#Examinar a los numeros de cluster
k3 <- kmeans(df, centers = 3, nstart = 100)
k4 <- kmeans(df, centers = 4, nstart = 100)
k5 <- kmeans(df, centers = 5, nstart = 100)
k6 <- kmeans(df, centers = 6, nstart = 100)
k7 <- kmeans(df, centers = 7, nstart = 100)
k8 <- kmeans(df, centers = 8, nstart = 100)
k9 <- kmeans(df, centers = 9, nstart = 100)

# plots to compare
p3 <- fviz_cluster(k3, geom = "point",  data = df) + ggtitle("k = 3")
p4 <- fviz_cluster(k4, geom = "point",  data = df) + ggtitle("k = 4")
p5 <- fviz_cluster(k5, geom = "point",  data = df) + ggtitle("k = 5")
p6 <- fviz_cluster(k6, geom = "point",  data = df) + ggtitle("k = 6")
p7 <- fviz_cluster(k7, geom = "point",  data = df) + ggtitle("k = 7")
p8 <- fviz_cluster(k8, geom = "point",  data = df) + ggtitle("k = 8")
p9 <- fviz_cluster(k9, geom = "point",  data = df) + ggtitle("k = 9")

grid.arrange(p3, nrow = 1)

grid.arrange(p4, p5, nrow = 1)

grid.arrange(p6, p7, nrow = 1)

grid.arrange(p8, p9, nrow = 1)

K means

for (i in 3:5)
{
finalK <- kmeans(df, centers = i, nstart = 100)
#print
print(finalK)
}
## K-means clustering with 3 clusters of sizes 19, 46, 8
## 
## Cluster means:
##   n.tot.V1419 n.esp.V1419 n.ue27.V1419  hotel2019 alq.num.V1519 alq.pmq.V1519
## 1  -0.2057338 -0.08801494  -0.19582276 -0.3531142   -0.46521737     0.3610643
## 2  -0.1306014 -0.15312551  -0.08967284  0.2290616    0.06803908     0.2519388
## 3   1.2395757  1.08950716   0.98069790 -0.4784579    0.71366658    -2.3061759
##   alq.pm.V1519 tot_ann.V1519  ent.V1519 priv.V1519 shared.V1519 pmedio.V1519
## 1    0.2749311     0.9635255  0.6980150  0.7304444   -0.2451888  -0.35962781
## 2    0.2684673    -0.2825856 -0.1565626 -0.2350451    0.1513337   0.07299698
## 3   -2.1966484    -0.6635060 -0.7575505 -0.3832961   -0.2878456   0.43438342
##   pm_ent.V1519   RFD.2017 tot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1  -0.28067569 -0.6144944     0.96929160      0.83757206     0.9114548
## 2   0.13034851  0.3616030    -0.40363492     -0.33893983    -0.1909224
## 3  -0.08289918 -0.6197933     0.01883324     -0.04032963    -1.0669013
##   usat.eur.V1519 tot.eurm2.V1519
## 1      0.8616022      0.62534989
## 2     -0.1862966     -0.05746573
## 3     -0.9750998     -1.15477806
## 
## Clustering vector:
##                                     el Raval 
##                                            2 
##                               el Barri Gòtic 
##                                            2 
##                               la Barceloneta 
##                                            2 
##        Sant Pere, Santa Caterina i la Ribera 
##                                            2 
##                                el Fort Pienc 
##                                            2 
##                           la Sagrada Família 
##                                            2 
##                       la Dreta de l'Eixample 
##                                            2 
##              l'Antiga Esquerra de l'Eixample 
##                                            2 
##               la Nova Esquerra de l'Eixample 
##                                            2 
##                                  Sant Antoni 
##                                            2 
##                                 el Poble Sec 
##                                            1 
##                   la Marina del Prat Vermell 
##                                            3 
##                            la Marina de Port 
##                                            1 
##                        la Font de la Guatlla 
##                                            1 
##                                  Hostafrancs 
##                                            1 
##                                   la Bordeta 
##                                            1 
##                                Sants - Badal 
##                                            2 
##                                        Sants 
##                                            2 
##                                    les Corts 
##                                            2 
##                   la Maternitat i Sant Ramon 
##                                            2 
##                                    Pedralbes 
##                                            2 
##        Vallvidrera, el Tibidabo i les Planes 
##                                            3 
##                                       Sarrià 
##                                            2 
##                              les Tres Torres 
##                                            2 
##                   Sant Gervasi - la Bonanova 
##                                            2 
##                       Sant Gervasi - Galvany 
##                                            2 
##                         el Putxet i el Farró 
##                                            2 
##                    Vallcarca i els Penitents 
##                                            2 
##                                      el Coll 
##                                            2 
##                                     la Salut 
##                                            2 
##                            la Vila de Gràcia 
##                                            2 
##           el Camp d'en Grassot i Gràcia Nova 
##                                            2 
##                             el Baix Guinardó 
##                                            2 
##                                     Can Baró 
##                                            2 
##                                  el Guinardó 
##                                            2 
##                         la Font d'en Fargues 
##                                            2 
##                                    el Carmel 
##                                            1 
##                                 la Teixonera 
##                                            1 
##                     Sant Genís dels Agudells 
##                                            1 
##                                      Montbau 
##                                            1 
##                             la Vall d'Hebron 
##                                            2 
##                                     la Clota 
##                                            3 
##                                        Horta 
##                                            1 
##                Vilapicina i la Torre Llobeta 
##                                            2 
##                                        Porta 
##                                            1 
##                          el Turó de la Peira 
##                                            1 
##                                  Can Peguera 
##                                            3 
##                                 la Guineueta 
##                                            2 
##                                    Canyelles 
##                                            3 
##                                 les Roquetes 
##                                            1 
##                                       Verdun 
##                                            2 
##                               la Prosperitat 
##                                            2 
##                             la Trinitat Nova 
##                                            1 
##                                   Torre Baró 
##                                            3 
##                             Ciutat Meridiana 
##                                            1 
##                                     Vallbona 
##                                            3 
##                            la Trinitat Vella 
##                                            1 
##                                Baró de Viver 
##                                            3 
##                                el Bon Pastor 
##                                            1 
##                                  Sant Andreu 
##                                            2 
##                                   la Sagrera 
##                                            2 
##                     el Congrés i els Indians 
##                                            1 
##                                        Navas 
##                                            2 
##                   el Camp de l'Arpa del Clot 
##                                            2 
##                                      el Clot 
##                                            2 
##            el Parc i la Llacuna del Poblenou 
##                                            2 
##                la Vila Olímpica del Poblenou 
##                                            2 
##                                  el Poblenou 
##                                            2 
## Diagonal Mar i el Front Marítim del Poblenou 
##                                            2 
##                        el Besòs i el Maresme 
##                                            2 
##                      Provençals del Poblenou 
##                                            2 
##                     Sant Martí de Provençals 
##                                            2 
##                          la Verneda i la Pau 
##                                            1 
## 
## Within cluster sum of squares by cluster:
## [1] 312.2099 480.1119 244.5400
##  (between_SS / total_SS =  24.2 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"      
## K-means clustering with 4 clusters of sizes 5, 8, 51, 9
## 
## Cluster means:
##   n.tot.V1419 n.esp.V1419 n.ue27.V1419  hotel2019 alq.num.V1519 alq.pmq.V1519
## 1  -0.4105844 -0.17307972   -0.3627319 -0.5214989   -0.61115574     0.2999935
## 2   1.2395757  1.08950716    0.9806979 -0.4784579    0.71366658    -2.3061759
## 3  -0.0419127 -0.00592637   -0.0423587 -0.2431746   -0.05983749     0.2905458
## 4  -0.6362373 -0.83871264   -0.4301811  2.0930067    0.04423980     0.2368450
##   alq.pm.V1519 tot_ann.V1519   ent.V1519 priv.V1519 shared.V1519 pmedio.V1519
## 1    0.4398503     2.8464356  1.94616216  2.4750186   -0.2451888   -0.4483805
## 2   -2.1966484    -0.6635060 -0.75755055 -0.3832961   -0.2878456    0.4343834
## 3    0.2142828    -0.1069230 -0.04480413 -0.1160114   -0.2338137   -0.3090965
## 4    0.4939461    -0.3856732 -0.15393286 -0.3769045    1.7170229    1.6145284
##   pm_ent.V1519   RFD.2017 tot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1   0.03873398 -0.8665752     1.60234558      1.75284607    1.17684295
## 2  -0.08289918 -0.6197933     0.01883324     -0.04032963   -1.06690132
## 3  -0.12424225  0.1182019    -0.07084936     -0.07942434   -0.06035493
## 4   0.75620869  0.3625475    -0.50545296     -0.48788354    0.63656635
##   usat.eur.V1519 tot.eurm2.V1519
## 1     1.29966345      0.71047587
## 2    -0.97509979     -1.15477806
## 3    -0.07564961     -0.02761248
## 4     0.57340124      0.78823131
## 
## Clustering vector:
##                                     el Raval 
##                                            4 
##                               el Barri Gòtic 
##                                            4 
##                               la Barceloneta 
##                                            3 
##        Sant Pere, Santa Caterina i la Ribera 
##                                            4 
##                                el Fort Pienc 
##                                            3 
##                           la Sagrada Família 
##                                            4 
##                       la Dreta de l'Eixample 
##                                            4 
##              l'Antiga Esquerra de l'Eixample 
##                                            4 
##               la Nova Esquerra de l'Eixample 
##                                            3 
##                                  Sant Antoni 
##                                            4 
##                                 el Poble Sec 
##                                            4 
##                   la Marina del Prat Vermell 
##                                            2 
##                            la Marina de Port 
##                                            3 
##                        la Font de la Guatlla 
##                                            3 
##                                  Hostafrancs 
##                                            3 
##                                   la Bordeta 
##                                            3 
##                                Sants - Badal 
##                                            3 
##                                        Sants 
##                                            3 
##                                    les Corts 
##                                            3 
##                   la Maternitat i Sant Ramon 
##                                            3 
##                                    Pedralbes 
##                                            3 
##        Vallvidrera, el Tibidabo i les Planes 
##                                            2 
##                                       Sarrià 
##                                            3 
##                              les Tres Torres 
##                                            3 
##                   Sant Gervasi - la Bonanova 
##                                            3 
##                       Sant Gervasi - Galvany 
##                                            3 
##                         el Putxet i el Farró 
##                                            3 
##                    Vallcarca i els Penitents 
##                                            3 
##                                      el Coll 
##                                            3 
##                                     la Salut 
##                                            3 
##                            la Vila de Gràcia 
##                                            4 
##           el Camp d'en Grassot i Gràcia Nova 
##                                            3 
##                             el Baix Guinardó 
##                                            3 
##                                     Can Baró 
##                                            3 
##                                  el Guinardó 
##                                            3 
##                         la Font d'en Fargues 
##                                            3 
##                                    el Carmel 
##                                            1 
##                                 la Teixonera 
##                                            3 
##                     Sant Genís dels Agudells 
##                                            1 
##                                      Montbau 
##                                            3 
##                             la Vall d'Hebron 
##                                            3 
##                                     la Clota 
##                                            2 
##                                        Horta 
##                                            3 
##                Vilapicina i la Torre Llobeta 
##                                            3 
##                                        Porta 
##                                            3 
##                          el Turó de la Peira 
##                                            3 
##                                  Can Peguera 
##                                            2 
##                                 la Guineueta 
##                                            3 
##                                    Canyelles 
##                                            2 
##                                 les Roquetes 
##                                            1 
##                                       Verdun 
##                                            3 
##                               la Prosperitat 
##                                            3 
##                             la Trinitat Nova 
##                                            1 
##                                   Torre Baró 
##                                            2 
##                             Ciutat Meridiana 
##                                            3 
##                                     Vallbona 
##                                            2 
##                            la Trinitat Vella 
##                                            1 
##                                Baró de Viver 
##                                            2 
##                                el Bon Pastor 
##                                            3 
##                                  Sant Andreu 
##                                            3 
##                                   la Sagrera 
##                                            3 
##                     el Congrés i els Indians 
##                                            3 
##                                        Navas 
##                                            3 
##                   el Camp de l'Arpa del Clot 
##                                            3 
##                                      el Clot 
##                                            3 
##            el Parc i la Llacuna del Poblenou 
##                                            3 
##                la Vila Olímpica del Poblenou 
##                                            3 
##                                  el Poblenou 
##                                            3 
## Diagonal Mar i el Front Marítim del Poblenou 
##                                            3 
##                        el Besòs i el Maresme 
##                                            3 
##                      Provençals del Poblenou 
##                                            3 
##                     Sant Martí de Provençals 
##                                            3 
##                          la Verneda i la Pau 
##                                            3 
## 
## Within cluster sum of squares by cluster:
## [1] 123.9226 244.5400 396.9172 145.3292
##  (between_SS / total_SS =  33.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"      
## K-means clustering with 5 clusters of sizes 13, 7, 8, 44, 1
## 
## Cluster means:
##    n.tot.V1419 n.esp.V1419 n.ue27.V1419  hotel2019 alq.num.V1519 alq.pmq.V1519
## 1 -0.333233504  -0.2142491  -0.30622969 -0.2814549   -0.45601578   -0.01912597
## 2  0.749316153   0.2339863   0.05215582 -0.4870544    0.86718244   -2.31217776
## 3 -0.678572942  -0.8232848  -0.41300987  2.1922230    0.11771774    0.22845510
## 4 -0.003545151   0.0148968  -0.01273905 -0.2284365   -0.01642885    0.38341827
## 5  4.671392665   7.0781531   7.48049249 -0.4182822   -0.36094450   -2.26416259
##   alq.pm.V1519 tot_ann.V1519  ent.V1519 priv.V1519 shared.V1519 pmedio.V1519
## 1   0.09135285     1.2597162  0.9089623  1.0059922   -0.2451888   -0.3383576
## 2  -2.19496231    -0.5684428 -0.6239732 -0.2856289   -0.2939394    0.5604926
## 3   0.43608319    -0.3950877 -0.1639396 -0.3937282    1.9622994    1.7488493
## 4   0.29311214    -0.1797173 -0.1010134 -0.1559478   -0.2320040   -0.2969821
## 5  -2.20845077    -1.3289490 -1.6925918 -1.0669666   -0.2451888   -0.4483805
##   pm_ent.V1519    RFD.2017 tot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1  -0.11606404 -0.63888460      1.0959222       0.9625912     1.2669916
## 2   0.15597456 -0.70776247      0.1399880       0.0716077    -1.2501726
## 3   0.82008964  0.44143916     -0.5343425      -0.5107326     0.1870872
## 4  -0.09974298  0.22118939     -0.2300662      -0.1842088    -0.2143722
## 5  -1.75501535 -0.00400922     -0.8292500      -0.8238909     0.2159977
##   usat.eur.V1519 tot.eurm2.V1519
## 1      1.2950347      0.71096160
## 2     -1.1824273     -1.38316602
## 3      0.1172746      0.40506209
## 4     -0.2266557     -0.07374484
## 5      0.4761929      0.44393762
## 
## Clustering vector:
##                                     el Raval 
##                                            3 
##                               el Barri Gòtic 
##                                            3 
##                               la Barceloneta 
##                                            4 
##        Sant Pere, Santa Caterina i la Ribera 
##                                            3 
##                                el Fort Pienc 
##                                            4 
##                           la Sagrada Família 
##                                            3 
##                       la Dreta de l'Eixample 
##                                            3 
##              l'Antiga Esquerra de l'Eixample 
##                                            3 
##               la Nova Esquerra de l'Eixample 
##                                            4 
##                                  Sant Antoni 
##                                            3 
##                                 el Poble Sec 
##                                            1 
##                   la Marina del Prat Vermell 
##                                            2 
##                            la Marina de Port 
##                                            4 
##                        la Font de la Guatlla 
##                                            1 
##                                  Hostafrancs 
##                                            1 
##                                   la Bordeta 
##                                            4 
##                                Sants - Badal 
##                                            4 
##                                        Sants 
##                                            4 
##                                    les Corts 
##                                            4 
##                   la Maternitat i Sant Ramon 
##                                            4 
##                                    Pedralbes 
##                                            4 
##        Vallvidrera, el Tibidabo i les Planes 
##                                            2 
##                                       Sarrià 
##                                            4 
##                              les Tres Torres 
##                                            4 
##                   Sant Gervasi - la Bonanova 
##                                            4 
##                       Sant Gervasi - Galvany 
##                                            4 
##                         el Putxet i el Farró 
##                                            4 
##                    Vallcarca i els Penitents 
##                                            4 
##                                      el Coll 
##                                            4 
##                                     la Salut 
##                                            4 
##                            la Vila de Gràcia 
##                                            3 
##           el Camp d'en Grassot i Gràcia Nova 
##                                            4 
##                             el Baix Guinardó 
##                                            4 
##                                     Can Baró 
##                                            4 
##                                  el Guinardó 
##                                            4 
##                         la Font d'en Fargues 
##                                            4 
##                                    el Carmel 
##                                            1 
##                                 la Teixonera 
##                                            4 
##                     Sant Genís dels Agudells 
##                                            1 
##                                      Montbau 
##                                            1 
##                             la Vall d'Hebron 
##                                            4 
##                                     la Clota 
##                                            5 
##                                        Horta 
##                                            4 
##                Vilapicina i la Torre Llobeta 
##                                            4 
##                                        Porta 
##                                            4 
##                          el Turó de la Peira 
##                                            4 
##                                  Can Peguera 
##                                            2 
##                                 la Guineueta 
##                                            4 
##                                    Canyelles 
##                                            2 
##                                 les Roquetes 
##                                            1 
##                                       Verdun 
##                                            4 
##                               la Prosperitat 
##                                            4 
##                             la Trinitat Nova 
##                                            1 
##                                   Torre Baró 
##                                            2 
##                             Ciutat Meridiana 
##                                            1 
##                                     Vallbona 
##                                            2 
##                            la Trinitat Vella 
##                                            1 
##                                Baró de Viver 
##                                            2 
##                                el Bon Pastor 
##                                            1 
##                                  Sant Andreu 
##                                            4 
##                                   la Sagrera 
##                                            4 
##                     el Congrés i els Indians 
##                                            1 
##                                        Navas 
##                                            4 
##                   el Camp de l'Arpa del Clot 
##                                            4 
##                                      el Clot 
##                                            4 
##            el Parc i la Llacuna del Poblenou 
##                                            4 
##                la Vila Olímpica del Poblenou 
##                                            4 
##                                  el Poblenou 
##                                            4 
## Diagonal Mar i el Front Marítim del Poblenou 
##                                            4 
##                        el Besòs i el Maresme 
##                                            4 
##                      Provençals del Poblenou 
##                                            4 
##                     Sant Martí de Provençals 
##                                            4 
##                          la Verneda i la Pau 
##                                            1 
## 
## Within cluster sum of squares by cluster:
## [1] 256.64936 125.19042  97.21708 320.13072   0.00000
##  (between_SS / total_SS =  41.6 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Plot

final3 <- kmeans(df, centers = 3, nstart = 100)
final4 <- kmeans(df, centers = 4, nstart = 100)
final5 <- kmeans(df, centers = 5, nstart = 100)
fviz_cluster(final3, data = df)

fviz_cluster(final4, data = df)

fviz_cluster(final5, data = df)

kkk <- merged2 %>% mutate(Cluster = final3$cluster) %>% group_by(Cluster) %>% summarise_all(“mean”)

z_cluster(finalK, data = df)

for (i in 3:9) { finalK <- kmeans(df, centers = i, nstart = 100) #plot x <- fviz_cluster(finalK, data = df) x }

Descriptive Statistic

kkk <- merged %>% mutate(Cluster = finalK$cluster) %>% group_by(Nom_Barri) #%>% #summarise_all(“mean”)

k.1 <- kkk %>% filter(Cluster == 1)

k.1$Nom_Barri

k.2 <- kkk %>% filter(Cluster == 2)

k.2$Nom_Barri

k.3 <- kkk %>% filter(Cluster == 3)

k.3$Nom_Barri