Plotting Marginal Effects of an Interaction term in STATA 11.0

Hi guys,

I have an interaction term between two variables = xz = x*z and have run the regression

xtreg y x z xz i.year

y is also a continuous variable 1.4-23.4

x ranges from 0-142 and z ranges from 0-21.8 (not sure if these ranges help)

when running the regression my coefficient of xz is 0.3 with a p value of 0.05 but i want to plot the effect to see what the effect is for more than z=0

I am using stata 11 and am quite a novice so a step by step guide would be really appreciated.

My data looks a bit like this

country year y x z xz
a 2000 1.4 3 5 6
a 2001 2.3 5 3 8

for over 20 countries from years 2000-2011

I also want to have confidence intervals as well to make the graph more descriptive. Please can someone help me I can't find any good websites to help me on my problem specifically


You need to use the factor variable syntax for your interaction:
xtreg y c.x##c.z i.year
margins, at(z=0 x=(0/142)) at(z=5 x=(0/142)) at(z=10 x=(0/142)) at(z=15 x=(0/142)) at(z=20 x=(0/142))
You may also want to use the -marginsplot- noci option to suppress the CI lines.

The -marginsplot- feature was not added until Stata version 12.0. See paragraph 8 in the following:

help whatsnew11to12
The -margins- command did exist in Stata version 11.x, so @Spuz123 can graph the margins in Stata 11.x but will have to do it manually using -twoway graph-.
The easiest way would be to use the margins code bukharin provided (less the marginsplot line) to get the margins output in a table (actually a matrix) on the screen.

Then screen scrape (i.e., copy and paste using copy/Table from right-clicking in Stata) the margins output into an Excel sheet. Clean it up as you wish and change the column headings to variable names acceptable in Stata.

Then copy/paste the margins table back into an empty Stata data sheet. (It is possible to get the data directly into a Stata matrix without screen scraping, but unless you have a lot of repeated tasks, screen scraping is easier and quicker.)

Then you have the data in Stata that you need to do the graphing.

For the graphing itself, you would use a series plots within a single -twoway- command. For the CI bars, you would use -rcap- as the graph type. For the lines, you would use -connected- as the graph type. To obtain different CIs and lines for different combinations, you would just add an -if- selection in the several -rcap- and -connected- graph segments.

If you can get access to Stata 12 or 13 (perhaps in a university computer lab), all of this can be done just as buharin showed in his code -- which is far easier and quicker.


Sorry about that - I was in a rush and misread it as version 12.

In version 11 I always took the easy route and used -parmest-, which is available from SSC using:
ssc install parmest
-parmest- allows you to convert the results of an estimation command (including a regression model, but also including the result of -margins-) to a dataset for subsequent use such as graphing.

Here is an example. You'll see it's a little fiddly, but with a bit of playing around it's not too bad. Note that I've added the "post" option to the -margins- command so that it posts its output as an estimation command, allowing -parmest- to access the results.
sysuse auto, clear
reg price c.mpg##c.weight
margins, at(weight=2000 mpg=(12/41)) at(weight=3000 mpg=(12/41)) ///
	at(weight=4000 mpg=(12/41)) post
parmest, norestore
gen weight=2000 in 1/30
replace weight=3000 in 31/60
replace weight=4000 in 61/90
gen n=_n
bysort weight (n): gen mpg=11 + _n
separate estimate, by(weight) gen(price) shortlabel
line price* mpg
That's a good, easy to use solution, @bukharin.

@Spuz123, if you want to add confidence intervals around the lines in the plot, insert the following below -line price* mpg- and immediately before -restore- in bukharin's code.

twoway (rcap max95 min95 mpg if weight==2000, lcolor(brown)) (rcap max95 min95 mpg if weight==3000, lcolor(navy)) (rcap max95 min95 mpg if weight==4000, lcolor(gs11)) (connected estimate mpg if weight==2000, msym(Sh) mcolor(brown) lcolor(brown)) (connected estimate mpg if weight==3000, msym(Oh) mcolor(navy) lcolor(navy)) (connected estimate mpg if weight==4000, msym(Dh) mcolor(gs11) lcolor(gs11)) , ytitle("Estimate") xscale(range(10 43)) legend(off) note("Gold: 2000 lbs.     Navy: 3000 lbs.     Gray: 4000 lbs.") scheme(s1color) name(lineci, replace)
Hi everybody,

I have Stata 11 - and I want to plot an interaction effect between a cont. variable and a dummy variable. I may be extremely stupid - but I can't find how to do that.

I mean all I want is the fitted regression line for my cont. variable depending whether my dummy is 0 or 1.

Can anyone help please?
Hi everybody,

I have Stata 11 - and I want to plot an interaction effect between a cont. variable and a dummy variable. I may be extremely stupid - but I can't find how to do that.

I mean all I want is the fitted regression line for my cont. variable depending whether my dummy is 0 or 1.

Can anyone help please?

Today I received tremendous help on this and want to share the solution with you - just in case you are a Stata rookie like me.

Imagine, our model looks like this:

DV = alpha + ß1 predictor1(cont.) + ß2 predictor2(dummy) + ß3 predictor1*predictor2+ controls.

We want to visualize the interaction effect.

This means in essence that we want to visualize the effect of predictor1 on the DV under two conditions: first, for predictor2 = 0 and second for predictor2 = 1, holding all other variables constant.

In essence, we want two regression lines. One visualizes the effect of predictor1 on our DV, given predictor2=0. And the other one visualizes the effect, given predictor2=1.

For this, we run the regression of the full model, including predictor1, predictor2, the interaction, and the controls.

Then we check the range of predictor1, e.g. by using the codebook command. In my case, it is [-2.9;2.9].

Then we use this command:
margins, at(predictor1=(-2.93(.5)2.93) predictor2=0) vsquish

- predictor1 = my main predictor, continuous
- predictor2 = my main predictor, dummy
- .5 sets the increment and can be adjusted of course
- vsquish just makes sure that the table looks nice

This gives a table with the predicted DV values, for each predictor1 value, given predictor2 = 0 and the other variables are set to their means (=neutralized).

Then we enter the command again, but with predictor2 =1:
margins, at(predictor1=(-2.93(.5)2.93) predictor2=1) vsquish

So, we get 2 tables with values for our two regression lines. Now we can plot them:
If you have Stata 12, use the command marginsplot
If you have a lower version, copy these tables to excel and do it from there.

If the two lines have different slopes, this visualizes the interaction effect.