Announcement

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

  • Modifying internal temporary variables in coefplot

    I'm using Ben Jann's outstanding coefplot package for plotting regression estimates. I can't figure out, however, if there is a way to modify internal temporary variables before using them. Specifically, in the image below, I'd like to replace 0.000 with <.001 and 0.167 with .167. The code I've used to generate that example is below. It shows that I'm accessing @pval to get the P values for the estimates. I'd like to be able to reformat them before using them. Thanks in advance.

    Partha

    Click image for larger version

Name:	coefplot-example.png
Views:	1
Size:	30.9 KB
ID:	1563240


    Code:
    clear all
    sysuse auto
    replace weight = weight/100
    
    glm price weight trunk , link(log) family(gaussian)
    estimates store coeff
    
    gen xlocation = .08
    coefplot coeff, drop(_cons) addplot(scatter @at xloc, msymbol(none) mlabel(@pval) mlabf(%-5.3f) text(.7 .09 "P value"))

  • #2
    Perhaps this will set you on a useful path. This demonstrates obtaining the p-value and then formatting them to your liking. You will then replace the mlabel(@pval) malbf(%-5.3f) options with something else, I'm not sure what, that uses the formatted values.
    Code:
    sysuse auto
    replace weight = weight/100
    quietly glm price weight trunk , link(log) family(gaussian)
    matrix list r(table)
    
    scalar pv = r(table)["pvalue", "price:weight"]
    local pvf : display %5.3f pv
    if "`pvf'"=="0.000" local pvf "<.000"
    else local pvf = substr("`pvf'",2,4)
    display "`pvf'"
    
    scalar pv = r(table)["pvalue", "price:trunk"]
    local pvf : display %5.3f pv
    if "`pvf'"=="0.000" local pvf "<.000"
    else local pvf = substr("`pvf'",2,4)
    display "`pvf'"
    Code:
    . matrix list r(table)
    
    r(table)[9,3]
                 price:      price:      price:
                weight       trunk       _cons
         b   .04590547  -.01933636   7.5526096
        se   .00864448   .01398481   .21971329
         z   5.3103789  -1.3826694   34.374841
    pvalue   1.094e-07   .16676626   5.99e-259
        ll    .0289626  -.04674608   7.1219795
        ul   .06284834   .00807335   7.9832397
        df           .           .           .
      crit    1.959964    1.959964    1.959964
     eform           0           0           0
    
    . 
    . scalar pv = r(table)["pvalue", "price:weight"]
    
    . local pvf : display %5.3f pv
    
    . if "`pvf'"=="0.000" local pvf "<.000"
    
    . else local pvf = substr("`pvf'",2,4)
    
    . display "`pvf'"
    <.000
    
    . 
    . scalar pv = r(table)["pvalue", "price:trunk"]
    
    . local pvf : display %5.3f pv
    
    . if "`pvf'"=="0.000" local pvf "<.000"
    
    . else local pvf = substr("`pvf'",2,4)
    
    . display "`pvf'"
    .167

    Comment


    • #3
      Thanks, William. You did, in fact, set me on a useful path beginning with the inspiration to get the appropriate string to show the p-values as needed. My use case is complex so I didn't even think to try and get mlabel values in from an external source. But I made some guesses and they provided a logic (at least for my case) that seems to work. My solution is not elegant so I'm still hoping there's a better one out there, but for now I can live with it. Code below.

      Code:
      clear all
      
      sysuse auto
      
      replace weight = weight/100
      
      glm price weight trunk if foreign==0, link(log) family(gaussian)
      estimates store d
      matrix pvd = r(table)["pvalue", "price:weight".."price:trunk"]
      matrix pvd = pvd'
      
      glm price weight trunk if foreign==1, link(log) family(gaussian)
      estimates store f
      matrix pvf = r(table)["pvalue", "price:weight".."price:trunk"]
      matrix pvf = pvf'
      
      matrix pv = pvd \ pvf \ pvd \ pvf // adding 2 more regressions - clones so just to establish the principle
      matrix pv = vec(pv)
      
      svmat pv
      gen pv = strofreal(pv1,"%5.3f")
      replace pv = "<.001" if pv=="0.000"
      replace pv = subinstr(pv,"0.","  .",.)
      format pv %6s
      
      gen xlocation = .12
      coefplot (d, drop(_cons)) (f, drop(_cons)) || (d, drop(_cons)) (f, drop(_cons)) , ///
          addplot(scatter @at xloc, msymbol(none) mlabel(pv) mlabpos(9) text(.7 .1 "P value"))
      Last edited by Partha Deb; 13 Jul 2020, 19:13.

      Comment

      Working...
      X