2016-09-19 4 views
2

R의 주성분 분석을 처음 접했고 제 질문은 매우 순진합니다. 저는 R에서 'prcomp'함수를 사용하여 행렬 (A)의 PCA를 작성했습니다. 이제는 A의 PC1과 PC2의 PCA 공간에 벡터를 그립니다.이 벡터의 플로팅은 어떻게해야합니까?R의 PCA 공간에 새 벡터를 그릴 방법

답변

1

사용 행렬도 (빨간색 arrrows은 원래 공간의 차원은) 다음과 같이

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 (수치 적으로 더 안정적 임)를 사용한 동일한 플롯 :

,144,

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

+2

그것은 사용하는'prcomp'는 R.에서 선호하는 방법이라고 지적 가치는'princomp'는 S-PLUS와의 호환성을 위해 존재한다. –

+0

수치 안정성이 필요하다면 svd를 사용하기 때문에 대신 prcomp를 사용하십시오. –

관련 문제