专栏名称: 连享会
连玉君老师团队分享,主页:lianxh.cn。白话计量,代码实操;学术路上,与君同行。
目录
相关文章推荐
湖北经视  ·  上个公厕,夫妻接连感染此病毒!紧急提醒 ·  2 天前  
湖北经视  ·  明起预约!这笔钱别忘了领 ·  3 天前  
湖北经视  ·  刘诗诗、吴奇隆,突传消息! ·  3 天前  
51好读  ›  专栏  ›  连享会

Stata:iematch-近邻贪婪匹配

连享会  · 公众号  ·  · 2025-01-18 22:00

正文


👇 连享会 · 推文导航 | www.lianxh.cn

🍓 课程推荐: 连享会:2025 寒假前沿班
嘉宾:杨海生,中山大学
时间:2025 年 1 月 13-24 日
咨询:王老师 18903405450(微信)




作者 :王乔 (中南财经政法大学)
邮箱 [email protected]

温馨提示: 文中链接在微信中无法生效。请点击底部 「阅读原文」 。或直接长按/扫描如下二维码,直达原文:


目录

  • 1. iematch 简介

  • 2. 命令语法

  • 3. 案例应用

  • 4. 相关推文



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 ,以及使用 iematch seedok 选项。同时,在 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

温馨提示: 文中链接在微信中无法生效。请点击底部 「阅读原文」







请到「今天看啥」查看全文