Skip to contents

Native rmorie implementation of genetic matching (Diamond & Sekhon 2013): a real-coded genetic algorithm searches the diagonal Mahalanobis weight matrix maximizing worst-case covariate balance (paired-t p-value of the worst covariate). Deterministic given seed. No Matching/rgenoud at runtime; returns a morie_match_result with the selected weights in details$best_weights.

Usage

morie_matching_genetic(
  data,
  treatment,
  covariates,
  n_neighbors = 1L,
  pop_size = 50L,
  n_generations = 20L,
  seed = 42L
)

Arguments

data

Data frame.

treatment

Binary treatment column name.

covariates

Character vector of covariates.

n_neighbors

Number of matches per treated unit (M in Matching).

pop_size

Genetic-algorithm population size (default 50).

n_generations

Number of GA generations.

seed

Random seed.

Value

A list of class morie_match_result.

References

Diamond, A., & Sekhon, J. S. (2013). Genetic matching for estimating causal effects. Review of Economics and Statistics, 95(3), 932–945.

Examples

# \donttest{
set.seed(1)
df <- data.frame(y = rnorm(200), d = rbinom(200, 1, 0.4),
                 x1 = rnorm(200), x2 = rnorm(200))
morie_matching_genetic(df, "d", c("x1", "x2"),
                       pop_size = 50, n_generations = 20)
#> $matched_data
#>                y d           x1           x2
#> 1   -0.626453811 1  0.893673702  0.077303123
#> 2    0.183643324 0 -1.047298149 -0.296868642
#> 3   -0.835628612 1  1.971337386 -1.183242240
#> 4    1.595280802 1 -0.383632106  0.011292688
#> 5    0.329507772 1  1.654145302  0.991601036
#> 6   -0.820468384 1  1.512212694  1.593967454
#> 7    0.487429052 0  0.082965734 -1.372711271
#> 8    0.738324705 1  0.567220915 -0.249610933
#> 9    0.575781352 0 -1.024548480  1.159424527
#> 10  -0.305388387 1  0.323006503 -1.114222348
#> 11   1.511781168 1  1.043612458 -2.528500689
#> 12   0.389843236 0  0.099078487 -0.935902559
#> 13  -0.621240581 1 -0.454136909 -0.967239458
#> 14  -2.214699887 1 -0.655781852  0.047488592
#> 15   1.124930918 1 -0.035922423 -0.403736793
#> 16  -0.044933609 0  1.069161461  0.231496128
#> 17  -0.016190263 0 -0.483974930 -0.422372408
#> 18   0.943836211 0 -0.121010111  0.374118395
#> 19   0.821221195 1 -1.294140004 -0.366005775
#> 20   0.593901321 1  0.494312836  1.190101447
#> 21   0.918977372 0  1.307901520 -0.737327525
#> 22   0.782136301 0  1.497041009  0.290666645
#> 23   0.074564983 1  0.814702731 -0.884849568
#> 24  -1.989351696 0 -1.869788790  0.208006479
#> 25   0.619825748 0  0.482029504 -0.047730172
#> 27  -0.155795507 0 -0.353400286 -0.144226557
#> 28  -1.470752384 1  0.170489471  1.180213666
#> 29  -0.478150055 1 -0.864035954  0.681399923
#> 30   0.417941560 1  0.679230774  0.143247631
#> 31   1.358679552 1 -0.327101015 -1.192316444
#> 32  -0.102787727 0 -1.569082185  1.169228653
#> 33   0.387671612 0 -0.367450756  0.079201709
#> 34  -0.053805041 0  1.364434929 -0.451773753
#> 35  -1.377059557 0 -0.334281365  1.642028213
#> 36  -0.414994563 1  0.732750042 -0.769592322
#> 37  -0.394289954 1  0.946585640  0.303360961
#> 38  -0.059313397 0  0.004398704  1.281737421
#> 39   1.100025372 0 -0.352322306  0.602222795
#> 40   0.763175748 1 -0.529695509 -0.307022265
#> 41  -0.164523596 0  0.739589226 -0.418418103
#> 42  -0.253361680 0 -1.063457415  0.355135530
#> 43   0.696963375 0  0.246210844  0.513481115
#> 44   0.556663199 1 -0.289499367  0.018607400
#> 46  -0.707495157 0 -1.408850456 -0.065832000
#> 48   0.768532925 1 -0.191278951  0.537326132
#> 49  -0.112346212 0  0.803283216 -2.201782322
#> 50   0.881107726 0  1.887474463  0.391973744
#> 51   0.398105880 1  1.473881181  0.496960952
#> 52  -0.612026393 0  0.677268492 -0.224874715
#> 53   0.341119691 0  0.379962687 -1.117143165
#> 54  -1.129363096 0 -0.192798426 -0.394994603
#> 55   1.433023702 0  1.577891795  1.549830342
#> 56   1.980399899 1  0.596234109 -0.743514480
#> 57  -0.367221476 0 -1.173576941 -2.331712118
#> 58  -1.044134626 1 -0.155642535  0.812245442
#> 59   0.569719627 1 -1.918909820 -0.501310657
#> 62  -0.039240003 0  1.314002167 -0.022558628
#> 63   0.689739362 0 -0.635543001  0.701239300
#> 64   0.028002159 0 -0.429978839 -0.587482026
#> 66   0.188792300 1  0.612218174  1.096640215
#> 67  -1.804958629 1  0.678340177 -0.247509677
#> 68   1.465554862 0  0.567951972 -0.159901713
#> 69   0.153253338 0 -0.572542604 -0.625778251
#> 70   2.172611670 1 -1.363291256  0.900434636
#> 71   0.475509529 0 -0.388722244 -0.994193629
#> 72  -0.709946431 1  0.277914132  0.849250386
#> 73   0.610726353 1 -0.823081122  0.805702289
#> 74  -0.934097632 0 -0.068840934 -0.467600936
#> 75  -1.253633400 0 -1.167662326  0.848420314
#> 76   0.291446236 0 -0.008309014  0.986769864
#> 77  -0.443291873 1  0.128855402  0.575620289
#> 78   0.001105352 0 -0.145875628  2.024842045
#> 80  -0.589520946 1  1.763552003 -1.164920931
#> 81  -0.568668733 1  0.762586512 -1.376519214
#> 82  -0.135178615 0  1.111431081  0.167679934
#> 83   1.178086997 1 -0.923206953  1.584629079
#> 84  -1.523566800 1  0.164341838  1.677888953
#> 85   0.593946188 0  1.154825187  0.488296698
#> 86   0.332950371 0 -0.056521425  0.878673263
#> 87   1.063099837 0 -2.129360648 -0.144874874
#> 88  -0.304183924 0  0.344845762  0.468971760
#> 89   0.370018810 1 -1.904955446  0.376235477
#> 90   0.267098791 0 -0.811170153 -0.761040275
#> 91  -0.542520031 0  1.324004321 -0.293294934
#> 92   1.207867806 1  0.615636849 -0.134841264
#> 93   1.160402616 0  1.091668956  1.393845816
#> 94   0.700213650 0  0.306604862 -1.036988690
#> 96   0.558486426 0 -0.924312773  0.768278218
#> 97  -1.276592208 0  1.592913754 -0.816160621
#> 99  -1.224612615 1 -0.715128401  0.904705031
#> 100 -0.473400636 0  0.865223100 -0.763086265
#> 101 -0.620366677 0  1.074440958 -0.341066980
#> 102  0.042115873 1  1.895654774  1.502424534
#> 103 -0.910921649 1 -0.602997304  0.528307712
#> 104  0.158028772 1 -0.390867821  0.542191355
#> 105 -0.654584644 0 -0.416222032 -0.136673356
#> 106  1.767287269 1 -0.375657423 -1.136733853
#> 107  0.716707476 0 -0.366630946 -1.496627154
#> 108  0.910174229 1 -0.295677453 -0.223385644
#> 109  0.384185358 1  1.441820410  2.001719228
#> 110  1.682176081 0 -0.697538292  0.221703816
#> 111 -0.635736454 1 -0.388167506  0.164372909
#> 112 -0.461644730 0  0.652536452  0.332623609
#> 113  1.432282239 0  1.124772447 -0.385207999
#> 114 -0.650696353 1 -0.772110803 -1.398754027
#> 115 -0.207380744 1 -0.508086216  2.675740796
#> 116 -0.392807929 0  0.523620590 -0.423686089
#> 117 -0.319992869 1  1.017754227 -0.298601512
#> 119  0.494188331 0 -1.429993447 -0.248008225
#> 120 -0.177330482 1  1.709121032 -0.247303918
#> 121 -0.505957462 1  1.435069572 -0.255510379
#> 122  1.343038825 1 -0.710371146 -1.786938100
#> 123 -0.214579409 0 -0.065067574  1.784662816
#> 124 -0.179556530 0 -1.759468735  1.763586348
#> 125 -0.100190741 0  0.569722972  0.689600222
#> 126  0.712666307 1  1.612346798 -1.100740644
#> 127 -0.073564404 1 -1.637280647  0.714509357
#> 128 -0.037634171 1 -0.779568513 -0.246470317
#> 129 -0.681660479 1 -0.641176934 -0.319786166
#> 130 -0.324270272 1 -0.681131394  1.362644293
#> 131  0.060160440 1 -2.033285596 -1.227882590
#> 132 -0.588894486 0  0.500963559 -0.511219233
#> 133  0.531496193 0 -1.531798140 -0.731194999
#> 135  0.306557861 0  0.592984721 -1.572863915
#> 137 -0.300976127 0  0.892008392  0.715932089
#> 138 -0.528279904 0 -0.025715071  0.465214906
#> 140 -0.056896778 1  0.646359415  0.559217730
#> 142  1.176583312 1  1.772611185 -0.340484927
#> 143 -1.664972436 1 -0.018259711  0.713033195
#> 144 -0.463530401 0  0.852814994 -0.659037386
#> 147  2.087166546 1 -1.366111931  0.847792797
#> 149 -1.286300530 0  0.236803664 -0.323650632
#> 150 -1.640605534 1 -2.342723120 -0.255248113
#> 151  0.450187101 1  0.961696633  0.060921227
#> 152 -0.018559833 0 -0.604425734 -0.823491629
#> 153 -0.318068375 0 -0.752877279  1.829730485
#> 154 -0.929362147 1 -1.555611593 -1.429916216
#> 155 -1.487460310 1 -1.453893738  0.254137143
#> 157  1.000028804 0  0.509369407  0.002415809
#> 158 -0.621266695 0 -2.097882960  0.509665571
#> 159 -1.384426847 1 -1.004361979 -1.084720001
#> 160  1.869290622 1  0.535771722  0.704832977
#> 161  0.425100377 1 -0.453037085  0.330976350
#> 162 -0.238647101 0  2.165368502  0.976327473
#> 164  0.886422651 1  0.595498034 -0.970579905
#> 165 -0.619243048 1  0.004884450 -1.771531349
#> 166  2.206102465 1  0.279360782 -0.322470342
#> 167 -0.255027030 0 -0.705906125 -1.338800742
#> 168 -1.424494650 1  0.628017153  0.688156028
#> 169 -0.144399602 0  1.480213960  0.071280652
#> 170  0.207538339 1  1.083429910  2.189752359
#> 171  2.307978399 0 -0.813244257 -1.157707599
#> 172  0.105802368 0 -1.618876849  1.181688064
#> 174 -0.077152935 0  0.440889371 -1.456628011
#> 175 -0.334000842 1  1.350993980  0.572967370
#> 176 -0.034726028 0 -1.318609485 -1.433377705
#> 177  0.787639606 1  0.364384593 -1.055185019
#> 178  2.075245009 0  0.233499835 -0.733111877
#> 179  1.027392439 1  1.193955261  0.210907264
#> 180  1.207908398 1 -0.027909972 -0.998920727
#> 181 -1.231323422 0 -0.357298855  1.077850323
#> 182  0.983895570 1 -1.146814136 -1.198974383
#> 183  0.219924804 1 -0.517420484  0.216637035
#> 184 -1.467250029 0 -0.362123773  0.143087030
#> 185  0.521022743 0  2.350554326 -1.065750091
#> 186 -0.158754605 0  2.446531376 -0.428623411
#> 189 -0.430211754 1 -1.972934934  1.556052636
#> 190 -0.926109497 1  0.514671633 -1.040796434
#> 193 -0.731748173 1 -0.885617573 -1.967195349
#> 194  0.830373168 1  0.111106430 -0.755903643
#> 195 -1.208082786 1  3.810276681  0.461149161
#> 196 -1.047984413 1 -1.108909998  0.145106631
#> 199  0.411974712 0  0.347653649  0.655051998
#> 200 -0.381076051 0 -0.873264535 -0.304508837
#> 
#> $n_treated
#> [1] 87
#> 
#> $n_matched_control
#> [1] 87
#> 
#> $match_pairs
#>    treated_idx control_idx   distance
#> 1            1          16 0.60734087
#> 2            3         185 1.24400205
#> 3            4          33 0.10312159
#> 4            5          55 0.75849192
#> 5            6          93 1.39508227
#> 6            8          68 0.11600456
#> 7           10          94 0.11228422
#> 8           11          49 0.88079829
#> 9           13          71 0.21471532
#> 10          14         110 0.26072704
#> 11          15          74 0.13627336
#> 12          19         119 0.46430179
#> 13          20         125 0.68747297
#> 14          23         100 0.22943740
#> 15          28          38 0.55295778
#> 16          29          96 0.22393756
#> 17          30         112 0.25815605
#> 18          31         107 0.41593722
#> 19          36         144 0.41783201
#> 20          37         137 0.55881880
#> 21          40          17 0.20850842
#> 22          44         184 0.28312062
#> 23          48          18 0.30796368
#> 24          51          22 0.27569533
#> 25          56         132 0.42722764
#> 26          58          86 0.33476491
#> 27          59          87 0.81737971
#> 28          66         199 1.04032789
#> 29          67          52 0.02939588
#> 30          70          75 0.63784834
#> 31          72          43 0.44781586
#> 32          73          63 0.62160894
#> 33          77         138 0.52451828
#> 34          80          97 0.70804298
#> 35          81         135 0.61078780
#> 36          83         153 0.64258550
#> 37          84         123 0.75545121
#> 38          89          24 0.24374842
#> 39          92         157 0.38523920
#> 40          99           9 1.05199439
#> 41         102         162 1.09920569
#> 42         103          39 0.82166137
#> 43         104         181 0.70276627
#> 44         106          64 0.72830770
#> 45         108          27 0.21196496
#> 46         109          85 2.18204741
#> 47         111         105 0.40120956
#> 48         114         167 0.22993137
#> 49         115          78 1.43390077
#> 50         117         101 0.19119518
#> 51         120         169 0.84340534
#> 52         121          34 0.34512870
#> 53         122         171 0.87352863
#> 54         126          21 1.08710731
#> 55         127         172 0.60780248
#> 56         128         200 0.31481475
#> 57         129          69 0.45042079
#> 58         130          35 1.18947960
#> 59         131         133 1.76190643
#> 60         140          25 0.95684642
#> 61         142          50 1.02381093
#> 62         143          76 0.35582815
#> 63         147          32 0.77287926
#> 64         150         158 1.28006813
#> 65         151          82 0.50803581
#> 66         154         176 0.76986145
#> 67         155          46 0.43611834
#> 68         159          90 0.76056448
#> 69         160          88 0.69607511
#> 70         161          54 1.25141158
#> 71         164          53 0.72868933
#> 72         165           7 0.57859491
#> 73         166         149 0.13828821
#> 74         168          41 1.46888665
#> 75         170          62 2.94234630
#> 76         175          91 1.12454809
#> 77         177         174 0.57116300
#> 78         179         113 0.80643937
#> 79         180          12 0.42195539
#> 80         182           2 1.21551491
#> 81         183         152 1.37865751
#> 82         189         124 0.74808217
#> 83         190         116 0.79859058
#> 84         193          57 1.05504296
#> 85         194         178 0.39923511
#> 86         195         186 4.59736155
#> 87         196          42 0.31135532
#> 
#> $method
#> [1] "genetic (rmorie native)"
#> 
#> $details
#> $details$engine
#> [1] "native-genetic-ga"
#> 
#> $details$best_weights
#>        x1        x2 
#> 11.879878  1.758416 
#> 
#> $details$best_fitness
#> [1] 0.5530513
#> 
#> $details$pop_size
#> [1] 50
#> 
#> $details$n_generations
#> [1] 20
#> 
#> 
#> attr(,"class")
#> [1] "morie_match_result" "list"              
# }