I am using the dataEllipse function from the car package in R to get an elliptic confidence region for my data. For example:
datapoints_x = c(1,3,5,7,8,6,5,4,9)
datapoints_y = c(3,6,8,9,5,8,7,4,8)
ellipse = dataEllipse(cbind(datapoints_x, datapoints_y), levels=0.95)
The output are two vectors x and y corresponding to points that define the ellipse:
head(ellipse)
# x y
# [1,] 12.79906 10.27685
# [2,] 12.74248 10.84304
# [3,] 12.57358 11.34255
# [4,] 12.29492 11.76781
# [5,] 11.91073 12.11238
# [6,] 11.42684 12.37102
But rather than that I am interested in the length of the ellipsis axes and its center. Is there a way to get this without carrying out the PCA by myself?
From
?dataEllipse
you read that these functions are mostly plotting functions, not functions designed to give you the fitted ellipse. However reading the source code ofdataEllipse
, it becomes clear that the function used to fit the ellipse iscov.wt
from thestats
package. This function should be able to give you the center and covariance matrix used to specify the ellipse location and shape:The center of the ellipse is now readily available from
ell.info$center
. The directions of the axes are accessible as the eigenvectors of the covariance matrix (columns ofeigen.info$vectors
below).Finally you need to know the length of the axes (I'll give the length from the center to the ellipse, aka the radius on that axis):
Now we can get the four endpoints of the axes of the ellipse:
We can confirm these are accurate from using
dataEllipse
: