Variables

merged2 <- merged %>% 
           remove_rownames %>% 
           column_to_rownames(var="Nom_Barri") %>% 
           select("n.tot.V1419","n.esp.V1419","n.ext.V1419","n.ue27-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","pm_priv.V1519","pm_sha.V1519",
                  "RFD.2017",
                  "tot.comp.V1519","nou.comp.V1519","prot.comp.V1519","usat.comp.V1519","tot.eur.V1519","nou.eur.V1519","usat.eur.V1519","tot.eurm2.V1519","nou.eurm2.V1519","usat.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.833415 5.528018 0.6946035 0.01824059
##  [2,] 4.771626 5.439784 0.6681577 0.01651354
##  [3,] 4.711042 5.375361 0.6643189 0.01485263
##  [4,] 4.662957 5.322417 0.6594596 0.01393703
##  [5,] 4.621855 5.278703 0.6568480 0.01459150
##  [6,] 4.572049 5.240487 0.6684385 0.01421386
##  [7,] 4.533246 5.205072 0.6718260 0.01476023
##  [8,] 4.499721 5.172680 0.6729593 0.01503993
##  [9,] 4.463067 5.141424 0.6783578 0.01525816
## [10,] 4.421751 5.111643 0.6898914 0.01489496
#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 8, 23, 42
## 
## Cluster means:
##   n.tot.V1419 n.esp.V1419 n.ext.V1419 n.ue27-esp.V1419 n.ue27.V1419  hotel2019
## 1   1.2395757  1.08950716  0.24035468       -0.6209154   0.98069790 -0.4784579
## 2  -0.1082208 -0.30017923 -0.13964472       -0.2667018  -0.18865861  0.7897558
## 3  -0.1768459 -0.04314131  0.03069027        0.2643206  -0.08348655 -0.3413505
##   alq.num.V1519 alq.pmq.V1519 alq.pm.V1519 tot_ann.V1519  ent.V1519 priv.V1519
## 1     0.7136666    -2.3061759   -2.1966484    -0.6635060 -0.7575505 -0.3832961
## 2     0.3891447     0.3608841    0.4176141    -0.3878609 -0.2309869 -0.3194104
## 3    -0.3490395     0.2416446    0.1897158     0.3387821  0.2707882  0.2479240
##   shared.V1519 pmedio.V1519 pm_ent.V1519 pm_priv.V1519 pm_sha.V1519   RFD.2017
## 1   -0.2878456    0.4343834  -0.08289918     0.8495700    0.4125156 -0.6197933
## 2    0.5701120    0.5092635   0.66426372    -0.0485234    0.4231359  0.9904684
## 3   -0.2573765   -0.3616221  -0.34797315    -0.1352505   -0.3102917 -0.4243435
##   tot.comp.V1519 nou.comp.V1519 prot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1     0.01883324     -0.1724909      0.06331251     -0.04032963   -1.06690132
## 2    -0.63991313     -0.1309045      0.29103257     -0.52681728   -0.07772811
## 3     0.34684134      0.1045412     -0.17143450      0.29617701    0.24578469
##   nou.eur.V1519 usat.eur.V1519 tot.eurm2.V1519 nou.eurm2.V1519 usat.eurm2.V1519
## 1    -0.1122872     -0.9750998    -1.154778062     -0.02871866        0.9360855
## 2     0.2802413     -0.1397510    -0.005694437      0.13499280       -0.1016502
## 3    -0.1320774      0.2622636     0.223076108     -0.06845441       -0.1226364
## 
## Clustering vector:
##                                     el Raval 
##                                            2 
##                               el Barri Gòtic 
##                                            2 
##                               la Barceloneta 
##                                            3 
##        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 
##                                            3 
##                   la Marina del Prat Vermell 
##                                            1 
##                            la Marina de Port 
##                                            3 
##                        la Font de la Guatlla 
##                                            3 
##                                  Hostafrancs 
##                                            3 
##                                   la Bordeta 
##                                            3 
##                                Sants - Badal 
##                                            3 
##                                        Sants 
##                                            2 
##                                    les Corts 
##                                            2 
##                   la Maternitat i Sant Ramon 
##                                            2 
##                                    Pedralbes 
##                                            2 
##        Vallvidrera, el Tibidabo i les Planes 
##                                            1 
##                                       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 
##                                            3 
##                                      el Coll 
##                                            3 
##                                     la Salut 
##                                            3 
##                            la Vila de Gràcia 
##                                            2 
##           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 
##                                            3 
##                                 la Teixonera 
##                                            3 
##                     Sant Genís dels Agudells 
##                                            3 
##                                      Montbau 
##                                            3 
##                             la Vall d'Hebron 
##                                            3 
##                                     la Clota 
##                                            1 
##                                        Horta 
##                                            3 
##                Vilapicina i la Torre Llobeta 
##                                            3 
##                                        Porta 
##                                            3 
##                          el Turó de la Peira 
##                                            3 
##                                  Can Peguera 
##                                            1 
##                                 la Guineueta 
##                                            3 
##                                    Canyelles 
##                                            1 
##                                 les Roquetes 
##                                            3 
##                                       Verdun 
##                                            3 
##                               la Prosperitat 
##                                            3 
##                             la Trinitat Nova 
##                                            3 
##                                   Torre Baró 
##                                            1 
##                             Ciutat Meridiana 
##                                            3 
##                                     Vallbona 
##                                            1 
##                            la Trinitat Vella 
##                                            3 
##                                Baró de Viver 
##                                            1 
##                                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 
##                                            2 
##            el Parc i la Llacuna del Poblenou 
##                                            3 
##                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 
##                                            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] 447.8749 455.5921 752.9474
##  (between_SS / total_SS =  17.8 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"      
## K-means clustering with 4 clusters of sizes 8, 51, 7, 7
## 
## Cluster means:
##   n.tot.V1419 n.esp.V1419 n.ext.V1419 n.ue27-esp.V1419 n.ue27.V1419  hotel2019
## 1  1.23957572  1.08950716  0.24035468       -0.6209154   0.98069790 -0.4784579
## 2 -0.04563199 -0.02707409 -0.06907618        0.1873014  -0.05727516 -0.1827388
## 3 -0.66920339 -0.82630691  0.17123693       -0.1984694  -0.36271925  2.1417366
## 4 -0.41499289 -0.22159003  0.05734132       -0.4565375  -0.34078789 -0.2635446
##   alq.num.V1519 alq.pmq.V1519 alq.pm.V1519 tot_ann.V1519   ent.V1519 priv.V1519
## 1    0.71366658    -2.3061759   -2.1966484    -0.6635060 -0.75755055 -0.3832961
## 2   -0.02938348     0.2861754    0.2243006    -0.1153780 -0.03271039 -0.1163327
## 3    0.11159404     0.2314797    0.4421492    -0.4237561 -0.20579872 -0.4287729
## 4   -0.71313338     0.3191579    0.4341162     2.0226599  1.30988935  1.7143921
##   shared.V1519 pmedio.V1519 pm_ent.V1519 pm_priv.V1519 pm_sha.V1519   RFD.2017
## 1   -0.2878456    0.4343834  -0.08289918    0.84956996    0.4125156 -0.6197933
## 2   -0.2287952   -0.2547431  -0.06826802   -0.09361520   -0.1521694  0.1461607
## 3    2.2410919    1.6667363   0.53149188   -0.09136117    0.9827600  0.4850056
## 4   -0.2451888   -0.3071888   0.06063136   -0.19752236   -0.3455433 -0.8415559
##   tot.comp.V1519 nou.comp.V1519 prot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1     0.01883324    -0.17249087      0.06331251     -0.04032963   -1.06690132
## 2    -0.12843777     0.04049994     -0.07432982     -0.12609617   -0.05797939
## 3    -0.59885654     0.02313418      0.17236823     -0.55189354    0.06955395
## 4     1.51309371    -0.12107276      0.29682046      1.51668524    1.57218314
##   nou.eur.V1519 usat.eur.V1519 tot.eurm2.V1519 nou.eurm2.V1519 usat.eurm2.V1519
## 1    -0.1122872    -0.97509979     -1.15477806    -0.028718659        0.9360855
## 2    -0.1823376    -0.05387115      0.02796767    -0.127600059       -0.1148548
## 3     1.5957782    -0.15106385      0.30787397     0.970720900       -0.1035420
## 4    -0.1389900     1.65795342      0.80810794    -0.008242003       -0.1294710
## 
## Clustering vector:
##                                     el Raval 
##                                            3 
##                               el Barri Gòtic 
##                                            3 
##                               la Barceloneta 
##                                            2 
##        Sant Pere, Santa Caterina i la Ribera 
##                                            3 
##                                el Fort Pienc 
##                                            3 
##                           la Sagrada Família 
##                                            2 
##                       la Dreta de l'Eixample 
##                                            3 
##              l'Antiga Esquerra de l'Eixample 
##                                            3 
##               la Nova Esquerra de l'Eixample 
##                                            2 
##                                  Sant Antoni 
##                                            2 
##                                 el Poble Sec 
##                                            4 
##                   la Marina del Prat Vermell 
##                                            1 
##                            la Marina de Port 
##                                            2 
##                        la Font de la Guatlla 
##                                            2 
##                                  Hostafrancs 
##                                            2 
##                                   la Bordeta 
##                                            2 
##                                Sants - Badal 
##                                            2 
##                                        Sants 
##                                            2 
##                                    les Corts 
##                                            2 
##                   la Maternitat i Sant Ramon 
##                                            2 
##                                    Pedralbes 
##                                            2 
##        Vallvidrera, el Tibidabo i les Planes 
##                                            1 
##                                       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 
##                                            3 
##           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 
##                                            4 
##                                 la Teixonera 
##                                            2 
##                     Sant Genís dels Agudells 
##                                            4 
##                                      Montbau 
##                                            2 
##                             la Vall d'Hebron 
##                                            2 
##                                     la Clota 
##                                            1 
##                                        Horta 
##                                            2 
##                Vilapicina i la Torre Llobeta 
##                                            2 
##                                        Porta 
##                                            2 
##                          el Turó de la Peira 
##                                            2 
##                                  Can Peguera 
##                                            1 
##                                 la Guineueta 
##                                            2 
##                                    Canyelles 
##                                            1 
##                                 les Roquetes 
##                                            4 
##                                       Verdun 
##                                            2 
##                               la Prosperitat 
##                                            2 
##                             la Trinitat Nova 
##                                            4 
##                                   Torre Baró 
##                                            1 
##                             Ciutat Meridiana 
##                                            4 
##                                     Vallbona 
##                                            1 
##                            la Trinitat Vella 
##                                            4 
##                                Baró de Viver 
##                                            1 
##                                el Bon Pastor 
##                                            2 
##                                  Sant Andreu 
##                                            2 
##                                   la Sagrera 
##                                            2 
##                     el Congrés i els Indians 
##                                            2 
##                                        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 
##                                            2 
## 
## Within cluster sum of squares by cluster:
## [1] 447.8749 702.7602 141.0299 227.0885
##  (between_SS / total_SS =  24.7 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"      
## K-means clustering with 5 clusters of sizes 7, 7, 1, 7, 51
## 
## Cluster means:
##   n.tot.V1419 n.esp.V1419 n.ext.V1419 n.ue27-esp.V1419 n.ue27.V1419  hotel2019
## 1 -0.41499289 -0.22159003  0.05734132       -0.4565375  -0.34078789 -0.2635446
## 2  0.74931615  0.23398631  0.55456635       -0.9234231   0.05215582 -0.4870544
## 3  4.67139266  7.07815309 -1.95912704        1.4966383   7.48049249 -0.4182822
## 4 -0.66920339 -0.82630691  0.17123693       -0.1984694  -0.36271925  2.1417366
## 5 -0.04563199 -0.02707409 -0.06907618        0.1873014  -0.05727516 -0.1827388
##   alq.num.V1519 alq.pmq.V1519 alq.pm.V1519 tot_ann.V1519   ent.V1519 priv.V1519
## 1   -0.71313338     0.3191579    0.4341162     2.0226599  1.30988935  1.7143921
## 2    0.86718244    -2.3121778   -2.1949623    -0.5684428 -0.62397322 -0.2856289
## 3   -0.36094450    -2.2641626   -2.2084508    -1.3289490 -1.69259185 -1.0669666
## 4    0.11159404     0.2314797    0.4421492    -0.4237561 -0.20579872 -0.4287729
## 5   -0.02938348     0.2861754    0.2243006    -0.1153780 -0.03271039 -0.1163327
##   shared.V1519 pmedio.V1519 pm_ent.V1519 pm_priv.V1519 pm_sha.V1519    RFD.2017
## 1   -0.2451888   -0.3071888   0.06063136   -0.19752236   -0.3455433 -0.84155587
## 2   -0.2939394    0.5604926   0.15597456    1.01502050    0.5208098 -0.70776247
## 3   -0.2451888   -0.4483805  -1.75501535   -0.30858382   -0.3455433 -0.00400922
## 4    2.2410919    1.6667363   0.53149188   -0.09136117    0.9827600  0.48500564
## 5   -0.2287952   -0.2547431  -0.06826802   -0.09361520   -0.1521694  0.14616075
##   tot.comp.V1519 nou.comp.V1519 prot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1      1.5130937    -0.12107276      0.29682046       1.5166852    1.57218314
## 2      0.1399880    -0.17249087      0.03550343       0.0716077   -1.25017261
## 3     -0.8292500    -0.17249087      0.25797604      -0.8238909    0.21599771
## 4     -0.5988565     0.02313418      0.17236823      -0.5518935    0.06955395
## 5     -0.1284378     0.04049994     -0.07432982      -0.1260962   -0.05797939
##   nou.eur.V1519 usat.eur.V1519 tot.eurm2.V1519 nou.eurm2.V1519 usat.eurm2.V1519
## 1    -0.1389900     1.65795342      0.80810794    -0.008242003       -0.1294710
## 2    -0.2266421    -1.18242731     -1.38316602    -0.203156636        1.0860512
## 3     0.6881970     0.47619289      0.44393762     1.192347179       -0.1136745
## 4     1.5957782    -0.15106385      0.30787397     0.970720900       -0.1035420
## 5    -0.1823376    -0.05387115      0.02796767    -0.127600059       -0.1148548
## 
## Clustering vector:
##                                     el Raval 
##                                            4 
##                               el Barri Gòtic 
##                                            4 
##                               la Barceloneta 
##                                            5 
##        Sant Pere, Santa Caterina i la Ribera 
##                                            4 
##                                el Fort Pienc 
##                                            4 
##                           la Sagrada Família 
##                                            5 
##                       la Dreta de l'Eixample 
##                                            4 
##              l'Antiga Esquerra de l'Eixample 
##                                            4 
##               la Nova Esquerra de l'Eixample 
##                                            5 
##                                  Sant Antoni 
##                                            5 
##                                 el Poble Sec 
##                                            1 
##                   la Marina del Prat Vermell 
##                                            2 
##                            la Marina de Port 
##                                            5 
##                        la Font de la Guatlla 
##                                            5 
##                                  Hostafrancs 
##                                            5 
##                                   la Bordeta 
##                                            5 
##                                Sants - Badal 
##                                            5 
##                                        Sants 
##                                            5 
##                                    les Corts 
##                                            5 
##                   la Maternitat i Sant Ramon 
##                                            5 
##                                    Pedralbes 
##                                            5 
##        Vallvidrera, el Tibidabo i les Planes 
##                                            2 
##                                       Sarrià 
##                                            5 
##                              les Tres Torres 
##                                            5 
##                   Sant Gervasi - la Bonanova 
##                                            5 
##                       Sant Gervasi - Galvany 
##                                            5 
##                         el Putxet i el Farró 
##                                            5 
##                    Vallcarca i els Penitents 
##                                            5 
##                                      el Coll 
##                                            5 
##                                     la Salut 
##                                            5 
##                            la Vila de Gràcia 
##                                            4 
##           el Camp d'en Grassot i Gràcia Nova 
##                                            5 
##                             el Baix Guinardó 
##                                            5 
##                                     Can Baró 
##                                            5 
##                                  el Guinardó 
##                                            5 
##                         la Font d'en Fargues 
##                                            5 
##                                    el Carmel 
##                                            1 
##                                 la Teixonera 
##                                            5 
##                     Sant Genís dels Agudells 
##                                            1 
##                                      Montbau 
##                                            5 
##                             la Vall d'Hebron 
##                                            5 
##                                     la Clota 
##                                            3 
##                                        Horta 
##                                            5 
##                Vilapicina i la Torre Llobeta 
##                                            5 
##                                        Porta 
##                                            5 
##                          el Turó de la Peira 
##                                            5 
##                                  Can Peguera 
##                                            2 
##                                 la Guineueta 
##                                            5 
##                                    Canyelles 
##                                            2 
##                                 les Roquetes 
##                                            1 
##                                       Verdun 
##                                            5 
##                               la Prosperitat 
##                                            5 
##                             la Trinitat Nova 
##                                            1 
##                                   Torre Baró 
##                                            2 
##                             Ciutat Meridiana 
##                                            1 
##                                     Vallbona 
##                                            2 
##                            la Trinitat Vella 
##                                            1 
##                                Baró de Viver 
##                                            2 
##                                el Bon Pastor 
##                                            5 
##                                  Sant Andreu 
##                                            5 
##                                   la Sagrera 
##                                            5 
##                     el Congrés i els Indians 
##                                            5 
##                                        Navas 
##                                            5 
##                   el Camp de l'Arpa del Clot 
##                                            5 
##                                      el Clot 
##                                            5 
##            el Parc i la Llacuna del Poblenou 
##                                            5 
##                la Vila Olímpica del Poblenou 
##                                            5 
##                                  el Poblenou 
##                                            5 
## Diagonal Mar i el Front Marítim del Poblenou 
##                                            5 
##                        el Besòs i el Maresme 
##                                            5 
##                      Provençals del Poblenou 
##                                            5 
##                     Sant Martí de Provençals 
##                                            5 
##                          la Verneda i la Pau 
##                                            5 
## 
## Within cluster sum of squares by cluster:
## [1] 227.0885 311.9432   0.0000 141.0299 702.7602
##  (between_SS / total_SS =  31.4 %)
## 
## 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