I'm having issues understanding how the veganCovEllipse() function from the vegan package v 2.5-7 calculates an ellipse.
veganCovEllipse <- function (cov, center = c(0, 0), scale = 1, npoints = 100)
{
theta <- (0:npoints) * 2 * pi/npoints
Circle <- cbind(cos(theta), sin(theta))
t(center + scale * t(Circle %*% chol(cov)))
}
Specifically, what is occurring in the last line within the function. This function calculates an ellipse based off of a covariance matrix, but I'm not sure what type of ellipse is calculated. Would this be an error ellipse? If so, what does the scale argument represent? Here's an example creating ellipses from PCA scores:
library(stats)
# fit pca on mtcars dataset
mtcars_pca <- prcomp(mtcars[,c(1:7,10,11)], center = TRUE,scale. = TRUE)
# dataframe of pc1 and pc2 scores
pcs <- data.frame(PC1 = mtcars_pca$x[,1],
PC2 = mtcars_pca$x[,2])
# calculate weighted covariance of PC1 and PC2
weight_cov <- cov.wt(cbind(pcs$PC1, pcs$PC2), wt=rep(1/length(pcs$PC1), length(pcs$PC1)))$cov
# calculate mean of PC1 and PC2, used as the center of the ellipse
center <- c(mean(pcs$PC1),
mean(pcs$PC2))
# fit ellipse given weighted covariance and center
ellipse <- veganCovEllipse(weight_cov, center)