如何在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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