-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeduplicate.Rmd
987 lines (774 loc) · 35.7 KB
/
deduplicate.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
---
title: "Records Deduplication"
output:
unhcrdown::html_page:
toc: true
toc_depth: 2
toc_float: true
---
```{r package, message=FALSE, warning=FALSE}
##################################################
### A script workflow for Record linkage ----------
##################################################
library(tidyverse)
# install.packages("fastLink")
library(fastLink)
```
```{r data, message=FALSE, warning=FALSE}
## Load the data -
# which is here already the results of merging multiple list from different excel files
data <- readxl::read_excel(here::here("data-raw", "Registros3.xlsx"),
sheet = "Sheet1",
col_types = c("numeric",
"text", "text", "text", "text", "text",
"text", "text", "date", "date", "numeric",
"numeric", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text")) |>
janitor::clean_names()
#dput(names(data))
```
## Cleaning functions
```{r function-clean_age}
#' clean_age
#'
#' This functions cleans the age identifiers in the context of record linkage.
#'
#' If the date of birth is present, it will parse it, extract year and month,
#' and recalculate both age and age range
#'
#' If the date of birth is not mentioned, then it will use age and date_record
#' to reconstruct an estimated date of birth - and recalculate the rest
#'
#' @param frame frame with the data
#' @param date_birth variable name for the date of birth in the frame
#' @param date_record variable name for the date of birth in the frame
#' @param age variable name for the date of birth in the frame
#' @param age_range variable name for the date of birth in the frame
#'
#' @return the same column but cleaned...
#'
#' @export
clean_age <- function(frame,
date_birth,
date_record,
age,
age_range){
#frame$date_birth
frame2 <- frame |>
## Rename variable
dplyr::rename( date_birth = paste0(date_birth),
date_record = paste0(date_record),
age = paste0(age),
age_range = paste0(age_range) ) |>
dplyr::mutate(
## In case there is no date of birth - but we have an age, we recalculate it..
date_birth = dplyr::case_when(
# Case we age but no DOB and date registration
is.na(date_birth) & !(is.na(age)) & !(is.na(date_record)) ~
lubridate::as_date(date_record) - lubridate::dyears(age) ,
# Case we age but no DOB and date registration
is.na(date_birth) & !(is.na(age)) & is.na(date_record) ~
lubridate::today() - lubridate::dyears(age) ,
# Case take what we have...
TRUE ~ lubridate::as_date(date_birth) ) ,
## make sure it is in the correct format
date_birth = lubridate::as_date(date_birth),
day_birth = as.numeric(
lubridate::day(date_birth)),
month_birth = as.numeric(
lubridate::month(date_birth)),
year_birth= as.numeric(
lubridate::year(date_birth)),
age = round( as.numeric(lubridate::today() - date_birth) / 365),
age_range = dplyr::case_when(
## if age cohort was already present and we have no DOb, retain it
# !(is.na(age_range )) & is.na( date_birth) &
age <5 ~ "0-4",
# !(is.na(age_range )) & is.na( date_birth) &
age >=5 & age <= 11 ~ "4-11",
#!(is.na(age_range )) & is.na( date_birth) &
age >= 12 & age <= 17 ~ "12-17",
# !(is.na(age_range )) & is.na( date_birth) &
age >= 18 & age <= 59 ~ "18-59",
# !(is.na(age_range )) & is.na( date_birth) &
age >= 60 ~ "60+",
TRUE ~ age_range ) )
return(frame2)
}
## Testing...
# frame <- data |>
# dplyr::select(fecha_de_nacimiento,
# date_record, edad, age_range) |>
# clean_age( date_birth = "fecha_de_nacimiento",
# date_record = "date_record",
# age = "edad",
# age_range = "age_range")
```
```{r function-spaces_based_on_patterns}
#' remove_spaces_based_on_patterns
#' Utility sub-function to remove spaces based on patterns
#' This function helps cleaning name decomposition - in case it is not included in the original data
#'
#' In case, there's only 2 elements, it will fill only firstname and fathername
#' The function also identify family prefix to be bind such as for spanish
#' "DEL", 'DE", "DE LOS", "DE LAS"
#'
#' @param vector a list of string with names
remove_spaces_based_on_patterns <- function(vector,
nameprefix = data.frame(
pat1 = c( "DE LA ", "DEL ", "DE LOS ", "DE LAS ","DE ", "SAN ", "LA ", "DA "),
pat2 = c( "DE_LA_", "DEL_", "DE_LOS_", "DE_LAS_", "DE_", "SAN_", "LA_", "DA_") )
) {
for (i in 1:nrow(nameprefix) ) {
# i <- 4
#cat(paste0(nameprefix[i, c("pat1")],"\n", vector, "\n"))
vector <- stringr::str_replace_all(string = vector,
pattern = nameprefix[i, c("pat1")],
replacement = nameprefix[i, c("pat2")])
# cat(paste0(vector, "\n"))
}
return(vector)
}
## Test function
remove_spaces_based_on_patterns(vector = "ADRIENNE DE LOS ANGELES MILANESE PARISIANNA")
#' reset_spaces_based_on_patterns
#' Utility sub-function to remove spaces based on patterns
#' This function helps cleaning name decomposition - in case it is not included in the original data
#'
#' In case, there's only 2 elements, it will fill only firstname and fathername
#' The function also identify family prefix to be bind such as for spanish
#' "DEL", 'DE", "DE LOS", "DE LAS"
#'
#' @param vector a list of string with names
reset_spaces_based_on_patterns <- function(vector,
nameprefix = data.frame(
pat1 = c( "DE LA ", "DEL ", "DE LOS ", "DE LAS ","DE ", "SAN ", "LA ", "DA "),
pat2 = c( "DE_LA_", "DEL_", "DE_LOS_", "DE_LAS_", "DE_", "SAN_", "LA_", "DA_") )
) {
for (i in 1:nrow(nameprefix) ) {
# i <- 1
# cat(paste0( nameprefix[i, c("pat1")],"\n",vector, "\n"))
vector <- stringr::str_replace_all(string = vector,
pattern = nameprefix[i, c("pat2")],
replacement = nameprefix[i, c("pat1")])
#cat(paste0(vector, "\n"))
}
return(vector)
}
```
```{r function-separate_fullname}
#' separate_fullname
#'
#' This function clean name decomposition - in case it is not included in the original data
#'
#' Performing this name decomposition is important in order to enhance record linkage
#' as the name pattern can be different (either "firstname_fathername_mother_name" or
#' "fathername_mothername_firstname") which will minimise linkage probabibility
#'
#' In case, there's only 2 elements, it will fill only firstname and fathername
#' The function also identify family prefix to be bind such as for spanish
#' "DEL", 'DE", "DE LOS", "DE LAS"
#'
#' @param fullname full name including everything together
#' @param firstname first name
#' @param fathername father name
#' @param mothername mother name
#' @param namepattern either "firstname_fathername_mother_name" or
#' "fathername_mothername_firstname"
#'
#' @return a clean list with c("firstname","fathername","mothername")
#'
#' @export
separate_fullname <- function(frame,
fullname,
firstname,
fathername,
mothername,
namepattern
){
## Let's go!
framesp <- frame |>
## Rename variable
dplyr::rename( fullname = paste0(fullname),
firstname = paste0(firstname),
fathername = paste0(fathername),
mothername = paste0(mothername),
namepattern = paste0(namepattern) ) |>
### Lets clean all spaces and get everything to upper
dplyr::mutate( fullname_or = fullname,
fullname = trimws(stringr::str_squish(fullname),
which = "both",
whitespace = "[ \t\r\n]"),
fullname = toupper(fullname)) |>
### Lets apply the prefix space replacement...
dplyr::mutate( fullname_pref = remove_spaces_based_on_patterns(vector = fullname),
fullname = fullname_pref ) |>
## Counting th enumber of space to understand the structure of the full name
dplyr::mutate( numspace = stringr::str_count(fullname, ' ')) |>
tidyr::separate_wider_delim(fullname,
delim = " ",
names_sep = "",
too_few = "align_start") |>
### Now reconstruct the first name, father and mother name based on cases
## Let summarize the logic...!
## Based on the number of componnent in the full name - ranging from 0 to 4
## Case A: "firstname_fathername_mother_name"
# numspace == 0 ## only firstname = fullname1
# numspace = 1 ## firstname = fullname1 & fathername = fullname2
# numspace = 2 ## firstname = fullname1 & fathername = fullname2 & mothername = fullname3
# numspace = 3 ## firstname = paste0(fullname1, " ", fullname2) & fathername = fullname3 & mothername = fullname4
# numspace = 4 ## firstname = paste0(fullname1, " ", fullname2, " ", fullname3) & fathername = fullname4 & mothername = fullname5
# ## Case B: "fathername_mothername_firstname"
# numspace == 0 ## only fathername = fullname1
# numspace = 1 ## fathername = fullname1 & firstname = fullname2
# numspace = 2 ## fathername = fullname1 & mothername = fullname2 & firstname = fullname3
# numspace = 3 ## fathername = fullname1 & mothername = fullname2 & firstname = paste0(fullname3, " ", fullname3)
# numspace = 4 ## fathername = fullname1 & mothername = fullname2 & firstname = paste0(fullname3, " ", fullname4, " ", fullname5)
### Now reconstruct fathername
dplyr::mutate( fathername = dplyr::case_when(
is.na(fathername) & namepattern == "firstname_fathername_mother_name" &
numspace == 0 ~ "",
is.na(fathername) & namepattern == "firstname_fathername_mother_name" &
numspace == 1 ~ fullname2,
is.na(fathername) & namepattern == "firstname_fathername_mother_name" &
numspace == 2 ~ fullname2,
is.na(fathername) & namepattern == "firstname_fathername_mother_name" &
numspace == 3 ~ fullname3,
is.na(fathername) & namepattern == "firstname_fathername_mother_name" &
numspace == 4 ~ fullname4,
is.na(fathername) & namepattern == "fathername_mothername_firstname" &
numspace == 0 ~ fullname1,
is.na(fathername) & namepattern == "fathername_mothername_firstname" &
numspace == 1 ~ fullname1,
is.na(fathername) & namepattern == "fathername_mothername_firstname" &
numspace == 2 ~ fullname1,
is.na(fathername) & namepattern == "fathername_mothername_firstname" &
numspace == 3 ~ fullname1,
is.na(fathername) & namepattern == "fathername_mothername_firstname" &
numspace == 4 ~ fullname1,
TRUE ~ fathername )) |>
### Now reconstruct mothername
dplyr::mutate( mothername = dplyr::case_when(
is.na(mothername) & namepattern == "firstname_fathername_mother_name" &
numspace == 0 ~ "",
is.na(mothername) & namepattern == "firstname_fathername_mother_name" &
numspace == 1 ~ "",
is.na(mothername) & namepattern == "firstname_fathername_mother_name" &
numspace == 2 ~ fullname3,
is.na(mothername) & namepattern == "firstname_fathername_mother_name" &
numspace == 3 ~ fullname4,
is.na(mothername) & namepattern == "firstname_fathername_mother_name" &
numspace == 4 ~ fullname5,
is.na(mothername) & namepattern == "fathername_mothername_firstname" &
numspace == 0 ~ "",
is.na(mothername) & namepattern == "fathername_mothername_firstname" &
numspace == 1 ~ "",
is.na(mothername) & namepattern == "fathername_mothername_firstname" &
numspace == 2 ~ fullname2,
is.na(mothername) & namepattern == "fathername_mothername_firstname" &
numspace == 3 ~ fullname2,
is.na(mothername) & namepattern == "fathername_mothername_firstname" &
numspace == 4 ~ fullname2,
TRUE ~ mothername )) |>
### Now reconstruct firstname
dplyr::mutate( firstname = dplyr::case_when(
is.na(firstname) & namepattern == "firstname_fathername_mother_name" &
numspace == 0 ~ fullname1,
is.na(firstname) & namepattern == "firstname_fathername_mother_name" &
numspace == 1 ~ fullname1,
is.na(firstname) & namepattern == "firstname_fathername_mother_name" &
numspace == 2 ~ fullname1,
is.na(firstname) & namepattern == "firstname_fathername_mother_name" &
numspace == 3 ~ paste0(fullname1, " ", fullname2 ),
is.na(firstname) & namepattern == "firstname_fathername_mother_name" &
numspace == 4 ~ paste0(fullname1, " ", fullname2, " ", fullname3),
is.na(firstname) & namepattern == "fathername_mothername_firstname" &
numspace == 0 ~ "",
is.na(firstname) & namepattern == "fathername_mothername_firstname" &
numspace == 1 ~ fullname2,
is.na(firstname) & namepattern == "fathername_mothername_firstname" &
numspace == 2 ~ fullname3,
is.na(firstname) & namepattern == "fathername_mothername_firstname" &
numspace == 3 ~ paste0(fullname3, " ", fullname4 ) ,
is.na(firstname) & namepattern == "fathername_mothername_firstname" &
numspace == 4 ~ paste0(fullname3, " ", fullname4, " ", fullname5) ,
TRUE ~ firstname )) |>
### Lets reset the prefix space replacement...
dplyr::mutate(
firstname = reset_spaces_based_on_patterns(vector = firstname),
fathername = reset_spaces_based_on_patterns(vector = fathername),
mothername = reset_spaces_based_on_patterns(vector = mothername)) |>
## then clean intermediate variables
dplyr::select( - fullname1, - fullname2, - fullname3, - fullname4,
- fullname5, fullname_or, - fullname_pref, - numspace )
return(framesp)
}
## Testing...
# frame <- data |>
# dplyr::filter( is.na(nombres) ) |>
# # dplyr::select(nombre_completo, name_pattern,
# # nombres, apellido_paterno, apellido_materno) |>
# separate_fullname(fullname= "nombre_completo",
# namepattern= "name_pattern",
# firstname = "nombres",
# fathername = "apellido_paterno",
# mothername = "apellido_materno")
```
```{r function-separate_firstname }
#' separate_firstname
#'
#' This function decomposition - in case there is a space in
#'
#' Performing this name decomposition is important in order to enhance record linkage
#'
#' @param firstname first name
#' @return a clean list with c("firstname1","firstname2","firstname3")
#'
#' @export
separate_firstname <- function(frame,
firstname ) {
## Let's go!
framesp <- frame |>
## Rename variable
dplyr::rename( firstname = paste0(firstname) ) |>
### Lets clean all spaces and get everything to upper
dplyr::mutate( firstname_or = firstname,
firstname = trimws(stringr::str_squish(firstname),
which = "both",
whitespace = "[ \t\r\n]"),
## Clean strange typ e of space...
# firstname = gsub(" ", "[[:space:]]", firstname),
firstname = toupper(firstname)) |>
dplyr::mutate( firstname = remove_spaces_based_on_patterns(vector = firstname) ) |>
## Counting the number of space to understand the structure of the full name
tidyr::separate_wider_delim(firstname,
delim = " ",
names_sep = "",
too_few = "align_start") |>
dplyr::mutate( firstname1 = reset_spaces_based_on_patterns(vector = firstname1),
firstname2 = reset_spaces_based_on_patterns(vector = firstname2),
firstname3 = reset_spaces_based_on_patterns(vector = firstname3))
return(framesp)
}
## Testing
# framefirstname <- data |>
# separate_firstname(firstname = "nombres" )
```
```{r function-separate_familyname}
#' separate_familyname
#'
#' This function helps in decomposing names in case there is a space
#' in the father name and the mother name is empty
#'
#' Performing this name decomposition is important in order to enhance record linkage
#'
#' @param fathernamevar father name
#' @param mothernamevar mother name
#' @return a clean list with c("firstname1","firstname2","firstname3")
#'
#' @export
separate_familyname <- function(frame,
fathernamevar,
mothernamevar) {
## Let's go!
framesp <- frame |>
## Rename variable
dplyr::rename( fathername = paste0(fathernamevar),
mothername = paste0(mothernamevar) ) |>
### Lets clean all spaces and get everything to upper
dplyr::mutate( fathername_or = fathername,
fathername= trimws(stringr::str_squish( fathername),
which = "both",
whitespace = "[ \t\r\n]"),
# fathername = gsub(" ", "[[:space:]]", fathername),
fathername = toupper( fathername)) |>
dplyr::mutate( fathername = remove_spaces_based_on_patterns(vector = fathername) ) |>
# separate
tidyr::separate_wider_delim( fathername,
delim = " ",
names_sep = "",
too_few = "align_start") |>
dplyr::mutate( fathername1 = reset_spaces_based_on_patterns(vector = fathername1),
fathername2 = reset_spaces_based_on_patterns(vector = fathername2),
fathername3 = reset_spaces_based_on_patterns(vector = fathername3)) |>
dplyr::mutate( fathername = dplyr::if_else( is.na(mothername), fathername1, fathername_or ) ) |>
dplyr::mutate( mothername = dplyr::if_else( is.na(mothername), fathername2, mothername)) |>
dplyr::select( - fathername1, - fathername2, - fathername3, - fathername_or)
return(framesp)
}
# frame <- data |>
# dplyr::select(apellido_paterno, apellido_materno) |>
# separate_familyname( fathernamevar = "apellido_paterno",
# mothernamevar = "apellido_materno" )
```
```{r function-cleanvar}
#' cleanvar
#'
#' function for data cleaning with additional name removal logic
#'
#' @param names_column name of the column to treat
#' @param toRemove default vector with stuff to remove from name
#' c(" JR", " SR", " IV", " III", " II")
#' @return names_column_new name of the column treat
#'
#' @export
cleanvar <- function(names_column,
toRemove = c(" JR", " JUNIOR", " SR", " IV", " III", " II")) {
# Convert to uppercase
names_column_new <- toupper(names_column)
# Remove specified name suffixes
for (tR in toRemove) {
names_column_new <- gsub(tR, "", names_column_new)
}
# Convert special characters to ASCII equivalents
names_column_new <- iconv(names_column_new, "latin1", "ASCII//TRANSLIT", sub = "")
# Remove punctuation, digits, and all spaces
names_column_new <- gsub("[[:punct:][:digit:]][[:space:]]", "", names_column_new)
# Create a new variable with only alphabetic characters
names_column_new <- gsub("[^[:alpha:]]", "", names_column_new)
return(names_column_new)
}
```
## Apply Cleaning
```{r apply_clean}
## Pipeline for data post processing #####################
data.prep <- data |>
## Filter where the phone number is not available -- "NO REFIERE"
dplyr::filter( telefono != "NO REFIERE") |>
## Clean age_range
#data |> dplyr::select(age_range) |> dplyr::distinct() |> dplyr::pull()
dplyr::mutate( age_range = dplyr::case_when(
age_range == "18 A 59 AÑOS" ~ "0-4",
TRUE ~ age_range )) |>
## Clean DOb & Age
clean_age( date_birth = "fecha_de_nacimiento",
date_record = "date_record",
age = "edad",
age_range = "age_range") |>
## Clean the names
separate_fullname(fullname= "nombre_completo",
namepattern= "name_pattern",
firstname = "nombres",
fathername = "apellido_paterno",
mothername = "apellido_materno") |>
separate_firstname(firstname = "firstname") |>
separate_familyname( fathername = "fathername",
mothername = "mothername") |>
## Clean the gender variable
# data |> dplyr::select(genero) |> dplyr::distinct() |> dplyr::pull()
dplyr::mutate(gender = dplyr::case_when(
genero %in% c("F" ,"FEMENINO" ,"f", "Femenino") ~ "F",
genero %in% c("M" , "MASCULINO" , "Masculino") ~ "M",
genero %in% c("X", "Otro") ~ "Ot",
TRUE ~ NA )) |>
## Clean missing gender - using gender prediction based on first name -
# table(data.prep$gender, useNA = "ifany")
# devtools::install_github("kalimu/genderizeR")
# dplyr::mutate(gender = dplyr::case_when(
# is.na(gender) ~ genderize(firstname1, genderDB = givenNames, progress = FALSE),
# TRUE ~ gender )) |>
## Only retain the nationality of interest
# data |> dplyr::select(nacionalidad) |> dplyr::distinct() |> dplyr::pull()
dplyr::mutate(nationality = dplyr::case_when(
nacionalidad %in% c("Venzuela", "venezuela", "Venezolana",
"VENEZUELA",
"Venezuela", "VENEZOLANO", "VENEZOLANA") ~ "VEN",
nacionalidad %in% c("COLOMBIANO", "COLOMBIANA", "COLOMBIA",
"colombia", "Colombia", "Nac. Colombia",
"Colombiana" ) ~ "COL",
TRUE ~ "other" )) |>
dplyr::filter( nationality %in% c("VEN", "COL" )) |>
## Apply cleanvar()
# Perform data cleaning on dfA using the clean_names function
dplyr::mutate_at( dplyr::vars(firstname1, firstname2, fathername, mothername,
asistencia, departamento, #telefono,
planilla, socio),
list(new = cleanvar)) |>
### identify single data source
dplyr::mutate(datasource = paste0(socio_new, "_", planilla_new)) #|>
# # ## Retain only fields for record linkage
# dplyr::select(datasource, nationality,
# firstname_new, fathername_new, mothername_new,
# asistencia_new, departamento_new,
# telefono, gender)
# dput(names(data.prep))
#
#
# table(data.prep$gender, useNA = "ifany")
# table(data.prep$nationality, useNA = "ifany")
```
```{r missing_gender}
# beofre cleaning
table(data.prep$gender, useNA = "ifany")
## identify the mising..
missing_gender <- data.prep |>
dplyr::filter( is.na(gender)) |>
dplyr::select( firstname1, gender) |>
dplyr::distinct() |>
dplyr::mutate(firstname1 = gsub("[[:punct:][:digit:]][[:space:]]", "-", firstname1)) |>
dplyr::mutate(firstname1 = gsub(" ", "-", firstname1)) |>
dplyr::pull(firstname1)
## let's us an API to identify gender based on firstname
# devtools::install_github("coccopuffs/GenderGuesser")
#missing_gender_result <- GenderGuesser::guessGender(missing_gender)
# write.csv(missing_gender_result, here::here("data-raw","missing_gender_result.csv"), row.names = FALSE)
# Results are saved locally to avoid calling too many times the API
missing_gender_result <- readr::read_csv(here::here("data-raw","missing_gender_result.csv"))
missing_gender_results <- missing_gender_result |>
dplyr::filter(!(is.na(gender))) |>
dplyr::mutate( gender2 = dplyr::recode( gender,
male = "M", female = "F")) |>
dplyr::rename( firstname1 = "name") |>
dplyr::select(firstname1, gender2)
## and now updata data.prep with the gender when missing..
data.prep <- data.prep |>
dplyr::left_join(missing_gender_results, by = c("firstname1")) |>
dplyr::mutate(gender = dplyr::if_else(is.na(gender), gender2, gender))
table(data.prep$gender, useNA = "ifany")
```
## List all data sources
```{r list_source }
## Check the datasource that we will compare
table(data.prep$datasource, useNA = "ifany") |>
as.data.frame() |>
dplyr::rename( DataSource = "Var1") |>
knitr::kable()
## See if can use departamento for blocking
#table(data.prep$datasource, data.prep$departamento_new, useNA = "ifany")
alldatasource <- data.prep |>
dplyr::select(datasource) |>
dplyr::distinct() |>
dplyr::pull()
#alldatasource
alldatasource <- t(combn(alldatasource, 2, simplify = TRUE)) |>
as.data.frame() |>
dplyr::mutate ( comparison = paste0(V1, "--to--", V2))
```
## Recursively search for all fuzzy duplicates
```{r match, message=TRUE, warning=FALSE, comment=""}
## Initialise the list with duplicate
###
dup <- data.prep[0,]
for (i in 1:nrow(alldatasource)) {
# i <- 1
datasourcea <- as.character(alldatasource[i,c("V1")]) #alldatasource[9]
datasourceb <- as.character(alldatasource[i,c("V2")]) # alldatasource[6]
cat(paste0("\n ==================== \n"))
cat(paste0(i, " - Now comparing \n", datasourcea, "\n with ", datasourceb, "\n\n"))
## Let's get 2 comparison dataset... ##########
dfA <- data.prep |>
dplyr::filter(datasource == datasourcea ) |>
dplyr::select( - datasource)
dfB <- data.prep |>
dplyr::filter(datasource == datasourceb )|>
dplyr::select( - datasource)
# Delete rows that have missing First Name (FN), Last Name (LN), or Date of Birth (DOB).
dfA <- dfA |>
dplyr::filter(!is.na(firstname1_new) & firstname1_new != "" &
!is.na(firstname2_new) & firstname2_new != "" &
!is.na(firstname1_new) & firstname1_new != "" &
!is.na(fathername_new) & fathername_new != "" &
!is.na(mothername_new) & mothername_new != "" &
!is.na(day_birth) & day_birth != "" &
!is.na(month_birth) & month_birth != "" &
!is.na(year_birth) & year_birth != "" &
!is.na(gender) & gender != "" )
dfB <- dfB |>
dplyr::filter(!is.na(firstname1_new) & firstname1_new != "" &
!is.na(firstname2_new) & firstname2_new != "" &
!is.na(firstname1_new) & firstname1_new != "" &
!is.na(fathername_new) & fathername_new != "" &
!is.na(mothername_new) & mothername_new != "" &
!is.na(day_birth) & day_birth != "" &
!is.na(month_birth) & month_birth != "" &
!is.na(year_birth) & year_birth != "" &
!is.na(gender) & gender != "" )
if( nrow(dfA) > 0 & nrow(dfB) > 0 ) {
matches.out <- fastLink::fastLink(
dfA = dfA,
dfB = dfB,
# Specify the vector of variable names to be used for matching.
# These variable names should exist in both dfA and dfB
varnames = c(#"nationality" ,
"firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new",
"day_birth", "month_birth", "year_birth",
# "asistencia_new" , "departamento_new", "telefono_new" ,
"gender" ),
# Specify which variables among varnames should be compared using string distance
stringdist.match = c( "firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new"),
# Specify which variables present in stringdist.match can be partially matched
partial.match = c( "firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new"),
# Specify which variables should be matched numerically
# Must be a subset of 'varnames' and must not be present in 'stringdist.match'.
numeric.match = c( "day_birth", "month_birth", "year_birth"
# "telefono_new"
#'dob_day', 'dob_month', 'dob_year'
),
#gender.field = "gender" ,
threshold.match = c(.85, .95),
# Specify the number of CPU cores to utilize (parallel processing).
## Get the number of detected cores minus 1, Reserve one core for
#non-computational tasks to help prevent system slowdowns or unresponsiveness
n.cores = parallel::detectCores() - 1,
return.all = TRUE,
return.df = TRUE)
# Summarize the accuracy of the match:
# each column gives the match count, match rate,
# false discovery rate (FDR) and false negative rate (FNR)
# under different cutoffs for matches based on the posterior
# probability of a match.
print(summary(matches.out))
## Review the matching in each original frame
matchedA <- dfA[matches.out$matches$inds.a, ]
#matchedB <- dfB[matches.out$matches$inds.b, ]
# matchedA$listea <- datasourcea
# matchedA$listeb <- datasourceb
if ( nrow(matchedA) >0 ) {
print(knitr::kable(matchedA |>
dplyr::select( nationality ,
firstname1_new ,firstname2_new ,
fathername_new ,mothername_new,
day_birth, month_birth, year_birth,
gender )))
}
dup <- dplyr::rows_append(dup, matchedA )
# Confusion Matrice
#fastLink::confusion(matches.out, threshold = 0.98)
} else {
cat( paste0("\n FAIL because of missing data!!! \n", datasourcea, ": ",nrow(dfA)," \n",
datasourceb, ": ", nrow(dfB), "\n\n\n"))
}
}
dup1 <- dup |>
dplyr::distinct()
write.csv(dup1, here::here("data-raw","dup.csv"), row.names = FALSE )
write.csv(data.prep, here::here("data-raw","data.prep.csv"), row.names = FALSE )
# Examine the EM object:
#matches.out$EM
# ## Get the output...
# matched_dfs <- fastLink::getMatches(
# dfA = dfA,
# dfB = dfB,
# fl.out = matches.out,
# threshold.match = 0.85
# )
#
# # Display the matches ###################
# # convert dfA rownames to a column
# dfA_clean <- dfA |> rownames_to_column()
#
# # convert dfB rownames to a column
# dfB_clean <- dfB |> rownames_to_column()
#
# # convert all columns in matches dataset to character,
# #so they can be joined to the rownames
# matches_clean <- matched_dfs |>
# dplyr::mutate(dplyr::across(dplyr::everything(), as.character))
#
# # Join matches to dfA, then add dfB
# # column "inds.b" is added to dfA
# complete <- dplyr::left_join(dfA_clean,
# matches_clean,
# by = c("rowname" = "inds.a"))
#
# # column(s) from dfB are added
# complete <- dplyr::left_join(complete,
# dfB_clean,
# by = c("inds.b" = "rowname"))
```
```{r blocking, include=FALSE, eval = FALSE}
# Preprocessing Matches via Blocking #################
blockgender_out <- fastLink::blockData(dfA, dfB, varnames = "gender")
## Subset dfA into blocks
dfA_block1 <- dfA[blockgender_out$block.1$dfA.inds,]
dfA_block2 <- dfA[blockgender_out$block.2$dfA.inds,]
## Subset dfB into blocks
dfB_block1 <- dfB[blockgender_out$block.1$dfB.inds,]
dfB_block2 <- dfB[blockgender_out$block.2$dfB.inds,]
## Run fastLink on each
link.1 <- fastLink::fastLink(
dfA = dfA_block1,
dfB = dfB_block1,
# Specify the vector of variable names to be used for matching.
# These variable names should exist in both dfA and dfB
varnames = c("nationality" ,
"firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new",
"day_birth", "month_birth", "year_birth",
# "asistencia_new" , "departamento_new", "telefono_new" ,
"gender" ),
# Specify which variables among varnames should be compared using string distance
stringdist.match = c( "firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new"),
# Specify which variables present in stringdist.match can be partially matched
partial.match = c( "firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new"),
# Specify which variables should be matched numerically
# Must be a subset of 'varnames' and must not be present in 'stringdist.match'.
numeric.match = c( "day_birth", "month_birth", "year_birth"
# "telefono_new"
#'dob_day', 'dob_month', 'dob_year'
),
# Specify the number of CPU cores to utilize (parallel processing).
## Get the number of detected cores minus 1, Reserve one core for
#non-computational tasks to help prevent system slowdowns or unresponsiveness
n.cores = parallel::detectCores() - 1,
return.df = TRUE)
link.2 <- fastLink::fastLink(
dfA = dfA_block2,
dfB = dfB_block2,
# Specify the vector of variable names to be used for matching.
# These variable names should exist in both dfA and dfB
varnames = c("nationality" ,
"firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new",
"day_birth", "month_birth", "year_birth",
# "asistencia_new" , "departamento_new", "telefono_new" ,
"gender" ),
# Specify which variables among varnames should be compared using string distance
stringdist.match = c( "firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new"),
# Specify which variables present in stringdist.match can be partially matched
partial.match = c( "firstname1_new" ,"firstname2_new" ,
"fathername_new" ,"mothername_new"),
# Specify which variables should be matched numerically
# Must be a subset of 'varnames' and must not be present in 'stringdist.match'.
numeric.match = c( "day_birth", "month_birth", "year_birth"
# "telefono_new"
#'dob_day', 'dob_month', 'dob_year'
),
# Specify the number of CPU cores to utilize (parallel processing).
## Get the number of detected cores minus 1, Reserve one core for
#non-computational tasks to help prevent system slowdowns or unresponsiveness
n.cores = parallel::detectCores() - 1,
return.df = TRUE)
## aggregate multiple matches into a single summary with aggregateEM()
agg.out <- fastLink::aggregateEM(em.list = list(link.1, link.2))
# Preprocessing Matches via Blocking #################
```
```{r}
## Duplicate manually identified in dup
table(data.prep$dup, useNA = "ifany")
## Remove the duplicate
sampling.universe <- data.prep |>
## Remove duplicate
dplyr::filter( dup == "0") |>
## Keep only people over 15
dplyr::filter(age > 14) |>
dplyr::select( n, firstname1, firstname2, firstname3,fathername, mothername,
gender, nationality, date_birth,
age, telefono, departamento )
#set.seed function
set.seed(1976)
# Let set set a desired sample of 600
# Draw a random sample from the data frame
sample <- sampling.universe |>
dplyr::slice_sample( n = 600, replace = FALSE)
write.csv(sample, here::here("data-raw","sample.csv"), row.names = FALSE )
```