Stata:iematch-近邻贪婪匹配

发布时间:2021-03-26 阅读 3255

Stata连享会   主页 || 视频 || 推文 || 知乎

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装命令如下:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

作者:王乔 (中南财经政法大学)
邮箱zuelerqiao@foxmail.com

目录

1. iematch 简介

在进行倾向得分匹配 (Propensity Score Matching, PSM) 时,psmatch2 命令可以很快的实现匹配,类似的还有很多命令。但是,当 PSM 某个步骤需要特殊处理时,如只对特定种族或年份匹配,psmatch2 等命令可能并不方便。此时,我们可以选择 iematch 命令来执行匹配。

iematch 命令主要是根据某个连续变量取值执行近邻贪婪匹配,即在限定范围内,为基本观测值依次寻找最近邻目标观测值。为帮助大家更好了解和使用 iematch 命令,在下文中,我们将详细介绍命令语法和案例使用。

2. 命令语法

由于 iematch 命令包含在 ietoolkit 中,故通过以下命令安装:

ssc install ietoolkit, replace

iematch 命令语法如下:

iematch [if] [in] , grpdummy(varname) matchvar(varname) [idvar(varname) m1 maxdiff(numlist) seedok matchidname(string) matchdiffname(string)
    matchresultname(string) matchcountname(string) replace]
  • grpdummy(varname):用来指示观测值是基本观测值还是目标观测值,其中基本观测值可以理解为处理组,目标观测值可以理解为控制组;
  • matchvar(varname):用于匹配的变量;
  • idvar(varname):表示唯一且完全标识数据集的变量;
  • maxdiff(numlist):设定匹配范围;
  • m1:多对一匹配,即允许多个基础观测值与单个目标观测值匹配,默认情况下是一对一匹配;
  • maxmatch(integer):设置 m1 匹配中目标观测值允许匹配的最大基本观测值数量;
  • seedok:当 matchvar() 的变量值存在重复时,seedok 会抑制抛出的错误消息。当存在重复时,iematch 命令会使用一个随机变量来区分要匹配哪个变量。不过,我们可以通过设定种子值和 seedok 选项来避免这种随机;
  • matchresultname(string):设置输出结果的变量名称;
  • matchidname(string):设置匹配对中目标观察值 ID 的变量名称;
  • matchdiffname(string):设置基本观测值和目标观测值间差异的变量名称;
  • matchcountname(string):设置目标观测值匹配数量的变量名称;
  • replace:允许 iemmatch 在遇到变量名称冲突时,替代内存中已有的变量。

3. 案例应用

在这里,本文以数据「nlsy」为例进行演示。

*数据地址:https://gitee.com/arlionn/data/blob/master/data01/nlsy.dta

首先,通过 des 命令了解一下数据基本情况。由下文可以看出,该数据共有 3100 个观测值,并且在本文中,我们将 first 作为处理变量,将 ppvt 作为结果变量。


. use nlsy.dta, clear

. des 

Contains data from nlsy.dta
  obs:         3,100                          
 vars:            29                          20 Oct 2004 15:03
------------------------------------------------------------
              value
variable      label      variable label
------------------------------------------------------------
row_names                
hispanic      hispanic   hispanic
black         black      black
white         white      white
momrace       momrace    mother's race
female        female     female
b_marr        b_marr     mom married at birth?
pr0           pr0        was household in poverty the year before child was born?
lths                     mom's ed is less than high school
hs                       mom is hs grad
ltcoll                   mom attended some college
college                  mom finished college
momed         momed      mom's education level when she gave birth
lnfinc_a0                logged family income in the year before the child was born
age                      child's age (in months) on 1/1/90
momage                   
afqt                     mom's score on the armed forces qualifying test
brthwt                   birthweight of child (oz)
brorddum      brordum    first born?
preterm                  weeks preterm
rmomwk        rmomwk     did respondent's momther work while she was in high school?
work          treat      did mom work in the first 3 years?
ppvt                     peabody picture vocab test score at 36 months
piatm56                  score on the piat math component at age 5 or 6
piatm78                  score on the piat mathcomponent at age 7 or 8
piatr56                  score on the piat reading component at age 5 or 6
piatr78                  score on the piat reading component at age 7 or 8
notfirst                 
first                    
------------------------------------------------------------
Sorted by: 

然后,将变量 ppvt 对全局暂元 x 进行 Logit 回归,并计算 pscore 值。

global x lths hs ltcoll college lnfinc_a0 age momage afqt brthwt preterm //设定全局变量
logit ppvt $x     //进行 logit 回归
predict pscore, p //计算 pscore 值

最后,使用 iematch 命令完成匹配。值得注意的是,为了保证结果的可重新,我们需要设定 set seed 12345,以及使用 iematchseedok 选项。同时,在 iematch 命令语句中,grpdummy(first) matchvar(pscore) 表示根据 pscore 值,为 first 等于 1 的组中观测值匹配 first 等于 0 的组中观测值。maxmatch(3) 表示目标观测值允许匹配的最大观测值数为 3。

. set seed 12345

. iematch, grpdummy(first) matchvar(pscore) seedok m1 maxmatch(3)

----------------------------------------------------------------------

    Observations are excluded from the matching for the following reasons:
    285 observation(s) were excluded due to missing value in matchvar(pscore).
max count

----------------------------------------------------------------------

    Matching many-to-one. Base observations left to match:
    2034 1473 1028 708 537 426 361 313 281 256 234 218 208 200 193 
    186 180 175 170 166 162 159 156 153 150 147 144 141 138 136 133
    130 128 126 124 122 121 120 119 118 117 116 115 114 113 112 111 
    110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93
    92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 
    71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50
    49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 
    28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
    5 4 3 2 1 0

----------------------------------------------------------------------

    Output of Matching Result:

         _matchResult              first          
   |----------------------+-----------------------|
   |   value and result   | 1 (base)   0 (target) |
   |----------------------+----------+------------|
   | 1 Matched            |    2,034 |        727 |
   | 0 Not matched        |        0 |         54 |
   |.m Missing matchvar() |      252 |         33 |
   |----------------------+----------+------------|
   |     N per group      |    2,286 |        814 |
   |----------------------+-----------------------|
   |       Total N        |         3,100         |
   +----------------------------------------------+

匹配结果显示,Matched 组中目标观测值 (0 代表目标观测值) 的数量为 727,基础观测值 (1 代表基础观测值) 的数量为 2034。Not matched 组中目标观测值数量为 54,基础观测值数量为 0。遗漏值组中,目标观测值遗漏数量为 33,基础观测值遗漏数量为 252。

接下来,将通过在上述 iematch 命令语句的基础上增加匹配的限定条件,来进一步丰富 iematch 命令功能。在这里,我们仅以增加 if white == 1 为例,其作用是将匹配限定在 white 等于 1 的组中。另外,我们也增加了 maxdiff(.03) 选项,其作用是将基本观测和目标观测间差距限定在 0.03 内。

. iematch if white == 1, grpdummy(first) matchvar(pscore) seedok m1 maxmatch(3) maxdiff(.03)

----------------------------------------------------------------------------

    Observations are excluded from the matching for the following reasons:
    1498 observation(s) were excluded in if/in: (if white == 1).
    203 observation(s) were excluded due to missing value in matchvar(pscore).
max count

----------------------------------------------------------------------------

    Matching many-to-one. Base observations left to match:
    1031 760 554 394 309 256 219 192 171 159 147 136 128 120 
    114 112 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94
    93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 
    74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51
    50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 
    29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
    6 5 4 3 2 1 0

----------------------------------------------------------------------------

    Output of Matching Result:

          _matchResult               first          
   |------------------------+-----------------------|
   |    value and result    | 1 (base)   0 (target) |
   |------------------------+----------+------------|
   | 1 Matched              |    1,031 |        353 |
   | 0 Not matched          |        0 |         15 |
   |.i Excluded using if/in |    1,078 |        420 |
   |.m Missing matchvar()   |      177 |         26 |
   |------------------------+----------+------------|
   |      N per group       |    2,286 |        814 |
   |------------------------+-----------------------|
   |        Total N         |         3,100         |
   +------------------------------------------------+

匹配结果显示,Matched 组中目标观测值 (0 代表目标观测值) 的数量为 353,基础观测值 (1 代表基础观测值) 的数量为 1031。Not matched 组中目标观测值数量为 15,基础观测值数量为 0。遗漏值组中,目标观测值遗漏数量为 26,基础观测值遗漏数量为 177。

由于在进行匹配的时候,我们增加了条件 if white == 1,所以剔除观测值数量为 1498,则进行匹配的观察值数量为 1602。当然,我们也可以对变量 white 进行分类统计,来验证上述结论。

. tab white

      white |      Freq.     Percent        Cum.
------------+-----------------------------------
  non-white |      1,498       48.32       48.32
      white |      1,602       51.68      100.00
------------+-----------------------------------
      Total |      3,100      100.00

4. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh 匹配, m
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,400+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxh 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh