I am trying to find a way make 3D PCA visualization from R more portable;
I have run a PCA on 2D matrix using prcomp().
- How do I export the 3D coordinates of data points, along with labels and colors (RGB) associated with each?
- Whats the practical difference with
princomp()andprcomp()? - Any ideas on how to best view the 3D PCA plot using HTML5 and canvas?
Thanks!
Here is an example to work from:
The axis scores are extracted from component
x; as such you can just write out (you don’t say how you want the exported) as CSV using:If you want to assign information to these scores (the colours and labels, which are not something associated with the PCA but something you assign yourself), then add them to the matrix of scores and then export. In the example above there are three species with 50 observations each. If we want that information exported alongside the scores then something like this will work
scrslooks like this:Update missed the point about RGB. See
?rgbfor ways of specifying this in R, but if all you want are the RGB strings then change the above to use something likeinstead, where you specify the RGB strings you want.
The essential difference between
princomp()andprcomp()is the algorithm used to calculate the PCA.princomp()uses a Eigen decomposition of the covariance or correlation matrix whilstprcomp()uses the singular value decomposition (SVD) of the raw data matrix.princomp()only handles data sets where there are at least as many samples (rows) and variables (columns) in your data.prcomp()can handle that type of data and data sets where there are more columns than rows. In addition, and perhaps of greater importance depending on what uses you had in mind, the SVD is preferred over the eigen decomposition for it’s better numerical accuracy.I have tagged the Q with html5 and canvas in the hope specialists in those can help. If you don’t get any responses, delete point 3 from your Q and start a new one specifically on the topic of displaying the PCs using canvas, referencing this one for detail.