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

                  Working...
                  X