I have followed this tutorial to create and visualise a PCA. The part Im particularly interested in is adding new data points to the existing model.
As the tutorial suggests, one would use predict (ir.pca, newdata=tail(log.ir, 2)) to predict new PCs. But how do I add these new observations to the existing plot ? It doesnt look like predict function returns the same object as the ir.pca used in ggplot function.
I have found similar questions here and here but these are calculating new PCA scores and adding them to the variance plot (if I understood it correctly).
Ultimately what Im after is to see whether new points fall in within the confidence ellipse defined/derived using the initial dataset.
The code I'm using from the tutorial:
# log transform
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]
# apply PCA - scale. = TRUE is highly
# advisable, but default is FALSE.
ir.pca <- prcomp(log.ir,
center = TRUE,
scale. = TRUE)
library(devtools)
install_github("ggbiplot", "vqv")
library(ggbiplot)
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1,
groups = ir.species, ellipse = TRUE,
circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal',
legend.position = 'top')
print(g)
And as the tutorial suggests I'd like to add new data which came in to the existing plot visualised with ggplot
Thanks