Announcement

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

  • -collect stars- does not work with named expressions

    I'm trying to use a named expression as the values used to attach stars to the coefficients, but I am unable to get it to work.
    The values display just fine in the table.
    I suspect the issue is that there is some tag that doesn't match, as per this, however, I cannot fathom what tag that would be.
    collect stars requires that the attach() result items must have the same tag elements as the p-value result items for the stars labels to show up


    MWE below. Stars don't display using p_named, despite the values showing up in the table, but display just fine for _r_p.
    My real named expression is more complex, so simply using _r_p would not be a solution.
    Code:
    sysuse auto, replace
    collect clear
    collect p_named=_r_p: reg mpg weight
    collect style cell result, nformat(%6.3fc)
    collect stars p_named 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b)
    collect layout (colname) (result[_r_b _r_p p_named])
    collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b)
    collect preview
    Thank you in advance!

  • #2
    See

    Code:
    help collect remap

    Code:
    sysuse auto, replace
    collect clear
    collect p_named=_r_p: reg mpg weight
    collect style cell result, nformat(%6.3fc)
    collect stars p_named 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b)
    collect remap result[_r_p] = result[p_named]
    collect layout (colname) (result[_r_b])
    Res.:

    Code:
    . collect layout (colname) (result[_r_b])
    
    Collection: default
          Rows: colname
       Columns: result[_r_b]
       Table 1: 2 x 1
    
    ---------------------------
                  | Coefficient
    --------------+------------
    Weight (lbs.) |   -0.006***
    Intercept     |   39.440***
    ---------------------------
    Last edited by Andrew Musau; 30 Nov 2024, 19:16.

    Comment


    • #3
      Thank you!
      I tried working with remap. But doesn't that simply change the tag on the _r_p results?
      As mentioned in my question, I'm trying to do this with results that are not simply _r_p.
      For example, suppose I want one-tailed p-values:
      Code:
      sysuse auto, replace
      collect clear
      collect p_named=(r(table)["pvalue",1...]/2): reg price mpg-foreign
      collect style cell result, nformat(%6.3fc)
      collect stars p_named 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b)
      collect layout (colname) (result[_r_b _r_p p_named])
      collect remap result[_r_p] = result[p_named]
      collect layout (colname) (result[_r_b])
      Res:
      Code:
      . collect layout (colname) (result[_r_b _r_p p_named])
      
      Collection: default
            Rows: colname
         Columns: result[_r_b _r_p p_named]
         Table 1: 11 x 3
      
      ----------------------------------------------------
                             | Coefficient p-value p_named
      -----------------------+----------------------------
      Mileage (mpg)          |     -21.805   0.779   0.390
      Repair record 1978     |     184.793   0.580   0.290
      Headroom (in.)         |    -635.492   0.102   0.051
      Trunk space (cu. ft.)  |      71.499   0.455   0.227
      Weight (lbs.)          |       4.521   0.002   0.001
      Length (in.)           |     -76.491   0.063   0.032
      Turn circle (ft.)      |    -114.278   0.359   0.179
      Displacement (cu. in.) |      11.540   0.174   0.087
      Gear ratio             |    -318.648   0.778   0.389
      Car origin             |    3334.848   0.001   0.000
      Intercept              |    9789.494   0.150   0.075
      ----------------------------------------------------
      
      . 
      . collect remap result[_r_p] = result[p_named]
      (11 items remapped in collection default)
      
      . 
      . collect layout (colname) (result[_r_b])
      
      Collection: default
            Rows: colname
         Columns: result[_r_b]
         Table 1: 11 x 1
      
      ------------------------------------
                             | Coefficient
      -----------------------+------------
      Mileage (mpg)          |  -21.805   
      Repair record 1978     |  184.793   
      Headroom (in.)         | -635.492   
      Trunk space (cu. ft.)  |   71.499   
      Weight (lbs.)          |    4.521***
      Length (in.)           |  -76.491*  
      Turn circle (ft.)      | -114.278   
      Displacement (cu. in.) |   11.540   
      Gear ratio             | -318.648   
      Car origin             | 3334.848***
      Intercept              | 9789.494   
      ------------------------------------
      p_named collects one-tailed p-values, and length should then get two stars, but now it's simply overridden by the value from _r_p.
      Or am I misuderstanding?

      Comment


      • #4
        My suspicion about non-matching tags being the issue was correct. Named expressions can have different tags than the standard results (specifically, _r_b) that are collected, leading to the stars not being attached.
        Some experimentation helped me find which tags were different and enabled me to cobble together this extremely hacky "solution" to make the tags match in this particular case:
        Code:
        sysuse auto, replace
        collect clear
        collect p_named=(r(table)["pvalue",1...]/2): reg price mpg-foreign
        
        collect style cell result, nformat(%6.3fc)
        collect stars p_named 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b)
        
        collect recode program_class nclass = eclass, fortags(result[p_named])
        collect addtags coleq[price], fortags(result[p_named])
        collect addtags rowname[pvalue], fortags(result[_r_b])
        
        collect layout (colname) (result[_r_b])
        Is there a better solution than this?
        Jeff Pitblado (StataCorp) I'd love if you could weigh in on this, since you are the one who posted that helpful nugget about collect stars requiring a perfect match of tags.

        Thanks!

        Comment


        • #5
          Originally posted by Yehuda Davis View Post
          Thank you!
          I tried working with remap. But doesn't that simply change the tag on the _r_p results?
          Correct. In #1, the two sets of p-values coincide. Otherwise, you need to modify the tags as you have shown.

          Comment


          • #6
            Thank you, Andrew!

            Comment


            • #7
              Remapping _r_p seems to work, but is not quite the correct solution.

              When you use an expression to define a result for collect, that new result gets most of the tag elements you would expect from a copy of the original result, like _r_p's colname and result_type, but the program_class is nclass instead of eclass. This is what is getting in your way. There also may be extra tag elements added to your new result because of the expression used to construct it. In your second example computing "one-tailed" p-values, you have
              Code:
              collect p_named=(r(table)["pvalue",1...]/2): reg price mpg-foreign
              So in addition to the usual colname and result_type tag elements, result[p_named] items also get tagged with rowname[pvalue] but not coleq[price].

              The simplest solution I can figure here is to also construct your own name for the coefficient results you want the stars to be attached to. Here is how I accomplished this.
              Code:
              sysuse auto, replace
              collect clear
              * p_named for the "one-tailed" p-values
              * b_named for the corresponding coefficients
              collect p_named=(r(table)["pvalue",1...]/2) ///
                  b_named=(r(table)["b",1...]) ///
                  : reg price mpg-foreign
              collect style cell result, nformat(%6.3fc)
              * label our custom results
              collect label levels result ///
                  b_named "Coefficient" ///
                  p_named "p-value (one-tail)"
              * define our stars rules for the results of interest
              collect stars _r_p p_named ///
                  0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", ///
                  attach(_r_b b_named)
              * the stars still did not get attached :(
              collect layout (colname) (result[_r_b _r_p b_named p_named])
              * we need to force the -rowname- level to agree for our custom results
              collect recode rowname pvalue = ignore b = ignore, ///
                  fortags(result[p_named b_named])
              * now the stars are properly constructed and attached
              collect preview
              Here is the resulting table.
              Code:
              ---------------------------------------------------------------------------
                                     | Coefficient p-value Coefficient p-value (one-tail)
              -----------------------+---------------------------------------------------
              Mileage (mpg)          |  -21.805      0.779  -21.805                 0.390
              Repair record 1978     |  184.793      0.580  184.793                 0.290
              Headroom (in.)         | -635.492      0.102 -635.492*                0.051
              Trunk space (cu. ft.)  |   71.499      0.455   71.499                 0.227
              Weight (lbs.)          |    4.521***   0.002    4.521***              0.001
              Length (in.)           |  -76.491*     0.063  -76.491**               0.032
              Turn circle (ft.)      | -114.278      0.359 -114.278                 0.179
              Displacement (cu. in.) |   11.540      0.174   11.540*                0.087
              Gear ratio             | -318.648      0.778 -318.648                 0.389
              Car origin             | 3334.848***   0.001 3334.848***              0.000
              Intercept              | 9789.494      0.150 9789.494*                0.075
              ---------------------------------------------------------------------------

              Comment


              • #8
                Thank you, Jeff, for the thorough and helpful response!

                Comment

                Working...
                X