> library(MatchIt) Ben Hansen matching Exs using MatchIt wrapper Loading required package: MASS Stat 209 2/26/08 Loading required package: optmatch Loading required package: Matching ## MatchIt (Version 2.3-1, built: 2007-10-11) ## Please refer to http://gking.harvard.edu/matchit for full documentation > data(nuclear) > nuclear cost date t1 t2 cap pr ne ct bw cum.n pt 1 460.1 68.58 14 46 687 0 1 0 0 14 0 2 453.0 67.33 10 73 1065 0 0 1 0 1 0 3 443.2 67.33 10 85 1065 1 0 1 0 1 0 4 652.3 68.00 11 67 1065 0 1 1 0 12 0 5 642.2 68.00 11 78 1065 1 1 1 0 12 0 .............................................. 24 608.8 70.08 19 58 821 1 0 0 0 3 0 29 284.9 67.83 12 63 886 0 0 0 1 11 1 32 270.7 67.83 7 80 886 1 0 0 1 11 1 > bennuke = subset(nuclear, subset = pt == 0, select = c(date,cap,pr)) > bennuke$cap = bennuke$cap - 400 > bennuke$date = bennuke$date - 65 > bennuke date cap pr date cap pr 1 3.58 287 0 14 3.92 650 0 2 2.33 665 0 15 3.92 450 0 3 2.33 665 1 16 3.42 378 0 4 3.00 665 0 17 4.50 445 0 5 3.00 665 1 18 3.42 130 1 6 2.92 114 0 19 4.17 690 0 7 3.17 422 0 20 3.92 650 1 8 3.42 57 0 21 3.75 513 0 9 3.42 422 1 22 5.92 428 1 10 3.33 392 0 23 4.67 386 0 11 3.58 160 0 24 5.08 421 1 12 3.75 390 0 25 5.42 138 0 13 3.42 130 0 26 6.08 730 0 > mopt2= matchit(pr ~ date + cap, data = bennuke, method = "optimal", ratio = 2) > summary(mopt2) Percent Balance Improvement: Sample sizes: Control Treated All 19 7 Matched 14 7 Unmatched 5 0 Discarded 0 0 > #subclass gives you the matching > mopt2.data = match.data(mopt2) > mopt2.data date cap pr distance weights subclass 2 2.33 665 0 0.3409 1 1 3 2.33 665 1 0.3409 1 1 4 3.00 665 0 0.3513 1 2 5 3.00 665 1 0.3513 1 2 6 2.92 114 0 0.1514 1 4 7 3.17 422 0 0.2518 1 6 9 3.42 422 1 0.2551 1 3 10 3.33 392 0 0.2427 1 3 12 3.75 390 0 0.2473 1 3 13 3.42 130 0 0.1601 1 4 14 3.92 650 0 0.3590 1 2 15 3.92 450 0 0.2727 1 7 17 4.50 445 0 0.2787 1 6 18 3.42 130 1 0.1601 1 4 19 4.17 690 0 0.3817 1 5 20 3.92 650 1 0.3590 1 5 21 3.75 513 0 0.2960 1 1 22 5.92 428 1 0.2917 1 6 23 4.67 386 0 0.2578 1 7 24 5.08 421 1 0.2770 1 7 26 6.08 730 0 0.4328 1 5 > #this corresponds to hansen slide 45 p2 stat209 2/26/08 Ben Hansen, Gender Equity Example > geneq = read.table(file="D:\\drr08\\stat209\\week8\\genderdata", header = T) > geneq log10Grant gender 1 5.7 W 2 4.0 W 3 3.4 W 4 3.1 W 5 5.5 M 6 5.3 M 7 4.9 M 8 4.9 M 9 3.9 M # had some problems matchit; outcome has to be 1=W, 0=M or do a as.numeric > #outcome has to be numeric categorical, not W/M > mfullgen= matchit(gender ~ Grant, data = geneq, method = "full") > summary(mfullgen) Call: matchit(formula = gender ~ Grant, data = geneq, method = "full") Summary of balance for all data: Means Treated Means Control Mean Diff eQQ Med eQQ Mean eQQ Max distance 0.576 0.339 0.237 0.275 0.252 0.421 Grant 4.050 4.900 -0.850 0.850 0.850 1.500 Summary of balance for matched data: Means Treated Means Control Mean Diff eQQ Med eQQ Mean eQQ Max distance 0.576 0.532 0.045 0.129 0.121 0.463 Grant 4.050 4.213 -0.163 0.500 0.507 1.800 Percent Balance Improvement: Mean Diff. eQQ Med eQQ Mean eQQ Max distance 81.18 53.10 51.96 -9.927 Grant 80.88 41.18 40.37 -20.000 Sample sizes: Control Treated All 5 4 Matched 5 4 Unmatched 0 0 Discarded 0 0 > mfullgen.dat = match.data(mfullgen) > mfullgen.dat Grant gender distance weights subclass 1 5.7 1 0.1561 1.0000 1 2 4.0 1 0.5903 1.0000 2 3 3.4 1 0.7484 1.0000 2 4 3.1 1 0.8103 1.0000 2 5 5.5 0 0.1907 0.3125 1 6 5.3 0 0.2307 0.3125 1 7 4.9 0 0.3271 0.3125 1 8 4.9 0 0.3271 0.3125 1 9 3.9 0 0.6192 3.7500 2 > #corresponds to slide, subclass