library(pca.utils)

The pca.utils package

This package aims at making standard PCA related tasks easier, requiring fewer lines of code. At this point it provides two main functions:

plot_pca() example

Here we will quickly create 2D and 3D PCA plots of the iris data set, using 1 line of code.

data(iris)

# 2D PCA
plot_pca(x=iris[1:4],col=iris$Species)

# 3D PCA
plot_pca(x=iris[1:4],npcs=3,col=iris$Species)

2D PCA plot

3D PCA plot

project_pca() example

We will use 100 random data points of the iris data set to compute the PCA space and then project the remaining new 50 data points in the existing PCA space, as follows:

set.seed(1)
data(iris)

#randomly select 100 data points from the iris data set
#to perform PCA
ix=rep(FALSE,nrow(iris))
ix[sample(nrow(iris),100)]=TRUE

# perform PCA
pc=stats::prcomp(iris[ix,1:4])

# plot the PCA
plot_pca(pc,col=iris$Species[ix])

# project the remaining 50 data points (given by !ix) in the existing PCA space
Xpc=project_pca(iris[!ix,1:4],pc)

#plot the new data points in the existing plot
points(Xpc[,1],Xpc[,2])

3D PCA plot

Installation instructions

Make sure you have the devtools package installed, then you can use devtools::install_github, as follows:

devtools::install_github('nchlis/pca.utils')


nchlis/pca.utils documentation built on May 23, 2019, 1:06 p.m.