Reputation: 853
I am trying to graph a PCA with factor analysis to show how groups of observations are located differently along the resulting dimensions.
x = data.frame(v1=c(10, 20, 5, 26, 2, 30),
v2=c(23, 31, 34, 63, 12, 7),
v3=c(2, 6, 1, 0, 3, 5),
group=c("A", "B", "A", "B", "A", "B"))
result <- PCA(x[1:3])
This results in two graphs: Observations in PCA
What I am trying to do is, instead of having observations 1 through 6 individually located in the first graph, I would like to have group A and group B, consisting of the average position of their component observations (1, 3, 5 for A, and 2, 4, 6 for B).
Thank you very much if you have a solution!
Upvotes: 0
Views: 507
Reputation: 601
You can extract the values and do the mean by groups:
x = data.frame(v1=c(10, 20, 5, 26, 2, 30),
v2=c(23, 31, 34, 63, 12, 7),
v3=c(2, 6, 1, 0, 3, 5),
group=c("A", "B", "A", "B", "A", "B"))
result <- PCA(x[1:3])
values<-as.data.frame(result$ind$coord)
values$group<-x$group
final<-aggregate(. ~ group, values, mean)
plot(final$Dim.1,final$Dim.2,xlim=c(-2.5,2.5),ylim=c(-2.5,2.5))
abline(h = 0, v = 0, col = "gray60")
text(final$Dim.1,final$Dim.2-0.09,labels = final$group)
Upvotes: 1