代码之家  ›  专栏  ›  技术社区  ›  LucasMation

在coefplot中只绘制交互项

  •  0
  • LucasMation  · 技术社区  · 7 年前

    在斯塔塔回归之后,我试图只绘制交互项的系数。

    我不能用 社区贡献 命令 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 )
    

    还有别的办法吗?

    我把这个问题交叉贴在 Statalist .

    1 回复  |  直到 7 年前
        1
  •  3
  •   Pearly Spencer Paul Cruz    7 年前

    您只需要指定交互:

    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')
    

    编辑:

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

    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')
    
    推荐文章