Graphic displays or results from margins

9 messages
Open this post in threaded view
|

Graphic displays or results from margins

 I am warming up to the new margins command.  However, unlike the older adjust command, margins does not include the ability to generate new variables. I am therefore finding it hard to create graphic displays of the results. Consider the following: webuse nhanes2f, clear logit diabetes i.black i.female age i.female#c.age, nolog margins female#black, at(age = (20(10)70)) I would love to create a graphic where the Y axis was the probability of diabetes, the X axis was age, and there were 4 separate lines, for black males, black females, nonblack males, nonblack females. I imagine I could generate variables myself (basically replicating what margins is doing) or (ugh!) cut and paste results into an excel spreadsheet. But, is there some nice, simple approach for doing this? The prgen command in Long & Freese's spost9 suite of commands can do things like this, but it doesn't support many of the features of margins, e.g. it doesn't work with factor variables, and things like interaction terms are going to confuse it. ------------------------------------------- Richard Williams, Notre Dame Dept of Sociology OFFICE: (574)631-6668, (574)631-6463 HOME:   (574)289-5227 EMAIL:  [hidden email] WWW:    http://www.nd.edu/~rwilliam* *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 -margins- does have the post option. As start, you could do something this: webuse nhanes2f, clear logit diabetes i.black i.female age i.female#c.age, nolog margins female#black, at(age = (20(10)70)) post matrix b = r(b) matrix at = r(at) mata    b =st_matrix("b")    b = rowshape(b,6)    st_matrix("b", b) end clear svmat at svmat b line b* at5 It would be nice to extract the information in the r(b) matrix to relabel the legend, but I am not sure how. Scott On Mon, Sep 20, 2010 at 10:32 AM, Richard Williams <[hidden email]> wrote: > I am warming up to the new margins command.  However, unlike the older > adjust command, margins does not include the ability to generate new > variables. I am therefore finding it hard to create graphic displays of the > results. > > Consider the following: > > webuse nhanes2f, clear > logit diabetes i.black i.female age i.female#c.age, nolog > margins female#black, at(age = (20(10)70)) > > I would love to create a graphic where the Y axis was the probability of > diabetes, the X axis was age, and there were 4 separate lines, for black > males, black females, nonblack males, nonblack females. I imagine I could > generate variables myself (basically replicating what margins is doing) or > (ugh!) cut and paste results into an excel spreadsheet. But, is there some > nice, simple approach for doing this? The prgen command in Long & Freese's > spost9 suite of commands can do things like this, but it doesn't support > many of the features of margins, e.g. it doesn't work with factor variables, > and things like interaction terms are going to confuse it. > > ------------------------------------------- > Richard Williams, Notre Dame Dept of Sociology > OFFICE: (574)631-6668, (574)631-6463 > HOME:   (574)289-5227 > EMAIL:  [hidden email] > WWW:    http://www.nd.edu/~rwilliam> > * > *   For searches and help try: > *   http://www.stata.com/help.cgi?search> *   http://www.stata.com/support/statalist/faq> *   http://www.ats.ucla.edu/stat/stata/> * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 In reply to this post by Richard Williams-5 On Sep 20, 2010, at 10:32 AM, Richard Williams wrote: > I am warming up to the new margins command.  However, unlike the   > older adjust command, margins does not include the ability to   > generate new variables. I am therefore finding it hard to create   > graphic displays of the results. > > Consider the following: > > webuse nhanes2f, clear > logit diabetes i.black i.female age i.female#c.age, nolog > margins female#black, at(age = (20(10)70)) > > I would love to create a graphic where the Y axis was the   > probability of diabetes, the X axis was age, and there were 4   > separate lines, for black males, black females, nonblack males,   > nonblack females. I believe that the only option currently is to go manually through   e(b).  For example, if you add the -post- option to your -margins-   command above, you could do something like this:      mat A = e(b)'      svmat A      egen _age = fill(10 10 10 10 20 20 20 20)      egen _grp = fill(1 2 3 4 1 2 3 4)      separate A1, by(_grp)      line A11-A14 _age if !mi(A1) It wouldn't be difficult to write a little command to do this in a   more general way (indeed, someone may have done it already). -- Phil * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

RE: Graphic displays or results from margins

 <> Not sure whether I am being helpful, but Roger`s -ssc d parmest-is also available after -mergins, post-: ************* cap which parmest if _rc ssc inst parmest webuse nhanes2f, clear logit diabetes i.black i.female age i.female#c.age, nolog margins female#black, at(age = (20(10)70)) post parmest, saving(myfile, replace) u myfile, clear l ************* HTH Martin -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Phil Schumm Sent: Montag, 20. September 2010 17:31 To: [hidden email] Subject: Re: st: Graphic displays or results from margins On Sep 20, 2010, at 10:32 AM, Richard Williams wrote: > I am warming up to the new margins command.  However, unlike the   > older adjust command, margins does not include the ability to   > generate new variables. I am therefore finding it hard to create   > graphic displays of the results. > > Consider the following: > > webuse nhanes2f, clear > logit diabetes i.black i.female age i.female#c.age, nolog > margins female#black, at(age = (20(10)70)) > > I would love to create a graphic where the Y axis was the   > probability of diabetes, the X axis was age, and there were 4   > separate lines, for black males, black females, nonblack males,   > nonblack females. I believe that the only option currently is to go manually through   e(b).  For example, if you add the -post- option to your -margins-   command above, you could do something like this:      mat A = e(b)'      svmat A      egen _age = fill(10 10 10 10 20 20 20 20)      egen _grp = fill(1 2 3 4 1 2 3 4)      separate A1, by(_grp)      line A11-A14 _age if !mi(A1) It wouldn't be difficult to write a little command to do this in a   more general way (indeed, someone may have done it already). -- Phil * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/* *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 In reply to this post by Scott Merryman Scott Merryman: > > It would be nice to extract the information in the r(b) matrix to > relabel the legend, but I am not sure how. -matnames- (on SSC) seems to do what you want: TITLE      'MATNAMES': module to return matrix row and column names DESCRIPTION/AUTHOR(S)      matnames returns matrix row and column names as locals in r().      The macro extended functions that retrieve matrix row and column      names do not quote the names, so that if names contain spaces,      the number of tokens may not match the number of rows or columns      (that is, you may not be able to rename the matrix rows or      columns with its own names that you just retrieved). matnames      uses the Mata functions st_matrixrowstripe and st_matrixcolstripe      to circumvent this difficulty.      KW: matrix      KW: names      KW: row      KW: column      Requires: Stata version 9.2      Distribution-Date: 20090712      Author: Austin Nichols      Support: email [hidden email] INSTALLATION FILES                             (type net install matnames)      matnames.ado      matnames.hlp ----- Original Message ----- From: "Scott Merryman" <[hidden email]> To: <[hidden email]> Sent: Monday, September 20, 2010 5:24 PM Subject: Re: st: Graphic displays or results from margins -margins- does have the post option. As start, you could do something this: webuse nhanes2f, clear logit diabetes i.black i.female age i.female#c.age, nolog margins female#black, at(age = (20(10)70)) post matrix b = r(b) matrix at = r(at) mata   b =st_matrix("b")   b = rowshape(b,6)   st_matrix("b", b) end clear svmat at svmat b line b* at5 It would be nice to extract the information in the r(b) matrix to relabel the legend, but I am not sure how. Scott On Mon, Sep 20, 2010 at 10:32 AM, Richard Williams <[hidden email]> wrote: > I am warming up to the new margins command. However, unlike the older > adjust command, margins does not include the ability to generate new > variables. I am therefore finding it hard to create graphic displays of > the > results. > > Consider the following: > > webuse nhanes2f, clear > logit diabetes i.black i.female age i.female#c.age, nolog > margins female#black, at(age = (20(10)70)) > > I would love to create a graphic where the Y axis was the probability of > diabetes, the X axis was age, and there were 4 separate lines, for black > males, black females, nonblack males, nonblack females. I imagine I could > generate variables myself (basically replicating what margins is doing) or > (ugh!) cut and paste results into an excel spreadsheet. But, is there some > nice, simple approach for doing this? The prgen command in Long & Freese's > spost9 suite of commands can do things like this, but it doesn't support > many of the features of margins, e.g. it doesn't work with factor > variables, > and things like interaction terms are going to confuse it. > > ------------------------------------------- > Richard Williams, Notre Dame Dept of Sociology > OFFICE: (574)631-6668, (574)631-6463 > HOME: (574)289-5227 > EMAIL: [hidden email] > WWW: http://www.nd.edu/~rwilliam> > * > * For searches and help try: > * http://www.stata.com/help.cgi?search> * http://www.stata.com/support/statalist/faq> * http://www.ats.ucla.edu/stat/stata/> * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/  Am 20.09.2010 um 17:24 schrieb Scott Merryman: > -margins- does have the post option. > > As start, you could do something this: > > webuse nhanes2f, clear > logit diabetes i.black i.female age i.female#c.age, nolog > margins female#black, at(age = (20(10)70)) post > matrix b = r(b) > matrix at = r(at) > mata >   b =st_matrix("b") >   b = rowshape(b,6) >   st_matrix("b", b) > end > clear > svmat at > svmat b > line b* at5 > > It would be nice to extract the information in the r(b) matrix to > relabel the legend, but I am not sure how. > > Scott > > > On Mon, Sep 20, 2010 at 10:32 AM, Richard Williams > <[hidden email]> wrote: >> I am warming up to the new margins command.  However, unlike the older >> adjust command, margins does not include the ability to generate new >> variables. I am therefore finding it hard to create graphic displays of the >> results. >> >> Consider the following: >> >> webuse nhanes2f, clear >> logit diabetes i.black i.female age i.female#c.age, nolog >> margins female#black, at(age = (20(10)70)) >> >> I would love to create a graphic where the Y axis was the probability of >> diabetes, the X axis was age, and there were 4 separate lines, for black >> males, black females, nonblack males, nonblack females. I imagine I could >> generate variables myself (basically replicating what margins is doing) or >> (ugh!) cut and paste results into an excel spreadsheet. But, is there some >> nice, simple approach for doing this? The prgen command in Long & Freese's >> spost9 suite of commands can do things like this, but it doesn't support >> many of the features of margins, e.g. it doesn't work with factor variables, >> and things like interaction terms are going to confuse it. >> >> ------------------------------------------- >> Richard Williams, Notre Dame Dept of Sociology >> OFFICE: (574)631-6668, (574)631-6463 >> HOME:   (574)289-5227 >> EMAIL:  [hidden email] >> WWW:    http://www.nd.edu/~rwilliam>> >> * >> *   For searches and help try: >> *   http://www.stata.com/help.cgi?search>> *   http://www.stata.com/support/statalist/faq>> *   http://www.ats.ucla.edu/stat/stata/>> > > * > *   For searches and help try: > *   http://www.stata.com/help.cgi?search> *   http://www.stata.com/support/statalist/faq> *   http://www.ats.ucla.edu/stat/stata/------------------------------------------------------- "'kill -9' is a sure kill"         - UNIX man pages * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 In reply to this post by Scott Merryman Scott, thanks much.  It never would have occurred to me to use Mata, mostly because I don't know Mata. Perhaps still not as simple as I would like, but to be able to do it at all is great. As far as extracting the information in the r(b) matrix to relabel the legend, that might be good, but I suspect it might be better just to go into the graphics editor.  I am not overly impressed with how margins labels its own output, so it may just be that some manual intervention is needed to get a nice looking graphic. (But I am about as good with graphics as I am with Mata, so there may well be ways to make this easier.) Also thanks to everyone else who made suggestions.  I will check them out. If I come up with a better solution I will post it. I would like to see a built-in Stata command or a user-written command that easily generates such graphics -- it seems to me like the margins output often cries out for a nice graphical display. At 10:24 AM 9/20/2010, Scott Merryman wrote: >-margins- does have the post option. > >As start, you could do something this: > >webuse nhanes2f, clear >logit diabetes i.black i.female age i.female#c.age, nolog >margins female#black, at(age = (20(10)70)) post >matrix b = r(b) >matrix at = r(at) >mata >    b =st_matrix("b") >    b = rowshape(b,6) >    st_matrix("b", b) >end >clear >svmat at >svmat b >line b* at5 > >It would be nice to extract the information in the r(b) matrix to >relabel the legend, but I am not sure how. > >Scott > > >On Mon, Sep 20, 2010 at 10:32 AM, Richard Williams ><[hidden email]> wrote: > > I am warming up to the new margins command.  However, unlike the older > > adjust command, margins does not include the ability to generate new > > variables. I am therefore finding it hard to create graphic displays of the > > results. > > > > Consider the following: > > > > webuse nhanes2f, clear > > logit diabetes i.black i.female age i.female#c.age, nolog > > margins female#black, at(age = (20(10)70)) > > > > I would love to create a graphic where the Y axis was the probability of > > diabetes, the X axis was age, and there were 4 separate lines, for black > > males, black females, nonblack males, nonblack females. I imagine I could > > generate variables myself (basically replicating what margins is doing) or > > (ugh!) cut and paste results into an excel spreadsheet. But, is there some > > nice, simple approach for doing this? The prgen command in Long & Freese's > > spost9 suite of commands can do things like this, but it doesn't support > > many of the features of margins, e.g. it doesn't work with factor > variables, > > and things like interaction terms are going to confuse it. > > > > ------------------------------------------- > > Richard Williams, Notre Dame Dept of Sociology > > OFFICE: (574)631-6668, (574)631-6463 > > HOME:   (574)289-5227 > > EMAIL:  [hidden email] > > WWW:    http://www.nd.edu/~rwilliam> > > > * > > *   For searches and help try: > > *   http://www.stata.com/help.cgi?search> > *   http://www.stata.com/support/statalist/faq> > *   http://www.ats.ucla.edu/stat/stata/> > > >* >*   For searches and help try: >*   http://www.stata.com/help.cgi?search>*   http://www.stata.com/support/statalist/faq>*   http://www.ats.ucla.edu/stat/stata/------------------------------------------- Richard Williams, Notre Dame Dept of Sociology OFFICE: (574)631-6668, (574)631-6463 HOME:   (574)289-5227 EMAIL:  [hidden email] WWW:    http://www.nd.edu/~rwilliam* *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 On Mon, Sep 20, 2010 at 8:16 PM, Richard Williams <[hidden email]> wrote: > Also thanks to everyone else who made suggestions.  I will check them out. > If I come up with a better solution I will post it. I would like to see a > built-in Stata command or a user-written command that easily generates such > graphics -- it seems to me like the margins output often cries out for a > nice graphical display. > FWIW, the latest Stata News ( http://www.stata.com/news/newse.html ) discusses graphing marginal effects. Scott * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 In reply to this post by Richard Williams-5 Richard, I'm getting into the margins command as well.  Here is what I've been able to learn so far.  After producing the predicted probabilities using the margins command after the logistic regression, I use Roger Newsom's parmest command to save the results in a dataset and then use the 'eclplot' command to graph the predicted probabilities with the confidence intervals.  There are some matrices to create after the parmest command, but I don't find it too cumbersome.   Try this.  This will produce 4 panels in the figure.  If you feel they are too cramped together, try using if statements on the command. webuse nhanes2f, clear logit diabetes i.black i.female age i.female#c.age, nolog margins female#black, at(age = (20(10)70)) post parmest, format(estimate min95 max95) norestore matrix input age = (20 \ 20 \ 20 \ 20 \ 30 \ 30 \ 30 \ 30 \ 40 \ 40 \ 40 \ 40 \ 50 \ 50 \ 50 \ 50 \ 60 \ 60 \ 60 \ 60 \ 70 \ 70 \ 70 \ 70) matrix input female = (0 \ 0 \ 1 \ 1 \ 0 \ 0 \ 1 \ 1 \ 0 \ 0 \ 1 \ 1 \ 0 \ 0 \ 1 \ 1 \ 0 \ 0 \ 1 \ 1 \ 0 \ 0 \ 1 \ 1) matrix input black = (0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1 \ 0 \ 1) matrix vars = age, female, black matrix colnames vars = age female black svmat vars, names(matcol) label define female 0 "Male" 1 "Female" label values varsfemale female label var varsfemale "Gender" label define black 0 "Non-Black" 1 "Black" label values varsblack black label var varsblack "Race" eclplot estimate min95 max95 varsage, rplottype(rspike) estopts(msize(medium)) ciopts(msize(large)) ///         xtitle("") ///         by(varsfemale varsblack, style(compact) legend(off) title("Predicted Probability of Diabetes ({±}95% C.I.)")) ///         b2title("Age", color(black) size(medlarge)) ///         xlabel(20(10)70)  ///         ytitle("Diabetes Estimated Probability", color(black) size(medlarge)) eclplot estimate min95 max95 varsage if varsfemale == 0 & varsblack == 1, rplottype(rspike) estopts(msize(medium)) ciopts(msize(large)) ///         title("Predicted Probability of Diabetes ({±}95% C.I.)") ///         xtitle("") ///         b2title("Age", color(black) size(medlarge)) ///         xlabel(20(10)70)  ///         ytitle("Diabetes Estimated Probability", color(black) size(medlarge)) Tom Weichle Math Statistician Center for Management of Complex Chronic Care (CMC3) Hines VA Hospital, Bldg 1, C202 708-202-8387 ext. 24261 [hidden email] * *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/
Open this post in threaded view
|

Re: Graphic displays or results from margins

 In reply to this post by Scott Merryman At 09:53 PM 9/20/2010, Scott Merryman wrote: >FWIW, the latest Stata News ( http://www.stata.com/news/newse.html ) >discusses graphing marginal effects. > >Scott 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'                  }          } end 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.black 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:  [hidden email] WWW:    http://www.nd.edu/~rwilliam* *   For searches and help try: *   http://www.stata.com/help.cgi?search*   http://www.stata.com/support/statalist/faq*   http://www.ats.ucla.edu/stat/stata/