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

我对R中的主成分分析还是新手,我的提问可能有点简单。我已经使用R中的’prcomp’函数对矩阵A进行了PCA分析。现在我想将一个向量绘制到A的PC1和PC2的PCA空间中。我该如何进行这个向量的绘制呢?


回答:

使用biplot(红色箭头表示原始空间的维度):

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注