问题
I have a few datasets that I'd like to visualise with convex hull (and derive some statistics from that convex hull). However, each dataset contains some noise. Therefore, convex hull covers not only points in the main data cloud, but also all the outliers making the area of convex hull pretty large and not very different between datasets. An example of the dataset may be seen below:
The whole area is not unimodal, but we can certainly observe some outliers (especially on the left) that mess up convex hull shape. The estimated KDE looks like below:
Therefore, I'd like to remove those outliers. What algorithm could be used to fit minimal area convex hull to n - k points from the dataset, where k is set to some number respective to given percentage of observations?
Please note that pictures refer to an example and I'm in fact dealing with plenty of different datasets
回答1:
This is in R
set.seed(42)
#DATA
x = rnorm(20)
y = rnorm(20)
#Run convex hull
i = chull(x, y)
#Draw original data and convex hull
graphics.off()
plot(x, y, pch = 19, cex = 2)
polygon(x[i], y[i])
#Get coordinates of the center
x_c = mean(x)
y_c = mean(y)
#Calculate distance of each point from the center
d = sapply(seq_along(x), function(ind){
dist(rbind(c(x_c, y_c), c(x[ind], y[ind])))
})
#Remove k points furthest from the center
k = 2
x2 = head(x[order(d)], -k)
y2 = head(y[order(d)], -k)
i2 = chull(x2, y2)
#Draw the smaller convex hull
points(x2, y2, pch = 19, col = "red")
polygon(x2[i2], y2[i2], border = "red", lty = 2)
来源:https://stackoverflow.com/questions/57559432/removing-outliers-from-convex-hull