ds: 2r, 4r, 5r, 8r
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user