This document will calculate the Inverse propensity score weighting (IPSW),
Train a classier of my choice to predict the probability of an individual having t == 1 based on features x1-x25.
Create a function that calculates the weights for the samples based on their propensity scores.
Calculate the value of the average treatment effect (ATE)
Let’s load the The Infant Health Development Program (IHDP) dataset
library(datasets)
dataset <- read.csv(file = 'ihdp_npci_repl532.csv')
data(dateaset)
## Warning in data(dateaset): data set 'dateaset' not found
summary(dataset)
## x1 x2 x3 x4
## Min. :-2.7313 Min. :-3.8008 Min. :-1.8503 Min. :-0.8796
## 1st Qu.:-0.6669 1st Qu.:-0.6027 1st Qu.:-0.7333 1st Qu.:-0.8796
## Median : 0.1653 Median : 0.1968 Median :-0.3609 Median : 0.1617
## Mean : 0.0000 Mean : 0.0000 Mean : 0.0000 Mean : 0.0000
## 3rd Qu.: 0.8138 3rd Qu.: 0.5966 3rd Qu.: 0.7562 3rd Qu.: 0.1617
## Max. : 1.5055 Max. : 2.5954 Max. : 2.9904 Max. : 2.2443
## x5 x6 x7 x8
## Min. :-5.1304 Min. :-1.85148 Min. :0.0000 Min. :0.00000
## 1st Qu.:-0.5667 1st Qu.:-0.85779 1st Qu.:0.0000 1st Qu.:0.00000
## Median : 0.1210 Median :-0.02971 Median :1.0000 Median :0.00000
## Mean : 0.0000 Mean : 0.00000 Mean :0.5141 Mean :0.09371
## 3rd Qu.: 0.6837 3rd Qu.: 0.63275 3rd Qu.:1.0000 3rd Qu.:0.00000
## Max. : 2.3716 Max. : 2.95137 Max. :1.0000 Max. :1.00000
## x9 x10 x11 x12
## Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000
## Median :1.0000 Median :0.0000 Median :0.0000 Median :0.0000
## Mean :0.5207 Mean :0.3641 Mean :0.2691 Mean :0.2195
## 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:0.0000
## Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.0000
## x13 x14 x15 x16
## Min. :0.0000 Min. :1.000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:1.000 1st Qu.:0.0000 1st Qu.:1.0000
## Median :0.0000 Median :1.000 Median :0.0000 Median :1.0000
## Mean :0.3588 Mean :1.463 Mean :0.1406 Mean :0.9598
## 3rd Qu.:1.0000 3rd Qu.:2.000 3rd Qu.:0.0000 3rd Qu.:1.0000
## Max. :1.0000 Max. :2.000 Max. :1.0000 Max. :1.0000
## x17 x18 x19 x20
## Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:1.0000 1st Qu.:0.0000 1st Qu.:0.0000
## Median :1.0000 Median :1.0000 Median :0.0000 Median :0.0000
## Mean :0.5944 Mean :0.9639 Mean :0.1352 Mean :0.1352
## 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:0.0000 3rd Qu.:0.0000
## Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.0000
## x21 x22 x23 x24
## Min. :0.0000 Min. :0.00000 Min. :0.00000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.0000
## Median :0.0000 Median :0.00000 Median :0.00000 Median :0.0000
## Mean :0.1566 Mean :0.08166 Mean :0.07363 Mean :0.1285
## 3rd Qu.:0.0000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.0000
## Max. :1.0000 Max. :1.00000 Max. :1.00000 Max. :1.0000
## x25 t outcome
## Min. :0.000 Min. :0.0000 Min. : 0.1986
## 1st Qu.:0.000 1st Qu.:0.0000 1st Qu.: 3.0655
## Median :0.000 Median :0.0000 Median : 4.3477
## Mean :0.158 Mean :0.1861 Mean : 4.7939
## 3rd Qu.:0.000 3rd Qu.:0.0000 3rd Qu.: 6.4974
## Max. :1.000 Max. :1.0000 Max. :11.3128
Train a classifier to predict the probability of individuals
m_ps <- glm(t ~ x1 + x2 + x3 + x4 + x5+ x6 + x7 + x8 + x9 + x10+ x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 +x21 + x22 + x23 + x24 + x25,
family = binomial(), data = dataset)
summary(m_ps)
##
## Call:
## glm(formula = t ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 +
## x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 +
## x20 + x21 + x22 + x23 + x24 + x25, family = binomial(), data = dataset)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.3081 -0.6833 -0.4587 -0.2551 2.6058
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.09631 1.39061 -1.507 0.131688
## x1 0.41312 0.22088 1.870 0.061434 .
## x2 0.11524 0.19528 0.590 0.555113
## x3 0.26353 0.17391 1.515 0.129684
## x4 -0.14269 0.20789 -0.686 0.492475
## x5 -0.12316 0.10950 -1.125 0.260730
## x6 0.13718 0.13861 0.990 0.322350
## x7 0.24232 0.20892 1.160 0.246113
## x8 0.01847 0.37105 0.050 0.960299
## x9 0.37377 0.25024 1.494 0.135269
## x10 -0.02288 0.38508 -0.059 0.952630
## x11 -0.22789 0.34764 -0.656 0.512114
## x12 -0.39701 0.32444 -1.224 0.221077
## x13 0.27545 0.22709 1.213 0.225145
## x14 0.31837 0.38282 0.832 0.405606
## x15 -0.20067 0.30647 -0.655 0.512623
## x16 -0.69736 0.47631 -1.464 0.143168
## x17 0.14507 0.23579 0.615 0.538374
## x18 1.09658 1.06253 1.032 0.302052
## x19 -0.54651 0.34842 -1.569 0.116759
## x20 -1.48254 0.43309 -3.423 0.000619 ***
## x21 -0.76808 0.33192 -2.314 0.020666 *
## x22 -1.26719 0.55367 -2.289 0.022096 *
## x23 -2.30204 0.77549 -2.968 0.002993 **
## x24 -1.31844 0.46471 -2.837 0.004552 **
## x25 -0.10107 0.31499 -0.321 0.748307
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 717.84 on 746 degrees of freedom
## Residual deviance: 628.89 on 721 degrees of freedom
## AIC: 680.89
##
## Number of Fisher Scoring iterations: 6
The predicted probability of individuals
summary(m_ps)
##
## Call:
## glm(formula = t ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 +
## x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 +
## x20 + x21 + x22 + x23 + x24 + x25, family = binomial(), data = dataset)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.3081 -0.6833 -0.4587 -0.2551 2.6058
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.09631 1.39061 -1.507 0.131688
## x1 0.41312 0.22088 1.870 0.061434 .
## x2 0.11524 0.19528 0.590 0.555113
## x3 0.26353 0.17391 1.515 0.129684
## x4 -0.14269 0.20789 -0.686 0.492475
## x5 -0.12316 0.10950 -1.125 0.260730
## x6 0.13718 0.13861 0.990 0.322350
## x7 0.24232 0.20892 1.160 0.246113
## x8 0.01847 0.37105 0.050 0.960299
## x9 0.37377 0.25024 1.494 0.135269
## x10 -0.02288 0.38508 -0.059 0.952630
## x11 -0.22789 0.34764 -0.656 0.512114
## x12 -0.39701 0.32444 -1.224 0.221077
## x13 0.27545 0.22709 1.213 0.225145
## x14 0.31837 0.38282 0.832 0.405606
## x15 -0.20067 0.30647 -0.655 0.512623
## x16 -0.69736 0.47631 -1.464 0.143168
## x17 0.14507 0.23579 0.615 0.538374
## x18 1.09658 1.06253 1.032 0.302052
## x19 -0.54651 0.34842 -1.569 0.116759
## x20 -1.48254 0.43309 -3.423 0.000619 ***
## x21 -0.76808 0.33192 -2.314 0.020666 *
## x22 -1.26719 0.55367 -2.289 0.022096 *
## x23 -2.30204 0.77549 -2.968 0.002993 **
## x24 -1.31844 0.46471 -2.837 0.004552 **
## x25 -0.10107 0.31499 -0.321 0.748307
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 717.84 on 746 degrees of freedom
## Residual deviance: 628.89 on 721 degrees of freedom
## AIC: 680.89
##
## Number of Fisher Scoring iterations: 6
prs_df <- data.frame(pr_score = predict(m_ps, type = "response"),
t = m_ps$model$t)
print(prs_df)
## pr_score t
## 1 0.376432969 0
## 2 0.258931106 0
## 3 0.027801956 0
## 4 0.028217940 0
## 5 0.019336567 0
## 6 0.271821819 0
## 7 0.039649973 0
## 8 0.199985499 0
## 9 0.155821960 1
## 10 0.351186968 0
## 11 0.045371645 0
## 12 0.073449144 0
## 13 0.093004788 0
## 14 0.028636553 0
## 15 0.027171307 0
## 16 0.374981750 0
## 17 0.321014337 0
## 18 0.426404371 1
## 19 0.200912391 0
## 20 0.106174713 0
## 21 0.131189232 0
## 22 0.158185748 0
## 23 0.120307750 0
## 24 0.060363745 0
## 25 0.087676309 0
## 26 0.332998711 0
## 27 0.277587893 0
## 28 0.264043804 0
## 29 0.384543489 1
## 30 0.088606554 0
## 31 0.473473046 0
## 32 0.112485589 0
## 33 0.323441151 1
## 34 0.285673695 1
## 35 0.312387707 0
## 36 0.134400142 0
## 37 0.317402642 1
## 38 0.268174016 0
## 39 0.034523111 0
## 40 0.196847359 1
## 41 0.110289819 0
## 42 0.103077717 0
## 43 0.242170851 1
## 44 0.170815493 1
## 45 0.289028450 0
## 46 0.046659028 0
## 47 0.285885888 0
## 48 0.160294876 0
## 49 0.119303838 1
## 50 0.401187635 1
## 51 0.214731753 0
## 52 0.272011019 0
## 53 0.193635764 0
## 54 0.034255158 0
## 55 0.175673564 0
## 56 0.358603291 0
## 57 0.519073842 0
## 58 0.255949239 0
## 59 0.094240419 0
## 60 0.280527626 0
## 61 0.040145393 0
## 62 0.279437519 0
## 63 0.279057520 0
## 64 0.088732081 0
## 65 0.036596227 0
## 66 0.154151539 0
## 67 0.278786464 1
## 68 0.161182602 0
## 69 0.311344284 0
## 70 0.245431998 0
## 71 0.112733087 0
## 72 0.106393488 0
## 73 0.354399652 1
## 74 0.283698699 0
## 75 0.270533663 1
## 76 0.145870834 0
## 77 0.453857185 0
## 78 0.278745952 0
## 79 0.051437461 0
## 80 0.167266672 1
## 81 0.031013806 0
## 82 0.494459336 1
## 83 0.057496651 0
## 84 0.184782593 0
## 85 0.146859870 1
## 86 0.211101016 1
## 87 0.047151009 0
## 88 0.263807816 0
## 89 0.256834792 1
## 90 0.293687710 0
## 91 0.213047599 1
## 92 0.130858656 0
## 93 0.042843095 0
## 94 0.099057061 0
## 95 0.018101492 0
## 96 0.301936142 0
## 97 0.518389328 0
## 98 0.064136731 0
## 99 0.449969211 0
## 100 0.027109851 0
## 101 0.120407607 1
## 102 0.016534580 0
## 103 0.162893458 1
## 104 0.047451886 0
## 105 0.226424585 0
## 106 0.342957872 0
## 107 0.233755524 0
## 108 0.116836134 0
## 109 0.049544476 0
## 110 0.231579729 0
## 111 0.021083466 0
## 112 0.086362365 0
## 113 0.274786168 0
## 114 0.119132978 0
## 115 0.047558988 0
## 116 0.217222587 0
## 117 0.143236930 0
## 118 0.334105966 1
## 119 0.238375847 0
## 120 0.371255297 0
## 121 0.297913693 1
## 122 0.060044132 0
## 123 0.115938933 0
## 124 0.166750283 0
## 125 0.074649391 0
## 126 0.248947995 1
## 127 0.362976372 0
## 128 0.569153973 0
## 129 0.052648906 0
## 130 0.213731105 0
## 131 0.103385651 0
## 132 0.075523030 0
## 133 0.031244432 0
## 134 0.423947200 0
## 135 0.291747607 0
## 136 0.217935222 0
## 137 0.115774179 0
## 138 0.044812359 0
## 139 0.309277638 0
## 140 0.037687424 0
## 141 0.097765011 0
## 142 0.013293433 0
## 143 0.289264330 0
## 144 0.061985832 0
## 145 0.371128176 1
## 146 0.034335489 0
## 147 0.447688796 0
## 148 0.147005725 0
## 149 0.448965909 0
## 150 0.180728578 0
## 151 0.290996869 0
## 152 0.043768905 0
## 153 0.108922104 0
## 154 0.100145007 0
## 155 0.070561577 0
## 156 0.118869975 0
## 157 0.103455877 0
## 158 0.525637083 1
## 159 0.042501850 0
## 160 0.267677857 0
## 161 0.356670494 1
## 162 0.239536260 1
## 163 0.119170665 0
## 164 0.383375286 0
## 165 0.311011498 0
## 166 0.532057550 1
## 167 0.437677877 0
## 168 0.177699392 0
## 169 0.239194275 0
## 170 0.129827055 0
## 171 0.117480146 0
## 172 0.244215268 0
## 173 0.494925905 1
## 174 0.183898178 0
## 175 0.233547180 0
## 176 0.336070640 0
## 177 0.014446091 0
## 178 0.456054826 1
## 179 0.046273286 1
## 180 0.064728242 0
## 181 0.423918187 0
## 182 0.034961909 0
## 183 0.070443008 0
## 184 0.145251805 1
## 185 0.093948446 0
## 186 0.171802422 0
## 187 0.127577874 0
## 188 0.050085467 0
## 189 0.078654485 0
## 190 0.264415117 0
## 191 0.032660619 0
## 192 0.339855452 0
## 193 0.145228587 0
## 194 0.070141082 0
## 195 0.561867303 0
## 196 0.146351260 0
## 197 0.110955557 0
## 198 0.032298391 0
## 199 0.126479853 0
## 200 0.060197952 0
## 201 0.039781188 0
## 202 0.072790607 0
## 203 0.293840898 0
## 204 0.256739696 0
## 205 0.092532927 1
## 206 0.069878283 0
## 207 0.043509092 0
## 208 0.368729624 1
## 209 0.259371243 1
## 210 0.143222633 0
## 211 0.120144183 0
## 212 0.065325027 0
## 213 0.058129704 0
## 214 0.026041409 0
## 215 0.046558079 0
## 216 0.388691742 1
## 217 0.050816622 0
## 218 0.288996376 0
## 219 0.121553772 0
## 220 0.277426998 0
## 221 0.272500069 1
## 222 0.133682511 0
## 223 0.202684423 1
## 224 0.130582327 0
## 225 0.045256518 0
## 226 0.200191318 0
## 227 0.179297447 0
## 228 0.178956914 0
## 229 0.036490872 0
## 230 0.159194813 0
## 231 0.146272239 1
## 232 0.080280585 0
## 233 0.365293654 1
## 234 0.108328480 1
## 235 0.103350650 0
## 236 0.168628831 0
## 237 0.103756423 0
## 238 0.215354878 0
## 239 0.068026725 0
## 240 0.067557796 0
## 241 0.143030959 0
## 242 0.198556220 0
## 243 0.095085060 0
## 244 0.055157899 1
## 245 0.353485866 0
## 246 0.267855695 0
## 247 0.299312163 0
## 248 0.579021004 1
## 249 0.103465708 0
## 250 0.114795246 0
## 251 0.111389312 0
## 252 0.117494463 0
## 253 0.282716011 1
## 254 0.104166879 0
## 255 0.293555410 0
## 256 0.502489232 1
## 257 0.046822630 0
## 258 0.055651495 0
## 259 0.062514208 0
## 260 0.048958647 0
## 261 0.401642445 1
## 262 0.341397167 0
## 263 0.026815830 0
## 264 0.056416875 0
## 265 0.104128866 0
## 266 0.042304394 0
## 267 0.566960892 1
## 268 0.217141717 1
## 269 0.324622241 0
## 270 0.301016809 1
## 271 0.038563721 0
## 272 0.547357253 1
## 273 0.459327750 0
## 274 0.014272813 0
## 275 0.202503739 1
## 276 0.311640405 0
## 277 0.063781658 0
## 278 0.421637110 0
## 279 0.212104558 0
## 280 0.311884092 1
## 281 0.059056070 0
## 282 0.339991506 0
## 283 0.024318463 0
## 284 0.305852892 0
## 285 0.410896318 0
## 286 0.063436743 0
## 287 0.126421391 0
## 288 0.074515985 0
## 289 0.086848831 0
## 290 0.354035824 0
## 291 0.050111815 0
## 292 0.141227524 0
## 293 0.312015917 0
## 294 0.052946181 0
## 295 0.229580665 0
## 296 0.222092027 0
## 297 0.042476058 0
## 298 0.137279641 0
## 299 0.081721023 0
## 300 0.247686887 1
## 301 0.268864359 0
## 302 0.121525559 1
## 303 0.551992900 1
## 304 0.033537072 1
## 305 0.204597422 0
## 306 0.053985736 0
## 307 0.173389185 0
## 308 0.224617665 1
## 309 0.241469609 0
## 310 0.046692458 0
## 311 0.338604245 1
## 312 0.109495198 0
## 313 0.118700037 1
## 314 0.322077869 0
## 315 0.214344260 1
## 316 0.056495597 0
## 317 0.179520154 0
## 318 0.217863979 1
## 319 0.040597815 0
## 320 0.307786742 0
## 321 0.094575690 0
## 322 0.079613508 0
## 323 0.031220753 0
## 324 0.175865928 0
## 325 0.107478615 0
## 326 0.046317244 0
## 327 0.089910443 0
## 328 0.052521149 0
## 329 0.329762597 0
## 330 0.245255352 1
## 331 0.221375750 0
## 332 0.099229034 0
## 333 0.261129620 1
## 334 0.176971086 0
## 335 0.114268620 0
## 336 0.416984354 0
## 337 0.575954440 1
## 338 0.034964539 0
## 339 0.089935366 0
## 340 0.597978160 1
## 341 0.075862146 0
## 342 0.225625623 0
## 343 0.111709964 0
## 344 0.113034738 0
## 345 0.159680102 1
## 346 0.141647580 0
## 347 0.010127649 0
## 348 0.459051082 0
## 349 0.166483107 1
## 350 0.127274677 1
## 351 0.052896712 0
## 352 0.032314409 0
## 353 0.095693422 0
## 354 0.018893222 0
## 355 0.105729019 0
## 356 0.252172494 0
## 357 0.182386655 0
## 358 0.424065418 0
## 359 0.468865546 0
## 360 0.011985327 0
## 361 0.071977850 0
## 362 0.226729392 0
## 363 0.094466161 0
## 364 0.126790218 0
## 365 0.099305781 0
## 366 0.047158055 0
## 367 0.203562449 0
## 368 0.385270079 0
## 369 0.096141981 1
## 370 0.062357847 0
## 371 0.038878856 0
## 372 0.402539959 1
## 373 0.228622954 0
## 374 0.361416095 1
## 375 0.574943313 0
## 376 0.105877059 0
## 377 0.013565911 0
## 378 0.111258263 0
## 379 0.395725222 0
## 380 0.145474188 0
## 381 0.137163172 0
## 382 0.201543290 0
## 383 0.100154443 0
## 384 0.335118093 0
## 385 0.043537053 0
## 386 0.167680763 0
## 387 0.116814366 0
## 388 0.187201998 1
## 389 0.249326514 0
## 390 0.074489086 0
## 391 0.071631998 0
## 392 0.051353956 0
## 393 0.067192190 0
## 394 0.488609124 0
## 395 0.205226600 0
## 396 0.153277966 0
## 397 0.399456288 0
## 398 0.382924994 1
## 399 0.123046516 0
## 400 0.054816206 0
## 401 0.018242284 0
## 402 0.313959228 0
## 403 0.062899500 0
## 404 0.102193399 0
## 405 0.144872715 0
## 406 0.199208877 0
## 407 0.214991279 0
## 408 0.378091060 0
## 409 0.481078163 0
## 410 0.026869438 0
## 411 0.216495878 0
## 412 0.035226943 0
## 413 0.353115635 0
## 414 0.088008630 0
## 415 0.288665187 0
## 416 0.139814726 0
## 417 0.081198443 0
## 418 0.142682537 0
## 419 0.498973926 0
## 420 0.068834495 0
## 421 0.088578504 0
## 422 0.287363579 0
## 423 0.468825003 1
## 424 0.120618420 0
## 425 0.023716882 0
## 426 0.042644051 0
## 427 0.289524153 0
## 428 0.040870857 0
## 429 0.164866912 1
## 430 0.031724912 0
## 431 0.108413755 0
## 432 0.177897685 1
## 433 0.253846852 0
## 434 0.157505765 0
## 435 0.151395381 0
## 436 0.091358325 0
## 437 0.052450507 0
## 438 0.023381810 0
## 439 0.221782336 0
## 440 0.179974824 0
## 441 0.058899685 0
## 442 0.255343465 0
## 443 0.144392187 0
## 444 0.301546775 1
## 445 0.163963732 0
## 446 0.054569637 0
## 447 0.033904414 0
## 448 0.072753223 0
## 449 0.144715031 0
## 450 0.236529158 1
## 451 0.381929494 0
## 452 0.365278530 1
## 453 0.200878977 0
## 454 0.047725906 0
## 455 0.075941273 0
## 456 0.014303025 0
## 457 0.079916213 0
## 458 0.086713197 0
## 459 0.026253308 0
## 460 0.143073672 0
## 461 0.184111534 0
## 462 0.377089435 0
## 463 0.060852551 0
## 464 0.122674816 0
## 465 0.230894387 0
## 466 0.527874002 1
## 467 0.302020016 0
## 468 0.321106834 0
## 469 0.245226088 0
## 470 0.174098350 0
## 471 0.255156010 0
## 472 0.020535463 0
## 473 0.079224292 0
## 474 0.112662098 1
## 475 0.123791008 0
## 476 0.214520873 0
## 477 0.077722502 0
## 478 0.095174889 0
## 479 0.040864588 0
## 480 0.176175703 0
## 481 0.077058438 0
## 482 0.026224412 0
## 483 0.407093859 1
## 484 0.198905931 0
## 485 0.472688248 1
## 486 0.029274691 0
## 487 0.321005579 1
## 488 0.025020567 0
## 489 0.019444024 0
## 490 0.277949073 0
## 491 0.022878637 0
## 492 0.245422430 0
## 493 0.098172295 1
## 494 0.172477790 0
## 495 0.126215912 0
## 496 0.207502803 0
## 497 0.077792672 0
## 498 0.243722199 1
## 499 0.282366949 0
## 500 0.329697990 0
## 501 0.211050377 1
## 502 0.269524156 0
## 503 0.287809349 1
## 504 0.092843244 0
## 505 0.104467966 0
## 506 0.180798044 0
## 507 0.185356792 0
## 508 0.255326582 1
## 509 0.304836685 0
## 510 0.346810004 1
## 511 0.141030294 0
## 512 0.120754794 0
## 513 0.326296281 0
## 514 0.197776142 0
## 515 0.379257947 0
## 516 0.107648853 0
## 517 0.163650664 1
## 518 0.459755826 1
## 519 0.110373459 0
## 520 0.192411289 1
## 521 0.137835415 1
## 522 0.070575551 0
## 523 0.584583401 1
## 524 0.047300772 0
## 525 0.148782421 0
## 526 0.302179192 0
## 527 0.070729099 0
## 528 0.260827029 1
## 529 0.190037114 0
## 530 0.394609148 1
## 531 0.317014856 0
## 532 0.091981680 0
## 533 0.037489469 0
## 534 0.140273479 0
## 535 0.258416730 0
## 536 0.220870083 0
## 537 0.218740529 1
## 538 0.168906733 0
## 539 0.224456750 0
## 540 0.405292663 1
## 541 0.084541569 0
## 542 0.187013468 0
## 543 0.334557354 0
## 544 0.183456280 0
## 545 0.028506811 0
## 546 0.252238949 0
## 547 0.437983527 0
## 548 0.246354187 0
## 549 0.066300251 0
## 550 0.130435287 0
## 551 0.125865087 1
## 552 0.325421461 0
## 553 0.548137100 0
## 554 0.087599744 1
## 555 0.023128812 0
## 556 0.032447884 0
## 557 0.588651841 1
## 558 0.262093370 0
## 559 0.252093489 1
## 560 0.411891043 0
## 561 0.103421485 0
## 562 0.278502146 0
## 563 0.215447231 0
## 564 0.094084441 0
## 565 0.052493132 0
## 566 0.068522275 1
## 567 0.053309565 0
## 568 0.063592600 0
## 569 0.164062658 0
## 570 0.052529647 0
## 571 0.152646581 0
## 572 0.121686473 0
## 573 0.267713267 0
## 574 0.008520508 0
## 575 0.026074621 0
## 576 0.105589057 0
## 577 0.043933654 1
## 578 0.042828341 0
## 579 0.458704620 0
## 580 0.104706167 0
## 581 0.170757067 0
## 582 0.349759948 0
## 583 0.419576437 1
## 584 0.230111896 0
## 585 0.137383295 0
## 586 0.073190695 0
## 587 0.305415532 0
## 588 0.496377885 0
## 589 0.345148577 1
## 590 0.393050912 0
## 591 0.177210440 0
## 592 0.347138666 0
## 593 0.110904159 0
## 594 0.043551578 0
## 595 0.502585417 0
## 596 0.099874999 0
## 597 0.281802282 0
## 598 0.125273854 0
## 599 0.203747945 0
## 600 0.064296803 0
## 601 0.058024470 0
## 602 0.562593353 1
## 603 0.392765890 0
## 604 0.271620880 0
## 605 0.073166995 1
## 606 0.039023777 0
## 607 0.362261741 1
## 608 0.245810373 0
## 609 0.082523506 0
## 610 0.265905583 1
## 611 0.114773815 0
## 612 0.343059469 1
## 613 0.390758884 0
## 614 0.070843193 0
## 615 0.140193166 0
## 616 0.042974831 0
## 617 0.094356765 0
## 618 0.075005418 1
## 619 0.049135198 0
## 620 0.169740247 0
## 621 0.094036020 0
## 622 0.101231464 0
## 623 0.323337889 0
## 624 0.149473661 0
## 625 0.256209673 0
## 626 0.140950590 1
## 627 0.104830745 0
## 628 0.267193913 1
## 629 0.027584999 0
## 630 0.306482200 1
## 631 0.065643086 0
## 632 0.260650556 1
## 633 0.126564008 0
## 634 0.054357234 0
## 635 0.558905236 0
## 636 0.446018037 1
## 637 0.382852027 1
## 638 0.024910053 0
## 639 0.142758356 0
## 640 0.072019873 0
## 641 0.309411130 1
## 642 0.301236961 0
## 643 0.349972746 0
## 644 0.333677816 1
## 645 0.096684526 0
## 646 0.070993285 0
## 647 0.296469559 0
## 648 0.261617456 0
## 649 0.201479656 0
## 650 0.253670009 1
## 651 0.405315676 1
## 652 0.145262959 0
## 653 0.049397093 0
## 654 0.042082147 0
## 655 0.036229673 0
## 656 0.343672488 0
## 657 0.109341732 0
## 658 0.085668795 0
## 659 0.059373930 0
## 660 0.188100242 1
## 661 0.324054544 1
## 662 0.169604688 0
## 663 0.195461611 0
## 664 0.319410306 0
## 665 0.097389843 0
## 666 0.133689432 0
## 667 0.112444476 0
## 668 0.023944116 0
## 669 0.012229609 0
## 670 0.197057938 0
## 671 0.172622145 0
## 672 0.332710219 1
## 673 0.115901384 0
## 674 0.132106291 0
## 675 0.095814369 1
## 676 0.107805274 1
## 677 0.066564953 0
## 678 0.319228417 0
## 679 0.329399519 0
## 680 0.114932136 1
## 681 0.122007074 0
## 682 0.347765987 0
## 683 0.010774452 0
## 684 0.163143119 0
## 685 0.045570287 0
## 686 0.378249311 0
## 687 0.321847294 0
## 688 0.022702931 0
## 689 0.196665627 1
## 690 0.072382715 0
## 691 0.085090024 0
## 692 0.338549444 1
## 693 0.135656807 0
## 694 0.213373228 1
## 695 0.177536419 0
## 696 0.120032287 0
## 697 0.349505919 0
## 698 0.105189079 0
## 699 0.208847196 0
## 700 0.198467710 0
## 701 0.405103487 0
## 702 0.138075691 0
## 703 0.187931212 0
## 704 0.121639768 0
## 705 0.063605680 0
## 706 0.195498105 0
## 707 0.265002701 0
## 708 0.047727997 0
## 709 0.127408152 1
## 710 0.128298175 0
## 711 0.179686208 0
## 712 0.233380729 0
## 713 0.139569099 1
## 714 0.080462986 0
## 715 0.123702151 0
## 716 0.067661034 0
## 717 0.068237273 0
## 718 0.275772534 0
## 719 0.058354348 0
## 720 0.495215334 0
## 721 0.385970657 1
## 722 0.075829208 0
## 723 0.056991118 0
## 724 0.132230412 0
## 725 0.180986970 0
## 726 0.050295058 0
## 727 0.104727511 0
## 728 0.115784117 0
## 729 0.183303098 0
## 730 0.213487780 0
## 731 0.306588883 1
## 732 0.087083758 0
## 733 0.041497059 0
## 734 0.046137993 0
## 735 0.159971616 0
## 736 0.087115730 1
## 737 0.354221607 0
## 738 0.115625061 0
## 739 0.055176644 0
## 740 0.029154349 0
## 741 0.011333229 0
## 742 0.019571540 0
## 743 0.093012318 0
## 744 0.192489270 0
## 745 0.060104710 0
## 746 0.058434525 0
## 747 0.113551885 0
Plotting probability of treatment and control groups
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
labs <- paste("A family was part of the:", c("treatment groups", "control group"))
prs_df %>%
mutate(t = ifelse(t == 1, labs[1], labs[2])) %>%
ggplot(aes(x = pr_score)) +
geom_histogram(color = "white") +
facet_wrap(~t) +
xlab("Probability of treatment and control groups") +
theme_bw()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#Converting predicted probabilities into weights
dataset <- dataset %>%
mutate(propensity_gam = predict(m_ps, type = "response"),
weight_gam = 1 / propensity_gam * t +
1 / (1 - propensity_gam) * (1 - t))
ggplot(dataset, aes(x = weight_gam, fill = as.factor(t))) +
geom_density(alpha = 0.5, colour = "grey50") +
geom_rug() +
scale_x_log10(breaks = c(1, 5, 10, 20, 40)) +
ggtitle("Distribution of inverse probability weights")
WE need create a function that calculates the weights for the samples based on their propensity scores.
The basic idea behind inverse probability weighting is that individuals who were assigned to the treatment group even though they were much more likely to be assigned to the control group are a rare, and valuable breed. We want to give their outcomes as much weight as possible, whereas the much larger group of individuals who were placed in the expected treatment group need less weight, simply because we have much more information on individuals like this. One of the criticisms of this inverse probability of treatment weighting approach is that individual observations can get very high weights and become unduly influential.
# Define SampleWeight function
SampleWeight <- function(w)
{
# sample weight
x = prs_df[,2] / prs_df[,1] + ((1 - prs_df[,2]) / (1 - prs_df[,1]))
return( x)
}
sample_weight <- SampleWeight(prs_df)
print(sample_weight)
## [1] 1.603677 1.349402 1.028597 1.029037 1.019718 1.373290 1.041287
## [8] 1.249977 6.417581 1.541276 1.047528 1.079272 1.102542 1.029481
## [15] 1.027930 1.599953 1.472785 2.345192 1.251427 1.118787 1.150999
## [22] 1.187911 1.136761 1.064242 1.096102 1.499247 1.384251 1.358777
## [29] 2.600486 1.097221 1.899238 1.126742 3.091753 3.500497 1.454308
## [36] 1.155268 3.150572 1.366445 1.035758 5.080078 1.123962 1.114924
## [43] 4.129316 5.854270 1.406526 1.048943 1.400336 1.190894 8.381960
## [50] 2.492599 1.273450 1.373647 1.240134 1.035470 1.213112 1.559097
## [57] 2.079321 1.343994 1.104046 1.389907 1.041824 1.387805 1.387073
## [64] 1.097372 1.037986 1.182245 3.586975 1.192155 1.452104 1.325262
## [71] 1.127057 1.119061 2.821673 1.396061 3.696398 1.170783 1.831023
## [78] 1.386474 1.054227 5.978477 1.032006 2.022411 1.061004 1.226667
## [85] 6.809212 4.737069 1.049484 1.358341 3.893553 1.415804 4.693787
## [92] 1.150561 1.044761 1.109948 1.018435 1.432534 2.076366 1.068532
## [99] 1.818080 1.027865 8.305123 1.016813 6.138982 1.049816 1.292699
## [106] 1.521972 1.305067 1.132293 1.052127 1.301371 1.021538 1.094526
## [113] 1.378904 1.135245 1.049934 1.277502 1.167184 2.993062 1.312984
## [120] 1.590471 3.356677 1.063880 1.131144 1.200120 1.080671 4.016903
## [127] 1.569800 2.321015 1.055575 1.271830 1.115307 1.081693 1.032252
## [134] 1.735952 1.411926 1.278666 1.130933 1.046915 1.447760 1.039163
## [141] 1.108359 1.013473 1.406993 1.066082 2.694487 1.035556 1.810573
## [148] 1.172341 1.814770 1.220597 1.410431 1.045772 1.122236 1.111290
## [155] 1.075919 1.134906 1.115394 1.902453 1.044388 1.365519 2.803708
## [162] 4.174733 1.135294 1.621732 1.451403 1.879496 1.778340 1.216100
## [169] 1.314396 1.149197 1.133119 1.323128 2.020504 1.225337 1.304712
## [176] 1.506184 1.014658 2.192719 21.610741 1.069208 1.735865 1.036229
## [183] 1.075781 6.884596 1.103690 1.207441 1.146234 1.052726 1.085369
## [190] 1.359462 1.033763 1.514820 1.169903 1.075432 2.282414 1.171442
## [197] 1.124803 1.033376 1.144793 1.064054 1.041429 1.078505 1.416111
## [204] 1.345424 10.806964 1.075128 1.045488 2.712014 3.855478 1.167164
## [211] 1.136550 1.069891 1.061717 1.026738 1.048832 2.572733 1.053537
## [218] 1.406463 1.138374 1.383943 3.669724 1.154311 4.933778 1.150195
## [225] 1.047402 1.250299 1.218468 1.217963 1.037873 1.189336 6.836567
## [232] 1.087288 2.737524 9.231183 1.115263 1.202832 1.115768 1.274462
## [239] 1.072992 1.072453 1.166903 1.247748 1.105076 18.129770 1.546757
## [246] 1.365851 1.427169 1.727053 1.115406 1.129682 1.125352 1.133137
## [253] 3.537118 1.116279 1.415539 1.990092 1.049123 1.058931 1.066683
## [260] 1.051479 2.489777 1.518366 1.027555 1.059790 1.116232 1.044173
## [267] 1.763790 4.605287 1.480653 3.322074 1.040111 1.826960 1.849549
## [274] 1.014479 4.938180 1.452729 1.068127 1.729018 1.269204 3.206319
## [281] 1.062763 1.515132 1.024925 1.440617 1.697494 1.067734 1.144717
## [288] 1.080516 1.095109 1.548073 1.052755 1.164453 1.453522 1.055906
## [295] 1.297994 1.285499 1.044360 1.159124 1.088994 4.037355 1.367735
## [302] 8.228722 1.811618 29.817749 1.257225 1.057067 1.209759 4.452010
## [309] 1.318339 1.048979 2.953300 1.122959 8.424597 1.475096 4.665392
## [316] 1.059878 1.218799 4.590020 1.042316 1.444642 1.104455 1.086500
## [323] 1.032227 1.213395 1.120421 1.048567 1.098793 1.055433 1.492009
## [330] 4.077383 1.284317 1.110160 3.829516 1.215024 1.129010 1.715220
## [337] 1.736248 1.036231 1.098823 1.672302 1.082090 1.291365 1.125758
## [344] 1.127440 6.262521 1.165023 1.010231 1.848603 6.006615 7.857022
## [351] 1.055851 1.033394 1.105820 1.019257 1.118229 1.337207 1.223072
## [358] 1.736308 1.882762 1.012131 1.077560 1.293208 1.104321 1.145200
## [365] 1.110255 1.049492 1.255591 1.626731 10.401283 1.066505 1.040452
## [372] 2.484225 1.296383 2.766894 2.352627 1.118414 1.013752 1.125186
## [379] 1.654876 1.170240 1.158968 1.252416 1.111302 1.504026 1.045519
## [386] 1.201462 1.132265 5.341823 1.332137 1.080484 1.077159 1.054134
## [393] 1.072032 1.955451 1.258220 1.181025 1.665158 2.611477 1.140311
## [400] 1.057995 1.018581 1.457639 1.067121 1.113826 1.169417 1.248765
## [407] 1.273871 1.607952 1.927072 1.027611 1.276317 1.036513 1.545871
## [414] 1.096502 1.405808 1.162540 1.088374 1.166429 1.995904 1.073923
## [421] 1.097187 1.403240 2.132992 1.137163 1.024293 1.044544 1.407507
## [428] 1.042612 6.065498 1.032764 1.121596 5.621209 1.340207 1.186952
## [435] 1.178405 1.100544 1.055354 1.023942 1.284988 1.219475 1.062586
## [442] 1.342901 1.168760 3.316235 1.196120 1.057719 1.035094 1.078462
## [449] 1.169201 4.227809 1.617938 2.737637 1.251375 1.050118 1.082182
## [456] 1.014511 1.086858 1.094946 1.026961 1.166961 1.225658 1.605367
## [463] 1.064796 1.139828 1.300212 1.894391 1.432706 1.472986 1.324900
## [470] 1.210798 1.342563 1.020966 1.086041 8.876100 1.141280 1.273108
## [477] 1.084272 1.105186 1.042606 1.213851 1.083492 1.026931 2.456436
## [484] 1.248293 2.115559 1.030158 3.115211 1.025663 1.019830 1.384944
## [491] 1.023414 1.325245 10.186173 1.208427 1.144447 1.261834 1.084355
## [498] 4.103032 1.393470 1.491865 4.738205 1.368971 3.474522 1.102345
## [505] 1.116655 1.220700 1.227531 3.916553 1.438511 2.883423 1.164185
## [512] 1.137339 1.484332 1.246535 1.610975 1.120635 6.110577 2.175068
## [519] 1.124067 5.197200 7.255030 1.075935 1.710620 1.049649 1.174788
## [526] 1.433033 1.076112 3.833958 1.234624 2.534153 1.464161 1.101299
## [533] 1.038950 1.163161 1.348466 1.283483 4.571627 1.203234 1.289419
## [540] 2.467353 1.092349 1.230033 1.502759 1.224674 1.029343 1.337326
## [547] 1.779307 1.326883 1.071008 1.150001 7.945015 1.482407 2.213061
## [554] 11.415559 1.023676 1.033536 1.698797 1.355185 3.966782 1.700365
## [561] 1.115351 1.386006 1.274612 1.103856 1.055401 14.593794 1.056312
## [568] 1.067911 1.196262 1.055442 1.180145 1.138546 1.365585 1.008594
## [575] 1.026773 1.118054 22.761594 1.044745 1.847420 1.116952 1.205919
## [582] 1.537894 2.383356 1.298890 1.159263 1.078971 1.439710 1.985616
## [589] 2.897303 1.647585 1.215378 1.531719 1.124738 1.045535 2.010395
## [596] 1.110957 1.392374 1.143215 1.255884 1.068715 1.061599 1.777483
## [603] 1.646811 1.372911 13.667365 1.040608 2.760435 1.325926 1.089946
## [610] 3.760733 1.129655 2.914947 1.641386 1.076245 1.163052 1.044905
## [617] 1.104188 13.332370 1.051674 1.204442 1.103797 1.112634 1.477842
## [624] 1.175743 1.344465 7.094685 1.117107 3.742600 1.028368 3.262832
## [631] 1.070255 3.836554 1.144904 1.057482 2.267087 2.242062 2.611975
## [638] 1.025546 1.166532 1.077609 3.231946 1.431100 1.538397 2.996903
## [645] 1.107033 1.076418 1.421403 1.354312 1.252316 3.942129 2.467213
## [652] 1.169950 1.051964 1.043931 1.037592 1.523630 1.122765 1.093696
## [659] 1.063122 5.316314 3.085900 1.204246 1.242949 1.469314 1.107898
## [666] 1.154320 1.126690 1.024532 1.012381 1.245420 1.208638 3.005619
## [673] 1.131096 1.152215 10.436848 9.275984 1.071312 1.468921 1.491201
## [680] 8.700787 1.138961 1.533192 1.010892 1.194947 1.047746 1.608362
## [687] 1.474594 1.023230 5.084773 1.078031 1.093004 2.953778 1.156948
## [694] 4.686624 1.215859 1.136405 1.537293 1.117555 1.263978 1.247610
## [701] 1.680965 1.160195 1.231423 1.138485 1.067926 1.243005 1.360549
## [708] 1.050120 7.848791 1.147181 1.219046 1.304428 7.164910 1.087504
## [715] 1.141165 1.072571 1.073235 1.380782 1.061971 1.981043 2.590871
## [722] 1.082051 1.060435 1.152380 1.220982 1.052959 1.116978 1.130946
## [729] 1.224444 1.271436 3.261697 1.095391 1.043294 1.048370 1.190436
## [736] 11.478983 1.548519 1.130742 1.058399 1.030030 1.011463 1.019962
## [743] 1.102551 1.238374 1.063948 1.062061 1.128098
Now, need to estimate Average Treatment Effect (ATE).
library(MatchIt)
# Function to estimate Average treatment effect
my_function <- function(x, i){
resampled_data <- x[i, ]
match_data <- match.data(
matchit(t ~ x1 + x2 + x3 + x4 + x5+ x6 + x7 + x8 + x9 + x10+ x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 +x21 + x22 + x23 + x24 + x25,
data = resampled_data, method = "nearest")
)
# simple mean of matched groups
est1 <- with(match_data,
mean(outcome[t == 1]) -
mean(outcome[t == 0]))
return(c(est1))
}
# Call `myFunction` with that value
ATE1 <- (my_function(dataset, 1:nrow(dataset)))
print(ATE1)
## [1] 3.956498
The ATE measures the difference in mean (average) outcomes between units assigned to the treatment and units assigned to the control. By calculating the average effect, we can recognizie the possibility of variation; and if there is important, then maybe we care enough about this variation that we should be studying it directly, rather than just trying to reduce-form it away.