Announcement

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

  • Coefplot - multiple models with different dependent variables

    All, I am running multiple regressions with the same independent variable and 29 different dependent variable, and then running these same models multiple times for different time points. I would like to get a plot of the regression coefficients where the 29 dependent variables are along the Y axis, and multiple plots, side-by-side for the different time points. I found a post where one of the comments was getting at something similar, but I can't figure out how to get it the way I need it.
    So, using this code:

    Code:
    sysuse auto, clear  
    reg trunk foreign gear_ratio if rep78 == 5  // Outcome = Trunk and sub-group = 4
    estimates store trunk_s5
    reg trunk foreign gear_ratio if rep78 == 4    // Outcome = Trunk and sub-group = 5
    estimates store trunk_s4  
    
    reg mpg foreign gear_ratio if rep78 ==5  // Outcome = MPG and sub-group = 4
    estimates store mpg_s5
    reg mpg foreign gear_ratio if rep78 ==4  // Outcome = MPG and sub-group = 5
    estimates store mpg_s4  
    
    reg turn foreign gear_ratio if rep78 ==5 // Outcome = Turn and sub-group = 4
    estimates store turn_s5
    reg turn foreign gear_ratio if rep78 ==4  // Outcome = Turn and sub-group = 5
    estimates store turn_s4  
    
    coefplot (trunk_s4, keep(foreign) \ mpg_s4 \ turn_s4, keep(gear_ratio) \) ///      
    || (trunk_s5, keep(foreign) \ mpg_s5 \ turn_s5, keep(gear_ratio) \) ///      
    || , drop(_cons) aseq swapnames eqrename(*_s4 = "" *_s5 = "") ///          
    order(trunk turn mpg) bylabels("Subgroup 4" "Supgroup 5") ///            
    eqlabels(, asheading)
    I get the following plot:
    Click image for larger version

Name:	graph.png
Views:	2
Size:	294.4 KB
ID:	1679321

    What I would REALLY like to have happen is for the two trunk variables (trunk_s4 and trunk_s5) to be on the same line (i.e., side-by-side), AND to be able to label that as "Trunk". And the same goes for MPG, Turn, etc. Is there a way to do that?? I have tried everything I can think of, and have been at this for several days now.
    Last edited by amandacpac; 25 Aug 2022, 17:52.

  • #2
    Code:
    label var trunk "Trunk"
    label var mpg "MPG"
    label var turn "Turn"
    #delimit ;
        coefplot (trunk_s4 \ mpg_s4 \ turn_s4, keep(foreign gear_ratio) )  
                || (trunk_s5 \ mpg_s5 \ turn_s5, keep(foreign  gear_ratio) ) ,
                eqrename(*_s4 = "" *_s5 = "")
                swapnames aseq eqlabels("Origin" "Gear Ratio")
                bylabels("Subgroup 4" "Subgroup 5")
                scheme(s2color)
                    ;
    #delimit cr
    which produces:
    Click image for larger version

Name:	Screenshot 2022-08-26 at 8.37.32 PM.png
Views:	1
Size:	134.4 KB
ID:	1679421
    Last edited by Hemanshu Kumar; 26 Aug 2022, 09:11.

    Comment


    • #3
      So, that plot looks like exactly what I need, but when I run your code, I don't get that. Here is the code I am using:

      Code:
      sysuse auto, clear  
      reg trunk foreign gear_ratio if rep78 == 5  // Outcome = Trunk and sub-group = 4
      estimates store trunk_s5
      reg trunk foreign gear_ratio if rep78 == 4    // Outcome = Trunk and sub-group = 5
      estimates store trunk_s4  
      
      reg mpg foreign gear_ratio if rep78 ==5  // Outcome = MPG and sub-group = 4
      estimates store mpg_s5
      reg mpg foreign gear_ratio if rep78 ==4  // Outcome = MPG and sub-group = 5
      estimates store mpg_s4  
      
      reg turn foreign gear_ratio if rep78 ==5 // Outcome = Turn and sub-group = 4
      estimates store turn_s5
      reg turn foreign gear_ratio if rep78 ==4  // Outcome = Turn and sub-group = 5
      estimates store turn_s4  
      
      label var trunk "Trunk"
      label var mpg "MPG"
      label var turn "Turn"
      
      #delimit ;
          coefplot (trunk_s4 \ mpg_s4 \ turn_s4, keep(foreign gear_ratio) )  
                  || (trunk_s5 \ mpg_s5 \ turn_s5, keep(foreign  gear_ratio) ) ,
                  eqrename(*_s4 = "" *_s5 = "")
                  swapnames aseq eqlabels("Origin" "Gear Ratio")
                  bylabels("Subgroup 4" "Subgroup 5")
                  scheme(s2color)
                      ;
      #delimit cr
      And here is the plot I get:
      Click image for larger version

Name:	graph1.png
Views:	1
Size:	334.9 KB
ID:	1679426

      Last edited by amandacpac; 26 Aug 2022, 09:41.

      Comment


      • #4
        That's peculiar, I just copy-pasted your code into my Stata, and got what I showed you, not what you get. Perhaps this is a version issue?

        If you do

        Code:
        which coefplot
        do you get version 1.8.5?

        If not, you might want to

        Code:
        ssc install coefplot, replace
        or even do (to update all your ado files at one shot)

        Code:
        ado update, update
        Also, for what it's worth, I'm on Stata 17/MP on a Mac.

        Comment


        • #5
          That fixed it. Though, I had literally just updated Stata like 2 days ago. But, for some reason, I still had version 1.7.4. Thank you so much for your help!

          Comment


          • #6
            Great!

            Updating Stata by itself does not update community-contributed commands you may have installed, including coefplot. For that, one needs to run the ado update command. Hence the problem.

            Comment


            • #7
              I have a similar bit of code that I am using for a different dataset, but I am trying to get sample sizes listed on the right y-axis (each model has different sample sizes due to missing values and different time periods). I have code to pull the Ns, but can't figure out how to get it displayed. I am using the ymlab option with various local macros. But, for ease with this example, we can use simple text. So, for example, How can I modify the following code to display the strings: first, second, third, etc on the right y-axis, but differently for each plot. I hope that makes sense. I have attached a manually modified image to kind of show what I'm trying to do with the code below.


              Code:
              sysuse auto, clear  
              reg trunk foreign gear_ratio if rep78 == 5  // Outcome = Trunk and sub-group = 4
              estimates store trunk_s5
              reg trunk foreign gear_ratio if rep78 == 4    // Outcome = Trunk and sub-group = 5
              estimates store trunk_s4  
              
              reg mpg foreign gear_ratio if rep78 ==5  // Outcome = MPG and sub-group = 4
              estimates store mpg_s5
              reg mpg foreign gear_ratio if rep78 ==4  // Outcome = MPG and sub-group = 5
              estimates store mpg_s4  
              
              reg turn foreign gear_ratio if rep78 ==5 // Outcome = Turn and sub-group = 4
              estimates store turn_s5
              reg turn foreign gear_ratio if rep78 ==4  // Outcome = Turn and sub-group = 5
              estimates store turn_s4  
              
              label var trunk "Trunk"
              label var mpg "MPG"
              label var turn "Turn"
              
              #delimit ;
                  coefplot (trunk_s4 \ mpg_s4 \ turn_s4, keep(foreign gear_ratio)), ymlab(1 "First" 2 "Second" 3 "Third", axis(2)) ||  
                          (trunk_s5 \ mpg_s5 \ turn_s5, keep(foreign  gear_ratio)), ymlab(1 "Fourth" 2 "Fifth" 3 "Sixth", axis(2)) || ,
                          eqrename(*_s4 = "" *_s5 = "")
                          swapnames aseq eqlabels("Origin" "Gear Ratio")
                          bylabels("Subgroup 4" "Subgroup 5") nooffset
                  ;
              #delimit cr
              image_28411.png
              Last edited by amandacpac; 21 Jun 2023, 17:53.

              Comment

              Working...
              X