library(rsvd)
#
# Load Edgar Anderson's Iris Data
#
data(iris)
#
# log transform
#
log.iris <- log( iris[ , 1:4] )
iris.species <- iris[ , 5]
#
# Perform rPCA and compute all PCs, similar to \code{\link{prcomp}}
#
iris.rpca <- rpca(log.iris, retx=TRUE, svdalg = 'rsvd')
summary(iris.rpca) # Summary
print(iris.rpca) # Prints the loadings/ rotations
# You can compare the results with prcomp
# iris.pca <- prcomp(log.iris, center = TRUE, scale. = TRUE)
# summary(iris.pca) # Summary
# print(iris.pca) # Prints the loadings/ rotations
#
# Plot functions
#
ggscreeplot(iris.rpca) # Screeplot
ggscreeplot(iris.rpca, 'cum') # Screeplot
ggscreeplot(iris.rpca, type='eigenvals') # Screeplot of the eigenvalues
ggcorplot(iris.rpca, pcs=c(1,2)) # The correlation of the original variable with the PCs
ggbiplot(iris.rpca, groups = iris.species, circle = FALSE) #Biplot
#
# Perform rPCA and compute only the first two PCs
#
iris.rpca <- rpca(log.iris, k=2, svdalg = 'rsvd')
summary(iris.rpca) # Summary
print(iris.rpca) # Prints the loadings/ rotations
#
# Compute the scores of new observations
#
preds <- predict(iris.rpca, newdata=data.frame(log.iris))
Run the code above in your browser using DataLab