CAH file
This commit is contained in:
parent
c69517dc6f
commit
df54cf9961
1 changed files with 54 additions and 0 deletions
54
03_CAH_clust.R
Normal file
54
03_CAH_clust.R
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
library(data.table)
|
||||
library(mltools)
|
||||
library(dplyr)
|
||||
library(tidyr)
|
||||
library(here)
|
||||
library(ggplot2)
|
||||
library(ggdendro)
|
||||
library(factoextra)
|
||||
|
||||
data <- data.frame(read.csv(file.path(here(), "data", "02_preprocessed_data.csv"),
|
||||
header = TRUE
|
||||
), stringsAsFactors = TRUE)
|
||||
|
||||
colnames(data)[5:ncol(data)] <- c(
|
||||
"1AC-OUVERTURE", "1AC-MI",
|
||||
"2A-UEchoix-S1-UC1", "2A-UEchoix-S1-UC2", "2A-UEchoix-S2-UC3",
|
||||
"2A-UEchoix-S2-UC4",
|
||||
"2A-UEchoix-S2-UC5", "2A-UEchoix-S2-UC6",
|
||||
"2A-Projet-S2"
|
||||
)
|
||||
|
||||
data <- data %>%
|
||||
mutate_if(sapply(data, is.character), as.factor)
|
||||
|
||||
selected_cols <- c(
|
||||
"parcours", "domaine2A", "1AC-MI",
|
||||
"2A-UEchoix-S1-UC1", "2A-UEchoix-S1-UC2", "2A-UEchoix-S2-UC4",
|
||||
"2A-UEchoix-S2-UC3", "2A-UEchoix-S2-UC5", "2A-UEchoix-S2-UC6",
|
||||
"2A-Projet-S2", "1AC-OUVERTURE"
|
||||
)
|
||||
|
||||
onehot_data <- one_hot(as.data.table(data), cols = selected_cols, sparsifyNAs = TRUE)
|
||||
|
||||
# Fonctionne bien avec binary
|
||||
dist_eucl <- dist(x = onehot_data[, -c(1, 2, 3)], method = "binary")
|
||||
|
||||
hclust_avg <- hclust(dist_eucl, method = "average")
|
||||
|
||||
dhc <- as.dendrogram(hclust_avg)
|
||||
|
||||
plotdata <- dendro_data(dhc, type = "rectangle")
|
||||
p <- ggplot(segment(plotdata)) +
|
||||
geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
|
||||
coord_flip() +
|
||||
scale_y_reverse(expand = c(0.2, 0))
|
||||
p + theme_dendro()
|
||||
|
||||
fviz_nbclust(onehot_data[, -c(1,2,3)], FUNcluster = hcut, k.max = 30)
|
||||
|
||||
cut_avg <- cutree(hclust_avg, k = 6)
|
||||
names(cut_avg) <- data[["ine"]]
|
||||
table(cut_avg)
|
||||
|
||||
onehot_data[["cluster"]] <- cut_avg
|
||||
Loading…
Add table
Reference in a new issue