MomX / Momocs

:dove: Morphometrics using R
http://momx.github.io/Momocs/
52 stars 19 forks source link

coo_chull_union and coo_chull_median #204

Closed vbonhomme closed 4 years ago

vbonhomme commented 6 years ago

following a morphmet discussion these two would be helpful. below a primer for coo_chull_union

# for geometric operations
library(sf)
# for the pipe, dplyr verbs, coo_chull, coo_close and toy datasets
library(Momocs)

# creates two overlapping sets
xp <- bot %>% efourier %>% PCA
xp %>% plot_PCA(~type)
w <- filter(xp, type=="whisky")$x[, 1:2]
b <- filter(xp, type=="beer")$x[, 1:2]

# this function calculates simple area stats
chull_union <- function(coo1, coo2){
  # turns a set of point into a convex hull then into a sf polygon
  coo2chull_poly <- function(x) 
    x %>% coo_chull() %>% coo_close() %>% list %>% st_polygon()

  # apply to both
  coo1_p <- coo1 %>% coo2chull_poly()
  coo2_p <- coo2 %>% coo2chull_poly()

  # calculate some interesting areas and return them
  list(ca_coo1  = st_area(coo1_p),
              ca_coo2  = st_area(coo2_p),
              ca_union = st_union(coo1_p, coo2_p) %>% st_area(),
              ca_inter = st_intersection(coo1_p, coo2_p) %>% st_area())
}

(x <- chull_union(w, b))
$ca_coo1
[1] 0.008774579

$ca_coo2
[1] 0.01799339

$ca_union
[1] 0.01860516

$ca_inter
[1] 0.008162805

# then it depends of how you define overlap but for the proportion inter/union
#inter/union you can:
x$ca_inter/x$ca_union #0.4387388
vbonhomme commented 4 years ago

Probably useless and dirty compared to proper disparity measures.