# 同样都是鸾尾花iris数据，为什么PCA图相反？

PCA简介

PCA绘图代码1

library("FactoMineR")

library("factoextra")

data(iris)

iris.pca <- PCA(iris[,-5], graph = F)

fviz_pca_ind(iris.pca,

geom.ind = "point", # show points only (nbut not "text")

col.ind = iris$Species, # color by groups palette = c("#00AFBB", "#E7B800", "#FC4E07"), addEllipses = TRUE, # Concentration ellipses legend.title = "Groups") 绘图代码2 library(ggplot2) data(iris) iris.pca <- prcomp(iris[,-5], scale=T) df_pcs <-data.frame(iris.pca$x, Species = iris\$Species)

ggplot(df_pcs,aes(x=PC1,y=PC2,color = Species))+ geom_point()+stat_ellipse(level = 0.95, show.legend = F)

R中运行后，出的图确实是相反的。经过对比，发现计算PCA时用的函数不一样，一个是iris.pca <- PCA(iris[,-5], graph = F)（默认scale）；一个是iris.pca <- prcomp(iris[,-5], scale=T)。问题就出在这里。

A PCA decomposition maps the original variables into new dimensions which capture the highest amount of variability. Note that the directionality of these dimensions is completely irrelevant - given a dimension that captures some amount of variability,

the negation of that dimension also captures the exact same amount of variability.

Because of this, the positive/negative direction of a PCA dimension may be arbitrarily chosen. Different software packages may produce different results depending on how they are coded, and slight variations in the input data could also result in a near-identical but flipped PCA plot.

https://blog.sciencenet.cn/blog-707141-1392394.html

## 全部精选博文导读

GMT+8, 2023-12-7 15:28