用R语言做PCA的具体步骤

Python021

用R语言做PCA的具体步骤,第1张

#导入你的矩阵,我的矩阵是包含列名称和行名称的

exp = read.table('exp.txt', header = TRUE ,sep = '\t' )

require(graphics)#调用PCA画图的包

#做PCA时,不要行名称所以从第二列开始

PCA1 = princomp(exp[,2:(dim(exp)[2]-1)] )

summary(PCA1)

pc = loadings(PCA1)

Comp.1 = PCA1$loadings[,1]

Comp.2 = PCA1$loadings[,2]

plot(Comp.1,Comp.2 ,col = c('red','red','red','green','green','green','blue','blue','blue'))

#此数据分为三组,所以我画了不同的颜色。

#这是做PCA以及画图的简单流程,供参考!

使用R语言为PCA散点图加置信区间的方法,我知道的有三种,分别是使用ggplot2,ggord,ggfortify三个包去绘制。后面两个R包是基于ggplot2的快捷返方法。

现在拿一组数据集为例,使用先R中的prcomp()基础函数完成主成分分析