Announcement

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

  • Weight markers in coefplot by created matrix

    I am trying to weight markers in coefplot via a matrix of means (because they are proportions they vary from 0-1). Below i generate weights using mean, and save them (Rprop_p, Rprop_p, Rprop_p, Rprop_n), but the coefplot does NOT use them. This coefplot code outputs the attached figure (the one with 2 marker colors).

    Code:
        reghdfe wheat_yield c.hyv_prop#i.st66code $COV  if POSEFFECT==1, ///
            absorb(distcode i.st66code##i.year) vce(cluster distcode)
            estimates store W_p
        reghdfe wheat_yield c.hyv_prop#i.st66code $COV  if POSEFFECT==0, ///
            absorb(distcode i.st66code##i.year) vce(cluster distcode)
            estimates store W_n
        reghdfe rice_yield c.hyv_prop#i.st66code $COV if POSEFFECT==1, ///
            absorb(distcode i.st66code##i.year) vce(cluster distcode)
            estimates store R_p
        reghdfe rice_yield c.hyv_prop#i.st66code $COV if POSEFFECT==0, ///
            absorb(distcode i.st66code##i.year) vce(cluster distcode)
            estimates store R_n        
    
    * Weights:
        mean proprice if year<1970 & POSEFFECT==1, over(st66)
            mat Rprop_p = e(b)
        mean proprice if year<1970 & POSEFFECT==0, over(st66)
            mat Rprop_p = e(b)
        mean propwheat if year<1970 & POSEFFECT==1, over(st66)
            mat Rprop_p = e(b)
        mean propwheat if year<1970 & POSEFFECT==0, over(st66)
            mat Wprop_n = e(b)
            
        coefplot (R_p, label("Responsive") mcolor(green) ciopts(lcolor(green)))  ///
                (R_n, label("Unresponsive")), bylabel(Rice) ||  ///
                (W_p, label("Responsive") mcolor(green) ciopts(lcolor(green))) ////
                (W_n, label("Unresponsive")), bylabel(Wheat) ///
            drop(_cons $COV) xline(0) byopts(xrescale)
    The problem is, I can't figure out how to feed my weights into my coefplot. I need to put them within each of the 4 separate sets of markers, but adding weight(Rprop_p) after R_p, as below doesn't work -- no plot is created, I instead get the error type mismatch.

    Code:
        
        coefplot (R_p, weight(Rprop_p) Label("Responsive") mcolor(green) ciopts(lcolor(green)))  ///
                (R_n, label("Unresponsive")), bylabel(Rice) ||  ///
                (W_p, label("Responsive") mcolor(green) ciopts(lcolor(green))) ////
                (W_n, label("Unresponsive")), bylabel(Wheat) ///
            drop(_cons $COV) xline(0) byopts(xrescale)
    Following this help here I tried using aux as below, but that outputs a mostly empty graphic (very odd) with the error/note (R_p: e(Rprop_p) not found). I suspect the problem there is that aux() only recognizes e() objects, but since each of my weights is originally called e(b) I can't use e(b) to call them.

    Code:
        coefplot (R_p, aux(Rprop_p) weight(@aux1) label("Responsive") mcolor(green) ciopts(lcolor(green)))  ///
                (R_n, label("Unresponsive")), bylabel(Rice) ||  ///
                (W_p, label("Responsive") mcolor(green) ciopts(lcolor(green))) ////
                (W_n, label("Unresponsive")), bylabel(Wheat) ///
            drop(_cons $COV) xline(0) byopts(xrescale)
    Last note -- I would be happier, actually, to generate only 2 sets of coefficients and 2 sets of weights as with the code below, but then I need to be able to pick and choose which markers are which color by marker label, and I couldn't figure out how to do that. That graphic (all dark blue markers) is also attached. In general, estimating an equation twice by sub-sample will obviously NOT give the same coefficients, but in this particular case (where the markers are state-specific and the subset is for a group of states) the coefficients are very close to identical... though not actually identical because of the shared coefficients in $COV. So bonus if somebody can help me to figure out how to weight this coefplot below, AND how to color by marker such that markers associated with certain values of st66code are certain colors, e.g. 1.st66code#hyv_prop (Bihar's beta) is green but 2.st66code#hyv_prop (Gujarat's beta) is dark red.

    Code:
        reghdfe wheat_yield c.hyv_prop#i.st66code $COV , ///
            absorb(distcode i.st66code##i.year) vce(cluster distcode)
            estimates store W
        eststo: reghdfe rice_yield c.hyv_prop#i.st66code $COV , ///
            absorb(distcode i.st66code##i.year) vce(cluster distcode)
            estimates store R
    
        mean proprice if year<1970 , over(st66)
            mat Rprop = e(b)
        mean propwheat if year<1970 , over(st66)
            mat Wprop = e(b)
    
        coefplot (R), bylabel(Rice) || (W), bylabel(Wheat) ///
            drop(_cons $COV) xline(0) byopts(xrescale)
    LATER EDIT: As I read back over the last bit of my question, I realized that oddly, Gujarat's coefficients are NOT appearing in the coefplot where I plot markers for each subsets of states (green marker states vs. red marker states) -- And the Andhra Pradesh coefficient is not being produced in EITHER graphic! The regression matrix output e(b) all contain those AP and Gujarat coefficients, and I'm realizing that coefplot is dropping the first coefficient, the one that the matrix output lists with a b -- 1b.st66code#c.hyv_prop for the whole sample or for the POSEFFECT==1 sample, and 1b.st66code#c.hyv_prop for the POSEFFECT==0 sample. So in addition to the weight and the marker color question above, I would also like to know how I can keep coefplot from dropping the first coefficient from the regression e(b) matrix! I really apologize for the multi-layered nature of this post.
    Attached Files
    Last edited by Leah Bevis; 27 Mar 2024, 10:53.

  • #2
    Can you add a data example that reproduces the issues that you raise?

    Comment


    • #3
      Hi Andrew, sincere apologies for the delay.

      Here is a reproducible example of the problem I mention above under "LATER EDIT" --- the problem that coefplot is dropping the first of each coefficient that it should be storing from each regression. Note that I've made a few apparently silly/random choices, eg putting in covariates, using reghdfe to fix out foreign... all this is to make the example as similar to my own code as possible. When i do this I find the same problem as I'm finding in my own code: the first coefficient that should be stored after each regression is being dropped in the figure. I would really love to know how to fix this problem!

      I'd also like to know how to weight the coef markers by a mean stored in e(b) as mentioned above, but this is now the much-less-pressing-problem. I have included those weights in the example too, in case you can show me how to do this weighting.

      Reproducible example:

      Code:
      clear
      sysuse auto    
      
      gen cat = substr(make, 1, strpos(make, " ") - 1)
      encode cat, generate(ncat)
      
      gen CAT=ncat<=11
      
      gl COV "trunk weight length"
      
      ************************************************
      * Weights to ideally weight coef bubbles:
      mean turn if CAT==1, over(ncat)
      mat weight_p = e(b)
      
      mean turn if CAT==0, over(ncat)
      mat weight_n = e(b)
      ************************************************
      
      reghdfe price c.mpg#ncat $COV if CAT==1, absorb(foreign)
         estimates store h_p
      
      reghdfe price c.mpg#ncat $COV if CAT==0, absorb(foreign)
         estimates store h_n
              
      coefplot (h_p, label("cat 1") mcolor(green) ciopts(lcolor(green)))  ///
         (h_n, label("cat 2")), bylabel(HYV Adoption) ///
          drop(_cons $COV) xline(0) byopts(xrescale)
              
      tab ncat if CAT==0 /* note that Linc. is the first listed, but not in figure */
      
      tab ncat if CAT==1 /* note that AMC is the first listed, but not in figure */
      Last edited by Leah Bevis; 13 May 2024, 21:15.

      Comment


      • #4
        I do not have much time to provide a comprehensive reply due to other commitments, but this should allow you to make progress.

        1. The missing coefficients are treated as baselevels. So have the -baselevels- option.
        2. Your weights are constant within equations. Stata weights markers within equations, see https://journals.sagepub.com/doi/10....36867X20931008. So you need to do some adjustments to ensure that all weights are taken into account when weighting. Ben has shown how to do this in https://repec.sowi.unibe.ch/stata/coefplot/markers.html, towards the bottom.
        3. The weighting matrices must be added to the estimation results and must have the same dimensions as e(b). I show how to add these here and how to fill the extra columns with missing values.

        So what you have below does everything except address the between equation weighting. Note estadd is part of estout from SSC.

        Code:
        clear
        sysuse auto    
        
        gen cat = substr(make, 1, strpos(make, " ") - 1)
        encode cat, generate(ncat)
        
        gen CAT=ncat<=11
        
        gl COV "trunk weight length"
        
        
        
        reghdfe price c.mpg#ncat $COV if CAT==1, absorb(foreign)
           estimates store h_p
           mat l e(b)
        
        reghdfe price c.mpg#ncat $COV if CAT==0, absorb(foreign)
           estimates store h_n
          
          
        ************************************************
        * Weights to ideally weight coef bubbles:
        mean turn if CAT==1, over(ncat)
        mat weight_p = e(b), .z, .z, .z, .z
        estadd mat wp= weight_p: h_p
        
        
        mean turn if CAT==0, over(ncat)
        mat weight_n = e(b), .z, .z, .z, .z
        estadd mat wp= weight_n: h_n
        ************************************************
                
        coefplot (h_p,  label("cat 1")  aux(wp) weight(@aux1) ms(oh) mcolor(green) ciopts(lcolor(green)))  ///
           (h_n, label("cat 2") aux(wp) weight(@aux1) ms(oh) ), bylabel(HYV Adoption) ///
            drop(_cons $COV) xline(0) byopts(xrescale) baselevels
                
        tab ncat if CAT==0 /* note that Linc. is the first listed, but not in figure */
        
        tab ncat if CAT==1 /* note that AMC is the first listed, but not in figure */
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	37.6 KB
ID:	1753305



        Last edited by Andrew Musau; 14 May 2024, 09:36.

        Comment

        Working...
        X