# Plotting marginal effects of interaction terms in stata

In case your model includes interaction terms, interpretation of results is not straightforward anymore. Due to (at least) two different standard errors, you should be careful in interpreting results as significant. I found a very nice article explaining this in detail. You can find it here: http://pan.oxfordjournals.org/cgi/content/abstract/14/1/63. The article refers to paper of  Golder, who has included his relevant do-files on his personal website: http://homepages.nyu.edu/~mrg217/.

In the following, I present a do-file I used to plot marginal effects with the relevant standard errors:

* Regression:
xi: reg y x1 x2 x3 x4 x5 x6

Imagine, y denotes wage, x1 is a gender dummy, x2 is a variable showing years of schooling (0-10) and x3 is de interaction variable between gender and years of schooling.

* Generation of variables needed to construct the graph:
gen nr=_n-1
replace nr=. if nr>10
lab var nr “Education level”

matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1]
scalar b2=b[1,2]
scalar b3=b[1,3]

scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar covb2b3=V[2,3]

* Create full range of marginal effects
gen conb1=b1+b3*nr

* Create full range of standard errors
gen conse1=sqrt(varb1+varb3*nr^2+2*covb1b3*nr)

* Generate confidence intervals at the 95% level
gen a1=1.96*conse1
gen top1=conb1+a1
gen bottom1=conb1-a1

* Plotting
graph twoway  (line conb1 nr if nr>0, clwidth(medium) clcolor(blue) clcolor(black)) /*
*/  (line top1  nr if nr>0, clpattern(dash) clwidth(thin) clcolor(black)) /*
*/  (line bottom1 nr if nr>0, clpattern(dash) clwidth(thin) clcolor(black)), /*
*/  ytitle(“Marginal effect of” “gender”) /*
*/  xtitle(“Education level”) /*
*/  yscale(noline) xscale(noline) legend(off) yline(0, lcolor(black)) /*
*/  graphregion(fcolor(white)lcolor(white)icolor(white)ilcolor(white)ilstyle(none)color(white)) /*
*/  plotregion(fcolor(white)lcolor(white)icolor(white)ilcolor(white)ilstyle(none)color(white) )
graph2tex, epsfile(graph1)
pwd

Via this graph, you can easily see for which years of schooling the gender effect is significant, and for which years of schooling it isn’t.

## 3 thoughts on “Plotting marginal effects of interaction terms in stata”

1. Patrick on said:

Very useful. Thanks!

2. Erik on said:

Thanks. Your tip is really helpful

3. Erik on said:

GRAPH INTERACTION EFFECT OF TWO CONTINUOUS VARIABLES

Hi everybody,

I have the logistic and tobit regression models:
1. Logit (Y) = X1 + X2 + X1*X2 + control variables
2. Logit (Y) = X1 + X1^2 + X2 + X1^2*X2 + control variables
3. Tobit (Y) = X1 + X2 + X1*X2 + control variables
4. Tobit (Y) = X1 + X1^2 + X2 + X1^2*X2 + control variables
These independent and moderate variables are continuous and are standardized, with these models, after regression, how can I graph the interaction effects in Stata? Could you please help me to show any specific command in Stata that could do these for me?

In the case, the above independent and moderate variables are not standardized, how can I graph the interaction effects in Stata for this case?

Many thanks in advance.

Kind regards,
Erik.