Announcement

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

  • Coefficient plot for a Between-Within model, xthybrid

    Dear Statalist,

    I need you help with creating a coefficient plot after using the command xthybrid (Perales/Schunck) . My goal is to show within- and between coefficients so that they can be compared. I find that it would be best to have them in the same row. If I put them below each other the figure becomes very long on the vertical axis as my final model has many more variables than the example below.

    I tried to "trick" Stata/SE 18 into doing so by running the same model twice and give it different names, but was not succesfull (see below).

    Code:
    eststo within: xthybrid inconsistency3c_d1 agegroup1_1 agegroup1_2 agegroup1_3 agegroup1_4 haskids if female==1 , vce(robust) clusterid(id) family(gaussian) link(identity) full  
    eststo between: xthybrid inconsistency3c_d1 agegroup1_1 agegroup1_2 agegroup1_3 agegroup1_4 haskids if female==1 , vce(robust) clusterid(id) family(gaussian) link(identity) full  
    
    coefplot (within, drop(B__* _cons)) ||    ///
             (between, drop(W__* _cons)), xline(0) scheme(s2mono) graphregion(lwidth(none) fcolor(white))
    Here is the coef plot I get
    [ATTACH=CONFIG]n1734184[/ATTACH]

    The problem is that the within- and between coefficients have different names, which is why they cant be in the same row.

    I am sure there could be other approaches. If you have any idea on how I can accomplish what I need I would very much appreaciate your suggestions.

    Jasmin
    Attached Files

  • #2
    Adding a reproducible example is always your best bet to getting helpful replies. Also note that both coefplot and xthybrid are from SSC, as you are asked to explain in FAQ Advice #12.

    Comment


    • #3
      Thanks Andrew for pointing this out.
      Here I am trying to use a reproducible example using Stata 18. I am running a hybrid or between-within model for longitudinal data using the user-written command xthybrid from SSC. After that I try to create a plot of the regression results using the command coefplot from SSC.
      My goal is to show within- and between coefficients so that they can be compared. I find that it would be best to have them in the same row. If I put them below each other the figure becomes very long on the vertical axis as my real model has about 8 categorial variables .

      Code:
      webuse nlswork, clear
      
      tab age,m
      gen agegroup1=.
      replace agegroup1=1 if age>=20 & age<=24
      replace agegroup1=2 if age>=25 & age<=29
      replace agegroup1=3 if age>=30 & age<=34
      replace agegroup1=4 if age>=35 & age<=39
      tab agegroup1, gen(agegroup1_)
      
      eststo within: xthybrid ln_wage /*agegroup1_1*/ agegroup1_2 agegroup1_3 agegroup1_4 msp  , vce(robust) clusterid(id) family(gaussian) link(identity) full  
      eststo between: xthybrid ln_wage /*agegroup1_1*/ agegroup1_2 agegroup1_3 agegroup1_4 msp , vce(robust) clusterid(id) family(gaussian) link(identity) full  
      
      coefplot (within, drop(B__* _cons)) ||    ///
               (between, drop(W__* _cons)), xline(0) scheme(s2mono) graphregion(lwidth(none) fcolor(white)) ///
               saving(test.png, replace)
      I am attaching the resulting graph that I got. Click image for larger version

Name:	testgraph.png
Views:	1
Size:	22.3 KB
ID:	1734412


      The problem is that the within- and between coefficients have different names, which is why they cant be in the same row.

      In the meanwhile I also tried another approach, but I dont want the variable names/labels to appear twice.

      Code:
      coefplot within,  xline(0) ///
          title(Within-coefficients)    ///
          drop(B__* _cons) saving(WX_within, replace)    
          
      coefplot within,  xline(0) ///
          title(Between-coefficients)    ///
          drop(W__* _cons) saving(WX_between, replace)    
      
      graph combine WX_within.gph  WX_between.gph, scheme(s2mono) rows(1) ycommon xcommon

      Click image for larger version

Name:	testgraph2.png
Views:	1
Size:	28.6 KB
ID:	1734413
      I am sure there could be other approaches. If you have any idea on how I can accomplish what I need I would very much appreaciate your suggestions.

      Jasmin

      Comment


      • #4
        Thanks for the reproducible example. The following should do it. Note that erepost is from SSC.

        Code:
        webuse nlswork, clear
        
        tab age,m
        gen agegroup1=.
        replace agegroup1=1 if age>=20 & age<=24
        replace agegroup1=2 if age>=25 & age<=29
        replace agegroup1=3 if age>=30 & age<=34
        replace agegroup1=4 if age>=35 & age<=39
        tab agegroup1, gen(agegroup1_)
        
        eststo within: xthybrid ln_wage /*agegroup1_1*/ agegroup1_2 agegroup1_3 agegroup1_4 msp  , vce(robust) clusterid(id) family(gaussian) link(identity) full 
        eststo between: xthybrid ln_wage /*agegroup1_1*/ agegroup1_2 agegroup1_3 agegroup1_4 msp , vce(robust) clusterid(id) family(gaussian) link(identity) full 
        
        est restore within
        mat b1= e(b)
        mat colnames b1= `=ustrregexra("`:colnames b1'", "W__", "")'
        erepost b=b1, rename
        est sto within
        
        
        est restore between
        mat b2=e(b)
        mat colnames b2= `=ustrregexra("`:colnames b2'", "B__", "")'
        erepost b=b2, rename
        est sto between
        
         
        
        coefplot (within, drop(B__* _cons)) ||    ///
                 (between, drop(W__* _cons)), xline(0) scheme(s2mono) graphregion(lwidth(none) fcolor(white)) ///
                 saving(test.png, replace)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.4 KB
ID:	1734428

        Comment


        • #5
          Dear Andrew, thank you so much for providing that code. I can say it worked perfectly fine also with my original data. It is possible with that code to make further changes to variable labels, add headings etc. No problems.

          Comment

          Working...
          X