r - 如何在 R 中将新向量绘制到 PCA 空间上

标签 r machine-learning data-mining pca

我是 R 中主成分分析的新手,我的问题很幼稚。我已经使用 R 中的函数“prcomp”对矩阵 (A) 进行了 PCA。现在我想将向量绘制到 A 的 PC1 和 PC2 的 PCA 空间上。如何绘制向量?

最佳答案

使用双标图(红色箭头是原始空间中的尺寸):

a <- princomp(iris[1:4])
biplot(a, cex=0.5)

enter image description here

您也可以自己进行到 PCA 空间的投影,如下所示:

library(ggplot2)
data <- iris[1:4]
labels <- iris[,5]
res <- princomp(data)
res.proj <- as.matrix(data) %*% res$loadings[,1:2]
ggplot(as.data.frame(res.proj), aes(Comp.1, Comp.2, col=labels)) + geom_point()

使用 prcomp 的相同绘图(数值更稳定):

data <- iris[1:4]
labels <- iris[,5]
res <- prcomp(data)
res.proj <- as.matrix(data) %*% res$rotation[,1:2]
ggplot(as.data.frame(res.proj), aes(PC1, PC2, col=labels)) + geom_point()

enter image description here

爱好者 ggbiplot:

library(ggbiplot)
g <- ggbiplot(res, obs.scale = 1, var.scale = 1, 
              groups = labels, ellipse = TRUE, 
              circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', 
               legend.position = 'top')
print(g)

enter image description here

关于r - 如何在 R 中将新向量绘制到 PCA 空间上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39572412/

相关文章:

data-mining - 如何在 WEKA 中设置参数以平衡数据与 SMOTE 过滤器?

在安装了 64 位 R 的 64 位操作系统上运行 32 位 RStudio

r - 如何在 R 中对 Elasticsearch 执行 GET 请求

找不到JAVA renjin函数

validation - 训练数据的分布与测试/预测的分布

python - RNN 参数没有更新?

r - 合并R中的半重复行

Matlab SVM自定义核函数

java - 根据单词出现频率查找推特中的热门话题

algorithm - 轨迹聚类 : Which Clustering Method?