同样都是鸾尾花iris数据,为什么PCA图相反?

PCA简介

主成分分析(principle component analysis)是一种线性降维方法。它利用正交变换对一系列可能相关的变量的观测值进行线性变换,从而投影为一系列线性不相关变量的值,这些不相关变量称为主成分(Principal Components)。PCA是一种对数据进行简化分析的技术,可以有效地找出数据中最“主要”的元素和结构,去除噪音和冗余,将原有的复杂数据降维,揭示隐藏在复杂数据背后的简单结构。

网络上的两种不同结果

使用鸾尾花(iris)数据进行PCA分析时,相同的数据,在网上会有两种不同的结果图。

仔细看会发现,这两个图的X轴是一样的,Y轴反了。

这是怎么回事,到底哪个是对的?

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)。问题就出在这里。

经过检索,在stackexchange上有人回答了这个问题。

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.

也就是说不同的软件包/函数,包括:prcomp()princomp() [R内置stat]

PCA() [FactoMineR]dudi.pca() [ade4]epPCA() [ExPosition]ggbiplot[ggbiplot]等,它们的结果会依赖于代码、平台(linuxwindowsmac),及输入数据的微小变化,产生几乎一样但是翻转的(flippedPCA结果。

感兴趣的小伙伴可以带入代码试试看。

微生信助力高分文章,用户175000,谷歌学术3200

  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值