【发布时间】:2020-10-12 21:06:22
【问题描述】:
这是我使用的代码:
resources <- read.csv("https://raw.githubusercontent.com/umbertomig/intro-prob-stat-FGV/master/datasets/resources.csv")
res <- subset(resources, select = c("cty_name", "year", "regime",
"oil", "logGDPcp", "illit"))
resNoNA <- na.omit(res)
resNoNAS <- scale(resNoNA[, 3:6])
colMeans(resNoNA[, 3:6])
apply(resNoNA[, 3:6], 2, sd)
cluster2 <- kmeans(resNoNAS, centers = 2)
table(cluster2$cluster)
## this gives standardized answer, which is hard to interpret
cluster2$centers
## better to subset the original data and then compute means
g1 <- resNoNA[cluster2$cluster == 1, ]
colMeans(g1[, 3:6])
g2 <- resNoNA[cluster2$cluster == 2, ]
colMeans(g2[, 3:6])
plot(x = resNoNA$logGDPcp, y = resNoNA$illit, main = "Illiteracy v GDP",
xlab = "GDP per Capita", ylab = "Illiteracy",
col = cluster2$cluster, cex = resNoNA$oil)
但我想让圆圈变小以适应图形的限制
【问题讨论】:
-
关于 SO(尤其是在 R 中)的问题如果是可重复的和独立的,则效果会更好。我的意思是包括尝试的代码(请明确说明非基础包),示例代表性数据(可能通过
dput(head(x))或以编程方式构建数据(例如,data.frame(...)),可能在set.seed(1)之后随机生成),可能是实际输出(带有逐字错误/警告)与预期输出。参考:stackoverflow.com/q/5963269、minimal reproducible example 和 stackoverflow.com/tags/r/info。 (你提供的……谁知道呢。) -
也许这个对你有帮助:stackoverflow.com/questions/2579995/… ?
标签: r plot cluster-analysis k-means scatter-plot