Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #### libraries ----
  2.  
  3. # install.packages("intergraph")
  4.  
  5. library(dplyr)
  6. library(purrr)
  7. library(furrr)
  8. library(GGally)
  9. library(igraph)
  10. library(stringdist)
  11. library(tidyr) # for expand_grid
  12.  
  13. #### environment parameters ----
  14.  
  15. plan(multiprocess)
  16.  
  17. #### helper function(s) ----
  18.  
  19. similar_strings <- function(df, x, y = NULL, dist = 0.7, ...) {
  20.  
  21. x <- enquo(x)
  22. y <- enquo(y)
  23.  
  24. if (quo_name(y) == "NULL")
  25. y <- x
  26.  
  27. dat <- expand_grid(x = df[[quo_name(x)]],
  28. y = df[[quo_name(y)]]) %>%
  29. mutate(string_dist = pmap_dbl(., ~stringsim(.x, .y))) %>%
  30. filter(string_dist < 1, string_dist >= dist)
  31.  
  32. out <- list()
  33.  
  34. out$distances <- dat
  35.  
  36. out$graph <- dat %>%
  37. select(x, y) %>%
  38. graph_from_data_frame()
  39.  
  40. out$networks <- decompose.graph(out$graph)
  41.  
  42. out$similar_strings <- lapply(out$networks, function(x) V(x)$name)
  43.  
  44. out
  45.  
  46. }
  47.  
  48. #### load data ----
  49.  
  50. data(mtcars)
  51.  
  52. dat_mtcars <- as_tibble(mtcars) %>%
  53. mutate(models = rownames(mtcars))
  54.  
  55. #### Calculate string distance(s) & group into networks ----
  56.  
  57. dat <- similar_strings(dat_mtcars, models, dist = 0.6)
  58.  
  59. # list of similar strings
  60. dat$similar_strings
  61.  
  62. # plot full graph
  63.  
  64. ggnet2(dat$graph, label = TRUE, layout.exp = 1.1)
  65.  
  66. # plot networks individually
  67.  
  68. lapply(dat$networks, ggnet2, label = TRUE, layout.exp = 1.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement