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",
"restVAR1614"
)
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.849734 5.536687 0.6869531 0.01600527
## [2,] 4.786400 5.452523 0.6661235 0.01576850
## [3,] 4.731064 5.392758 0.6616941 0.01427925
## [4,] 4.679564 5.344780 0.6652159 0.01381448
## [5,] 4.639038 5.303633 0.6645945 0.01425203
## [6,] 4.590349 5.266686 0.6763378 0.01506708
## [7,] 4.550729 5.232733 0.6820042 0.01500276
## [8,] 4.514759 5.200427 0.6856677 0.01504755
## [9,] 4.480175 5.170212 0.6900375 0.01488368
## [10,] 4.445728 5.141231 0.6955038 0.01468642
#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 23, 8, 42
##
## Cluster means:
## n.tot.V1419 n.esp.V1419 n.ext.V1419 n.ue27-esp.V1419 n.ue27.V1419 hotel2019
## 1 -0.2383298 -0.1215215 0.1834381 -0.04635204 -0.23848901 -0.3742922
## 2 1.2395757 1.0895072 0.2403547 -0.62091538 0.98069790 -0.4784579
## 3 -0.1055957 -0.1409777 -0.1462360 0.14365286 -0.05619847 0.2961044
## alq.num.V1519 alq.pmq.V1519 alq.pm.V1519 tot_ann.V1519 ent.V1519 priv.V1519
## 1 -0.4429013 0.3202191 0.2896854 0.8531555 0.5765993 0.6611453
## 2 0.7136666 -2.3061759 -2.1966484 -0.6635060 -0.7575505 -0.3832961
## 3 0.1066047 0.2639135 0.2597720 -0.3408221 -0.1714614 -0.2890470
## shared.V1519 pmedio.V1519 pm_ent.V1519 pm_priv.V1519 pm_sha.V1519 RFD.2017
## 1 -0.2451888 -0.3716256 -0.29295627 -0.16807740 -0.3455433 -0.6208113
## 2 -0.2878456 0.4343834 -0.08289918 0.84956996 0.4125156 -0.6197933
## 3 0.1890978 0.1207695 0.17621875 -0.06978047 0.1106517 0.4580240
## tot.comp.V1519 nou.comp.V1519 prot.comp.V1519 usat.comp.V1519 tot.eur.V1519
## 1 0.84448209 -0.1397627 -0.26757519 0.72554692 0.6480605
## 2 0.01883324 -0.1724909 0.06331251 -0.04032963 -1.0669013
## 3 -0.46604176 0.1093921 0.13446975 -0.38964148 -0.1516710
## nou.eur.V1519 usat.eur.V1519 tot.eurm2.V1519 nou.eurm2.V1519 usat.eurm2.V1519
## 1 -0.1550439 0.7146761 0.44320278 -0.08532333 -0.1275224
## 2 -0.1122872 -0.9750998 -1.15477806 -0.02871866 0.9360855
## 3 0.1062930 -0.2056369 -0.02274856 0.05219490 -0.1084683
## restVAR1614
## 1 0.001862284
## 2 0.878266589
## 3 -0.168308696
##
## Clustering vector:
## el Raval
## 3
## el Barri Gòtic
## 3
## la Barceloneta
## 3
## Sant Pere, Santa Caterina i la Ribera
## 3
## el Fort Pienc
## 3
## la Sagrada Família
## 3
## la Dreta de l'Eixample
## 3
## l'Antiga Esquerra de l'Eixample
## 3
## la Nova Esquerra de l'Eixample
## 3
## Sant Antoni
## 3
## el Poble Sec
## 1
## la Marina del Prat Vermell
## 2
## la Marina de Port
## 1
## la Font de la Guatlla
## 1
## Hostafrancs
## 1
## la Bordeta
## 1
## Sants - Badal
## 1
## 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
## 3
## 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
## 1
## Sant Genís dels Agudells
## 1
## Montbau
## 1
## la Vall d'Hebron
## 3
## la Clota
## 2
## Horta
## 1
## Vilapicina i la Torre Llobeta
## 1
## Porta
## 1
## el Turó de la Peira
## 1
## Can Peguera
## 2
## la Guineueta
## 3
## Canyelles
## 2
## les Roquetes
## 1
## Verdun
## 1
## la Prosperitat
## 3
## 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
## 3
## la Sagrera
## 3
## el Congrés i els Indians
## 1
## 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
## 1
## la Verneda i la Pau
## 1
##
## Within cluster sum of squares by cluster:
## [1] 455.0575 502.3583 763.6713
## (between_SS / total_SS = 17.6 %)
##
## 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
## restVAR1614
## 1 0.87826659
## 2 -0.08462124
## 3 -0.31620793
## 4 -0.07099914
##
## 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] 502.3583 712.5038 141.1539 227.4662
## (between_SS / total_SS = 24.2 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
## K-means clustering with 5 clusters of sizes 7, 1, 7, 7, 51
##
## Cluster means:
## n.tot.V1419 n.esp.V1419 n.ext.V1419 n.ue27-esp.V1419 n.ue27.V1419 hotel2019
## 1 0.74931615 0.23398631 0.55456635 -0.9234231 0.05215582 -0.4870544
## 2 4.67139266 7.07815309 -1.95912704 1.4966383 7.48049249 -0.4182822
## 3 -0.41499289 -0.22159003 0.05734132 -0.4565375 -0.34078789 -0.2635446
## 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.86718244 -2.3121778 -2.1949623 -0.5684428 -0.62397322 -0.2856289
## 2 -0.36094450 -2.2641626 -2.2084508 -1.3289490 -1.69259185 -1.0669666
## 3 -0.71313338 0.3191579 0.4341162 2.0226599 1.30988935 1.7143921
## 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.2939394 0.5604926 0.15597456 1.01502050 0.5208098 -0.70776247
## 2 -0.2451888 -0.4483805 -1.75501535 -0.30858382 -0.3455433 -0.00400922
## 3 -0.2451888 -0.3071888 0.06063136 -0.19752236 -0.3455433 -0.84155587
## 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 0.1399880 -0.17249087 0.03550343 0.0716077 -1.25017261
## 2 -0.8292500 -0.17249087 0.25797604 -0.8238909 0.21599771
## 3 1.5130937 -0.12107276 0.29682046 1.5166852 1.57218314
## 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.2266421 -1.18242731 -1.38316602 -0.203156636 1.0860512
## 2 0.6881970 0.47619289 0.44393762 1.192347179 -0.1136745
## 3 -0.1389900 1.65795342 0.80810794 -0.008242003 -0.1294710
## 4 1.5957782 -0.15106385 0.30787397 0.970720900 -0.1035420
## 5 -0.1823376 -0.05387115 0.02796767 -0.127600059 -0.1148548
## restVAR1614
## 1 1.03660607
## 2 -0.23010977
## 3 -0.07099914
## 4 -0.31620793
## 5 -0.08462124
##
## 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
## 3
## la Marina del Prat Vermell
## 1
## 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
## 1
## 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
## 3
## la Teixonera
## 5
## Sant Genís dels Agudells
## 3
## Montbau
## 5
## la Vall d'Hebron
## 5
## la Clota
## 2
## Horta
## 5
## Vilapicina i la Torre Llobeta
## 5
## Porta
## 5
## el Turó de la Peira
## 5
## Can Peguera
## 1
## la Guineueta
## 5
## Canyelles
## 1
## les Roquetes
## 3
## Verdun
## 5
## la Prosperitat
## 5
## la Trinitat Nova
## 3
## Torre Baró
## 1
## Ciutat Meridiana
## 3
## Vallbona
## 1
## la Trinitat Vella
## 3
## Baró de Viver
## 1
## 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] 365.0226 0.0000 227.4662 141.1539 712.5038
## (between_SS / total_SS = 30.7 %)
##
## 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