Announcement

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

  • Problem with ‘marginsplot’ when using margins command with multiple at() predictions

    I want to create a "marginsplot" using a continuous independent variable on the x-axis, with predictions for two groups represented by two different lines. However, the two groups have different min/max values on the continuous variable on the x-axis. So, I want to constrain the predictions within the min/max for each group. Using the auto.dta, I wrote syntax that produces the graph that I want.


    Code:
    // Using Mac O/S
    version 17.0
    sysuse auto.dta    
    
    foreach n in 0 1 {
    sum mpg if for==`n'
    scalar min`n' = r(min)
    scalar max`n' = r(max)
        }
    
    reg price c.mpg##c.mpg##c.mpg##i.for trunk
    
    margins, at(mpg=(`=min0' (1) `=max0') for=0) at(mpg=(`=min1' (1) `=max1') for=1)
    
    marginsplot, recast(line) recastci(rarea) ciopt(color(%35)) ///
    plot(, label("Domestic" "Foreign")) ///
    plot2opts(lwidth(thick) lpattern(dash)) legend(rows(1) pos(6))
    Click image for larger version

Name:	g1.png
Views:	1
Size:	247.0 KB
ID:	1637685


    Here is the output before the plot is produced:

    Variables that uniquely identify margins: mpg _atopt
    Multiple at() options specified:
    _atoption=1: mpg=(12 (1) 34) for=0
    _atoption=2: mpg=(14 (1) 41) for=1
    However, the analogous code for my actual analysis does not produce the same graph. Here is the code:

    Code:
    // Using Mac O/S
    version 17.0
    use els_mini.dta, clear
     
    foreach n in 0 1 2 3 {
    sum ses if race==`n'
    scalar min`n' = r(min)
    scalar max`n' = r(max)
                }
     
    reg math10 c.ses##c.ses##c.ses##i.race
     
    margins, at(ses=(`=min0' (.1) `=max0') race=0) at(ses=(`=min3' (.1) `=max3') race=3)
     
    marginsplot, noci
    Click image for larger version

Name:	g2.png
Views:	1
Size:	240.2 KB
ID:	1637684


    For some reason, the ‘marginsplot’ for the second example does not recognize that I have two groups to plot, which is why all of the predictions are connected together with one line (not two). In the output that Stata produces before presenting the plot, the “_atopt” is missing as a variable that uniquely identifies the margins.


    Variables that uniquely identify margins: ses
    Multiple at() options specified:
    _atoption=1: ses=(-2.11 (.1) 1.8) race=0
    _atoption=2: ses=(-1.77 (.1) 1.82) race=3
    Why is this happening, and how can I get the properly formatted plot via ‘marginsplot’?

    els_mini.dta

  • #2
    The trick seems to be that `min' and `max' should not have more decimals than the step length. Your step length is 0.1 but min and max have two decimals. The code below should work (or retain min and max but adjust the step length to 0.01).

    Code:
    // Using Mac O/S
    version 17.0
    use els_mini.dta, clear
     
    foreach n in 0 1 2 3 {
    sum ses if race==`n'
    scalar min`n' = round(r(min),0.1)
    scalar max`n' = round(r(max),0.1)
                }
     
    reg math10 c.ses##c.ses##c.ses##i.race
     
    margins, at(ses=(`=min0' (.1) `=max0') race=0) at(ses=(`=min3' (.1) `=max3') race=3)
     
    marginsplot, noci
    Last edited by Fei Wang; 22 Nov 2021, 20:12.

    Comment


    • #3
      Perfect! Thank you so much for your help!

      Comment

      Working...
      X