Using this link as a starting point, How to find the smallest ellipse covering a given fraction of a set of points in R?
I would like to create the smallest circle that includes (touches or goes inside) a majority of a set of ellipses, instead of being limited to a set of 2d data points, as the link is.
I'm looking for a circle, as opposed to the ellipse that the ellipsoidhull function produces. Maybe there is a way to configure the shape that ellipsoidhull function produces, that I haven't found yet? My data set is:
The circle will include ellipses named d, b, e, a, i, and look like this: I've added the circle in paint.
library(ggplot2)
library(ggforce)
library(MASS)
library(cluster)
library(car)
d <- data.frame(
name = c("a", "b", "c", "d", "e", "f", "g", "h", "i"),
x0 = c(5, 3, 4, 1, 4, 5, 7, 9, 5),
y0 = c(4, 4, 9, 5, 5, 1, 7, 7, 7),
a = c(0.9, 0.6, 1.0, 1.1, 1.3, 1.6, 1.4, 0.5, 0.6),
b = c(0.7, 0.5, 0.6, 0.7, 0.8, 0.7, 0.7, 1.7, 1.8)
)
ggplot(data = d) + geom_ellipse(aes(x0 = x0, y0 = y0, a = a, b = b, angle = 0), fill = "grey", alpha = 0.3)+
geom_text(data=d, aes(x=x0, y=y0, label = name))
#This second part of the code is from the link, which creates an ellipse based on data points. Possibly it could be useful
xy <- cbind(d$x0, d$y0)
fit <- cov.rob(xy, quantile.used = 5, method = "mve")
# Finding the minimum volume ellipse that contains these three points
best_ellipse <- ellipsoidhull( xy[fit$best,] )
plot_min_ellipse <- function(xy, points_in_ellipse, color = "blue") {
fit <- cov.rob(xy, quantile.used = points_in_ellipse, method = "mve")
best_ellipse <- ellipsoidhull( xy[fit$best,] )
lines(predict(best_ellipse), col=color)
}
# The predict() function returns a 2d matrix defining the coordinates of
# the hull of the ellipse
plot(xy)+ lines(predict(best_ellipse), col="blue")
I believe the algorithm would become the red circle in this plot. Touching or penetrating or enveloping 5 out of 9 ellipses.