I compared ?prcomp
and ?princomp
and found something about Q-mode and R-mode principal component analysis (PCA). But honestly – I don't understand it. Can anybody explain the difference and maybe even explain when to apply which?
-
2$\begingroup$ Is the question oen about the difference between the functions prcomp and princomp in R or about the difference between "Q-mode" and "R-mode PCA"? The two are unrelated. $\endgroup$– BrettDec 21, 2011 at 15:34
-
$\begingroup$ It's been a while. But I'll check as soon as I have time. IIRC I actually had some differences... $\endgroup$– hans0l0Feb 5, 2015 at 6:29
-
2$\begingroup$ Related: Why do the R functions 'princomp' and 'prcomp' give different eigenvalues? $\endgroup$– amoebaFeb 7, 2015 at 21:47
4 Answers
The difference between them is nothing to do with the type of PCA they perform, just the method they use. As the help page for prcomp
says:
The calculation is done by a singular value decomposition of the (centered and possibly scaled) data matrix, not by using
eigen
on the covariance matrix. This is generally the preferred method for numerical accuracy.
On the other hand, the princomp
help page says:
The calculation is done using
eigen
on the correlation or covariance matrix, as determined bycor
. This is done for compatibility with the S-PLUS result. A preferred method of calculation is to usesvd
onx
, as is done inprcomp
."
So, prcomp
is preferred, although in practice you are unlikely to see much difference (for example, if you run the examples on the help pages you should get identical results).
-
1$\begingroup$ There are some gruesome technical details on the different underlying linear algebra and LAPACK routines used in my answer to Why are principal components in PCA (eigenvectors of the covariance matrix) mutually orthogonal? $\endgroup$ Feb 5, 2015 at 1:47
-
1$\begingroup$ Also, see Why PCA of data by means of SVD of the data? for a discussion of why SVD on the data matrix, as implemented by
prcomp
, is a preferred method. $\endgroup$– amoebaFeb 5, 2015 at 9:00 -
$\begingroup$ In my experience,
prcomp
andprincomp
give slightly different estimates (for the eigen vectors particuarly) when working with data were values are very close to 0 (e.g. return calculations for financial data over short time frames), while when the data values are "large", e.g. values not extremely close to 0, the eigenvectors are almost identical to a sig. number of decimal places. $\endgroup$ Jun 24, 2022 at 4:07
Usually a multivariate analysis (computing correlations, extracting latents, etc.) is done of data columns which are features or questions, - while sample units, the rows, are respondents. Hence this way is called R way analysis. Sometimes, though, you may want to do multivariate analysis of responsents, while questions are treated as sample units. That would be Q way analysis.
There is no formal difference between the two, so you can manage both with the same function, only transpose your data. There are differences, however, in issues of standardization and results interpretation.
This is a general reply: I don't touch specifically the R functions prcomp
and princomp
because I'm not an R user and am not aware of possible differences between them.
A useful and specific documentation from Gregory B. Anderson, titled PRINCIPAL COMPONENT ANALYSIS IN R AN EXAMINATION OF THE DIFFERENT FUNCTIONS AND METHODS TO PERFORM PCA
has given more information on this topic. Updated link (7 Jan 2021).
The following two paragraph were extracted from the introduction:
In R there are two general methods to perform PCA without any missing values: (1) spectral decomposition (R-mode [also known as eigendecomposition]) and (2) singular value decomposition (Q-mode; R Development Core Team 2011). Both of these methods can be performed longhand using the functions eigen (R-mode) and svd (Q-mode), respectively, or can be performed using the many PCA functions found in the stats package and other additional available packages. The spectral decomposition method of analysis examines the covariances and correlations between variables, whereas the singular value decomposition method looks at the covariances and correlations among the samples. While both methods can easily be performed within R, the singular value decomposition method (i.e., Q-mode) is the preferred analysis for numerical accuracy (R Development Core Team 2011).
This document focuses on comparing the different methods to perform PCA in R and provides appropriate visualization techniques to examine normality within the statistical package. More specifically this document compares six different functions either created for or can be used for PCA: eigen, princomp, svd, prcomp, PCA, and pca. Throughout the document the essential R code to perform these functions is embedded within the text using the font Courier New and is color coded using the technique provided in Tinn-R (https://sourceforge.net/projects/tinn-r). Additionally, the results from the functions are compared using simulation procedure to see if the different methods differ in the eigenvalues, eigenvectors, and scores provided from the output.
-
2$\begingroup$ Answers are better when they are standalone, because links tend to become broken (e.g. if the page moves or is deleted). Would you mind trying to expand on your answer? $\endgroup$ Sep 17, 2014 at 1:05
-
$\begingroup$ @PatrickCoulombe, It has been done. Thanks for your suggestion. I will take notice of this in the future. $\endgroup$– pengchySep 17, 2014 at 1:26
They are different when both using covariance matrix. When scaling (normalizing) the training data, prcomp
uses $n-1$ as denominator but princomp
uses $n$ as its denominator. Difference of these two denominators is explained in this tutorial on principal component analysis.
Below are my test results:
> job<-read.table("./job_perf.txt", header=TRUE, sep="")
> pc.cr<-prcomp(job, scale=TRUE, cor=TRUE, scores=TRUE)
> pc.cr1<-princomp(job, scale=TRUE, cor=TRUE, scores=TRUE)
> pc.cr$scale
commun probl_solv logical learn physical appearance
5.039841 1.689540 2.000000 4.655398 3.770700 4.526689
> pc.cr1$scale
commun probl_solv logical learn physical appearance
4.805300 1.610913 1.906925 4.438747 3.595222 4.316028
Test data:
commun probl_solv logical learn physical appearance
12 52 20 44 48 16
12 57 25 45 50 16
12 54 21 45 50 16
13 52 21 46 51 17
14 54 24 46 51 17
22 52 25 54 58 26
22 56 26 55 58 27
17 52 21 45 52 17
15 53 24 45 53 18
23 54 23 53 57 24
25 54 23 55 58 25