# FlexRL-vignette

    library(FlexRL)
    #> If you are happy with FlexRL, please cite us! Also, if you are unhappy, please cite us anyway.
    #> HERE ADD
    #> bibtex format in CITATION.

`FlexRL` is an algorithm for Record Linkage written in R and cpp. It uses a Stochastic Expectation Maximisation approach to combine 2 data sources and outputs the set of records referring to the same entities.

More details are provided in our paper.

This vignette uses example subsets from the SHIW data.

    df2016 = read.csv("exA.csv", row.names = 1)
    df2020 = read.csv("exB.csv", row.names = 1)

    head(df2016)
    #>   ANASCI SESSO STACIV STUDIO IREG       ID ANNO PAR NASCREG ETA
    #> 1   1953     1      1      2    2 501068_1 2016   1      19  63
    #> 2   1960     2      1      2    2 501068_2 2016   2      19  56
    #> 3   1955     1      1      3    2 501082_1 2016   1       2  61
    #> 4   1958     2      1      4    2 501082_2 2016   2       2  58
    #> 5   1961     2      1      3    2 680914_1 2016   1       2  55
    #> 6   1965     1      1      3    2 680914_2 2016   2       2  51

    head(df2020)
    #>   ANASCI SESSO STACIV STUDIO IREG       ID ANNO PAR NASCREG ETA
    #> 1   1953     1      1      2    2 501068_1 2020   1      19  67
    #> 2   1960     2      1      3    2 501068_2 2020   2      19  60
    #> 3   1955     1      1      3    2 501082_1 2020   1       2  65
    #> 4   1958     2      1      4    2 501082_2 2020   2       2  62
    #> 5   1961     2      3      3    2 686717_1 2020   1      18  59
    #> 6   1998     1      2      3    2 686717_4 2020   3       2  22

We will use several Partially Identifying Variables to link the records: birth year, sex, marital status, education level, regional code. We treat them all as stable i.e. not evolving across time (though it may not be true). We show an example with PIVs that evolve over time later.

    PIVs_config = list(
      ANASCI     = list(stable = TRUE),
      SESSO      = list(stable = TRUE),
      STACIV     = list(stable = TRUE),
      STUDIO     = list(stable = TRUE),
      IREG       = list(stable = TRUE)
      )
    PIVs = names(PIVs_config)
    PIVs_stable = sapply(PIVs_config, function(x) x$stable)

A first step in record linkage is to remove the records for which the linking variables are outside of the common support. If a record in the first file indicates a birth year which does not appear in the second file, we can suppose that this record has no link in the second file.

We need to reinitialise the rownames to be used as indices later (to compare the true pairs and the linked pairs for instance).

    for(i in 1:length(PIVs)){
      intersect_support_piv = intersect( unique(df2016[,PIVs[i]]), unique(df2020[,PIVs[i]]) )
      df2016 = df2016[df2016[,PIVs[i]] %in% c(NA,intersect_support_piv),]
      df2020 = df2020[df2020[,PIVs[i]] %in% c(NA,intersect_support_piv),]
    }

    rownames(df2016) = 1:nrow(df2016)
    rownames(df2020) = 1:nrow(df2020)

For these example data sets we know the true linkage structure.

    links = intersect(df2016$ID, df2020$ID)
    Nlinks = length(links)

    TrueDelta = data.frame( matrix(0, nrow=0, ncol=2) )
    for (i in 1:Nlinks)
    {
      id = links[i]
      id16 = which(df2016$ID == id)
      id20 = which(df2020$ID == id)
      TrueDelta = rbind(TrueDelta, cbind(rownames(df2016[id16,]),rownames(df2020[id20,])))
    }
    true_pairs = do.call(paste, c(TrueDelta, list(sep="_")))

    nrow(df2016)
    #> [1] 49

    nrow(df2020)
    #> [1] 42

    Nlinks
    #> [1] 27

We can look at the level of agreement of the PIVs in the set of true links, and the proportion of missing values:

    colSums(df2016[TrueDelta[,1],PIVs] == df2020[TrueDelta[,2],PIVs]) / Nlinks
    #>    ANASCI     SESSO    STACIV    STUDIO      IREG
    #> 1.0000000 1.0000000 0.9259259 0.7777778 1.0000000

    colSums(is.na(df2016[TrueDelta[,1],PIVs])) / Nlinks
    #> ANASCI  SESSO STACIV STUDIO   IREG
    #>      0      0      0      0      0

    colSums(is.na(df2020[TrueDelta[,2],PIVs])) / Nlinks
    #> ANASCI  SESSO STACIV STUDIO   IREG
    #>      0      0      0      0      0

`FlexRL` needs a specific encoding of the data to run properly. The categorical observed values in the data should be mapped to the set of natural numbers; missing values should be encoded as 0. The algorithm requires a column “source” and, it denotes by B the biggest data set.

    df2016$source = "df2016"
    df2020$source = "df2020"

    if(nrow(df2020)>nrow(df2016)){
      encodedA = df2016
      encodedB = df2020
      cat("df2020 is the largest file, denoted encodedB")
    }else{
      encodedB = df2016
      encodedA = df2020
      cat("df2016 is the largest file, denoted encodedB")
    }
    #> df2016 is the largest file, denoted encodedB

    levels_PIVs = lapply(PIVs, function(x) levels(factor(as.character(c(encodedA[,x], encodedB[,x])))))

    for(i in 1:length(PIVs))
    {
      encodedA[,PIVs[i]] = as.numeric(factor(as.character(encodedA[,PIVs[i]]), levels=levels_PIVs[[i]]))
      encodedB[,PIVs[i]] = as.numeric(factor(as.character(encodedB[,PIVs[i]]), levels=levels_PIVs[[i]]))
    }
    nvalues = sapply(levels_PIVs, length)
    names(nvalues) = PIVs

    encodedA[,PIVs][ is.na(encodedA[,PIVs]) ] = 0
    encodedB[,PIVs][ is.na(encodedB[,PIVs]) ] = 0

The number of unique values per PIV gives information on their discriminative power:

    nvalues
    #> ANASCI  SESSO STACIV STUDIO   IREG
    #>     28      2      4      5      1

We can now launch `FlexRL`:

We elaborate on all the parameters below.

    data = list( A                    = encodedA,
                 B                    = encodedB,
                 Nvalues              = nvalues,
                 PIVs_config          = PIVs_config,
                 controlOnMistakes    = c(TRUE, TRUE, FALSE, FALSE, FALSE),
                 sameMistakes         = TRUE,
                 phiMistakesAFixed    = FALSE,
                 phiMistakesBFixed    = FALSE,
                 phiForMistakesA      = c(NA, NA, NA, NA, NA),
                 phiForMistakesB      = c(NA, NA, NA, NA, NA)
                 )

    fit = FlexRL::stEM(  data                 = data,
                 StEMIter             = 50,
                 StEMBurnin           = 30,
                 GibbsIter            = 100,
                 GibbsBurnin          = 70,
                 musicOn              = TRUE,
                 newDirectory         = NULL,
                 saveInfoIter         = FALSE
                 )
    #> Starting FlexRL
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6142131979695430.35645798082346300.0293288212069938
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6172839506172840.35690235690235700.0258136924803591
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6094091903719910.36433260393873100.0262582056892779
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6269487750556790.34632516703786200.0267260579064588
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6345744680851060.34574468085106400.0196808510638298
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6203950880939670.35557928457020800.0240256273358249
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6171745152354570.35844875346260400.0243767313019391
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6200106439595530.3608302288451300.0191591271953167
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6204986149584490.35013850415512500.0293628808864266
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6200657894736840.35526315789473700.0246710526315789
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6073157611619150.3674018289402900.0252824098977945
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6013584117032390.36886102403343800.0297805642633229
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6217087587318650.34981192907039200.0284793121977431
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.625543478260870.3494565217391300.025
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6230479267635970.35110393107162100.0258481421647819
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6186895810955960.35069817400644500.0306122448979592
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6368051807879110.33513221802482500.0280626011872639
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6247379454926620.3474842767295600.0277777777777778
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6150627615062760.36035564853556500.024581589958159
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6164021164021160.35820105820105800.0253968253968254
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6157135221806520.35756280064136800.0267236771779797
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6183246073298430.34712041884816800.0345549738219895
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6129198966408270.35503875968992200.0320413436692506
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6146109052408680.35044997353096900.034939121228163
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6130890052356020.35654450261780100.0303664921465969
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6151419558359620.35120925341745500.0336487907465825
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6126461211477150.35387885228480300.0334750265674814
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.5995872033023740.37358101135190900.0268317853457172
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6116605934409160.36179073399271200.0265486725663717
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6165723108594960.3602676273803400.0231600617601647
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.617890382626680.35625646328852100.0258531540847983
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6249356002060790.34621329211746500.0288511076764554
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6126359399274990.35888140859658200.0284826514759192
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6226708074534160.35507246376811600.0222567287784679
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6190724335591450.35852006253256900.0224075039082856
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6170655567117590.35744016649323600.0254942767950052
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6255801959773080.35121196493037600.0232078390923156
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.620469083155650.35394456289978700.0255863539445629
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6231578947368420.34842105263157900.028421052631579
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6147278548559230.35699039487726800.028281750266809
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6182008368200840.34623430962343100.0355648535564854
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6248688352570830.33997901364113300.0351521511017838
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611
    #> Warning in FlexRL::stEM(data = data, StEMIter = 50, StEMBurnin = 30, GibbsIter = 100, :
    #> An error may appear due to some rare values in PIV3
    #> So far, eta 3:
    #> 0.6109674081738230.35695809622348700.0320744956026901
    #> Count values of PIV3in A:
    #> 241422
    #> and in B:
    #> 311611

- A

- B

- Nvalues

- PIVs_config

- controlOnMistakes

- sameMistakes

- phiMistakesAFixed

- phiMistakesBFixed

- phiForMistakesA

- phiForMistakesB

- newDirectory

- data

- StEMIter

- StEMBurnin

- GibbsIter

- GibbsBurnin

- sparseOutput

- musicOn

- newDirectory

- saveInfoIter

The algorithm returns:

- Delta

- gamma

- eta

- alpha

- phi

    DeltaResult = fit$Delta
    colnames(DeltaResult) = c("idx20","idx16","probaLink")
    DeltaResult = DeltaResult[DeltaResult$probaLink>0.5,]
    DeltaResult
    #>    idx20 idx16 probaLink
    #> 1      1     1     0.981
    #> 3     21     3     0.544
    #> 4      4     4     0.969
    #> 6     38     7     0.868
    #> 10    31    11     0.664
    #> 12     6    14     0.993
    #> 13     7    15     0.985
    #> 14     8    16     0.974
    #> 15     9    17     0.974
    #> 16    10    18     0.971
    #> 17    11    19     0.991
    #> 18    12    20     0.983
    #> 19    36    21     0.662
    #> 20    13    22     0.900
    #> 23    14    23     0.952
    #> 24    15    24     0.995
    #> 26    29    25     0.544
    #> 28    17    28     0.710
    #> 31    24    30     0.867
    #> 35    19    34     0.962
    #> 36    30    36     0.981
    #> 37    20    37     0.696
    #> 39     3    38     0.549
    #> 41     5    39     0.506
    #> 44     2    41     0.692
    #> 46    26    43     0.882
    #> 49    27    44     0.514
    #> 52    28    46     0.865
    #> 55    39    49     0.560

We can then compare the linked records with the true links:

    results = data.frame( Results=matrix(NA, nrow=6, ncol=1) )
    rownames(results) = c("tp","fp","fn","f1score","fdr","sens.")
    if(nrow(DeltaResult)>1){
      linked_pairs    = do.call(paste, c(DeltaResult[,c("idx16","idx20")], list(sep="_")))
      truepositive    = length( intersect(linked_pairs, true_pairs) )
      falsepositive   = length( setdiff(linked_pairs, true_pairs) )
      falsenegative   = length( setdiff(true_pairs, linked_pairs) )
      precision       = truepositive / (truepositive + falsepositive)
      fdr             = 1 - precision
      sensitivity     = truepositive / (truepositive + falsenegative)
      f1score         = 2 * (precision * sensitivity) / (precision + sensitivity)
      results[,"FlexRL"] = c(truepositive,falsepositive,falsenegative,f1score,fdr,sensitivity)
    }
    results
    #>         Results     FlexRL
    #> tp           NA 18.0000000
    #> fp           NA 11.0000000
    #> fn           NA  9.0000000
    #> f1score      NA  0.6428571
    #> fdr          NA  0.3793103
    #> sens.        NA  0.6666667

The level of agreement among the linked records differs from the one in the true links:

    colSums(encodedA[DeltaResult[,"idx20"],PIVs] == encodedB[DeltaResult[,"idx16"],PIVs]) / nrow(DeltaResult)
    #>    ANASCI     SESSO    STACIV    STUDIO      IREG
    #> 1.0000000 0.8620690 0.9310345 0.9655172 1.0000000

The missing values and potential mistakes in the registration, the size of the overlapping set of records between the files, the instability of some PIVs, their distribution, and dependencies among them, are obstacles to the linkage.

Several other algorithms for Record Linkage have been developed so far in the literature. We cite some of them in our paper but more can be found. Each has its own qualities and flaws. `FlexRL` usually outperforms in scenarios where the PIVs have low discriminative power with few expected registration errors; it is particularly efficient when there is enough information to model instability of some PIV changing across time (such as the postal code). More can be found on this topic in the simulation setting of the paper, which is provided in `FlexRL-experiments`.