作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将用于 pca 的变量添加到 clusplot
图中作为箭头。我不确定是否已实现某种方法(我在文档中找不到任何内容)。
我制作了一个如下所示的簇图:
使用 princomp 包,我可以在类似的表示空间中独立绘制观察结果,并将变量(列)作为箭头:
有没有办法通过在同一个图表上显示 pca 的簇和变量来同时完成这两件事?
最佳答案
我今天想做和 OP 一样的事情,最终将 clusplot 和 biplot 中的片段放在一起。如果您想做同样的事情,这是可能有用的结果:
clusplot2 <- function(dat, clustering, ...) {
clusplot(dat, clustering, ...)
## this is from clusplot.default
pca <- princomp(dat, scores = TRUE, cor = (ncol(dat) != 2))
## this is (adapted) from biplot.princomp
directions <- t(t(pca$loadings[, 1:2]) * pca$sdev[1:2]) * sqrt(pca$n.obs)
## all below is (adapted) from biplot.default
unsigned.range <- function(x) c(-abs(min(x, na.rm = TRUE)),
abs(max(x, na.rm = TRUE)))
x <- predict(pca)[, 1:2]
y <- directions
rangx1 <- unsigned.range(x[, 1L])
rangx2 <- unsigned.range(x[, 2L])
rangy1 <- unsigned.range(y[, 1L])
rangy2 <- unsigned.range(y[, 2L])
xlim <- ylim <- rangx1 <- rangx2 <- range(rangx1, rangx2)
ratio <- max(rangy1/rangx1, rangy2/rangx2)
par(new = T)
col <- par("col")
if (!is.numeric(col))
col <- match(col, palette(), nomatch = 1L)
col <- c(col, col + 1L)
cex <- rep(par("cex"), 2)
plot(y, axes = FALSE, type = "n", xlim = xlim * ratio, ylim = ylim *
ratio, xlab = "", ylab = "", col = col[1L])
axis(3, col = col[2L])
axis(4, col = col[2L])
box(col = col[1L])
text(y, labels = names(dat), cex = cex[2L], col = col[2L])
arrows(0, 0, y[, 1L] * 0.8, y[, 2L] * 0.8, col = col[2L],
length = 0.1)
}
############################################################
library(cluster)
dat <- iris[, 1:4]
clus <- pam(dat, k = 3)
clusplot2(dat, clus$clustering, main = "Test")
当然还有很大的改进空间(因为这只是复制在一起),但我认为任何人都可以根据需要轻松调整它。
如果您想知道为什么箭头(载荷 * sdev)以 0.8 * sqrt(n) 缩放:我完全不知道。我会绘制loads * sdev,它应该类似于主成分和变量之间的相关性,但这就是biplot
的做法。
无论如何,这应该产生与 biplot.princomp 相同的箭头,并使用与 clusplot 相同的 pca,这是我的主要目标。
关于r - clusplot - 显示变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29770547/
我想将用于 pca 的变量添加到 clusplot 图中作为箭头。我不确定是否已实现某种方法(我在文档中找不到任何内容)。 我制作了一个如下所示的簇图: 使用 princomp 包,我可以在类似的表示
我正在尝试绘制 GIS 坐标,特别是东向和北向相似的英国国家网格坐标:194630000 562220000 我可以使用 Cluster 库中的 clusplot 绘制这些图:clusplot (df
我是一名优秀的程序员,十分优秀!