I usually perform Principal Component Analyses with the prcomp
function, and plot the results in a fancy way with ggbiplot
(or otherwise just with ggplot2
extracting pca.obj$x
).
Like this:
#install_github("vqv/ggbiplot")
library(ggbiplot)
data(iris)
pca.obj <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE)
P <- ggbiplot(pca.obj,
obs.scale = 1,
var.scale=1,
ellipse=T,
circle=F,
varname.size=3,
var.axes=T,
groups=iris$Species, #no need for coloring, I'm making the points invisible
alpha=0) #invisible points, I add them below
P$layers <- c(geom_point(aes(color=iris$Species), cex=5), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test.png", height=600, width=600)
print(#or ggsave()
P
)
dev.off()
HOWEVER, now I am facing data with some amount of NAs, and I am using the pca
wrapper function from the pcaMethods package, applying the nipals
method (an iterative method capable of handling small amounts of missing values).
pca
returns an object of class pcaRes
, and ggbiplot
returns the following error:
#introduce NAs
iris$Sepal.Length[sample(1:150, 5)] <- NA
iris$Sepal.Width[sample(1:150, 5)] <- NA
iris$Petal.Length[sample(1:150, 5)] <- NA
iris$Petal.Width[sample(1:150, 5)] <- NA
#pca.obj2 <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE) #cannot use prcomp with NAs
#source("https://bioconductor.org/biocLite.R")
#biocLite("pcaMethods")
library(pcaMethods)
pca.obj2 <- pca(iris[,1:4], method="nipals", nPcs=3, center=TRUE, scale.=TRUE)
class(pca.obj2)
ggbiplot(pca.obj2)
Error in ggbiplot(pca.obj2) : Expected a object of class prcomp, princomp, PCA, or lda
MY QUESTIONS are:
How can I apply ggbiplot
to a pcaRes
object?
How can I convert this object to a prcomp
object?
Can I obtain the same kind of plot with another function instead of ggbiplot
that accepts a pcaRes
object?
Should I just replace the NA values with the mean of the variable and apply the prcomp
function as usual?
Many thanks!