在 coefplot 中仅绘制交互项

Luc*_*ion 2 stata coefplot stata-macros

在 Stata 中进行回归后,我试图仅绘制交互项的系数。

我无法使用社区贡献的命令来做到这一点coefplot

这是一个可重现的示例和我尝试的解决方案:

sysuse auto, clear
reg price foreign i.turn foreign#i.turn

*this plots all coefficients:
coefplot,

*this drops _cons and foreign but not i.turn
coefplot, drop(i.turn _cons foreign )

*variations with keep also do not work
coefplot, keep(foreign#i.turn )
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以做到这一点?

我已经在Statalist上交叉发布了这个问题。

小智 5

您只需要指定交互:

sysuse auto, clear

reg price foreign i.turn foreign#i.turn, coeflegend noheader

local coefinter 1.foreign#33.turn 1.foreign#34.turn 1.foreign#35.turn ///
                1.foreign#36.turn 1.foreign#37.turn

coefplot, keep(`coefinter')
Run Code Online (Sandbox Code Playgroud)

编辑:

您还可以获得所有非零系数,如下所示:

sysuse auto, clear
reg price foreign i.turn i.foreign#i.turn, coeflegend noheader

matrix A = e(b)
local namecol "`: colnames A'"

tokenize `namecol'

forvalues i = 1 / `=colsof(matrix(A))' {
    local mv = A[1,`i']
    if `mv' != 0 & strmatch("``i''" , "*#*") {
        local coefinter `coefinter' ``i''
    }
}

coefplot, keep(`coefinter')
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,在“regress”之后,您还可以使用“margins 1.foreign#i.turn”,然后使用“marginsplot”来获得相同的图表。 (2认同)
  • 从你的评论中我不清楚你有什么。我想您可能想发布一个关于此的新问题,提供更多详细信息。 (2认同)

小智 5

一种比已经提出的解决方案更简单的解决方案是使用通配符(“*”或“?”)。

sysuse auto, clear
reg price foreign i.turn foreign#i.turn
coefplot, keep(*.foreign#*.turn)
Run Code Online (Sandbox Code Playgroud)