In Stata
, I would like to run a regression and visually display each variable's coefficients and their confidence intervals relative to zero, as the code and figure shown below:
sysuse auto
regress price mpg weight length foreign gear_ratio headroom rep78
margins, dydx(*)
marginsplot, horizontal recast(scatter) xline(0, lcolor(red)) xscale(range()) yscale(reverse)
On the y-axis, I would like to display the variable label (on the right) instead of variable name. What kind of options can one use to make that configuration?
storage display value
variable name type format label variable label
-----------------------------------------------------------------------------------------------------------------------
price int %8.0gc Price
mpg int %8.0g Mileage (mpg)
weight int %8.0gc Weight (lbs.)
length int %8.0g Length (in.)
foreign byte %8.0g origin Car type
gear_ratio float %6.2f Gear Ratio
headroom float %6.1f Headroom (in.)
rep78 int %8.0g Repair Record 1978
I realize that this can be a basic question but any thoughts are appreciated!
To my knowledge, there is no direct way of instructing marginsplot
to show the labels of a variable instead of its name. However, as @Skimwhistle has pointed out, you can use ylabel
to manually get the desired output.
You can of course generalize this concept further by writing a small r-class
program:
program define foo, rclass
syntax varlist
local i = 0
foreach var of local varlist {
local ++i
local varlabel : variable label `var'
local varlabels `varlabels' `i' `" "`varlabel'" "'
}
return local xvarlabels `varlabels'
end
Using this little program, which here we call foo
, you can automatically obtain all variable labels in a list
and then feed them to marginsplot
:
clear
sysuse auto
local xvars mpg weight length foreign gear_ratio headroom rep78
foo `xvars'
local xvarlabels `r(xvarlabels)'
regress price `xvars'
margins, dydx(*)
marginsplot, horizontal allxlabels recast(scatter) xline(0, lcolor(red)) ///
xscale(range()) yscale(reverse) ylabel(`xvarlabels')
And voila, the labels are now all on the produced graph.
Hope this helps.