我在理解vegan软件包V2.5-7中的veganCovEllipse()函数如何计算椭圆时遇到了问题.
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)))
}
具体来说,函数中最后一行发生了什么.这个函数根据协方差矩阵计算椭圆,但我不确定计算的椭圆类型.这会是一个错误椭圆吗?如果是这样,scale参数代表什么?
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)