Announcement

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

  • coefplot: how to fix the scaling for different coefficients?!

    Hi

    I am not getting a clear figure with coefplot. I think the problem is that the coefficients and confidence intervals of the variables are on different scales.

    This is the code:



    Code:
    eststo clear
        
    
    foreach s in lnsp {
    
        foreach v in  g mp rra ira {
    
            eststo `s'_`v', title(`s'_`v'): qui ivreg2 `s'_`v' inv if  event==1 ,robust bw(auto) small                
        }
            
    
        coefplot `s'_g, bylabel(gt) || `s'_mp, bylabel(mo) || ///
                 `s'_rra, bylabel(rn) || ///
                 `s'_ira  , bylabel(ir)  ///
                 drop(_cons) bycoefs vertical rescale(100)    ///
                  yli(0, lp(solid) lc(gray))   /// levels(99 95 90)
                 title(event) name(`s', replace)       aspectratio(0, placement(top))                
    }
    It gives me this figure:


    Screen Shot 2022-12-08 at 2.27.56 pm.png

    How can I fix the scaling here to have a better figure?

    Data example is below

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(date lnsp_g lnsp_mp lnsp_rra lnsp_ira event)
    12421 .4692802532253717 .14198951396707926 .10629503389465267 -.9952776840946314 0
    12422 .08914587641991911 .10291307764690316 -.3318984640303393 .4418097664912821 0
    12423 .03529091803674955 .04109996125757931 .22157609911148768 -.17088358851430918 0
    12424 -.40765895173964317 .18539173387332397 -.2448045393640168 .31390102477364756 0
    12425 -.7795970204235063 .5142133254770442 -.2705502213848616 1.060767574317742 0
    12428 .11401619151197817 .26025975361029197 .46797481801336005 .2597474371801294 0
    12429 -.19364316376082868 -.09974189028947933 .0030346989873786256 .057563911711255855 0
    12430 .20776474761765673 -.04576813578812988 -.7547986524557153 .5274223146005728 0
    12431 .5462456669565521 -.15046456920850204 .2533801960820954 -1.0102612299187559 0
    12432 .21740829937344763 -.06713020348587406 .4007314312737229 -.08638537692535131 0
    12435 -.0943130556477616 -.05023424709232671 -.1411726291595432 -.08000458637084051 0
    12436 -.19579254714728211 .2547532420942886 .004020570122604816 .07535600427065425 0
    12437 .011814342583973847 -.0384665223628744 -.004766881421617519 -.017749674613249056 0
    12438 -.3952954577303407 .5130418929766485 -.039353952545760684 .03845383502026323 0
    12439 .19865771351208128 -.17136365875475335 -.08457641784507519 -.12305880387026846 0
    12442 -.184929500582121 -.15172187573763907 -.06425307037898165 -.12207302491143501 0
    12443 -.0758445834010617 -.10248966717791674 .21184217753263201 -.28128758179382984 0
    12444 .29312350646976615 -.027009516082973306 -.16455620223599166 .3226864723739702 0
    12445 .3058560568832407 -.07280011286059818 -.25670945269801493 .8170486473570566 0
    12446 -.11894466216942146 .11300240242352899 -.19101730208306744 .49295619750712305 0
    12449 .32812852104849366 -.20352284793473885 .3377431878653461 .10539099889690995 0
    12450 -.04325627853102798 -.640007449782459 .5350572696211113 -.23958322438683055 0
    12451 .08746131982394889 -.04122253568984391 .17343929315802242 .21865536475324454 0
    12452 .4434366135967496 -.5976933544288165 -.10846345884968134 -.04674236310928137 1
    12453 -.2922161232353835 -1.5241609583330238 .33673559010865706 -.8456219972021835 1
    12456 1.1090050486084282 .602494216605681 -.8097711552451052 -.448933729029525 1
    12457 -.15259408785790818 -.06398275352762539 .47247001615794915 -.4183412285672805 1
    12458 -.05906193891569611 .03847128328568061 -.026657655559586324 .36717136553900076 1
    12459 -.016098457679547984 -.4588374177327772 -.3315882108207253 -.06077085160416651 0
    12460 .13306199982793476 .12984045540265043 -.24874545326215625 .19019634225854176 0
    12463 -.2866019641137818 .10877933212218949 .5059049606559034 -.3281604010069472 0
    12464 .23539192752828786 .04470265843669807 -.09761899666576124 .31313018671568393 0
    end
    format %td date
    [/CODE]
    Last edited by Lisa Wilson; 07 Dec 2022, 20:36.

  • #2
    Your data example doesn't include the variable inv, so I am unable to run your code.

    Comment


    • #3
      Thanks. Here is a larger dataset that includes variable inv

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(date lnsp_g lnsp_mp lnsp_rra lnsp_ira event inv)
      12421    .4692802532253717   .14198951396707926   .10629503389465267   -.9952776840946314 0                       .
      12422   .08914587641991911   .10291307764690316   -.3318984640303393    .4418097664912821 0                       .
      12423   .03529091803674955   .04109996125757931   .22157609911148768  -.17088358851430918 0    .0001037300948808387
      12424  -.40765895173964317   .18539173387332397   -.2448045393640168   .31390102477364756 0   .00039471382360643337
      12425   -.7795970204235063    .5142133254770442   -.2705502213848616    1.060767574317742 0   -.0003026014198168511
      12428   .11401619151197817   .26025975361029197   .46797481801336005    .2597474371801294 0   -.0007423219750262642
      12429  -.19364316376082868  -.09974189028947933 .0030346989873786256  .057563911711255855 0    .0005207410454479737
      12430   .20776474761765673  -.04576813578812988   -.7547986524557153    .5274223146005728 0   .00019629184686866953
      12431    .5462456669565521  -.15046456920850204    .2533801960820954  -1.0102612299187559 0   .00035226007444310036
      12432   .21740829937344763  -.06713020348587406    .4007314312737229  -.08638537692535131 0    .0004888344012117783
      12435   -.0943130556477616  -.05023424709232671   -.1411726291595432  -.08000458637084051 0   .00020794923934610055
      12436  -.19579254714728211    .2547532420942886  .004020570122604816   .07535600427065425 0   -.0012942947083397468
      12437  .011814342583973847   -.0384665223628744 -.004766881421617519 -.017749674613249056 0     .000167796653747826
      12438   -.3952954577303407    .5130418929766485 -.039353952545760684   .03845383502026323 0  .000017297604942863378
      12439   .19865771351208128  -.17136365875475335  -.08457641784507519  -.12305880387026846 0    .0003565571417315326
      12442    -.184929500582121  -.15172187573763907  -.06425307037898165  -.12207302491143501 0   -.0005298995317184904
      12443   -.0758445834010617  -.10248966717791674   .21184217753263201  -.28128758179382984 0  -.00023676718411862037
      12444   .29312350646976615 -.027009516082973306  -.16455620223599166    .3226864723739702 0   -.0002536925576144698
      12445    .3058560568832407  -.07280011286059818  -.25670945269801493    .8170486473570566 0   .00010465381345717918
      12446  -.11894466216942146   .11300240242352899  -.19101730208306744   .49295619750712305 0  -.00006915705026261012
      12449   .32812852104849366  -.20352284793473885    .3377431878653461   .10539099889690995 0  -.00016776162403295855
      12450  -.04325627853102798    -.640007449782459    .5350572696211113  -.23958322438683055 0  -.00028226394117332747
      12451   .08746131982394889  -.04122253568984391   .17343929315802242   .21865536475324454 0    .0001400690480754924
      12452    .4434366135967496   -.5976933544288165  -.10846345884968134  -.04674236310928137 1  -.00026531191296280064
      12453   -.2922161232353835  -1.5241609583330238   .33673559010865706   -.8456219972021835 1     .000498699215280966
      12456   1.1090050486084282     .602494216605681   -.8097711552451052    -.448933729029525 1   .00008855198424112721
      12457  -.15259408785790818  -.06398275352762539   .47247001615794915   -.4183412285672805 1    -.011268856161307626
      12458  -.05906193891569611   .03847128328568061 -.026657655559586324   .36717136553900076 1    .0001265887660513266
      12459 -.016098457679547984   -.4588374177327772   -.3315882108207253  -.06077085160416651 0    -.002472796488412068
      12460   .13306199982793476   .12984045540265043  -.24874545326215625   .19019634225854176 0   -.0005486098544475761
      12463   -.2866019641137818   .10877933212218949    .5059049606559034   -.3281604010069472 0   -.0020734165851365475
      12464   .23539192752828786   .04470265843669807  -.09761899666576124   .31313018671568393 0   -.0010484851440303972
      12465  -.03073777296792546  -.00465283270456851   .08846867429443872  -.06256256111016079 0    .0005815619327903278
      12466    .5029341166894419  -.03862185625244052   .01339682096022532  -1.0261379795099774 0    .0006689745885743988
      12467  -.11932887493105149   -.1885650010588087   .40763215953040177   -.6712644220453114 0    -.006299996631268913
      12470                    0                    0                    0                    0 0  .000038762830913151976
      12471   .07776487527442022  -.26360630422050235   .19583792735356464    .7595633311172101 0  .000014154625097594052
      12472   .29968817034057826   -.7970723613659239    .3501562831425593 -.052974236206832614 0    -.000284166411652988
      12473   -.5946479361644026  -.46745392727038393    .5314446217103095   -.8362673659836393 0    -.001512915215064927
      12474   .18796483599834524   -.1756006452857939  -.04764897965636372    .3759865751720728 0    .0010296576298001042
      12477   -.5037018382052079  -.06644634848759523    .2913661295943868    .4616487563912859 0  .000033615991017381916
      12478   .40555379589603735   -.9827135296605114   .47920867243676746   -.5304869388766935 0  -.00007777547905085708
      12479    .1833508059578124   .05986719459956902   -.1482624025127155 -.036965618798893374 0    .0007523181266708543
      12480   .08933180139311503   .06867519769667751   .09536117142116218   -.6426555158290395 0   .00046030868966851537
      12481   .44052551908049864   -.9087019491324959    .0649748135515047    .7381841403550506 0  -.00016450976117771878
      12484  -.08502053962312095   .11424894279906539  -.07451411023691357   .48137157393176366 0  .000052121468152683785
      12485    .6057030275996218   -.2200596317386463  -.30139106208667776  -.36804614164038973 0  .000011934951373737468
      12486  -.12471454467865017  -.24614183236063525   .34679511476535474      .30116800128331 0   -.0012123803963590039
      12487    -.541525390023962  -.07657696751534004    .9257854477523324  -1.0332056893184358 0   .00040107548269711674
      12488    .6960189851725427    .4650207097336505    -.771378721674143   .07395022949964414 0    .0001656555164710466
      12491    .2250523761375378  -.24052502271182163    .2981137092765742  -.06675717108064916 0    .0001627977758392543
      12492    .1083878100415383 -.053474810700432585    -.379090234129265    .2355923840574441 0    .0017194182002690619
      12493 -.023440256007285498   .14824999874095823   -.3517364106707763     .699444313518903 0    .0007884412939661364
      12494   .14541018587812315   .18545081541048702   .09237895154431364   -.1822646239528023 0     -.23059915577788692
      12495   .43273096700831104   -.3434195944436276    .3877650437762554   -.5468967832374361 0     .000764871822498966
      12498    .4651280885553777   -.3134647481262469   -.3318113740781232  -.31544823022089474 1    -.004283617021478302
      12499   -.7478319443481514     .183764315008613   -.1980837350868523    .7983710245858902 1   .00036972463910815213
      12500  .010309545058286407   .15157444467459413  -.07467517492243658  -.17867270168573413 1    .0006978327948990435
      12501     .355268247865165   -.1865670012247378   .40933556160138096  -1.5199000598914196 1   -.0029557695847485434
      12502  -.03509320354160028  -.05176015002228951   -.3462072245791603   -.3905017526153456 1    .0003179581680870628
      12505    .4098021116665507    -.158110284545252   -.3516886737331113 -.050449730323952835 0      .05378878812445539
      12506   -.3143846701893702   -.8087767345334171    .1489624762214543   -.7841751312477294 0    .0027485727306269083
      12507   .15019141178002382    .5149907758299825   -.6434497739447655  -1.5593481982246367 0    -.002146782194386256
      12508  -.22160221133086155   -.5195725351143423   .30295869821245436   .45084662935719955 0     -.06563280713485505
      12509                    0                    0                    0                    0 0                       .
      12512    .9206932141391182  -2.5096576848178813   1.8516418771755152  -1.8237680966331111 0     .008256040954670892
      12513  -.24992517332605746    .6005766095829088   -.4354784042792037     2.19482352981398 0    .0006435656642090375
      12514  -.22265449225883738   .23676673817957905  -.11817697476776345 -.017428667169271606 0   -.0000240578731332216
      12515  -.10611324476221085  .031604804396177855    .1385809499008488    .5190651892891855 0   .00014123590722874265
      12516     .637306216950573  -.19656447705186864   -.5054460334012916   -.8140372307413806 0   .00014199452416871882
      12519   .14248430581263394   .26989618932177706  -.04392744098819357   .08824125666296467 0   .00013957966422859896
      12520   -.7041770698130934  -.02844908449399952  -.00518149370552301   .28670814950580414 0  -.00008030167631392637
      12521  -.07994709508394626  -.39033730740980843    .6255364016608329   -.5153248812734601 0  .000011078939423618028
      12522   -.1062914601353886  -.44360608148058844    .4478626576402469   .08580366272468076 0    .0001830072684389613
      12523   .40045591124569246  -.30149104341017363    -.644661948871784   .43489209322849565 0   .00010967633231909759
      12526    .8750269804545153  -1.1762307849606053    .2529751148229895    -.836642520112723 0    .0001035345477667827
      12527   .14159105063659655  -.22128707010509174   -.6456732213724337     .724431016279075 0    .0001303057001049105
      12528   -.5718257148162407  -.17438005178001958   .20373033810177787   .37759044215827586 0   .00009411662839086711
      12529    .1881967489847599    .3644562240335425   -.6425579099491117    1.549325331596135 0  -.00032866660708646576
      12530   .22425221607099488   -.2374442557770351 .0005816569195697952  -.23438196657137045 0    .0000529381320131179
      12533    .5401133297234969   -.0719786197298724   -.5402550009992275   1.1272541861295293 0  -.00011829353097620985
      12534   -.4880136636727229   -.0468931208073915   .37186098578779436  .005617298378021829 0   .00011195330332092085
      12535                    0                    0                    0                    0 0  2.6174817791865535e-06
      12536    .9523915230869424   -.5030555792371878   .12309039177896963  -1.2641314748758195 0   .00001460231841089133
      12537   -.7189116592423487  -.07051130345266898   1.1495473231957654  .012162013278211425 0  -.00010064172849282634
      12540     .528066364384915  -.22681367030726562   .07670191434760731   .07896109252402272 0   .00006314486635967267
      12541   .45809668929307035   -.3535213658094296  -.24203147789902069   .09889459714087234 0 -.000035714817780397826
      12542 -.017944819476272467  -.27797703817859976  -.05310904555383367   .06545151024093787 0   -.0012082584055322521
      12543  -.40186686267883026   -.3157484007688396   .17138456336937047   .42718029481309827 0   .00013060485745160872
      12544    .5551156965455097  -1.6131471465275258   1.2168307454035574  -1.0074455168301304 0  -.00021068616657187272
      12547    .3379184055527329   -1.000387170010665   .04051563637015576   -.5857145497675971 0   .00014796912388080616
      12548   .07696010220833727    .5638231213573022  -1.1347063944648106   1.2977285998873969 0     -.01796917533374649
      12549   -.7645162826626638   -.5359677198035264   1.2414292926372514   -1.018907603068036 0 -.000021756746488652105
      12550  -.33364453898697777   .16372676994704172   .23869719698747538     .394590600032406 0     .003803226844388918
      12551  -1.2535513511192091   .39724743034303067    .8650645779393482   .06510697650893693 0     -.19810729173092326
      12554    .1121161870534062  -.02991900853991325   -.4703094442329512   .43255069329114537 1   -.0019904839009410205
      12555  -.07579530055529449    .5568062190192382   -1.026868774621839   1.6394169016772517 1   -.0004689931588869173
      12556   .21648836779592862    .8717372015670666  -.04962118844739116  -.10586985651902658 1  -.00014082420960416405
      12557   -.1642737419575664    .9742895758375845  -.33175581185738245   .08455311003244503 1    .0003615204495665145
      12558   .44377672711663685  -.44272149594275056  -.04033481302643516  -.30404241330396964 1    -.004619191223525621
      end
      format %td date

      Comment


      • #4
        Here is what I get using your data. The only thing I had to do to make the code run, was to
        Code:
        tsset date
        At least in this graph, I cannot see what the issue is.

        Click image for larger version

Name:	Screenshot 2022-12-08 at 1.11.45 PM.png
Views:	1
Size:	744.4 KB
ID:	1692517

        Comment


        • #5
          You do not see the same figure as this is only a subset of my data. However, the issue is apparent when I use the full sample.

          Comment


          • #6
            Hi
            I am also open to any other codes that can provide a better figure to visualize the results here ( but with the "vertical" option).
            Thanks

            Comment


            • #7
              Hi, if the estimates have different orders of magnitude, then plotting them in the same graph might not be the best idea. If you want to do so, then you will need to apply some kind of standardization. One idea would be to apply standardization to the variables upfront (e.g. divide the variables by their standard deviations or some other values that make sense) and then estimate the models. However, you can also use option transform() in coefplot to rescale estimates while plotting them. Here is an example that standardizes the estimates by dividing them by their standard errors. Whether this makes sense in your application I don't know, but it solves the scaling problem (all confidence intervals will have same length with this approach).

              Code:
              tsset date
              foreach v in  g mp rra ira {
                  eststo lnsp_`v': qui ivreg2 lnsp_`v' inv, robust bw(auto) small
              }
              coefplot (lnsp_g lnsp_mp lnsp_rra lnsp_ira) ///
                  , aseq swap eqrename(lnsp_g = gt lnsp_* = "") ///
                    drop(_cons) yline(0) vertical ///
                    transform(* = @/@se) ///
                    ytitle(Coefficient divided by standard error)

              Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.7 KB
ID:	1693692

              Maybe a better option is to create a separate subgraph for each estimate, with individual scales. Example:

              Code:
              coefplot lnsp_g || lnsp_mp || lnsp_rra || lnsp_ira || ///
                  , drop(_cons) bylabels(gt mo rn ir) vertical ///
                    byopts(row(1) yrescale) yline(0)
              Click image for larger version

Name:	Graph2.png
Views:	1
Size:	27.0 KB
ID:	1693693

              An issue with this graph is that you probably want to align the scales such that zero is at the same place. This is not automatically possible, I believe. However, here is a manual solution using command addplot (type ssc install addplot), that can be applied after creating the graph to adjust the scales.

              Code:
              addplot 1:, ylabel(-10(2)10) xlabel(none)
              addplot 2:, ylabel(-3(1)3) yscale(range(-3.2 3.2)) xlabel(none)
              addplot 3:, ylabel(-4(1)4) yscale(range(-4.2 4.2)) xlabel(none)
              addplot 4:, ylabel(-2(.5)2) xlabel(none)
              Click image for larger version

Name:	Graph3.png
Views:	1
Size:	37.1 KB
ID:	1693694

              ben

              Comment

              Working...
              X