Purpose of Code Through

We often merge many datasets. Unfortunately, universal ID for each entity (e.g., firm) does not exist. Therefore, we end up relying on facial value such as firm names. However, even these values are irregular across datasets. For example, “Samsung”, an IT company, may appear as “Samsung co. ltd.” in some datasets. If we have to match multiple datasets, how can we reduce unmatched cases?

The purpose of this code through is to learn “fuzzyjoin”, a fuzzy table matching tool in R.

Example

Let’s load packages first

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(fuzzyjoin)

For the sample analysis, let’s bring misspelling data

data(misspellings)
misspellings
## # A tibble: 4,505 x 2
##    misspelling correct   
##    <chr>       <chr>     
##  1 abandonned  abandoned 
##  2 aberation   aberration
##  3 abilties    abilities 
##  4 abilty      ability   
##  5 abondon     abandon   
##  6 abbout      about     
##  7 abotu       about     
##  8 abouta      about a   
##  9 aboutit     about it  
## 10 aboutthe    about the 
## # ... with 4,495 more rows

We have about 4,000 misspelled words. To match with actual words, load a set of entire word(corpus) set

library(qdapDictionaries)
words <- tbl_df(DICTIONARY)
## Warning: `tbl_df()` is deprecated as of dplyr 1.0.0.
## Please use `tibble::as_tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
words
## # A tibble: 20,137 x 2
##    word  syllables
##    <chr>     <dbl>
##  1 hm            1
##  2 hmm           1
##  3 hmmm          1
##  4 hmph          1
##  5 mmhmm         2
##  6 mmhm          2
##  7 mm            1
##  8 mmm           1
##  9 mmmm          1
## 10 pff           1
## # ... with 20,127 more rows

We have about 20,000 words. Let’s match 1000 random sampled misspelled words to actual words. Use “stringdist_inner_join” function for the match. To acheive high accuracy, set the distance as 1.

set.seed(1)
sub_misspellings <- misspellings %>%
  sample_n(1000)

joined <- sub_misspellings %>%
  stringdist_inner_join(words, by = c(misspelling = "word"), max_dist = 1)

joined
## # A tibble: 756 x 4
##    misspelling   correct        word           syllables
##    <chr>         <chr>          <chr>              <dbl>
##  1 techician     technician     technician             3
##  2 reconaissance reconnaissance reconnaissance         4
##  3 temperarily   temporarily    temporarily            5
##  4 belive        believe        belie                  2
##  5 belive        believe        believe                2
##  6 belive        believe        belike                 2
##  7 foucs         focus          focus                  2
##  8 succeded      succeeded      succeed                2
##  9 fiels         fields         field                  1
## 10 fiels         fields         fils                   1
## # ... with 746 more rows

The accuracy seems okay. How many words are matched?

count(joined) 
## # A tibble: 1 x 1
##       n
##   <int>
## 1   756

How many words are exactly the same as the true spelling?

joined$match <- (joined$correct == joined$word)
sum(joined$match)
## [1] 392

The results show that we encounter a certain ratio of loss in matching. In addition, matching is not perfect but decent at some level. The main causes are 1) tense and 2) plural terms. Thus, if the words are not about subject to such issues (e.g., firm names), we can expect better match.

What if we increase the word criteria from 1 to 2? Will the loose condition increase match and precision at the same time?

joined2 <- sub_misspellings %>%
  stringdist_inner_join(words, by = c(misspelling = "word"), max_dist = 2)

joined2$match <- (joined2$correct == joined2$word)
count(joined2) 
## # A tibble: 1 x 1
##       n
##   <int>
## 1  7608
sum(joined2$match)
## [1] 448

The match count increase a lot while the perfect match case count didn’t increase as much. One reason for great number of match count is the multiple match per each misspelled word. Given this experimental test, we can conclude that increasing ‘max_dist’ count may help find the perfect match cases while it may generate other similar match cases. Users should set the criteria value carefully, considering the main purpose of the match (e.g., finding the perfect match and construction of a merged dataset)

joined2
## # A tibble: 7,608 x 5
##    misspelling correct    word      syllables match
##    <chr>       <chr>      <chr>         <dbl> <lgl>
##  1 konws       knows      bonus             2 FALSE
##  2 konws       knows      know              1 FALSE
##  3 konws       knows      known             1 FALSE
##  4 konws       knows      nones             1 FALSE
##  5 konws       knows      onus              2 FALSE
##  6 konws       knows      tongs             1 FALSE
##  7 altough     although   plough            1 FALSE
##  8 altough     although   slough            1 FALSE
##  9 altough     although   tough             1 FALSE
## 10 techician   technician tactician         3 FALSE
## # ... with 7,598 more rows