Announcement

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

  • Coefplot - multiple models with separate added text for each model.

    Hello Stata-listers,

    I am using coefplot with multiple models and 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 for the first plot, and "fourth", "fifth", and "sixth" on the y-axis of the second 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. I know that I can run two separate plots and combine them using grc1leg, however, the problem with that is that it will repeat the left y-axis labels, and if I remove them on the second plot, the aspect ratio of the two plots are different.

    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
    Click image for larger version

Name:	image_31485.png
Views:	1
Size:	18.5 KB
ID:	1765236

    Last edited by amandacpac; 07 Oct 2024, 13:21.

  • #2
    coefplot is from SSC, as you are asked to explain in FAQ Advice #12.

    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"  
    
    gen xpos=37
    gen mlab= "first" in 1
    replace mlab= "second" in 3
    replace mlab= "third" in 5
    replace mlab= "fourth" in 2
    replace mlab= "fifth" in 4
    replace mlab= "sixth" in 6
    
    #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") nooffset    
                 addplot(scatter @at xpos, ms(i) mlab(mlab) mlabcolor(black) mlabgap(3) mlabsize(vsmall)) ///
                 graphr(margin(r=10));
    #delimit cr
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.6 KB
ID:	1765304

    Last edited by Andrew Musau; 08 Oct 2024, 05:23.

    Comment

    Working...
    X