如何在全局宏中包含交互项以用于 reghdfe 命令?

How to include interaction term in global macro for use in the reghdfe command?

我想在 community-contributed regdhdfe 命令中创建一个交互项。

两个交互变量之一是连续变量,与全局变量一起使用。另一个变量是性别虚拟变量。

我的代码看起来像这样:

clear all
set obs 25000

local vlist v0 v1 v2 v3 v4 v5 v6 v7 v8 z4 z5 z6 

foreach v of local vlist { 
    generate `v' = runiform()
}


rename v8 i_female 
replace i_female=0 if i_female<0.5
replace i_female=1 if i_female>=0.5

foreach num of numlist 4 5 6 {
    global varlist_`num' "v`num' z`num'"
}

foreach num of numlist 4 5 6 {
    reghdfe v0 ${varlist_`num'} ${varlist_`num'}#i.i_female if v1<0.8, absorb(v7)
}

此returns以下错误:

error: there are repeated variables:
r(198);

上面代码中你想要的和你指示Stata做的是两个 不同的东西。

Stata 抱怨有重复的变量,因为当宏展开时你没有得到 v4 z4 v4*i_female z4*i_female

相反,变量 v4v5v6 被复制:

foreach num of numlist 4 5 6 {
    global varlist_`num' "v`num' z`num'"
}

foreach num of numlist 4 5 6 {
    display "v0 ${varlist_`num'} ${varlist_`num'}#i.i_female if v1<0.8, absorb(v7)"
}

v0 v4 z4 v4 z4#i.i_female if v1<0.8, absorb(v7)
v0 v5 z5 v5 z5#i.i_female if v1<0.8, absorb(v7)
v0 v6 z6 v6 z6#i.i_female if v1<0.8, absorb(v7)

为了获得所需的输出,您需要按如下方式修改代码:

foreach num of numlist 4 5 6 {
    global varlist1_`num' "v`num' z`num'"
    global varlist2_`num' "v`num'#i.i_female z`num'#i.i_female"
}

foreach num of numlist 4 5 6 {
    display "v0 ${varlist1_`num'} ${varlist2_`num'} if v1<0.8, absorb(v7)"
}

v0 v4 z4 v4#i.i_female z4#i.i_female if v1<0.8, absorb(v7)
v0 v5 z5 v5#i.i_female z5#i.i_female if v1<0.8, absorb(v7)
v0 v6 z6 v6#i.i_female z6#i.i_female if v1<0.8, absorb(v7)