### Stata FAQ How can I graph the results of the margins command? (Stata 11)

use http://www.ats.ucla.edu/stat/data/hsbdemo, clear

/* plot adjusted cell means */
margins prog#female, asbalanced
mat b=r(b)'
mat prg=(1\2\3)#(1\1)
mat fem=(1\1\1)#(0\1)
mat c=prg,fem,b
svmat c, names(c)

/* plot prog by female */
twoway (connect c3 c1 if c2==0)(connect c3 c1 if c2==1), ///
xlabel(1 2 3) legend(order(1 "female=0" 2 "female=1")) ///
xtitle(prog) ytitle(ajdusted cell means) ///
name(prog_by_female, replace)

/* plot female by prog */
twoway (connect c3 c2 if c1==1)(connect c3 c2 if c1==2)(connect c3 c2 if c1==3), ///
xlabel(0 1) legend(order(1 "prog=1" 2 "prog=2" 3 "prog=3")) ///
xtitle(female) ytitle(ajdusted cell means) ///
name(female_by_prog, replace)

use http://www.ats.ucla.edu/stat/data/logitcatcon, clear
logit y i.f##c.s, nolog

/* plot probabilities by categorical variable */
margins f, at(s=(20(2)70)) vsquish
mat b=r(b)'
mat at=r(at)
mat at=at[1...,3]#(1\1)
mat p=at,b
svmat p, names(p)
generate p3 = ~mod(_n,2) in 1/52
twoway (line p2 p1 if p3==0)(line p2 p1 if p3==1), ///
legend(order(1 "f=0" 2 "f=1")) xtitle(variable s) ///
ytitle(probability) name(probability, replace)

/* difference in probabilities */
margins, dydx(f) at(s=(20(2)70)) vsquish
mat b=r(b)'
mat at=r(at)
mat at=at[1...,3]
local nrow=rowsof(at)
mat b=b[nrow'+1...,1]
mat se=r(V)
mat se=se[nrow'+1...,nrow'+1...]
mat se=vecdiag(cholesky(diag(vecdiag(se))))'
mat d=at,b,se
mat lis d
svmat d, names(d)
gen ul = d2 + 1.96*d3
gen ll = d2 - 1.96*d3
twoway (rarea ul ll d1, color(gs13) lcolor(gs13)) ///
(line d2 d1), yline(0) legend(off) ///
xtitle(variable s) ytitle(difference in probability) ///
name(difference, replace)`

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.