Skip to content Skip to sidebar Skip to footer

Removing Outliers From Convex Hull

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 h

Solution 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)

enter image description here

Post a Comment for "Removing Outliers From Convex Hull"