Announcement

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

  • Adding percent changes to coefplot using mlabel()

    Hi all,

    I'm working with coefplot in Stata (coefplot is a community-contributed command available from the Stata Journal) and plotting treatment effects for several heterogeneous treatment effect (HTE) variables. Each HTE variable produces two point estimates (e.g. x_hte1 and x_hte2) which I'm plotting side-by-side. I want to label each point with the estimate and the percent change relative to the control group mean. I calculate and store these without any issue, there are 5 vars which each have 2 HTEs so there are 10 total points plotted.

    I want to plot BOTH the estimate (@b) AND the percent change, computed as:
    foreach hte in `hte_vars' {
    estimates restore m_`hte'
    matrix B = e(beta_pds)
    local ncol = colsof(B)
    local b1 = B[1, `=`ncol'-1']
    local b0 = B[1, `ncol']

    local ctrl1 = `ctrl1_`hte''
    local ctrl0 = `ctrl2_`hte''

    if `ctrl1' != 0 {
    local pct1 = 100 * `b1' / `ctrl1'
    }
    else {
    local pct1 = .
    }

    if `ctrl0' != 0 {
    local pct0 = 100 * `b0' / `ctrl0'
    }
    else {
    local pct0 = .
    }

    * Format nicely for labels
    local lab1 : display %5.3f `b1'
    local lab2 : display %5.3f `b0'
    local pc1 : display %4.1f `pct1'
    local pc0 : display %4.1f `pct0'

    * Create quoted label strings
    local label1 = `""`lab1', `pc1'%""'
    local label2 = `""`lab2', `pc0'%""'


    * Append properly quoted strings to mlabel macro
    local all_mlabels `"`all_mlabels' `label1' `label2'"'
    }


    I’ve created the labels as a local macro all_mlabels, and they look great when I display them (fake numbers):

    display _asis `"`all_mlabels'"'
    "123.000, 123.0%" "456.000, 456.0%" "789.000, 789.0%" "111.000, 111.0%" "222.000, 222.0%" "333.000, 333.0%" "444.000, 444.0%" "555.000, 555.0%" "666.000, 666.0%" "777.000, 777.0%"


    But when I try to run:

    coefplot ///
    (m_hte_var1, rename(x_hte_var11 = "Group 1" x_hte_var12 = "Group 2")) ///
    (m_hte_var2, rename(x_hte_var21 = "Group 1" x_hte_var22 = "Group 2")) ///
    (m_hte_var3, rename(x_hte_var31 = "Group 1" x_hte_var32 = "Group 2")) ///
    (m_hte_var4, rename(x_hte_var41 = "Group 1" x_hte_var42 = "Group 2")) ///
    (m_hte_var5, rename(x_hte_var51 = "Group 1" x_hte_var52 = "Group 2")) ///
    , mlabels(`all_mlabels') b(beta_pds) v(V_pds) keep(x_*) ///
    xline(0) legend(off)


    I get the error: mlabels(): invalid matchlist

    Which I am very confused by as I have 10 points plotted, and 10 mlabels() stored. I cannot share a sample of the data unfortuantely, but any advice on how to work through this issue would be very appreciated.

    Thanks so much in advance.
    Last edited by Sophia Scaglioni; 22 Apr 2025, 11:33.

  • #2
    I think what you need instead of the current mlabels option is the option
    Code:
    mlabel(string(@b, "%4.1f") + "%")
    Also, as you are requested in the forum FAQ to specify, coefplot is a community-contributed command available from the Stata Journal.
    Last edited by Hemanshu Kumar; 22 Apr 2025, 11:17.

    Comment


    • #3
      Hi Hemanshu. I want to plot BOTH the estimate (@b) AND the percent change. I updated the original post to reflect , and also added the specification about coefplot. Do you have any thoughts given the additional info? Sorry if the first post was unclear.

      Comment


      • #4
        Ah, your intent is clearer now. Your use of the same numbers for the coefficient and the percentage change was confusing.

        From the help for coefplot, the mlabels option has the following syntax:

        mlabels(matchlist) specifies marker labels for selected coefficients. matchlist is:

        coeflist = # "label" [coeflist = # "label" ...]

        where coeflist is as above for keep() and # is a number 0--12 for the location of the
        marker label
        You can see how your local macro does not follow this syntax.

        Comment


        • #5
          Originally posted by Sophia Scaglioni View Post

          I want to plot BOTH the estimate (@b) AND the percent change, computed as:
          foreach hte in `hte_vars' {
          estimates restore m_`hte'
          matrix B = e(beta_pds)
          local ncol = colsof(B)
          local b1 = B[1, `=`ncol'-1']
          local b0 = B[1, `ncol']

          local ctrl1 = `ctrl1_`hte''
          local ctrl0 = `ctrl2_`hte''

          if `ctrl1' != 0 {
          local pct1 = 100 * `b1' / `ctrl1'
          }
          else {
          local pct1 = .
          }

          if `ctrl0' != 0 {
          local pct0 = 100 * `b0' / `ctrl0'
          }
          else {
          local pct0 = .
          }
          Can you add a data example? You could use one of Stata's datasets.

          Comment

          Working...
          X