title: R: How to use ggbiplot with pcaRes object? plot PCA results of data with missing values
body: <p>I usually perform Principal Component Analyses with the <code>prcomp</code> function, and plot the results in a fancy way with <code>ggbiplot</code> (or otherwise just with <code>ggplot2</code> extracting <code>pca.obj$x</code>).</p>
  
  <p>Like this:</p>
  
  <pre><code>#install_github("vqv/ggbiplot")
  library(ggbiplot)
  data(iris)
  pca.obj &lt;- prcomp(iris[,1:4], center=TRUE, scale.=TRUE)
  P &lt;- 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 &lt;- 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()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/VdsSp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VdsSp.png" alt="test1"></a></p>
  
  <p>HOWEVER, now I am facing data with some amount of NAs, and I am using the <code>pca</code> wrapper function from the <a href="https://bioconductor.org/packages/release/bioc/html/pcaMethods.html" rel="nofollow noreferrer">pcaMethods</a> package, applying the <code>nipals</code> method (an iterative method capable of handling small amounts of missing values).</p>
  
  <p><code>pca</code> returns an object of class <code>pcaRes</code>, and <code>ggbiplot</code> returns the following error:</p>
  
  <pre><code>#introduce NAs
  iris$Sepal.Length[sample(1:150, 5)] &lt;- NA
  iris$Sepal.Width[sample(1:150, 5)] &lt;- NA
  iris$Petal.Length[sample(1:150, 5)] &lt;- NA
  iris$Petal.Width[sample(1:150, 5)] &lt;- NA
  #pca.obj2 &lt;- 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 &lt;- pca(iris[,1:4], method="nipals", nPcs=3, center=TRUE, scale.=TRUE)
  class(pca.obj2)
  ggbiplot(pca.obj2)
  </code></pre>
  
  <blockquote>
    <p>Error in ggbiplot(pca.obj2) :    Expected a object of class prcomp,
    princomp, PCA, or lda</p>
  </blockquote>
  
  <p>MY QUESTIONS are:</p>
  
  <p>How can I apply <code>ggbiplot</code> to a <code>pcaRes</code> object?</p>
  
  <p>How can I convert this object to a <code>prcomp</code> object?</p>
  
  <p>Can I obtain the same kind of plot with another function instead of <code>ggbiplot</code> that accepts a <code>pcaRes</code> object?</p>
  
  <p>Should I just replace the NA values with the mean of the variable and apply the <code>prcomp</code> function as usual?</p>
  
  <p>Many thanks!</p>