I want to estimate separate regressions and then create a "marginsplot" (using the twoway command) with the groups held at the grand means for the sample.
Here is my code using auto.dta:
sysuse auto.dta, clear
foreach var in trunk turn weight {
sum `var'
scalar `var'1 = r(mean)
}
foreach n in 0 1 {
reg price mpg trunk turn weight if for==`n'
margins, at(mpg=(10(5)30) trunk=`=trunk1' turn=`=turn1' weight=`=weight1') ///
saving(marg`n'.dta, replace)
}
foreach n in 0 1 {
use marg`n'.dta, replace
gen for = `n'
save, replace
}
use marg0.dta, replace
append using marg1.dta
twoway ///
(scatter _margin _at1 if for==0) (scatter _margin _at1 if for==1) ///
(line _margin _at1 if for==0) (line _margin _at1 if for==1) ///
(rcap _ci_ub _ci_lb _at1 if for==0) (rcap _ci_ub _ci_lb _at1 if for==1), ///
legend(order(1 3 2 4) label(1 " ") label(3 "Foreign") label(2 " ") ///
label(4 "Domestic") pos(6) col(2))
Here is the graph:

My question is: how do I get the marker symbol and the line for each group to appear on top of each other in the legend (as they would in a plot produced by the 'marginsplot' command)?
Here is my code using auto.dta:
sysuse auto.dta, clear
foreach var in trunk turn weight {
sum `var'
scalar `var'1 = r(mean)
}
foreach n in 0 1 {
reg price mpg trunk turn weight if for==`n'
margins, at(mpg=(10(5)30) trunk=`=trunk1' turn=`=turn1' weight=`=weight1') ///
saving(marg`n'.dta, replace)
}
foreach n in 0 1 {
use marg`n'.dta, replace
gen for = `n'
save, replace
}
use marg0.dta, replace
append using marg1.dta
twoway ///
(scatter _margin _at1 if for==0) (scatter _margin _at1 if for==1) ///
(line _margin _at1 if for==0) (line _margin _at1 if for==1) ///
(rcap _ci_ub _ci_lb _at1 if for==0) (rcap _ci_ub _ci_lb _at1 if for==1), ///
legend(order(1 3 2 4) label(1 " ") label(3 "Foreign") label(2 " ") ///
label(4 "Domestic") pos(6) col(2))
Here is the graph:
My question is: how do I get the marker symbol and the line for each group to appear on top of each other in the legend (as they would in a plot produced by the 'marginsplot' command)?
Comment