Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • formatting graphs using marginsplots

    Hi everyone,

    I am creating a combined marginsplot (graphs attached as .png) for the data below. The code (below) estimates a logit model for the binary variable defect with gender (0, 1) and treatment (0, 1, 2) as independent variables. I then calculate the marginal effects with respect to gender for treatment and use marginsplot to generate a graph for gender = 0 and another graph for gender = 1 and then finally combine the two graphs next to each other. Even though I set the yvalue range (0 0.8), the two y-axes are not formatted the same - they have different vertical tick marks. Also for the x-axis label, rather than 1.treat and 2.treat I would like "Asymmetric" and "Private (respectively). I'd also like to delete the chart titles. Any help would be much appreciated!

    Code:

    quietly logit defect i.gender i.treat
    margins if gender==0, dydx(treat)
    marginsplot, ytitle("Marginal Effect on Pr(Defect)") xtitle("Treatment") yscale(range(0 0.8)) name(a, replace)
    margins if gender==1, dydx(treat)
    marginsplot, ytitle("Marginal Effect on Pr(Defect)") xtitle("Treatment") yscale(range(0 0.8)) nodraw name(b, replace)
    graph combine a b

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(treat defect gender age baseline asymm private) str6 catgender str10 cattreat
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 0 1 18 0 1 0 "Female" "Asymmetric"
    1 1 1 20 0 1 0 "Female" "Asymmetric"
    1 1 0 21 0 1 0 "Male"   "Asymmetric"
    1 1 0 18 0 1 0 "Male"   "Asymmetric"
    1 1 0 18 0 1 0 "Male"   "Asymmetric"
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 0 1 19 0 1 0 "Female" "Asymmetric"
    1 1 0 18 0 1 0 "Male"   "Asymmetric"
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 1 1 18 0 1 0 "Female" "Asymmetric"
    1 0 0 21 0 1 0 "Male"   "Asymmetric"
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 0 0 24 0 1 0 "Male"   "Asymmetric"
    1 1 0 21 0 1 0 "Male"   "Asymmetric"
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 1 0 21 0 1 0 "Male"   "Asymmetric"
    0 1 1 21 1 0 0 "Female" "Common"    
    0 1 1 18 1 0 0 "Female" "Common"    
    0 0 1 18 1 0 0 "Female" "Common"    
    0 1 1 20 1 0 0 "Female" "Common"    
    0 1 1 20 1 0 0 "Female" "Common"    
    0 1 0 19 1 0 0 "Male"   "Common"    
    0 0 0 22 1 0 0 "Male"   "Common"    
    0 0 1 20 1 0 0 "Female" "Common"    
    0 0 1 19 1 0 0 "Female" "Common"    
    0 0 0 21 1 0 0 "Male"   "Common"    
    0 1 0 18 1 0 0 "Male"   "Common"    
    0 0 0 18 1 0 0 "Male"   "Common"    
    0 1 1 21 1 0 0 "Female" "Common"    
    0 0 1 18 1 0 0 "Female" "Common"    
    0 0 0 21 1 0 0 "Male"   "Common"    
    0 1 0 21 1 0 0 "Male"   "Common"    
    0 1 1 22 1 0 0 "Female" "Common"    
    0 1 1 20 1 0 0 "Female" "Common"    
    0 1 1 21 1 0 0 "Female" "Common"    
    0 0 0 21 1 0 0 "Male"   "Common"    
    2 1 1 23 0 0 1 "Female" "Private"   
    2 1 0 24 0 0 1 "Male"   "Private"   
    2 1 1 22 0 0 1 "Female" "Private"   
    2 1 1 21 0 0 1 "Female" "Private"   
    2 1 1 20 0 0 1 "Female" "Private"   
    2 1 0 22 0 0 1 "Male"   "Private"   
    2 1 1 22 0 0 1 "Female" "Private"   
    2 1 1 21 0 0 1 "Female" "Private"   
    2 1 1 21 0 0 1 "Female" "Private"   
    2 1 0 23 0 0 1 "Male"   "Private"   
    2 1 1 23 0 0 1 "Female" "Private"   
    2 1 0 24 0 0 1 "Male"   "Private"   
    2 1 1 23 0 0 1 "Female" "Private"   
    2 1 1 21 0 0 1 "Female" "Private"   
    2 1 1 20 0 0 1 "Female" "Private"   
    2 0 0 22 0 0 1 "Male"   "Private"   
    2 1 1 22 0 0 1 "Female" "Private"   
    2 1 1 21 0 0 1 "Female" "Private"   
    2 1 1 21 0 0 1 "Female" "Private"   
    2 1 0 23 0 0 1 "Male"   "Private"   
    1 1 1 18 0 1 0 "Female" "Asymmetric"
    1 1 0 22 0 1 0 "Male"   "Asymmetric"
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 1 1 19 0 1 0 "Female" "Asymmetric"
    1 0 0 23 0 1 0 "Male"   "Asymmetric"
    1 1 0 23 0 1 0 "Male"   "Asymmetric"
    1 0 1 18 0 1 0 "Female" "Asymmetric"
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 1 1 22 0 1 0 "Female" "Asymmetric"
    0 0 1 21 1 0 0 "Female" "Common"    
    0 1 0 21 1 0 0 "Male"   "Common"    
    0 1 1  . 1 0 0 "Female" "Common"    
    0 1 1 19 1 0 0 "Female" "Common"    
    0 1 0 21 1 0 0 "Male"   "Common"    
    0 1 1 21 1 0 0 "Female" "Common"    
    0 1 1 21 1 0 0 "Female" "Common"    
    0 1 1 21 1 0 0 "Female" "Common"    
    0 1 1 21 1 0 0 "Female" "Common"    
    0 0 0 19 1 0 0 "Male"   "Common"    
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 0 1 19 0 1 0 "Female" "Asymmetric"
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 1 1 20 0 1 0 "Female" "Asymmetric"
    1 1 0 19 0 1 0 "Male"   "Asymmetric"
    1 0 1 20 0 1 0 "Female" "Asymmetric"
    1 1 1 20 0 1 0 "Female" "Asymmetric"
    1 1 1 21 0 1 0 "Female" "Asymmetric"
    1 0 0 18 0 1 0 "Male"   "Asymmetric"
    1 0 0 19 0 1 0 "Male"   "Asymmetric"
    1 1 0 24 0 1 0 "Male"   "Asymmetric"
    0 0 0 22 1 0 0 "Male"   "Common"    
    0 0 1 20 1 0 0 "Female" "Common"    
    0 0 1 20 1 0 0 "Female" "Common"    
    0 0 0 18 1 0 0 "Male"   "Common"    
    0 1 1 20 1 0 0 "Female" "Common"    
    0 0 0 19 1 0 0 "Male"   "Common"    
    0 0 1 21 1 0 0 "Female" "Common"    
    0 0 1 19 1 0 0 "Female" "Common"    
    0 1 1 22 1 0 0 "Female" "Common"    
    0 1 0 23 1 0 0 "Male"   "Common"    
    end
    Attached Files

  • #2
    To deal with 1.treat and 2.treat, you need to define a value label for the treatment value and apply it to that variable before you do the -logit- command.

    To get the two vertical axes scaled alike, add the -ycommon- option to the -graph combine- command.

    To remove the titles from the two graphs, specify the -title("")- option in both -marginsplot- commands.

    Comment


    • #3
      Thank you so much, Clyde!! I have now labeled the treatments with code:

      label define treatl 0 "Common" 1 "Asymmetric" 2 "Private"
      label values treat treatl

      And can see in the logit results that "treat" has those names, but the graphs still have "1.treat" and "2.treat" on the x-axis.

      Any suggestions on what I am missing?

      The others worked perfectly. Thank you!

      Comment


      • #4
        Ugh! That's a problem. The workaround is cumbersome. There is an undocumented -saving()- option for the -margins- command. So save your -margins- results in data sets. Then -use- those data sets and use -graph twoway- to create the plots yourself. You will have to, in the course of doing that, supply the correct labeling information for treat yourself by changing the label on the variable called _deriv.

        By the way, the way you're using -margins-, though legal, is uncommon and probably is not getting you what you need. At the least, it's inefficient because you're having to run two different -margins- commands. -margins, over(gender) dydx(treat) will give you the same results with just a single command. And you can also then just run one -marginsplot- (although it will still have the same problem with regard to labeling of treat.)

        But also, when you do it this way, the average marginal effects you get are not adjusted for any differences between males and females on other variables in your model. In the code you show, there aren't any other variables in the model, so it won't make a difference. But if your real model contains other variables, there will be a difference, and in most situations, adjusted average marginal effects are what is needed. For adjusted average marginal effects, the code would be -margins gender, dydx(treat)-.


        Last edited by Clyde Schechter; 26 Aug 2017, 11:52.

        Comment


        • #5
          Although I agree with Clyde about the advantages of two-way graph, if you just want to change the label you can use xlabel options.
          Code:
          marginsplot, xlabel(2  "Asymmetric" 3  "Private")

          Comment


          • #6
            Hi Clyde,

            Wow this is super helpful!!! Thank you so much! I am new to using Stata for margins and expect to use it a lot so learning best practice is important. Plus the chart looks much better. Thanks again!

            Best,
            Mary

            Comment

            Working...
            X