ds: 2r, 4r, 5r, 8r

This commit is contained in:
2025-12-11 14:58:04 +03:00
parent 5fd0ff7154
commit ba66cbd0ff
6 changed files with 20203 additions and 4 deletions

View File

@ -95,7 +95,44 @@ plot_kmeans(data, k, log=FALSE)
plot_kmeans(data, klog, log=TRUE)
library(ggdendro)
distance = dist(df, method = "euclidean")
clust = hclust(distance, method = "complete")
plot_hclust = function(df, linkage, k) {
data = df[,c("income", "elec")]
distance = dist(data, method = "euclidean")
clust = hclust(distance, method = linkage)
data$cluster = as.factor(cutree(clust, k = k))
data$state = rownames(df)
print(cutree(clust, k = k))
print(data)
plt = ggplot() +
geom_point(
data = data,
aes(
x = income,
y = elec,
color = cluster
)
) +
geom_text(
data = data,
vjust = 1.5,
size = 2,
aes(
x = income,
y = elec,
label = state
)
)
theme_minimal()
print(plt)
}
plot_hclust(data, "average", 5)
distance = dist(data, method = "euclidean")
clust = hclust(distance, method = "single")
plot(ggdendrogram(clust))
cutree(clust, k = k)
cutree(clust, k = 3)