首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >主成分分析FactoMineR图数据

主成分分析FactoMineR图数据
EN

Stack Overflow用户
提问于 2012-04-21 03:49:32
回答 3查看 9.3K关注 0票数 6

我正在运行一个R脚本,使用FactorMineR生成主成分分析的曲线图。

我想输出生成的PCA图的坐标,但我很难找到正确的坐标。我找到了results1$ind$coordresults1$var$coord,但它们看起来都不像默认的绘图。

我找到了http://www.statistik.tuwien.ac.at/public/filz/students/seminar/ws1011/hoffmann_ausarbeitung.pdfhttp://factominer.free.fr/classical-methods/principal-components-analysis.html,但都没有描述由PCA创建的变量的内容

代码语言:javascript
复制
library(FactoMineR)
data1 <- read.table(file=args[1], sep='\t', header=T, row.names=1)
result1 <- PCA(data1,ncp = 4, graph=TRUE) # graphs generated automatically
plot(result1)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-21 05:27:42

我发现$ind$coord[,1]$ind$coord[,2]PCA对象中的前两个pca coord。下面是一个工作示例,其中包括您可能想要对PCA输出执行的其他一些操作……

代码语言:javascript
复制
# Plotting the output of FactoMineR's PCA using ggplot2
#
# load libraries
library(FactoMineR)
library(ggplot2)
library(scales)
library(grid)
library(plyr)
library(gridExtra)
#
# start with a clean slate
rm(list=ls(all=TRUE)) 
#
# load example data
data(decathlon)
#
# compute PCA
res.pca <- PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph = FALSE)
#
# extract some parts for plotting
PC1 <- res.pca$ind$coord[,1]
PC2 <- res.pca$ind$coord[,2]
labs <- rownames(res.pca$ind$coord)
PCs <- data.frame(cbind(PC1,PC2))
rownames(PCs) <- labs
#
# Just showing the individual samples...
ggplot(PCs, aes(PC1,PC2, label=rownames(PCs))) + 
  geom_text() 

代码语言:javascript
复制
# Now get supplementary categorical variables
cPC1 <- res.pca$quali.sup$coor[,1]
cPC2 <- res.pca$quali.sup$coor[,2]
clabs <- rownames(res.pca$quali.sup$coor)
cPCs <- data.frame(cbind(cPC1,cPC2))
rownames(cPCs) <- clabs
colnames(cPCs) <- colnames(PCs)
#
# Put samples and categorical variables (ie. grouping
# of samples) all together
p <- ggplot() + theme(aspect.ratio=1) + theme_bw(base_size = 20) 
# no data so there's nothing to plot...
# add on data 
p <- p + geom_text(data=PCs, aes(x=PC1,y=PC2,label=rownames(PCs)), size=4) 
p <- p + geom_text(data=cPCs, aes(x=cPC1,y=cPC2,label=rownames(cPCs)),size=10)
p # show plot with both layers

代码语言:javascript
复制
# Now extract the variables
#
vPC1 <- res.pca$var$coord[,1]
vPC2 <- res.pca$var$coord[,2]
vlabs <- rownames(res.pca$var$coord)
vPCs <- data.frame(cbind(vPC1,vPC2))
rownames(vPCs) <- vlabs
colnames(vPCs) <- colnames(PCs)
#
# and plot them
#
pv <- ggplot() + theme(aspect.ratio=1) + theme_bw(base_size = 20) 
# no data so there's nothing to plot
# put a faint circle there, as is customary
angle <- seq(-pi, pi, length = 50) 
df <- data.frame(x = sin(angle), y = cos(angle)) 
pv <- pv + geom_path(aes(x, y), data = df, colour="grey70") 
#
# add on arrows and variable labels
pv <- pv + geom_text(data=vPCs, aes(x=vPC1,y=vPC2,label=rownames(vPCs)), size=4) + xlab("PC1") + ylab("PC2")
pv <- pv + geom_segment(data=vPCs, aes(x = 0, y = 0, xend = vPC1*0.9, yend = vPC2*0.9), arrow = arrow(length = unit(1/2, 'picas')), color = "grey30")
pv # show plot 

代码语言:javascript
复制
# Now put them side by side in a single image
#
grid.arrange(p,pv,nrow=1)
# 
# Now they can be saved or exported...

票数 7
EN

Stack Overflow用户

发布于 2012-04-21 13:30:19

在Ben的回答中添加了一些额外的东西。您将注意到,在Ben的响应中的第一个图表中,标签有些重叠。maptools包中的pointLabel()函数尝试查找标签的位置,而不会重叠。它并不完美,但如果需要,您可以调整new数据帧中的位置(见下文)以进行微调。(此外,当您加载地图工具时,您会得到一个关于gpclibPermit()的注释。如果您担心受限制的许可证,可以忽略它)。下面脚本的第一部分是Ben的脚本。

代码语言:javascript
复制
# load libraries
library(FactoMineR)
library(ggplot2)
library(scales)
library(grid)
library(plyr)
library(gridExtra)
#
# start with a clean slate
# rm(list=ls(all=TRUE)) 
#
# load example data
data(decathlon)
#
# compute PCA
res.pca <- PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph = FALSE)
#
# extract some parts for plotting
PC1 <- res.pca$ind$coord[,1]
PC2 <- res.pca$ind$coord[,2]
labs <- rownames(res.pca$ind$coord)
PCs <- data.frame(cbind(PC1,PC2))
rownames(PCs) <- labs 
#

# Now, the code to produce Ben's first chart but with less overlap of the labels.

library(maptools)

PCs$label=rownames(PCs)

# Base plot first for pointLabels() to get locations
plot(PCs$PC1, PCs$PC2, pch = 20, col = "red")
new = pointLabel(PCs$PC1, PCs$PC2, PCs$label, cex = .7)
new = as.data.frame(new)
new$label = PCs$label

# Then plot using ggplot2
(p = ggplot(data = PCs) + 
   geom_hline(yintercept = 0, linetype = 3, colour = "grey20") +
   geom_vline(xintercept = 0, linetype = 3, colour = "grey20") +
   geom_point(aes(PC1, PC2), shape = 20, col = "red") +
   theme_bw())

(p = p +  geom_text(data = new, aes(x, y, label = label), size = 3))

结果是:

票数 1
EN

Stack Overflow用户

发布于 2013-06-10 05:34:40

另一种方法是使用CoreR中的biplot函数或心理包中的biplot.psych。这将把组件和数据放在同一张图上。

对于十项全能数据集,使用心理包中的主体和biplot:

代码语言:javascript
复制
 library(FactoMineR) #needed to get the example data
 library(psych)  #needed for principal 
 data(decathlon)  #the data set
 pc2 <- principal(decathlon[1:10],2) #just the first 10 columns
 biplot(pc2,labels = rownames(decathlon),cex=.5, main="Biplot of Decathlon results") 
 #this is a call to biplot.psych which in turn calls biplot.
 #adjust the cex parameter to change the type size of the labels.

这看起来像这样:

帐单

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10252639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档