Skip to content

Instantly share code, notes, and snippets.

@drammock
Last active September 17, 2020 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drammock/db05be8e03a4f09e5a14973a7a6a5c2c to your computer and use it in GitHub Desktop.
Save drammock/db05be8e03a4f09e5a14973a7a6a5c2c to your computer and use it in GitHub Desktop.
quick-and-dirty implementation to find the overlap area of the convex hulls of vowel measurements
library(phonR) # convexHullArea
library(sp) # SpatialPolygons, etc
library(rgeos) # gIntersection
data(indoVowels)
female_two <- indo[indo$subj == "F02",]
by_vowel <- split(female_two, female_two$vowel)
hull_indices <- sapply(by_vowel, function(df) with(df, chull(f1, f2)))
hulls <- sapply(names(by_vowel), function(v) by_vowel[[v]][hull_indices[[v]],],
simplify=FALSE)
matrices <- sapply(hulls, function(df) as.matrix(df[,c("f1", "f2")]))
closed_mats <- sapply(matrices, function(m) rbind(m, m[1,]))
polygons <- sapply(closed_mats, Polygon, hole=FALSE)
polygon_lists <- sapply(names(polygons), function(i) Polygons(polygons[i], ID=i))
spatial_polygons <- sapply(names(polygon_lists), function(i) SpatialPolygons(polygon_lists[i]))
cmbns <- combn(names(by_vowel), 2)
overlap <- apply(cmbns, 2, function(i) gIntersection(spatial_polygons[[i[1]]],
spatial_polygons[[i[2]]]))
names(overlap) <- apply(cmbns, 2, paste, collapse="-")
overlap_area <- sapply(overlap, function(i) if (is.null(i)) {0} else {i@polygons[[1]]@area})
hull_area <- with(indo, convexHullArea(f1, f2, group=vowel))
voach <- sapply(names(overlap_area), function(i) {
v <- strsplit(i, '-', fixed=TRUE)[[1]]
min(hull_area[v]) / overlap_area[[i]]
})
@pr-crypto
Copy link

pr-crypto commented Sep 17, 2020

Hi,

  1. Is there any code available anywhere to produce the visualizations as in Kelly and Tucker (2020), Fig 1 c)
  2. In the indoVowels data, the last command produces an output as below. How to interpret the "inf"? Thanks for putting this online.
    a-e a-i a-o a-u e-i e-o e-u i-o i-u o-u
    8.747026 Inf Inf Inf 19.018043 Inf Inf Inf Inf 4.528538

@drammock
Copy link
Author

Inf means that vowel pair does not overlap at all (division by zero yields inf, and the overlap area is in the denominator on line 23). For your other question, can you provide a doi for the paper you're asking about?

@pr-crypto
Copy link

  1. Thanks for such a quick reply.
  2. Here is the DOI: https://doi.org/10.1121/10.0000494

Cheers!

@drammock
Copy link
Author

I don't think there is an out-of-the-box way to make that plot with phonR. This gist computes the complete overlap of the 2 larger hulls, whereas that figure shows the (smaller) hull defined by the vowel tokens that are within the overlap area. It would be possible to use phonR to plot the 2 larger hulls, then use this code to find the overlap region, then find which vowel tokens are inside that region (using splancs::inpip() I think?), then defining the new hull based on those points (using this gist again, up through line 10), and finally adding the new hull to the phonR plot using polygon(). I'm writing all this from memory so don't be surprised if I got something wrong... Give it a go and let me know if you can't get it to work.

@pr-crypto
Copy link

Thanks for your replies. I will try it this weekend and hopefully report back my victory on Monday!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment