Title: | Functions, Data Sets and Vignettes to Aid in Learning Principal Components Analysis (PCA) |
---|---|
Description: | Principal component analysis (PCA) is one of the most widely used data analysis techniques. This package provides a series of vignettes explaining PCA starting from basic concepts. The primary purpose is to serve as a self-study resource for anyone wishing to understand PCA better. A few convenience functions are provided as well. |
Authors: | Bryan A. Hanson [aut, cre] |
Maintainer: | Bryan A. Hanson <[email protected]> |
License: | GPL-3 |
Version: | 0.3.4 |
Built: | 2025-03-13 03:17:08 UTC |
Source: | https://github.com/bryanhanson/learnpca |
Principal component analysis (PCA) is one of the most widely used data analysis techniques. This package provides a series of vignettes explaining PCA starting from basic concepts. The primary purpose is to serve as a self-study resource for anyone wishing to understand PCA better. A few convenience functions are provided as well.
Bryan A. Hanson and David T. Harvey. Maintainer: Bryan A. Hanson [email protected]
Useful links:
Report bugs at https://github.com/bryanhanson/LearnPCA/issues
This function allows one to reconstruct an approximation (Xhat
) of the
original data
using some or all of the principal components, starting from the results of PCA.
Inspired by and follows https://stackoverflow.com/a/23603958/633251 very closely.
We are grateful for this post by StackOverflow contributor "Marc in the box."
PCAtoXhat(pca, ncomp = NULL)
PCAtoXhat(pca, ncomp = NULL)
pca |
An object of class |
ncomp |
Integer. The number of principal components to use in reconstructing the data set. Must be no larger than the number of variables. If not specified, all the components are used and the original data set is reconstructed. |
A matrix with the same dimensions as pca$x
(the dimensions of the
original data set).
# Example data from ?prcomp (see discussion at Stats.StackExchange.com/q/397793) C <- chol(S <- toeplitz(.9 ^ (0:31))) set.seed(17) X <- matrix(rnorm(32000), 1000, 32) Z <- X %*% C pcaz <- prcomp(Z) tst <- PCAtoXhat(pcaz) all.equal(tst, Z, check.attributes = FALSE) # Plot to show the effect of increasing ncomp ntests <- ncol(Z) rmsd <- rep(NA_real_, ntests) for (i in 1:ntests) { ans <- XtoPCAtoXhat(X, i, sd) del<- ans - X rmsd[i] <- sqrt(sum(del^2)/length(del)) # RMSD } plot(rmsd, type = "b", main = "Root Mean Squared Deviation\nReconstructed - Original Data", xlab = "No. of Components Retained", ylab = "RMSD") abline(h = 0.0, col = "pink")
# Example data from ?prcomp (see discussion at Stats.StackExchange.com/q/397793) C <- chol(S <- toeplitz(.9 ^ (0:31))) set.seed(17) X <- matrix(rnorm(32000), 1000, 32) Z <- X %*% C pcaz <- prcomp(Z) tst <- PCAtoXhat(pcaz) all.equal(tst, Z, check.attributes = FALSE) # Plot to show the effect of increasing ncomp ntests <- ncol(Z) rmsd <- rep(NA_real_, ntests) for (i in 1:ntests) { ans <- XtoPCAtoXhat(X, i, sd) del<- ans - X rmsd[i] <- sqrt(sum(del^2)/length(del)) # RMSD } plot(rmsd, type = "b", main = "Root Mean Squared Deviation\nReconstructed - Original Data", xlab = "No. of Components Retained", ylab = "RMSD") abline(h = 0.0, col = "pink")
Shiny application to demonstrate the search for the 1st two principal components for a randomly generated set of data.
PCsearch()
PCsearch()
@return None. A web page opens with the application running.
@author Bryan A. Hanson, David T. Harvey
This function allows one to do "round trip" PCA by reducing a matrix X
using PCA and then reconstruct an approximation (Xhat
) using some or
all of the principal components.
Inspired by https://stats.stackexchange.com/q/229092/26909. We are grateful
for this post by StackOverflow contributor Amoeba.
XtoPCAtoXhat(X, ncomp = 3, scale.fun = NULL)
XtoPCAtoXhat(X, ncomp = 3, scale.fun = NULL)
X |
A matrix of data, or a structure which can be coerced to a matrix. Samples should be in rows, and variables in columns. |
ncomp |
Integer. The number of principal components to use in reconstructing the data set. Must be no larger than the number of variables. |
scale.fun |
A function to use to scale the data. If |
A matrix with the same dimensions as X
.
# Example data from ?prcomp (see discussion at Stats.StackExchange.com/q/397793) C <- chol(S <- toeplitz(.9 ^ (0:31))) set.seed(17) X <- matrix(rnorm(32000), 1000, 32) Z <- X %*% C tst <- XtoPCAtoXhat(Z) mean(tst - Z) # Plot to show the effect of increasing ncomp ntests <- ncol(Z) rmsd <- rep(NA_real_, ntests) for (i in 1:ntests) { ans <- XtoPCAtoXhat(X, i, sd) del<- ans - X rmsd[i] <- sqrt(sum(del^2)/length(del)) # RMSD } plot(rmsd, type = "b", main = "Root Mean Squared Deviation\nReconstructed - Original Data", xlab = "No. of Components Retained", ylab = "RMSD") abline(h = 0.0, col = "pink")
# Example data from ?prcomp (see discussion at Stats.StackExchange.com/q/397793) C <- chol(S <- toeplitz(.9 ^ (0:31))) set.seed(17) X <- matrix(rnorm(32000), 1000, 32) Z <- X %*% C tst <- XtoPCAtoXhat(Z) mean(tst - Z) # Plot to show the effect of increasing ncomp ntests <- ncol(Z) rmsd <- rep(NA_real_, ntests) for (i in 1:ntests) { ans <- XtoPCAtoXhat(X, i, sd) del<- ans - X rmsd[i] <- sqrt(sum(del^2)/length(del)) # RMSD } plot(rmsd, type = "b", main = "Root Mean Squared Deviation\nReconstructed - Original Data", xlab = "No. of Components Retained", ylab = "RMSD") abline(h = 0.0, col = "pink")