2016-08-29 61 views
2

我正在嘗試使用RecordLinkage包生成唯一的ID列。在使用較小數據集(< = 1,000,000)時,我已成功完成此操作,但未能在包中使用不同(但類似)功能的較大數據集(> 1,000,000)中重現此結果。儘管事實上在記錄中可能存在一些錯誤(接近匹配)或重複的事實,但我會爲其提供多個標識符變量,但我想生成一個唯一標識。使用RecordLinkage包爲大數據集生成唯一的ID列

鑑於標識符的一些數據幀:

data(RLdata500) 
df_identifiers <- RLdata500 

這是對於較小的datesets代碼(工作):

df_identifiers <- df_identifiers %>% mutate(ID = 1:nrow(df_identifiers)) 
rpairs <- compare.dedup(df_identifiers) 
p=epiWeights(rpairs) 
classify <- epiClassify(p,0.3) 
matches <- getPairs(object = classify, show = "links", single.rows = TRUE) 

# this code writes an "ID" column that is the same for similar identifiers 
classify <- matches %>% arrange(ID.1) %>% filter(!duplicated(ID.2)) 
df_identifiers$ID_prior <- df_identifiers$ID 

# merge matching information with the original data 
df_identifiers <- left_join(df_identifiers, matches %>% select(ID.1,ID.2), by=c("ID"="ID.2")) 

# replace matches in ID with the thing they match with from ID.1 
df_identifiers$ID <- ifelse(is.na(df_identifiers$ID.1), df_identifiers$ID, df_identifiers$ID.1) 

這種方法進行了討論here。但是,當使用其他函數應用於較大的數據集時,此代碼似乎不可擴展。例如,compare.dedup大數據相當於是RLBigDataDedup,其RLBigData類支持類似的功能,如epiWeightsepiClassifygetPairs等與RLBigDataDedup更換compare.dedup沒有在這種情況下工作。

考慮用於大型數據集的嘗試以下:

df_identifiers <- df_identifiers %>% mutate(ID = 1:nrow(df_identifiers)) 
rpairs <- RLBigDataDedup(df_identifiers) 
p=epiWeights(rpairs) 
(. . .) 

在此,其餘的代碼幾乎是相同的,所述第一的。儘管epiWeightsepiClassify按預期在RLBigData類上工作,但getPairs沒有。函數getPairs不使用show = "links"參數。因此,所有後續代碼都不起作用。

在使用RLBigData類中的較大數據集時,是否需要採取不同的方法來生成唯一ID列?或者這僅僅是一個限制嗎?

回答

0

首先,導入以下庫:

library(RecordLinkage) 
library(dplyr) 
library(magrittr) 

考慮從RecordLinkage包這些示例數據集:

data(RLdata500) 
data(RLdata10000) 

假設我們關心這些匹配變量和門檻:

matching_variables <- c("fname_c1", "lname_c1", "by", "bm", "bd") 
threshold <- 0.5 

SMALL數據集的記錄鏈接如下:

RLdata <- RLdata500 
df_names <- data.frame(RLdata[, matching_variables]) 
df_names %>% 
    compare.dedup() %>% 
    epiWeights() %>% 
    epiClassify(threshold) %>% 
    getPairs(show = "links", single.rows = TRUE) -> matching_data 

在此,下面的小數據操作可以被應用到附加適當的ID,以給定的數據集(從here相同的碼):

RLdata_ID <- left_join(mutate(df_names, ID = 1:nrow(df_names)), 
         select(matching_data, id1, id2) %>% 
         arrange(id1) %>% filter(!duplicated(id2)), 
         by = c("ID" = "id2")) %>% 
    mutate(ID = ifelse(is.na(id1), ID, id1)) %>% 
    select(-id1) 
RLdata$ID <- RLdata_ID$ID 

等效代碼對於大型數據集如下:

RLdata <- RLdata10000 
df_names <- data.frame(RLdata[, matching_variables]) 
df_names %>% 
    RLBigDataDedup() %>% 
    epiWeights() %>% 
    epiClassify(threshold) %>% 
    getPairs(filter.link = "link", single.rows = TRUE) -> matching_data 

在這裏,下面的大型數據處理可以被應用到附加適當的ID,以給定的數據集(類似於從here到代碼):

RLdata_ID <- left_join(mutate(df_names, ID = 1:nrow(df_names)), 
         select(matching_data, id.1, id.2) %>% 
         arrange(id.1) %>% filter(!duplicated(id.2)), 
         by = c("ID" = "id.2")) %>% 
    mutate(ID = ifelse(is.na(id.1), ID, id.1)) %>% 
    select(-id.1) 
RLdata$ID <- RLdata_ID$ID