Hi All, would appreciate an answer on this if you have a moment; Is there a function (before I try and write it !) that allows the input of a covariance or correlation matrix to calculate PCA, rather than the actual data as in princomp() Regards Glenn [[alternative HTML version deleted]]
"glenn" <g1enn.roberts at btinternet.com> writes:> Is there a function (before I try and write it !) that allows the input of a > covariance or correlation matrix to calculate PCA, rather than the actual > data as in princomp()Yes, there is: princomp(). :-) -- Bj?rn-Helge Mevik
The PCA is just a singular value decomposition on a sample covariance/ correlation matrix. Do a search for ?svd and get the eigenvalues and vectors from that function. On Feb 14, 10:30?am, "glenn" <g1enn.robe... at btinternet.com> wrote:> Hi All, would appreciate an answer on this if you have a moment; > > Is there a function (before I try and write it !) that allows the input of a > covariance or correlation matrix to calculate PCA, rather than the actual > data as in princomp() > > Regards > > Glenn > > ? ? ? ? [[alternative HTML version deleted]] > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
princomp uses the raw data and calculates the correlation or covariance matrix on the way to the PC's, so that doesn't use a correlation matrix itself. You do, however, get the choice. However, PC's are the eigenvectors of the correlation (or covariance) matrix, so in principle calling eigen() on either would be sufficient for the PC's. The signs may differ, though, as they are arbitrary; compare prcomp(USArrests)$rotation with eigen(cov(USArrests)). S>>> Bj?rn-Helge Mevik <b.h.mevik at usit.uio.no> 16/02/2009 09:05 >>>"glenn" <g1enn.roberts at btinternet.com> writes:> Is there a function (before I try and write it !) that allows the input of a > covariance or correlation matrix to calculate PCA, rather than the actual > data as in princomp()Yes, there is: princomp(). :-) -- Bj?rn-Helge Mevik ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Many apologies for the poor steer; you are quite right. 'fraid I hit 'send' before double-checking the help page myself. Next time... S>>> Gavin Simpson <gavin.simpson at ucl.ac.uk> 16/02/2009 10:59 >>>On Mon, 2009-02-16 at 10:45 +0000, S Ellison wrote:> princomp uses the raw data and calculates the correlation or > covariance matrix on the way to the PC's, so that doesn't use a > correlation matrix itself. You do, however, get the choice.That *isn't* what princomp() does. If you supply a valid covariance matrix via argument 'covmat', princomp() uses that instead of calculating one from the input data. That is what ?princomp says it does, as does the R source, the true reference. G> > However, PC's are the eigenvectors of the correlation (or covariance) > matrix, so in principle calling eigen() on either would be sufficient > for the PC's. The signs may differ, though, as they are arbitrary; > compare prcomp(USArrests)$rotation with eigen(cov(USArrests)). > > S > > > > > >>> Bj?rn-Helge Mevik <b.h.mevik at usit.uio.no> 16/02/2009 09:05 >>> > "glenn" <g1enn.roberts at btinternet.com> writes: > > > Is there a function (before I try and write it !) that allows the input of a > > covariance or correlation matrix to calculate PCA, rather than the actual > > data as in princomp() > > Yes, there is: princomp(). :-) > >-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}