Announcement

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

  • Coefplot with multiple models

    For my research I'm estimating the same regression with three different dependent variables, which are different variations of GDP per capita. In order to graphically compare my coefficients I'm using coefplot to plot them with a 95% confidence intervals above each other. So far I'm using the following code to store each estimation result as a seperate model to then tell coefplot to plot all three models.

    Code:
    reg logY_1 x1
    estimates store results_1
    reg logY_2 x1
    estimates store results_2
    reg logY_3 x1
    estimates store results_3
    
    coefplot results_1 results_2 results_3, drop(_cons)
    This results in a graph with a single "x1" on my y-axis and three different lines in my graph, which represent my three different models as the legend shows.

    In order for a neater graphical presentation I would like Stata to show my three model names (results_1, results_2 and results_3) on the y-axis (instead of the single "x1") at the height of the corresponding line in the graph, just as coefplot does for estimates with multiple independent variables.
    So far I've been tweaking around with the legend options, but it doesn't trun out nicely.
    I think this problem may be solved by storing all three results in a single matrix, but I have trouble how to construct such a matrix and how to tell coefplot to get it right.

  • #2
    if I understand you correctly, use of the "swapnames" option on your -coefplot- command should give you what you want;

    Comment


    • #3
      Unfortunately not, I have tried the option "swapnames" before and it only results in changing the "x1" label on the y-axis to "_".

      Comment


      • #4
        Try:
        Code:
        sysuse auto
        reg mpg foreign
        estimates store results_1
        reg trunk foreign
        estimates store results_2
        reg turn  foreign
        estimates store results_3
        coefplot (results_1 \ results_2 \ results_3), drop(_cons) aseq swapnames
        Or with custom labels:
        Code:
        coefplot (results_1, aseq(whatever I want) ///
                \ results_2, aseq(something else) ///
                \ results_3, aseq(no more ideas)), drop(_cons) swapnames
        Or:
        Code:
        coefplot (results_1 \ results_2 \ results_3), drop(_cons) aseq swapnames ///
            coeflabels(results_1 = "whatever I want" ///
                       results_2 = "something else"  ///
                       results_3 = "no more ideas")
        ben

        Comment


        • #5
          Hi,

          I am working with several models : 3 Outcome variables over 2 sub-groups and have 2 covariates. I want to plot to the sub-groups side by side and models should be separated by covariates, and within the covariates the I want to chose only a few outcomes e.g.
          the three outcomes would be mpg, trunk, and turn; subgroups would be rep78=4 and rep78 = 5; and covariates are: foreign and gear_ratio.

          For foreign (covariate), results from trunk and mpg regressions should be plotted for both subgroups, whereas for gear_ratio (covariate) results from turn and mpg should be plotted for both sub-groups. See ideal graph depicted below:

          Click image for larger version

Name:	Screen Shot 2017-05-09 at 12.03.54 PM.png
Views:	1
Size:	10.6 KB
ID:	1391793

          I modified the approach given above as follows but (1) it places foreign's coefficients on different lines for different subgroups whereas they should be on same horizontal line i.e. coefficients for foreign from trunk_s4 and trunk_s5 should appear on same horizontal line (2) I am unable to select different models across the two covariates.

          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) (mpg_s4) || (turn_s5) (mpg_s5)  , drop(_cons) aseq swapnames
          It produces the following graph which doesnt solve my issues listed above
          Click image for larger version

Name:	Incorrect.png
Views:	1
Size:	84.2 KB
ID:	1391794

          I was able to solve problem 2 of using different models for each covariate and plot them across different sub-groups, using matrix command.
          Code:
          forvalues x = 4/5 {
          foreach var in trunk mpg turn {
              est restore `var'_s`x'
              parmest, saving(temp_`var'_s`x'.dta, replace) eform
          }
          }
          use temp_trunk_s4.dta, clear
          gen str outcome = "trunk"
          gen byte subgroup = 4
          append using temp_trunk_s5
          replace outcome = "trunk"
          replace subgroup = 5 if subgroup == .
          forvalues x = 4/5 {
          foreach var in mpg turn {
              append using temp_`var'_s`x'.dta
              replace subgroup = `x' if subgroup ==.
              replace outcome = "`var'" if outcome ==""
              
          }
          }
          sencode parm, gen(covno)
          sencode outcome, gen(outno)
          mkmat estimate min95 max95 if covno==1 & subgroup==4 & inlist(outno,1,2), matrix(sg4top)
          mkmat estimate min95 max95 if covno==2 & subgroup==4 & inlist(outno,3,2), matrix(sg4bot)
          mkmat estimate min95 max95 if covno==1 & subgroup==5 & inlist(outno,1,2), matrix(sg5top)
          mkmat estimate min95 max95 if covno==1 & subgroup==5 & inlist(outno,3,2), matrix(sg5bot)
          mat sg4 = sg4top\sg4bot
          mat sg5 = sg5top\sg5bot
          matrix rowname sg4 = "Trunk" "MPG" "Turn" "MPG2"
          matrix rowname sg5 = "Trunk" "MPG" "Turn" "MPG2"
          
          coefplot   matrix(sg4[,1]), ci((sg4[,2] sg4[,3])) bylabel("Rep78=4") ///
                  || matrix(sg5[,1]), ci((sg5[,2] sg5[,3])) bylabel("Rep78=5") ///
                  mlabel format(%9.2f) mlabposition(12) mlabgap(*2) mlabsize(vsmall) ///
                  coeflabels(,labsize(small) wrap(20)) ///
                  msymbol(smsquare) ciopts(recast(rcap)) scheme(s1mono)
          But it produces the following graph: I am fine with this but it is unable to add sub-titles for Foreign or Gear-Ratio using this approach. Would appreciate if anyone can point how to add sub-titles for this matrix approach or modify the simpler approach using coefficients directly.
          Click image for larger version

Name:	Matrix.png
Views:	1
Size:	65.3 KB
ID:	1391795

          Thank you
          Last edited by Parth Aks; 09 May 2017, 10:18.

          Comment


          • #6
            Dear Ben,
            I have a related problem. is it possible to use the groups() or headings() option with swapnames?

            Specifically, say that I want to run the main specification (results 1, 2, and 3) and then re-run then same regression for different sub-groups:

            Code:
            sysuse auto
            *** Baseline
            reg mpg foreign
            estimates store results_1
            reg trunk foreign
            estimates store results_2
            reg turn foreign
            estimates store results_3
            
            *** Group 4
            reg mpg foreign if rep78 == 4
            estimates store results_1_g4
            reg trunk foreign  if rep78 == 4
            estimates store results_2_g4
            reg turn foreign  if rep78 == 4
            estimates store results_3_g4
            
            *** Group 5
            reg mpg foreign if rep78 == 5
            estimates store results_1_g5
            reg trunk foreign  if rep78 == 5
            estimates store results_2_g5
            reg turn foreign  if rep78 == 5
            estimates store results_3_g5
            The following code produces a nice graph, but without any groups names:
            Code:
            coefplot ///
                (results_1, aseq(whatever I want) \ results_2, aseq(something else) \ results_3, aseq(no more ideas)) ///
                (results_1_g4, aseq(whatever I want) \ results_2_g4, aseq(something else) \ results_3_g4, aseq(no more ideas)) ///
                (results_1_g5, aseq(whatever I want) \ results_2_g5, aseq(something else) \ results_3_g5, aseq(no more ideas)) ///
            , drop(_cons) swapnames ///
            groups(result_1 result_2 result_3 = "Baseline"  ///
                        result_1_g4 result_2_g4 result_3_g4 = "Group 4" ///
                        result_1_g5 result_2_g5 result_3_g5 = "Group 5" ///
                        )
            Is there a way of solving this? thanks
            Pietro

            Comment


            • #7
              Dear Pietro (or anyone else who may know),
              Did you ever figure this out?
              Thanks,
              Devorah

              Comment


              • #8
                With respect to #5, I think the following should do:
                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)

                Comment


                • #9
                  With respect to #6, I am not sure what the desired graph is, but maybe the following helps:
                  Code:
                  sysuse auto
                  *** Baseline
                  reg mpg foreign
                  estimates store results_1
                  reg trunk foreign
                  estimates store results_2
                  reg turn foreign
                  estimates store results_3
                  
                  *** Group 4
                  reg mpg foreign if rep78 == 4
                  estimates store results_1_g4
                  reg trunk foreign  if rep78 == 4
                  estimates store results_2_g4
                  reg turn foreign  if rep78 == 4
                  estimates store results_3_g4
                  
                  *** Group 5
                  reg mpg foreign if rep78 == 5
                  estimates store results_1_g5
                  reg trunk foreign  if rep78 == 5
                  estimates store results_2_g5
                  reg turn foreign  if rep78 == 5
                  estimates store results_3_g5
                  
                  coefplot (results_? results_?_g4 results_?_g5), drop(_cons) aseq swapnames ///
                      groups(results_? = "Baseline" results_?_g4 = "Group 4" results_?_g5 = "Group 5") ///
                      coeflabels(*1* = mpg *2* = trunk *3* = turn)

                  Comment


                  • #10
                    Ben, I am trying to do something very similar to what you are doing in post #8, though I have 29 different dependent variables (and 1 independent variable), and I am running all 29 models twice (over two different time points). However, when I run the example code you provided, I get the following plot:
                    Click image for larger version

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

                    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 rename that to "Trunk". And the same goes for MPG, Turn, etc. Any chance there is a way to do that??

                    Oh, and I would only need the "Foreign" part of this plot, as I only have the 1 independent variable.

                    Thanks!
                    Last edited by amandacpac; 24 Aug 2022, 16:17.

                    Comment


                    • #11
                      Hi all!

                      I have a few queries regarding the use of the Coefplot command and hope to receive assistance from this forum. My objective is to create a coefficient plot for a single variable within a set of regression models. The command I have used is as follows:

                      HTML Code:
                      coefplot (r16, label(Korean Style Restaurants)) (r12, label(Chinese Style Restaurants)) (r10, label(Japanese Style Restaurants)) (r8, label(Western Style Restaurants)) (r3, label(Other Eastern and Western Style Restaurants (r2, label(Cafeterias)) (r13, label(Catering Food Services)) (r11, label(Confectioners Shops)) (r15, label(Pizza, Hamburger, Sandwich and Similar Food Services)) (r14, label(Chicken Shops)) (r6, label(Noodle Houses)) (r1, label(Other Lunch Counters n.e.c.)) (r9, label(General Amusement Drinking Places)) (r5, label(Dancing Amusement Drinking Places)) (r4, label(Other Drinking Places)) (r7, label(Non-Alcoholic Beverages Places)) , keep(logkorean)  xline(0) mlabel(cond(@pval<.001, "<0.001" + "***",cond(@pval<.01, string(@pval,"%9.3f") + "**", cond(@pval<.05, string(@pval,"%9.3f") + "*",string(@pval,"%9.3f"))))) scheme(s1color)
                      However, the resulting graph is displayed in a such way:
                      Click image for larger version

Name:	2023-11-09 171817.png
Views:	1
Size:	46.9 KB
ID:	1733263


                      Is there a way to align the legend labels on the y-axis to correspond with each respective model?
                      Additionally, is it possible to group these labels based on their specific characteristics, like categorizing them under 'restaurant' and 'drinking places' and perhaps enclosing each group within large parentheses? Many thanks for your assistance!
                      Last edited by Jane Quan; 09 Nov 2023, 01:31.

                      Comment


                      • #12
                        Originally posted by Jane Quan View Post
                        Hi all!

                        I have a few queries regarding the use of the Coefplot command and hope to receive assistance from this forum. My objective is to create a coefficient plot for a single variable within a set of regression models. The command I have used is as follows:

                        HTML Code:
                        coefplot (r16, label(Korean Style Restaurants)) (r12, label(Chinese Style Restaurants)) (r10, label(Japanese Style Restaurants)) (r8, label(Western Style Restaurants)) (r3, label(Other Eastern and Western Style Restaurants (r2, label(Cafeterias)) (r13, label(Catering Food Services)) (r11, label(Confectioners Shops)) (r15, label(Pizza, Hamburger, Sandwich and Similar Food Services)) (r14, label(Chicken Shops)) (r6, label(Noodle Houses)) (r1, label(Other Lunch Counters n.e.c.)) (r9, label(General Amusement Drinking Places)) (r5, label(Dancing Amusement Drinking Places)) (r4, label(Other Drinking Places)) (r7, label(Non-Alcoholic Beverages Places)) , keep(logkorean) xline(0) mlabel(cond(@pval<.001, "<0.001" + "***",cond(@pval<.01, string(@pval,"%9.3f") + "**", cond(@pval<.05, string(@pval,"%9.3f") + "*",string(@pval,"%9.3f"))))) scheme(s1color)
                        However, the resulting graph is displayed in a such way: [ATTACH=CONFIG]n1733263[/ATTACH]

                        Is there a way to align the legend labels on the y-axis to correspond with each respective model?
                        Additionally, is it possible to group these labels based on their specific characteristics, like categorizing them under 'restaurant' and 'drinking places' and perhaps enclosing each group within large parentheses? Many thanks for your assistance!
                        I think I have solved the problem by using "ylabel" & "aseq swapnames" commands!

                        Comment

                        Working...
                        X