-
Notifications
You must be signed in to change notification settings - Fork 118
Principal Components Analysis
Dmitry Grapov edited this page Jun 26, 2013
·
5 revisions
###Check out a browser based application for conducting PCA
library(shiny)
runGist("5846650")
Here is an example for doing PCA and plotting the scores in R.
# I will use an example data set from R
data(mtcars)
data<-mtcars
#scale and center (always a good idea)
scaled.data<-scale(data,center=T,scale=T)
#do PCA
pca<-prcomp(data)
#plot PCA Scores
fct<-as.factor(data$gear) #some factor to use for plotting groups
plot(pca$x[,1],pca$x[,2],pch=21,bg=rainbow(nlevels(fct))[fct], xlab="PC 1",ylab="PC 2")
legend("topleft",levels(fct),fill=rainbow(nlevels(fct))) # add legend