Announcement

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

  • Stratified coefplot with multiple legends

    Hi Statalisters,


    I'm attempting to display some regression results via the coefplot command. My analyses are stratified by several categorical variables. I wish to display all of these different stratified plots in the same graph. I have provided an simplified example below using auto data:


    sysuse auto
    eststo: reg mpg trunk length turn if foreign==0
    eststo: reg mpg trunk length turn if foreign==1
    eststo: reg mpg trunk length turn if rep78==3
    eststo: reg mpg trunk length turn if rep78==4
    eststo: reg mpg trunk length turn if rep78==5
    coefplot est1 est2 || est3 est4 est5, drop(_cons) ///
    legend(label(2 "Domestic")label(4 "Foreign")label(6 "rep78=3")label(8 "rep78=4")label(10 "rep78=5") ///
    colfirst hole(3)colgap(30)) norecycle

    First, I am wondering if there is a way to create two different legends, one for each plot in the graph.

    Second, I am attempting to depict whether coefs. from different stratified levels are significantly different.
    E.g.:
    test [est4_mean]length=[est5_mean]length

    The only way I can think of portraying significant differences is by manually drawing the demarcations in graph editor as I have done for est4 length and est5 length in the attached graph.

    Any advice would be appreciated.

    -Nick Otis
    Attached Files

  • #2
    I've also attached .jpeg of the graph in case the .gph is not working.
    Last edited by Nicholas Otis; 21 Oct 2014, 12:46.

    Comment


    • #3
      If you want separate legends, you have to produce separate graphs and then use graph combine, as in the following example.
      Code:
      sysuse auto
      eststo: reg mpg trunk length turn if foreign==0
      eststo: reg mpg trunk length turn if foreign==1
      eststo: reg mpg trunk length turn if rep78==3
      eststo: reg mpg trunk length turn if rep78==4
      eststo: reg mpg trunk length turn if rep78==5
      coefplot est1 est2, drop(_cons) legend(rows(1)) name(a, replace)
      coefplot est3 est4 est5, drop(_cons) legend(rows(1)) yscale(off) name(b, replace)
      graph combine a b
      Youl could also add titles and so on to imitate the look of a by-graph, but it might take some time to figure out what exactly you have to specify. To adjust the size of the plots in the combined graph, the -fxsize()- option (see -help graph combine-) could be used, in theory. But I just noticed that -coefplot- does not allow -fxsize()-, unfortunately. I took a note to fix that in a future update.

      For your second question, maybe adding lines or similar using the -addplot()- option can be a solution. But this is quite tedious. Example:
      Code:
      coefplot est3 est4 est5, drop(_cons) ///
          addplot(line @at @b if round(@at)==2, lc(red))
      
      coefplot est3 est4 est5, drop(_cons) ///
          addplot((line @at @b if round(@at)==2, lc(red)) ///
                  (line @at @b if round(@at)==1 & @plot>1, lc(red)))
      ben

      Comment


      • #4
        Found a way to produce individual legends for the subgraphs using the -addplot- command (type -ssc install addplot-):
        Code:
        sysuse auto
        eststo: reg mpg trunk length turn if foreign==0
        eststo: reg mpg trunk length turn if foreign==1
        eststo: reg mpg trunk length turn if rep78==3
        eststo: reg mpg trunk length turn if rep78==4
        eststo: reg mpg trunk length turn if rep78==5
        coefplot est1 est2 || est3 est4 est5, drop(_cons) norecycle byopts(legend(off))
        addplot 1: , legend(order(2 "Domestic" 4 "Foreign") on) norescaling
        addplot 2: , legend(order(6 "rep78=3" 8 "rep78=4" 10 "rep78=5") on) norescaling
        This produces the following graph:
        Click image for larger version

Name:	myfile.png
Views:	1
Size:	26.1 KB
ID:	538452
        Furthermore, the newest coefplot update now supports fysize() and fxsize() (type -adoupdate coefplot-):
        Code:
        coefplot est1 est2, drop(_cons) legend(rows(1)) name(a, replace)
        coefplot est3 est4 est5, drop(_cons) legend(rows(1)) yscale(off) name(b, replace) fxsize(45)
        graph combine a b
        Click image for larger version

Name:	myfile2.png
Views:	1
Size:	20.3 KB
ID:	538453
        Ben
        Attached Files

        Comment

        Working...
        X