pre match check

test <- all %>% na.omit()
m.out0 <- matchit(group ~ Gender_Legal_Sex +age_bl+htn+dm+cad+              
                  chf+copd+cancer+nsaid+beta_blockers+statins+ace_arb+ppi+diur+immunosuppressant+
                  vaccine+pre_cre_freq+pre_inpt_freq+pre_outpt_freq+renal_group2+    
                  prior_infection, data = test,
                  method = NULL, distance = "glm")
summary(m.out0)
## 
## Call:
## matchit(formula = group ~ Gender_Legal_Sex + age_bl + htn + dm + 
##     cad + chf + copd + cancer + nsaid + beta_blockers + statins + 
##     ace_arb + ppi + diur + immunosuppressant + vaccine + pre_cre_freq + 
##     pre_inpt_freq + pre_outpt_freq + renal_group2 + prior_infection, 
##     data = test, method = NULL, distance = "glm")
## 
## Summary of Balance for All Data:
##                                   Means Treated Means Control Std. Mean Diff.
## distance                                 0.4814        0.2696          0.9409
## Gender_Legal_SexFemale                   0.5011        0.5785         -0.1550
## Gender_Legal_SexMale                     0.4989        0.4215          0.1550
## age_bl                                  72.7977       76.4126         -0.2693
## htn0                                     0.1926        0.0957          0.2457
## htn1                                     0.8074        0.9043         -0.2457
## dm0                                      0.5309        0.5990         -0.1366
## dm1                                      0.4691        0.4010          0.1366
## cad0                                     0.5085        0.5077          0.0015
## cad1                                     0.4915        0.4923         -0.0015
## chf0                                     0.9011        0.9281         -0.0905
## chf1                                     0.0989        0.0719          0.0905
## copd0                                    0.8372        0.8473         -0.0274
## copd1                                    0.1628        0.1527          0.0274
## cancer0                                  0.5702        0.3910          0.3619
## cancer1                                  0.4298        0.6090         -0.3619
## nsaid0                                   0.8138        0.8247         -0.0278
## nsaid1                                   0.1862        0.1753          0.0278
## beta_blockers0                           0.5947        0.5785          0.0329
## beta_blockers1                           0.4053        0.4215         -0.0329
## statins0                                 0.5362        0.3639          0.3454
## statins1                                 0.4638        0.6361         -0.3454
## ace_arb0                                 0.6181        0.4663          0.3125
## ace_arb1                                 0.3819        0.5337         -0.3125
## ppi0                                     0.6957        0.7024         -0.0145
## ppi1                                     0.3043        0.2976          0.0145
## diur0                                    0.6415        0.5885          0.1105
## diur1                                    0.3585        0.4115         -0.1105
## immunosuppressant0                       0.9500        0.9441          0.0269
## immunosuppressant1                       0.0500        0.0559         -0.0269
## vaccine0                                 0.6383        0.3280          0.6458
## vaccine1                                 0.3617        0.6720         -0.6458
## pre_cre_freq                             6.5819        4.5846          0.1737
## pre_inpt_freq                            0.8362        0.3551          0.2367
## pre_outpt_freq                          48.5894       43.5393          0.0981
## renal_group2eGFR <30, no ESKD            0.0787        0.0183          0.2245
## renal_group2eGFR 30-44.9, no ESKD        0.2309        0.1831          0.1134
## renal_group2eGFR 45-59.9, no ESKD        0.5840        0.7611         -0.3591
## renal_group2ESKD                         0.1064        0.0376          0.2230
## prior_infection0                         0.9713        0.9840         -0.0759
## prior_infection1                         0.0287        0.0160          0.0759
##                                   Var. Ratio eCDF Mean eCDF Max
## distance                              1.6862    0.2688   0.4118
## Gender_Legal_SexFemale                     .    0.0775   0.0775
## Gender_Legal_SexMale                       .    0.0775   0.0775
## age_bl                                1.5496    0.0767   0.1336
## htn0                                       .    0.0969   0.0969
## htn1                                       .    0.0969   0.0969
## dm0                                        .    0.0682   0.0682
## dm1                                        .    0.0682   0.0682
## cad0                                       .    0.0008   0.0008
## cad1                                       .    0.0008   0.0008
## chf0                                       .    0.0270   0.0270
## chf1                                       .    0.0270   0.0270
## copd0                                      .    0.0101   0.0101
## copd1                                      .    0.0101   0.0101
## cancer0                                    .    0.1792   0.1792
## cancer1                                    .    0.1792   0.1792
## nsaid0                                     .    0.0108   0.0108
## nsaid1                                     .    0.0108   0.0108
## beta_blockers0                             .    0.0161   0.0161
## beta_blockers1                             .    0.0161   0.0161
## statins0                                   .    0.1722   0.1722
## statins1                                   .    0.1722   0.1722
## ace_arb0                                   .    0.1518   0.1518
## ace_arb1                                   .    0.1518   0.1518
## ppi0                                       .    0.0067   0.0067
## ppi1                                       .    0.0067   0.0067
## diur0                                      .    0.0530   0.0530
## diur1                                      .    0.0530   0.0530
## immunosuppressant0                         .    0.0059   0.0059
## immunosuppressant1                         .    0.0059   0.0059
## vaccine0                                   .    0.3103   0.3103
## vaccine1                                   .    0.3103   0.3103
## pre_cre_freq                          0.9546    0.0333   0.1049
## pre_inpt_freq                         2.4329    0.0281   0.1174
## pre_outpt_freq                        2.4387    0.0655   0.2057
## renal_group2eGFR <30, no ESKD              .    0.0605   0.0605
## renal_group2eGFR 30-44.9, no ESKD          .    0.0478   0.0478
## renal_group2eGFR 45-59.9, no ESKD          .    0.1770   0.1770
## renal_group2ESKD                           .    0.0688   0.0688
## prior_infection0                           .    0.0127   0.0127
## prior_infection1                           .    0.0127   0.0127
## 
## Sample Sizes:
##           Control Treated
## All          1808     940
## Matched      1808     940
## Unmatched       0       0
## Discarded       0       0
plot(m.out0, type = "jitter", interactive = FALSE)

plot(summary(m.out0), abs = FALSE)

a <- test %>% group_by(group) %>% summarise(mean=mean(age_bl))

post match check

m.out1 <- matchit(group ~ Gender_Legal_Sex +age_bl+htn+dm+cad+              
             chf+copd+cancer+nsaid+beta_blockers+statins+ace_arb+ppi+diur+immunosuppressant+
             vaccine+pre_cre_freq+pre_inpt_freq+pre_outpt_freq+renal_group2+    
             prior_infection, data = test,
           method = "nearest", distance = "glm", ratio =2,caliper = .15)
## Warning: Not all treated units will get 2 matches.
summary(m.out1)
## 
## Call:
## matchit(formula = group ~ Gender_Legal_Sex + age_bl + htn + dm + 
##     cad + chf + copd + cancer + nsaid + beta_blockers + statins + 
##     ace_arb + ppi + diur + immunosuppressant + vaccine + pre_cre_freq + 
##     pre_inpt_freq + pre_outpt_freq + renal_group2 + prior_infection, 
##     data = test, method = "nearest", distance = "glm", caliper = 0.15, 
##     ratio = 2)
## 
## Summary of Balance for All Data:
##                                   Means Treated Means Control Std. Mean Diff.
## distance                                 0.4814        0.2696          0.9409
## Gender_Legal_SexFemale                   0.5011        0.5785         -0.1550
## Gender_Legal_SexMale                     0.4989        0.4215          0.1550
## age_bl                                  72.7977       76.4126         -0.2693
## htn0                                     0.1926        0.0957          0.2457
## htn1                                     0.8074        0.9043         -0.2457
## dm0                                      0.5309        0.5990         -0.1366
## dm1                                      0.4691        0.4010          0.1366
## cad0                                     0.5085        0.5077          0.0015
## cad1                                     0.4915        0.4923         -0.0015
## chf0                                     0.9011        0.9281         -0.0905
## chf1                                     0.0989        0.0719          0.0905
## copd0                                    0.8372        0.8473         -0.0274
## copd1                                    0.1628        0.1527          0.0274
## cancer0                                  0.5702        0.3910          0.3619
## cancer1                                  0.4298        0.6090         -0.3619
## nsaid0                                   0.8138        0.8247         -0.0278
## nsaid1                                   0.1862        0.1753          0.0278
## beta_blockers0                           0.5947        0.5785          0.0329
## beta_blockers1                           0.4053        0.4215         -0.0329
## statins0                                 0.5362        0.3639          0.3454
## statins1                                 0.4638        0.6361         -0.3454
## ace_arb0                                 0.6181        0.4663          0.3125
## ace_arb1                                 0.3819        0.5337         -0.3125
## ppi0                                     0.6957        0.7024         -0.0145
## ppi1                                     0.3043        0.2976          0.0145
## diur0                                    0.6415        0.5885          0.1105
## diur1                                    0.3585        0.4115         -0.1105
## immunosuppressant0                       0.9500        0.9441          0.0269
## immunosuppressant1                       0.0500        0.0559         -0.0269
## vaccine0                                 0.6383        0.3280          0.6458
## vaccine1                                 0.3617        0.6720         -0.6458
## pre_cre_freq                             6.5819        4.5846          0.1737
## pre_inpt_freq                            0.8362        0.3551          0.2367
## pre_outpt_freq                          48.5894       43.5393          0.0981
## renal_group2eGFR <30, no ESKD            0.0787        0.0183          0.2245
## renal_group2eGFR 30-44.9, no ESKD        0.2309        0.1831          0.1134
## renal_group2eGFR 45-59.9, no ESKD        0.5840        0.7611         -0.3591
## renal_group2ESKD                         0.1064        0.0376          0.2230
## prior_infection0                         0.9713        0.9840         -0.0759
## prior_infection1                         0.0287        0.0160          0.0759
##                                   Var. Ratio eCDF Mean eCDF Max
## distance                              1.6862    0.2688   0.4118
## Gender_Legal_SexFemale                     .    0.0775   0.0775
## Gender_Legal_SexMale                       .    0.0775   0.0775
## age_bl                                1.5496    0.0767   0.1336
## htn0                                       .    0.0969   0.0969
## htn1                                       .    0.0969   0.0969
## dm0                                        .    0.0682   0.0682
## dm1                                        .    0.0682   0.0682
## cad0                                       .    0.0008   0.0008
## cad1                                       .    0.0008   0.0008
## chf0                                       .    0.0270   0.0270
## chf1                                       .    0.0270   0.0270
## copd0                                      .    0.0101   0.0101
## copd1                                      .    0.0101   0.0101
## cancer0                                    .    0.1792   0.1792
## cancer1                                    .    0.1792   0.1792
## nsaid0                                     .    0.0108   0.0108
## nsaid1                                     .    0.0108   0.0108
## beta_blockers0                             .    0.0161   0.0161
## beta_blockers1                             .    0.0161   0.0161
## statins0                                   .    0.1722   0.1722
## statins1                                   .    0.1722   0.1722
## ace_arb0                                   .    0.1518   0.1518
## ace_arb1                                   .    0.1518   0.1518
## ppi0                                       .    0.0067   0.0067
## ppi1                                       .    0.0067   0.0067
## diur0                                      .    0.0530   0.0530
## diur1                                      .    0.0530   0.0530
## immunosuppressant0                         .    0.0059   0.0059
## immunosuppressant1                         .    0.0059   0.0059
## vaccine0                                   .    0.3103   0.3103
## vaccine1                                   .    0.3103   0.3103
## pre_cre_freq                          0.9546    0.0333   0.1049
## pre_inpt_freq                         2.4329    0.0281   0.1174
## pre_outpt_freq                        2.4387    0.0655   0.2057
## renal_group2eGFR <30, no ESKD              .    0.0605   0.0605
## renal_group2eGFR 30-44.9, no ESKD          .    0.0478   0.0478
## renal_group2eGFR 45-59.9, no ESKD          .    0.1770   0.1770
## renal_group2ESKD                           .    0.0688   0.0688
## prior_infection0                           .    0.0127   0.0127
## prior_infection1                           .    0.0127   0.0127
## 
## Summary of Balance for Matched Data:
##                                   Means Treated Means Control Std. Mean Diff.
## distance                                 0.4099        0.3958          0.0624
## Gender_Legal_SexFemale                   0.5280        0.5431         -0.0301
## Gender_Legal_SexMale                     0.4720        0.4569          0.0301
## age_bl                                  74.2588       74.0221          0.0176
## htn0                                     0.1491        0.1430          0.0156
## htn1                                     0.8509        0.8570         -0.0156
## dm0                                      0.5390        0.5575         -0.0370
## dm1                                      0.4610        0.4425          0.0370
## cad0                                     0.5048        0.4802          0.0493
## cad1                                     0.4952        0.5198         -0.0493
## chf0                                     0.9070        0.9029          0.0137
## chf1                                     0.0930        0.0971         -0.0137
## copd0                                    0.8345        0.8242          0.0278
## copd1                                    0.1655        0.1758         -0.0278
## cancer0                                  0.5144        0.4938          0.0415
## cancer1                                  0.4856        0.5062         -0.0415
## nsaid0                                   0.8126        0.8051          0.0193
## nsaid1                                   0.1874        0.1949         -0.0193
## beta_blockers0                           0.5800        0.5759          0.0084
## beta_blockers1                           0.4200        0.4241         -0.0084
## statins0                                 0.4815        0.4535          0.0562
## statins1                                 0.5185        0.5465         -0.0562
## ace_arb0                                 0.5677        0.5622          0.0113
## ace_arb1                                 0.4323        0.4378         -0.0113
## ppi0                                     0.6854        0.6888         -0.0074
## ppi1                                     0.3146        0.3112          0.0074
## diur0                                    0.6252        0.5937          0.0656
## diur1                                    0.3748        0.4063         -0.0656
## immunosuppressant0                       0.9466        0.9473         -0.0031
## immunosuppressant1                       0.0534        0.0527          0.0031
## vaccine0                                 0.5622        0.5595          0.0057
## vaccine1                                 0.4378        0.4405         -0.0057
## pre_cre_freq                             5.5910        5.6963         -0.0092
## pre_inpt_freq                            0.5705        0.5705          0.0000
## pre_outpt_freq                          48.9562       49.0055         -0.0010
## renal_group2eGFR <30, no ESKD            0.0438        0.0397          0.0152
## renal_group2eGFR 30-44.9, no ESKD        0.2298        0.2155          0.0341
## renal_group2eGFR 45-59.9, no ESKD        0.6525        0.6751         -0.0458
## renal_group2ESKD                         0.0739        0.0698          0.0133
## prior_infection0                         0.9795        0.9767          0.0164
## prior_infection1                         0.0205        0.0233         -0.0164
##                                   Var. Ratio eCDF Mean eCDF Max Std. Pair Dist.
## distance                              1.1352    0.0127   0.0561          0.0473
## Gender_Legal_SexFemale                     .    0.0150   0.0150          0.9938
## Gender_Legal_SexMale                       .    0.0150   0.0150          0.9938
## age_bl                                1.1324    0.0144   0.0328          0.8829
## htn0                                       .    0.0062   0.0062          0.5284
## htn1                                       .    0.0062   0.0062          0.5284
## dm0                                        .    0.0185   0.0185          0.9082
## dm1                                        .    0.0185   0.0185          0.9082
## cad0                                       .    0.0246   0.0246          1.0349
## cad1                                       .    0.0246   0.0246          1.0349
## chf0                                       .    0.0041   0.0041          0.5607
## chf1                                       .    0.0041   0.0041          0.5607
## copd0                                      .    0.0103   0.0103          0.7671
## copd1                                      .    0.0103   0.0103          0.7671
## cancer0                                    .    0.0205   0.0205          0.9120
## cancer1                                    .    0.0205   0.0205          0.9120
## nsaid0                                     .    0.0075   0.0075          0.7824
## nsaid1                                     .    0.0075   0.0075          0.7824
## beta_blockers0                             .    0.0041   0.0041          0.9831
## beta_blockers1                             .    0.0041   0.0041          0.9831
## statins0                                   .    0.0280   0.0280          0.9285
## statins1                                   .    0.0280   0.0280          0.9285
## ace_arb0                                   .    0.0055   0.0055          0.9805
## ace_arb1                                   .    0.0055   0.0055          0.9805
## ppi0                                       .    0.0034   0.0034          0.9406
## ppi1                                       .    0.0034   0.0034          0.9406
## diur0                                      .    0.0315   0.0315          0.9563
## diur1                                      .    0.0315   0.0315          0.9563
## immunosuppressant0                         .    0.0007   0.0007          0.4535
## immunosuppressant1                         .    0.0007   0.0007          0.4535
## vaccine0                                   .    0.0027   0.0027          0.6690
## vaccine1                                   .    0.0027   0.0027          0.6690
## pre_cre_freq                          0.7607    0.0065   0.0650          0.5372
## pre_inpt_freq                         0.7027    0.0070   0.0294          0.8068
## pre_outpt_freq                        1.5394    0.0333   0.1525          0.8422
## renal_group2eGFR <30, no ESKD              .    0.0041   0.0041          0.2248
## renal_group2eGFR 30-44.9, no ESKD          .    0.0144   0.0144          0.8242
## renal_group2eGFR 45-59.9, no ESKD          .    0.0226   0.0226          0.8076
## renal_group2ESKD                           .    0.0041   0.0041          0.3206
## prior_infection0                           .    0.0027   0.0027          0.2292
## prior_infection1                           .    0.0027   0.0027          0.2292
## 
## Sample Sizes:
##               Control Treated
## All           1808.       940
## Matched (ESS)  998.81     731
## Matched       1123.       731
## Unmatched      685.       209
## Discarded        0.         0
plot(m.out1, type = "jitter", interactive = FALSE)

plot(summary(m.out1), abs = FALSE)

m.data1 <- match.data(m.out1, data = test,
                      distance = "prop.score") %>% select(EMPI,prop.score,weights,subclass)
m.data1 <- m.data1 %>% mutate_at(c("EMPI","prop.score","weights","subclass"),as.numeric)

matched <- all %>% merge(m.data1,by="EMPI",all.y = T)


write_xlsx(matched,"C:\\Users\\to909\\Dropbox (Partners HealthCare)\\Merck COVID-19 project\\FOR TIANQI TO MATCH\\Matched_updated.xlsx")