Variables
merged2 <- merged %>%
remove_rownames %>%
column_to_rownames(var="Nom_Barri") %>%
select("n.tot","pc.esp","pc.ue27-esp","pc.20.34",#"var20192014",#"2019-2014","pc.ext",
"n.esp.V1419",#"n.ext.V1419",#"n.tot.V1419","n.ue27.V1419",
"hotel2019",
"alq.mq","alq.num",#"alq.pmq",
"alq.pm.V1519",#"alq.pmq.V1519",
"tot_ann","pmedio",#"pm_priv",
#"pc.shared","pm_sha","pm_hot","pm_ent","pc.ent","pc.priv","pc.hotel",
#"alq.num.V1519","alq.pmq.V1519",
#"tot_ann.V1519",#"ent.V1519","priv.V1519","shared.V1519",
"pmedio.V1519","pm_ent.V1519","pm_priv.V1519",
"RFD.2017",
"tot.comp",#"perc.nou.comp","perc.prot.comp","perc.usat.comp",
"tot.comp.V1519","usat.comp.V1519","tot.eur.V1519","usat.eur.V1519",#"tot.eurm2.V1519",
#"tot.m2","nou.m2","usat.m2",#"prot.m2",
"tot.eur","nou.eur","usat.eur",
#"tot.eurm2","nou.eurm2","usat.eurm2",
#"rest1614",
"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'): 3
## logW E.logW gap SE.sim
## [1,] 4.761082 5.386209 0.6251267 0.02077247
## [2,] 4.635891 5.279529 0.6436373 0.02053227
## [3,] 4.562370 5.208511 0.6461412 0.02024772
## [4,] 4.510979 5.153278 0.6422996 0.01996803
## [5,] 4.450068 5.107563 0.6574948 0.01950477
## [6,] 4.393540 5.066715 0.6731751 0.01955025
## [7,] 4.339079 5.029393 0.6903142 0.01962607
## [8,] 4.290470 4.994308 0.7038380 0.01999656
## [9,] 4.237245 4.960971 0.7237261 0.02000331
## [10,] 4.197485 4.929314 0.7318296 0.01998075
#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 12, 51, 10
##
## Cluster means:
## n.tot pc.esp pc.ue27-esp pc.20.34 n.esp.V1419 hotel2019
## 1 1.1004743 -1.4450855 1.57683618 1.6243319 -0.8661741 1.7074173
## 2 -0.2083623 0.2519242 -0.38591306 -0.2555734 0.1750330 -0.3652459
## 3 -0.2579214 0.4492893 0.07595321 -0.6457740 0.1467406 -0.1861466
## alq.mq alq.num alq.pm.V1519 tot_ann pmedio pmedio.V1519
## 1 0.2496085 1.51283752 0.49852451 1.9786542 0.4344530 1.23133418
## 2 -0.3478757 -0.34786843 -0.08617643 -0.3935369 -0.2286368 -0.28270761
## 3 1.4746360 -0.04127603 -0.15872963 -0.3673471 0.6447040 -0.03579222
## pm_ent.V1519 pm_priv.V1519 RFD.2017 tot.comp tot.comp.V1519
## 1 0.4745285 -0.07929993 0.3017366 0.63323897 -0.5528028
## 2 -0.2497603 0.02754898 -0.4245402 -0.06616577 0.2652772
## 3 0.7043432 -0.04533986 1.8030713 -0.42244136 -0.6895504
## usat.comp.V1519 tot.eur.V1519 usat.eur.V1519 tot.eur nou.eur usat.eur
## 1 -0.4826779 0.41130110 0.36261110 0.269840 0.5362879 0.2464714
## 2 0.2229695 0.02050245 0.02566715 -0.432278 -0.2412082 -0.4378555
## 3 -0.5579308 -0.59812381 -0.56603577 1.880810 0.5866162 1.9372973
## nou.eurm2.V1519 usat.eurm2.V1519
## 1 0.45346153 -0.1056187
## 2 -0.03046524 -0.1235567
## 3 -0.38878109 0.7568817
##
## Clustering vector:
## el Raval
## 1
## el Barri Gòtic
## 1
## la Barceloneta
## 1
## Sant Pere, Santa Caterina i la Ribera
## 1
## el Fort Pienc
## 1
## la Sagrada Família
## 1
## la Dreta de l'Eixample
## 1
## l'Antiga Esquerra de l'Eixample
## 1
## la Nova Esquerra de l'Eixample
## 1
## Sant Antoni
## 1
## el Poble Sec
## 1
## la Marina del Prat Vermell
## 2
## 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
## 3
## Vallvidrera, el Tibidabo i les Planes
## 3
## 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
## 2
## el Coll
## 2
## la Salut
## 2
## la Vila de Gràcia
## 1
## 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
## 2
## la Teixonera
## 2
## Sant Genís dels Agudells
## 2
## Montbau
## 2
## la Vall d'Hebron
## 2
## la Clota
## 2
## Horta
## 2
## Vilapicina i la Torre Llobeta
## 2
## Porta
## 2
## el Turó de la Peira
## 2
## Can Peguera
## 2
## la Guineueta
## 2
## Canyelles
## 2
## les Roquetes
## 2
## Verdun
## 2
## la Prosperitat
## 2
## la Trinitat Nova
## 2
## Torre Baró
## 2
## Ciutat Meridiana
## 2
## Vallbona
## 2
## la Trinitat Vella
## 2
## Baró de Viver
## 3
## 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
## 3
## el Poblenou
## 2
## Diagonal Mar i el Front Marítim del Poblenou
## 3
## 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] 272.4801 750.3547 240.5045
## (between_SS / total_SS = 29.8 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
## K-means clustering with 4 clusters of sizes 50, 1, 12, 10
##
## Cluster means:
## n.tot pc.esp pc.ue27-esp pc.20.34 n.esp.V1419 hotel2019
## 1 -0.1835085 0.2385530 -0.37700753 -0.2728861 0.1615136 -0.3621045
## 2 -1.4510511 0.9204820 -0.83118972 0.6100638 0.8510014 -0.5223148
## 3 1.1004743 -1.4450855 1.57683618 1.6243319 -0.8661741 1.7074173
## 4 -0.2579214 0.4492893 0.07595321 -0.6457740 0.1467406 -0.1861466
## alq.mq alq.num alq.pm.V1519 tot_ann pmedio pmedio.V1519
## 1 -0.35590204 -0.33442360 -0.04373094 -0.3894281 -0.3516123 -0.27939415
## 2 0.05344032 -1.02010997 -2.20845077 -0.5989730 5.9201381 -0.44838053
## 3 0.24960847 1.51283752 0.49852451 1.9786542 0.4344530 1.23133418
## 4 1.47463599 -0.04127603 -0.15872963 -0.3673471 0.6447040 -0.03579222
## pm_ent.V1519 pm_priv.V1519 RFD.2017 tot.comp tot.comp.V1519
## 1 -0.3075461 -0.13849602 -0.4078980 -0.04360935 0.2864085
## 2 2.6395313 8.32979891 -1.2566499 -1.19398646 -0.7912879
## 3 0.4745285 -0.07929993 0.3017366 0.63323897 -0.5528028
## 4 0.7043432 -0.04533986 1.8030713 -0.42244136 -0.6895504
## usat.comp.V1519 tot.eur.V1519 usat.eur.V1519 tot.eur nou.eur usat.eur
## 1 0.2361118 0.01588942 0.02080585 -0.4168594 -0.2341810 -0.4216391
## 2 -0.4341460 0.25115411 0.26873179 -1.2032067 -0.5925692 -1.2486762
## 3 -0.4826779 0.41130110 0.36261110 0.2698400 0.5362879 0.2464714
## 4 -0.5579308 -0.59812381 -0.56603577 1.8808098 0.5866162 1.9372973
## nou.eurm2.V1519 usat.eurm2.V1519
## 1 -0.02701142 -0.1231102
## 2 -0.20315664 -0.1458842
## 3 0.45346153 -0.1056187
## 4 -0.38878109 0.7568817
##
## 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
## 3
## 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
## 1
## les Corts
## 1
## la Maternitat i Sant Ramon
## 1
## Pedralbes
## 4
## Vallvidrera, el Tibidabo i les Planes
## 4
## 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
## 1
## el Coll
## 1
## la Salut
## 1
## la Vila de Gràcia
## 3
## el Camp d'en Grassot i Gràcia Nova
## 1
## el Baix Guinardó
## 1
## Can Baró
## 1
## el Guinardó
## 1
## la Font d'en Fargues
## 1
## el Carmel
## 1
## la Teixonera
## 1
## Sant Genís dels Agudells
## 1
## Montbau
## 1
## la Vall d'Hebron
## 1
## la Clota
## 1
## Horta
## 1
## Vilapicina i la Torre Llobeta
## 1
## Porta
## 1
## el Turó de la Peira
## 1
## Can Peguera
## 1
## la Guineueta
## 1
## Canyelles
## 1
## les Roquetes
## 1
## Verdun
## 1
## la Prosperitat
## 1
## la Trinitat Nova
## 1
## Torre Baró
## 1
## Ciutat Meridiana
## 1
## Vallbona
## 1
## la Trinitat Vella
## 1
## Baró de Viver
## 4
## el Bon Pastor
## 1
## Sant Andreu
## 1
## la Sagrera
## 1
## el Congrés i els Indians
## 1
## Navas
## 1
## el Camp de l'Arpa del Clot
## 1
## el Clot
## 1
## el Parc i la Llacuna del Poblenou
## 1
## la Vila Olímpica del Poblenou
## 4
## el Poblenou
## 1
## Diagonal Mar i el Front Marítim del Poblenou
## 4
## el Besòs i el Maresme
## 1
## Provençals del Poblenou
## 1
## Sant Martí de Provençals
## 1
## la Verneda i la Pau
## 1
##
## Within cluster sum of squares by cluster:
## [1] 619.0601 0.0000 272.4801 240.5045
## (between_SS / total_SS = 37.1 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
## K-means clustering with 5 clusters of sizes 42, 1, 9, 11, 10
##
## Cluster means:
## n.tot pc.esp pc.ue27-esp pc.20.34 n.esp.V1419 hotel2019
## 1 0.00101016 0.1174256 -0.3216376 -0.1796290 -0.02360567 -0.33717363
## 2 -1.45105110 0.9204820 -0.8311897 0.6100638 0.85100145 -0.52231480
## 3 0.07434730 0.3242974 0.1696570 -0.5007147 0.07253081 -0.04181549
## 4 1.13864551 -1.5181943 1.6604060 1.7162454 -0.89498136 1.81313440
## 5 -1.17856020 0.7929103 -0.5451409 -0.7437913 0.93324544 -0.48845320
## alq.mq alq.num alq.pm.V1519 tot_ann pmedio pmedio.V1519
## 1 -0.28736370 -0.2174290 0.2967905 -0.3464569 -0.3332900 -0.3312790
## 2 0.05344032 -1.0201100 -2.2084508 -0.5989730 5.9201381 -0.4483805
## 3 1.42546719 0.2524719 0.3196157 -0.2101759 0.7939070 -0.3262329
## 4 0.23686038 1.5831839 0.5309944 2.1038076 0.4606381 1.3381717
## 5 -0.34188338 -0.9535141 -1.8974231 -0.6100135 -0.4134139 0.2578306
## pm_ent.V1519 pm_priv.V1519 RFD.2017 tot.comp tot.comp.V1519
## 1 -0.2860784 -0.14246485 -0.3614606 0.1504818 0.32608767
## 2 2.6395313 8.32979891 -1.2566499 -1.1939865 -0.79128787
## 3 0.8575544 -0.01042271 1.9700380 -0.1809826 -0.80225595
## 4 0.5161363 -0.08301709 0.3018607 0.6566191 -0.53845589
## 5 -0.4019726 -0.13392829 -0.4612816 -1.0720217 0.02389241
## usat.comp.V1519 tot.eur.V1519 usat.eur.V1519 tot.eur nou.eur usat.eur
## 1 0.28889101 0.1847326 0.1831140 -0.3466091 -0.2019817 -0.3564627
## 2 -0.43414598 0.2511541 0.2687318 -1.2032067 -0.5925692 -1.2486762
## 3 -0.62893790 -0.2973329 -0.3674893 2.0002123 1.3416514 1.9703583
## 4 -0.47810729 0.4172551 0.4369984 0.1900407 0.1283538 0.2203175
## 5 -0.07796553 -0.9923733 -0.9459099 -0.4331571 -0.4410956 -0.3936606
## nou.eurm2.V1519 usat.eurm2.V1519
## 1 -0.05762229 -0.12152898
## 2 -0.20315664 -0.14588419
## 3 -0.23389356 -0.09593306
## 4 0.36955298 -0.10457043
## 5 0.06632519 0.72637738
##
## Clustering vector:
## el Raval
## 4
## el Barri Gòtic
## 4
## la Barceloneta
## 4
## 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
## 4
## Sant Antoni
## 4
## el Poble Sec
## 4
## 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
## 1
## les Corts
## 1
## la Maternitat i Sant Ramon
## 1
## Pedralbes
## 3
## Vallvidrera, el Tibidabo i les Planes
## 5
## 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
## 1
## el Coll
## 1
## la Salut
## 1
## la Vila de Gràcia
## 4
## el Camp d'en Grassot i Gràcia Nova
## 1
## el Baix Guinardó
## 1
## Can Baró
## 1
## el Guinardó
## 1
## la Font d'en Fargues
## 5
## el Carmel
## 1
## la Teixonera
## 1
## Sant Genís dels Agudells
## 1
## Montbau
## 1
## la Vall d'Hebron
## 5
## la Clota
## 5
## Horta
## 1
## Vilapicina i la Torre Llobeta
## 1
## Porta
## 1
## el Turó de la Peira
## 1
## Can Peguera
## 5
## la Guineueta
## 5
## Canyelles
## 5
## les Roquetes
## 1
## Verdun
## 1
## la Prosperitat
## 1
## la Trinitat Nova
## 1
## Torre Baró
## 5
## Ciutat Meridiana
## 1
## Vallbona
## 5
## la Trinitat Vella
## 1
## Baró de Viver
## 5
## el Bon Pastor
## 1
## Sant Andreu
## 1
## la Sagrera
## 1
## el Congrés i els Indians
## 1
## Navas
## 1
## el Camp de l'Arpa del Clot
## 1
## el Clot
## 1
## el Parc i la Llacuna del Poblenou
## 1
## la Vila Olímpica del Poblenou
## 3
## el Poblenou
## 1
## Diagonal Mar i el Front Marítim del Poblenou
## 3
## el Besòs i el Maresme
## 1
## Provençals del Poblenou
## 1
## Sant Martí de Provençals
## 1
## la Verneda i la Pau
## 1
##
## Within cluster sum of squares by cluster:
## [1] 395.1169 0.0000 153.8111 238.5878 234.3235
## (between_SS / total_SS = 43.2 %)
##
## 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