N2M
N2M

Reputation: 199

Reduce the dimensions of a dataset after applying pca on it in R

My question is how to use the principal components obtained using R.

Once you get the principal components, how do we use it to reduce the dimensions? I have a data_set containing 6 variables, I need to cluster it using k-means. K-means gives me a scattered plot when I do the clustering on 6 variables. I thought pca could help to reduce the dimensions, and so k-means could produce fruitful results.

I did this to get the principal components:

pca1 <- prcomp(data_set)

Please guide me as to how to proceed further to reduce the dimensionality of the data set.

Upvotes: 0

Views: 1725

Answers (1)

user2776074
user2776074

Reputation:

you can find the values you get from a function if you type for example ?prcomp this is what i used to do using another package:

library("FactoMineR")

pca <- PCA(dataset, scale.unit=TRUE, graph=FALSE)

scores <- data.frame(pca$ind$coord)

library(ggplot2)

ggplot(scores,aes(Dim.1,Dim.2)) + geom_text(label=rownames(scores),colour="red") + geom_hline(yintercept=0) + geom_vline(xintercept=0) + labs(title="Score plot")

you get the plot for the scores according to PC1 and PC2, and the same if you want the loadings plot

loadings <- data.frame(pca$var$coord)

Upvotes: 1

Related Questions