Announcement

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

  • Labes for coefplot with multiple _at:

    Hi all,

    I'm trying to produce a coefplot with multiple _at:s while keeping the variable labels in the coefplot. Is there a way to do this without needing to specify each coeflabel by hand? Since I get 144 _at, it would be great if there is a way to use my existing labels.

    Code:
    reg dv i.v1 i.v2 i.v3 i.v4 i.v5 
    
    margins, at(v1=(1(1)4) v2=(1(1)3) v3=(1 2) v4=(1(1)3) v5=(1 2)) post
    
    coefplot, etc etc
    Thanks in advance!

  • #2
    Somewhat unclear what exactly the desired result would be. For each _at a label that specifies the relevant combination of values, e.g. "v1=1, v2=1, ..., v5=1", "v1=1, v2=1, ..., v5=2", etc.? You would have to piece such labels together by processing the contents of matrix e(at) left behind by margins... ben

    Comment


    • #3
      Here is a way to implement Ben's suggestion, although with that many variables, the axis labels may start getting too long.

      Code:
      sysuse auto, clear
      regress mpg i.rep78 i.foreign
      qui margins, at(rep78=(1/5) foreign=(0/1)) post
      
      mat labs= e(at)
      local colnames = ustrregexra(" " + "`:colnames labs'", "([0-9]+)b(\.[a-zA-Z0-9_]+)", "$1$2")
      local colnames = subinstr(ustrregexra("`colnames'","[(\.)]",""), " ", " _", .)
      mat colnames labs= `colnames'
      clear
      svmat labs, names(matcol)
      gen lab=""
      qui ds lab, not
      foreach var in `r(varlist)'{
          replace lab= lab + ustrregexra("`var'", "labs_(\d+)(.*)", "$2") + "=" + ustrregexra("`var'", "labs_(\d+)(.*)", "$1")+ "; " if `var'
      }
      replace lab= substr(lab, 1, length(lab)-2)
      local ylab
      forval i= 1/`=_N'{
          local ylab `ylab' `i' `" `=lab[`i']' "'
      }
      
      sysuse auto, clear
      regress mpg i.rep78 i.foreign
      qui margins, at(rep78=(1/5) foreign=(0/1)) post
      coefplot, ylab(`ylab')
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.2 KB
ID:	1776575

      Last edited by Andrew Musau; 25 Apr 2025, 18:52.

      Comment

      Working...
      X