Announcement

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

  • coefplot: Arranging multiple models

    Hi

    I'd like to arrange regression coefficients from multiple models with -coefplot- (available from SSC) in a way that I am convinced should be possible but I can't figure out how. Here's an example (all code in one go is enclosed at the bottom for convenience):

    Code:
    *ssc install coefplot  // Install coefplot from SSC if necessary
    
    sysuse auto, clear // Open data
    
    generate headroomlarge = (headroom > 3) // Generate two groups ("large" and "small") of no substantive meaning
    I am interested in one coefficient (price, see below), and I have two model specifications (called 1 and 2) and two samples (called large and small), so in total it's four coefficients that I want to plot. These are the models:

    Code:
    regress mpg price if headroomlarge
    eststo large1
    
    regress mpg price trunk weight length if headroomlarge
    eststo large2
    
    regress mpg price if !headroomlarge
    eststo small1
    
    regress mpg price trunk weight length if !headroomlarge
    eststo small2
    I can plot the four coefficients without further options like this, with the legend on the right-hand side doing all the lifting, which is not optimal in my book:

    Code:
    coefplot large* small*, keep(price)
    A.png





    I can stratify the plot by large and small, which is better, because the legend is decluttered by only showing the model specification (that is once you relabel it properly, sorry):

    Code:
    coefplot large* || small*, keep(price)
    B.png





    I like this version better, but it's not optimal: The y-axis is sitting basically idle while the information displayed in the y-dimension has to be deciphered using a legend? Not ideal. Is there a way to get the information from the legend onto the y-axis?

    Many thanks for you consideration
    KS

    Code:
    // Data
    *ssc install coefplot
    
    sysuse auto, clear
    
    gen headroomlarge = (headroom > 3)
    
    // Models
    regress mpg price if headroomlarge
    eststo large1
    
    regress mpg price trunk weight length if headroomlarge
    eststo large2
    
    regress mpg price if !headroomlarge
    eststo small1
    
    regress mpg price trunk weight length if !headroomlarge
    eststo small2
    
    
    // Figures
    // A
    coefplot large* small*, keep(price)
    
    // B
    coefplot large* || small*, keep(price)
    Last edited by Klaus Steitzel; 07 Feb 2021, 10:23.

  • #2
    Thanks for the reproducible example. As the axis is continuous, you just have to find the offset.

    Code:
    coefplot large*|| small*, keep(price) ylab(0.83 "My lab 1" 1.17 ///
    "My lab 2") ytitle("Price") nokey scheme(s1mono)
    Res.:


    Click image for larger version

Name:	Graph.png
Views:	1
Size:	12.3 KB
ID:	1593322







    Last edited by Andrew Musau; 07 Feb 2021, 11:33.

    Comment


    • #3
      Many thanks, great idea!

      Klaus

      Comment


      • #4
        Hi Andrew and Klaus, I am trying to create the same plot but have two small questions regarding your post:

        - In my case I have 5 variables instead of 2, how do I know the points in the y axis to put the labels? (for two it is 0.83 and 1.17 but what should I put for 5?)
        - How do I change the titles of the two boxes "large1" and "small1"?

        Thank you very much!



        Comment


        • #5
          - How do I change the titles of the two boxes "large1" and "small1"?
          You need the -bylabel()- option

          - In my case I have 5 variables instead of 2, how do I know the points in the y axis to put the labels? (for two it is 0.83 and 1.17 but what should I put for 5?)
          Do a manual check on 2 consecutive points and then work out the rest. -yline()- can help you here.

          Code:
          sysuse auto, clear
          forval i=1/5{
              eststo small`i': regress mpg price if rep78==`i'
              eststo large`i': regress mpg price if rep78!=`i'
          }
          
          coefplot large*, bylabel("Whatever 1")|| small*, bylabel("Whatever 2") keep(price) ///
          ylab(0.665 "My lab 1" 0.835 "My lab 2" 1 "My lab 3" 1.165 "My lab 4" 1.335 "My lab 5") ///
          ytitle("Price") yline(1.335, lcolor(blue)) nokey scheme(s1mono)
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	20.4 KB
ID:	1619389

          Comment


          • #6
            Thank you very much Andrew! However I am afraid I do not quite get how to find the points to put the labels on the y axis. I have 7 variables and have been trying numbers but it seems like one of the labels is always at the very top. How do you find the initial two points from which you then calculate the rest?

            Thanks again,

            Comment


            • #7
              I just figured out what you meant and solved the issue, thank you very much!

              Comment


              • #8
                I am doing something very similar, but I actually have 29 models, not 2 or 5. There has to be a better way to automate this, no?

                Comment


                • #9
                  See this thread where you have the label names in place of p-values: https://www.statalist.org/forums/for...-on-right-axis

                  Comment

                  Working...
                  X