# Algorithm and R implementation of sparse PCA

Does anyone know where I can find an algorithm, as well as an R implementation of it, to carry out sparse principal component analysis (PCA)?

• Requests for where to find code might be argued not to be directly on topic. At that link, see the list of topics and the section headed There are certain subjects that will probably get better responses on our sister sites. and see if you can either edit your post to make it more clearly on topic, or whether there might be a better location for the question. – Glen_b Jun 5 '14 at 8:57
• This question appears to be off-topic because it is about a request for code. – Nick Cox Jun 5 '14 at 9:41
• @user603 I agree with you in general that we do not want to send programmers away as a policy. The problem here is that the question explicitly asks just for R code. If it were changed to request an algorithm I think there would be no controversy. – whuber Jun 5 '14 at 13:14
• For the record, the original version was closed after votes by 4 people, 1 being me. It was then revised and re-opened. It's inaccurate therefore to direct disagreement at me personally; I merely voiced the doubt in the comment above. – Nick Cox Jun 6 '14 at 11:04
• @user603 Thanks for your comment; I take no offence; I was just underlining facts. I don't know about "in general": in my experience, most poor questions shouldn't be edited to a different question because a good underlying question cannot be identified. But in this case you did edit the question to an acceptable one. Whether it was the OP's real question remains unclear. A side-effect of the edit is that neither of the two up-voted answers, both posted before your edit, really answers the question as it stands! Oh well. – Nick Cox Jun 6 '14 at 12:47

Another good package is the elasticnet package that Zou and Hastie put out. It has the function spca.
Be careful to select a good value of $\lambda$, the sparsity parameter (or vector of them).
Which function in which package are you interested in? All code is available on CRAN. For instance, if you are thinking about the PMA package (just the first one I found by googling for "CRAN Sparse Principal Components Analysis"), you can find the source code here.