Is there any way to calculate the area of this ellipse when type = "norm"?
Default is type = "t"
. type = "norm"
displays a different ellipse because it assumes a multivariate normal distribution instead of multivariate t-distribution
Here is the code and the plot (using similar code as other post):
library(ggplot2)
set.seed(1234)
data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
ggplot (data, aes (x = x, y = y))+
geom_point()+
stat_ellipse(type = "norm")
Previous answer was:
#Plot object
p = ggplot (data, aes (x = x, y = y))+
geom_point()+
stat_ellipse(segments=201) # Default is 51. We use a finer grid for more accurate area.
#Get ellipse coordinates from plot
pb = ggplot_build(p)
el = pb$data[[2]][c("x","y")]
# Center of ellipse
ctr = MASS::cov.trob(el)$center
# I tried changing this to 'stats::cov.wt' instead of 'MASS::cov.trob'
#from what is saw from (https://github.com/tidyverse/ggplot2/blob/master/R/stat-ellipse.R#L98)
# Calculate distance to center from each point on the ellipse
dist2center <- sqrt(rowSums((t(t(el)-ctr))^2))
# Calculate area of ellipse from semi-major and semi-minor axes.
These are, respectively, the largest and smallest values of dist2center.
pi*min(dist2center)*max(dist2center)
Changing to stats::cov.wt
wasn't enough to get the area of the "norm" ellipse (value calculated was the same). Any ideas on how to change the code?
Thanks!