👇 连享会 · 推文导航 | www.lianxh.cn
🍎 Stata:Stata基础 | Stata绘图 | Stata程序 | Stata新命令 📘 论文:数据处理 | 结果输出 | 论文写作 | 数据分享 💹 计量:回归分析 | 交乘项-调节 | IV-GMM | 时间序列 | 面板数据 | 空间计量 | Probit-Logit | 分位数回归 ⛳ 专题:SFA-DEA | 生存分析 | 爬虫 | 机器学习 | 文本分析 🔃 因果:DID | RDD | 因果推断 | 合成控制法 | PSM-Matching 🔨 工具:工具软件 | Markdown | Python-R-Stata 🎧 课程:最新专题 | 计量专题 | 关于连享会
🍓 课程推荐:连享会:2025 寒假前沿班
嘉宾:杨海生,中山大学
时间:2025 年 1 月 13-24 日
咨询:王老师 18903405450(微信)
作者:王乔 (中南财经政法大学)
邮箱:zuelerqiao@foxmail.com
温馨提示: 文中链接在微信中无法生效。请点击底部「阅读原文」。或直接长按/扫描如下二维码,直达原文:
目录
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
温馨提示: 文中链接在微信中无法生效。请点击底部「阅读原文」。
专题:数据处理 Stata:数据合并与匹配-merge-reclink 专题:PSM-Matching Stata:终极匹配 ultimatch Stata 手动:各类匹配方法大全 A——理论篇 Stata:psestimate-倾向得分匹配(PSM)中协变量的筛选 Stata:广义精确匹配-Coarsened-Exact-Matching-(CEM) Stata:psestimate-倾向得分匹配(PSM)中匹配变量的筛选 Stata+PSM:倾向得分匹配分析简介 Stata-从匹配到回归:精确匹配、模糊匹配和PSM Stata:PSM-倾向得分匹配分析的误区 Stata:模糊匹配之matchit
尊敬的老师 / 亲爱的同学们:
连享会致力于不断优化和丰富课程内容,以确保每位学员都能获得最有价值的学习体验。为了更精准地满足您的学习需求,我们诚挚地邀请您参与到我们的课程规划中来。请您在下面的问卷中,分享您 感兴趣的学习主题或您希望深入了解的知识领域 。您的每一条建议都是我们宝贵的资源,将直接影响到我们课程的改进和创新。我们期待您的反馈,因为您的参与和支持是我们不断前进的动力。感谢您抽出宝贵时间,与我们共同塑造更加精彩的学习旅程!https://www.wjx.cn/vm/YgPfdsJ.aspx# 再次感谢大家宝贵的意见!
New! Stata 搜索神器:
lianxh
和songbl
GIF 动图介绍
搜: 推文、数据分享、期刊论文、重现代码 ……
👉 安装:
. ssc install lianxh
. ssc install songbl
👉 使用:
. lianxh DID 倍分法
. songbl all
🍏 关于我们
连享会 ( www.lianxh.cn,推文列表) 由中山大学连玉君老师团队创办,定期分享实证分析经验。 直通车: 👉【百度一下: 连享会】即可直达连享会主页。亦可进一步添加 「知乎」,「b 站」,「面板数据」,「公开课」 等关键词细化搜索。