Skip to content

Instantly share code, notes, and snippets.

@brunaw
Created July 14, 2020 15:23
Show Gist options
  • Save brunaw/b6649bea8af788b05ddf563deea8c5c8 to your computer and use it in GitHub Desktop.
Save brunaw/b6649bea8af788b05ddf563deea8c5c8 to your computer and use it in GitHub Desktop.
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