Created
July 14, 2020 15:23
-
-
Save brunaw/b6649bea8af788b05ddf563deea8c5c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(factoextra) | |
agg_transp <- read_csv("data/aggregated_transport.csv") | |
transp_border <- agg_transp %>% | |
group_by(location) %>% | |
mutate(mean_value = mean_value/max(mean_value)) | |
pca_cases <- transp_border %>% | |
select(date, mean_value, location) %>% | |
ungroup() %>% | |
spread(key = date, value = mean_value, fill = 0) %>% | |
select(-location) %>% | |
as.matrix() | |
# PCA with this matrix | |
pca_cases <- pca_cases[ , which(colSums(pca_cases) > 0 )] | |
pca_m0 <- prcomp(pca_cases, center = TRUE, scale = TRUE) | |
resp <- transp_border %>% | |
group_by(location) %>% slice(1) %>% | |
pull(border_station) | |
fviz_pca_ind(pca_m0, geom.ind = "point", pointshape = 21, | |
pointsize = 2, | |
fill.ind = factor(resp), | |
col.ind = "black", | |
palette = "jco", | |
addEllipses = TRUE, | |
label = "var", | |
col.var = "black", | |
repel = TRUE, | |
legend.title = "Bordering county") + | |
ggtitle("2D PCA-plot") + | |
theme_classic(18) + | |
theme(plot.title = element_text(hjust = 0.5), | |
legend.position = "bottom") | |
# k-means | |
km <- kmeans(pca_cases, centers = 3) | |
clusters <- km$cluster | |
fviz_pca_ind(pca_m0, geom.ind = "point", pointshape = 21, | |
pointsize = 2, | |
fill.ind = factor(clusters), | |
col.ind = "black", | |
palette = "jco", | |
addEllipses = TRUE, | |
label = "var", | |
col.var = "black", | |
repel = TRUE, | |
legend.title = "Bordering county") + | |
ggtitle("2D PCA-plot") + | |
theme_classic(18) + | |
theme(plot.title = element_text(hjust = 0.5), | |
legend.position = "bottom") | |
#----------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment