Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Graphic displays or results from margins

From   Richard Williams <>
Subject   Re: st: Graphic displays or results from margins
Date   Tue, 21 Sep 2010 19:56:36 -0500

At 09:53 PM 9/20/2010, Scott Merryman wrote:

FWIW, the latest Stata News ( )
discusses graphing marginal effects.


Scott, thanks again. They use the -parmest- command, like Martin suggested yesterday. I've been trying to create a very flexible command that requires little user input, but it is not easy! But here is what I have at the moment. I think it would work very well for problems that are remarkably similar to the one I posed, but I am not sure how well it generalizes beyond that.

* Richard Williams, Notre Dame - Work in progress - Sept. 21, 2010
* parmest must be installed
* Generate line plots of predicted margins for one or more
* groups.  Groups must be mutually exclusive, e.g. you can't do
* both male/female and black/white in the same graph, but you
* could do gender or race separately or do their interaction
* female#race
version 11.1
capture program drop margins2dta
program margins2dta, eclass
        syntax [, ngrps(int 1) atvals(string)]
        quietly {
                parmest, norestore
                gen casenum = _n
                * Determine group membership.
                egen grpnum = seq(), from(1) to(`ngrps')
                * Create vars for each group with its predicted probabilities
                forval grp = 1/`ngrps' {
                        gen group`grp' = estimate if grpnum == `grp'
                * Determine the correct atvalue for each case
                sort grpnum, stable
                by grpnum: gen atvarindex = _n
                sort casenum
                gen atvar = .
                forval atvalue = `atvals' {
                        local atvarindex2 = `atvarindex2' + 1
replace atvar = `atvalue' if atvarindex == `atvarindex2'

webuse nhanes2f, clear
* You need to know how many groups are created by the margins command,
* e.g. how many groups are there with female#black
local ngrps = 4
local atvals 20(10)74
logit diabetes i.female age i.female#c.age, nolog
margins female#black, at(age=(`atvals')) post
margins2dta, ngrps(`ngrps') atvals(`atvals')

* Need to modify this as needed for the graph you have run
local legend label(1 "NonBlack males") label(2 "Black males")
local legend `legend' label(3 "NonBlack females") label (4 "Black females")
local atvarlabel Age in Years
local ytitle "Probability of diabetes"
local xlabel #10
label variable atvar "`atvarlabel'"
line group* atvar, legend(`legend') ytitle("`ytitle'") xlabel(`xlabel')

Richard Williams, Notre Dame Dept of Sociology
OFFICE: (574)631-6668, (574)631-6463
HOME:   (574)289-5227
EMAIL:  Richard.A.Williams.5@ND.Edu

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index