olliejay00
olliejay00

Reputation: 43

Trouble understanding veganCovEllipse covariance ellipse calculation

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)

Upvotes: 4

Views: 337

Answers (1)

Jari Oksanen
Jari Oksanen

Reputation: 3692

veganCovEllipse is not an exported function. This means that it is not intended for interactive use, but it is a support function only to be called from other functions in vegan. Therefore it is not documented. However, the answer is simple: it can calculate any kind of ellipse depending on the input. In vegan the function is called for instance from ordiellipse and there it can draw standard error ellipses, "confidence" ellipses (standard error multiplied by some value picked from statistical distribution), standard deviation ellipses, standard deviation ellipses multiplied by similar constants as standard errors, or enclosing ellipses that contain all points of a group, depending on the input to the function. In showvarparts function it is just re-used to draw circles. Actually veganCovEllipse does not fit anything: it just calculates the coordinates to draw what you asked it to draw, and your input defines the shape, size, orientation and location of the ellipse coordinates.

There are other functions in other packages that do the same: return you the points needed to plot an ellipse from your input data. For instance, the standard (recommended) package cluster makes similar calculations in non-exported functions cluster:::ellipsoidPoints with effectively the same mathematics, but in completely different way. This function is non-exported as well, and it is intended to be called from user function cluster::predict.ellipsoid. The vegan implementation is similar as in the ellipse function in the car package, where these calculations are embedded in that function and cannot be called separately from car::ellipse.

Upvotes: 3

Related Questions