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))
Array

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
Array

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’?

Array