3

After a regression in Stata, I am trying to plot only the coefficients of the interaction terms.

I was unable to do this using the community-contributed command coefplot.

Here is a reproducible example and my attempted solutions:

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 )

Is there any other way to to this?

I have cross-posted this question on Statalist.

LucasMation
  • 2,408
  • 2
  • 22
  • 45

2 Answers2

6

You just need to specify the interactions:

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

EDIT:

You can also get all non-zero coefficients as follows:

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')
  • 2
    By the way, after `regress` you can also use `margins 1.foreign#i.turn` and then `marginsplot` to get the same graph. –  Jul 23 '18 at 20:30
  • do you envision a more automatic way to list the coefficients? I have several res, each of which is saved with eststo. I have 108 time periods, which get interacted with the treatment variable – LucasMation Jul 23 '18 at 21:18
  • 2
    It is not clear to me from your comment what you have. I think you may want to post a new question about this providing more details. –  Jul 23 '18 at 21:29
5

A solution a little bit easier than the one already proposed is to use wildcards ("*" or "?").

sysuse auto, clear
reg price foreign i.turn foreign#i.turn
coefplot, keep(*.foreign#*.turn)