2

When trying to depict two coefficients from one regression on separate axes with Ben Jann's superb coefplot (ssc install coefplot) command, the coefficient to be shown on the 2nd axis is correctly displayed, but its confidence interval is depicted on the 1st scale.

Can anyone explain how I get the CI displayed on the same (2nd) axis as the coefficient it belongs to? I couldn't find any option to change this - and imagine it should be the default, if not the only, option to plot the CI around the point estimate it belongs to.

I use the latest coefplot version with Stata 16.

Here is a minimum example to illustrate the problem:

results plot

webuse union, clear
eststo results: reg idcode i.union grade
coefplot (results, keep(1.union)) (results, keep(grade) xaxis(2))

ChrisV
  • 23
  • 3

2 Answers2

3

In the line

coefplot (results, keep(1.union)) (results, keep(grade) xaxis(2))

you specify the option xaxis(2), but this is not a documented option of coefplot, although it is a valid option of twoway rspike which is called by coefplot. Apparently, if you use xaxis(2) something goes wrong with the communication between coefplot and rspike.

This works for me:

coefplot (results, keep(1.union)) (results, keep(grade) axis(2))
Wouter
  • 3,201
  • 6
  • 17
0

I'm trying to create something similar. Since this option is not built-in we need to write a program to tweak how coefplot works. I'm sharing the code from the user manual here: http://repec.sowi.unibe.ch/stata/coefplot/markers.html

capt program drop coefplot_mlbl
*! version 1.0.0  10jun2021  Ben Jann
program coefplot_mlbl, sclass
    _parse comma plots 0 : 0
    syntax [, MLabel(passthru) * ]
    if `"`mlabel'"'=="" local mlabel mlabel(string(@b, "%5.2f") + " (" + string(@ll, "%5.2f") + "; " + string(@ul, "%5.2f") + ")")
    preserve
    qui coefplot `plots', `options' `mlabel' generate replace nodraw
    sreturn clear
    tempvar touse
    qui gen byte `touse' = __at<.
    mata: st_global("s(mlbl)", ///
        invtokens((strofreal(st_data(.,"__at","`touse'")) :+ " " :+ ///
        "`" :+ `"""' :+ st_sdata(.,"__mlbl","`touse'") :+ `"""' :+ "'")'))
    sreturn local plots `"`plots'"'
    sreturn local options `"`options'"'
end

capt program drop coefplot_ymlbl
*! version 1.0.0  10jun2021  Ben Jann
program coefplot_ymlbl
    _parse comma plots 0 : 0
    syntax [, MLabel(str asis) * ]
    _parse comma mlspec mlopts : mlabel
    local mlopts = substr(`"`mlopts'"', 2, .) // remove leading comma
    if `"`mlspec'"'!="" local mlabel mlabel(`mlspec')
    else                local mlabel
    coefplot_mlbl `plots', `options' `mlabel'
    coefplot `plots',  ///
        yaxis(1 2) yscale(alt) yscale(axis(2) alt noline) ///
        ylabel(none, axis(2)) yti("", axis(2)) ///
        ymlabel(`s(mlbl)', axis(2) notick angle(0) `mlopts') `options'
end

coefplot_ymlbl D F, drop(_cons) xline(0)

However, the above program does not allow for the option 'bylabel'. I get a stata error saying "bylabel not allowed". I wanted to ask if there is a way to edit this code and include the bylabel option which is used to label subplots?