R prcomp PCA rotation incorrectly mirroring data - r

Has anyone had any issue with R's prcomp PCA rotation function rotating THEN MIRRORING the data? When I use principle from the psych package, the rotation is correct. Also, when I do it manually with cosine angle, it is correct. But when I do prcomp, it mirrors the data after rotating it. prcomp has some nice visualizations that I want to use that principle does not have.
Correct rotation:
principal(PCADataFrame, nfactors=2, rotate="varimax")
Incorrect (mirrored) rotation)
prcomp(PCADataFrame, scale = TRUE)
I want to use
fviz_pca_biplot(res.pca)
to plot factor loadings, but this only works with prcomp. So if anyone knows of similar visualizations with principle, that would be helpful to.

Related

How can I create a biplot with factoextra after using varimax rotation in R?

I have performed a PCA on my data set with prcomp() and then used fviz_pca_biplot() (package factoextra) to create a nice biplot (including different colors by group, in my case four different sites).
It was suggested to me to use varimax rotation to increase the interpretability of the PCA axes. I've tried many options including varimax(), or a different package (psych), but I was not able to get the same nice biplot I produced with factoextra.
Does someone know whether it is at all possible to create a biplot with factoextra after using the varimax() function in R?

Confidence ellipse formula in JS or R

What I have: A scatter chart(plot) of PCA. Plotted in JS. I have Rtools that Ive used to push PCA data to the client side.
What I'm trying to do: Plot a confidence ellipse formula.
I can't seem to find a straight forward formula for the CI ellipse. I came across a lot of theory and a lot of examples in R which give you the end result - an ellipse (One can use ggplot or CRAN to plot it).
But Im looking for a formula that I could use in the client side to plug my scatter chart points and calculate the ellipse or even better a function in R that would give me a formula for the ellipse.
I have the covariance matrix and Eigen vectors as well (calculated in R).
All suggestions much appreciated.
Haven't found a formula but after using Momocs:::conf_ell library I managed to get the vertices and the x,y points of an ellipse.
I will update this answer once I find the second part to my answer - a straight forward formula.

Define and draw a hyperplane for hierarchical clustering result

I used hclust on my data and got a two-cluster solution. Then I used the scatter3d() to create a 3D scatterplot to visualize the two clusters by getting x, y,z coordinates multidemsional scaling (see below).
I am wondering how can I define and draw a linear discrminant hyperplane that separate these two clusters.
The most useful and sensible answer to the first part of my question can be found here:
https://stat.ethz.ch/pipermail/r-help/2010-May/239195.html
For the second part, I think surface3d() will do the trick.

How to reproduce this graphical explanation (a scatter plot) of how covariance works?

I found this graphical intuitive explanation of covariance:
32 binormal points drawn from distributions with the given covariances, ordered from most negative (bluest) to most positive (reddest)
The whole material can be found at:
https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean
I would like to recreate this sort of graphical illustration in R, but I'm not sufficiently familiar with R's plotting tools. I don't even know where to start in order to get those colored rectangles between each pair of data points, let alone make them semi-transparent.
I think this could make a very efficient teaching tool.
The cor.rect.plot function in the TeachingDemos package makes plots similar to what is shown. You can modify the code for the function to make the plot even more similar if you desire.

3d Vector plotting and visualization in R

Anyone know of any packages in R that can plot 3D vectors? I would like to plot some vectors over a 3 dimensional data set. I know scatterplot3d looks like it should be able to do the job, but I didn't see any documentation for vectors specifically (I would like to be able to change the origin of vectors as well).
rgl is a great package for 3d plotting. It is very like 2d plotting except that you use plot3d instead of plot. This answer on SO suggests a way to draw arrows in rgl.

Resources