Announcement

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

  • Multiple collect stars based on different conditions

    Dear Statalist,

    I would have a couple of questions related to the new command collect stars.

    First, I would like to know if it is possible to attach stars to coefficients based on custom statistics. To illustrate the point, in the example below I obtain r_p2 by dividing the p-value in half, and I would like to attach stars to the coefficients based on this (this is just an example to illustrate a more general point). The process does not seem to work (but it works if I use the p value which is automatically collected from the regress command, that is _r_p).

    Second, I would like to know if it is possible to attach stars to different statistics based on different conditions (for instance, attach stars to _r_b based on _r_p, and attach stars to _r_z based on _r_p2). Again, this is not a real - life example, but this feature could be useful to attach stars to different coefficients in the table based on different tests.


    Code:
    clear all
    use https://www.stata-press.com/data/r17/nhanes2
    quietly: collect r_p2 = r(_r_p)/2: regress bpsystol bmi i.region age
    collect label levels result r_p2 "r_p2"
    collect style showbase off
    collect layout (colname) (result[_r_b _r_z])
    
    collect stars _r_p 0.01 "***" 0.05 "**" 0.1 "*", attach(_r_b)
    collect preview
    
    collect stars r_p2 0.01 "***" 0.05 "**" 0.1 "*", attach(_r_b)
    collect preview
    Many thanks in advance!

  • #2
    Why not this:

    Code:
    collect stars _r_p 0.005 "***" 0.025 "**" 0.05 "*" 0.5 "X", attach(_r_b)
    collect preview
    I through in the X to see if worked since the stars don't change.

    Comment


    • #3
      Dear George,

      To better highlight my use case, let's consider the following table.

      Code:
      clear all
      use https://www.stata-press.com/data/r17/nhanes2l
      
      collect clear
      collect _r_b _r_se, tag(model[(1)]): regress bpsystol weight i.diabetes i.sex
      collect _f=r(F) _p_d=r(p), tag(model[(1)]): testparm i.diabetes
      
      collect _r_b _r_se, tag(model[(2)]): regress bpsystol weight diabetes##sex
      collect _f=r(F) _p_d=r(p), tag(model[(2)]): testparm i.diabetes i.diabetes#i.sex
      
      collect layout (colname#result) (model)
      
      collect style showbase off
      collect style cell, nformat(%5.2f)
      collect style cell border_block, border(right, pattern(nil))
      collect style cell result[_r_se], sformat("(%s)")
      
      collect style cell cell_type[item column-header], halign(center)
      
      collect style header result, level(hide)
      collect style column, extraspace(1)
      collect style row stack, spacer delimiter(" x ")
      
      collect layout (colname#result result[_f _p_d]) (model)
      
      collect style header result[_f _p_d], level(label)
      collect label levels result _f "F-statistic" _p_d "p-value", modify
      collect preview
      Now I would like to assign stars to coefficients (_r_b) based on their p-values (_r_p), and to assign stars to the F statistic (_f) based on its p-value (_p_d). I am almost there, but apparently the 2 actions exclude each other. When I assign stars to _f based on _p_d, the program forgets about the previous assignment of stars to _r_b based on _r_p.

      Code:
      collect stars _r_p 0.01 "***" 0.05 "**" 0.1 "*", attach(_r_b)
      collect preview
      collect stars _p_d 0.01 "***" 0.05 "**" 0.1 "*", attach(_f)
      collect preview
      As you can notice, the previous stars are removed after the second assignment of stars. It would be really nice to be able to cumulate the 2 actions.

      Comment


      • #4
        Dear Matteo,

        I came across this thread because I'm having a very similar problem right now. It seems that `collect stars` can currently only attach a single `result`-level of p-values to a corresponding `result`-level of coefficients.

        The following code does what you want, but my solution is a bit awkward.

        It would really be nice if `collect stars` could "accumulate" attachments!


        Code:
        clear all
        
        use https://www.stata-press.com/data/r17/nhanes2l
         
        // Collect results; tag F-statistic and p-value with new dims `F` and `F_p`
        collect _r_b _r_se, tag(model[(1)]): regress bpsystol weight i.diabetes i.sex
        testparm i.diabetes
        collect get F=`r(F)', tag(model[(1)] F[1])
        collect get F_p=`r(p)', tag(model[(1)] F_p[1])
        
        collect _r_b _r_se, tag(model[(2)]): regress bpsystol weight diabetes##sex
        testparm i.diabetes i.diabetes#i.sex
        collect get F=`r(F)', tag(model[(2)] F[1])
        collect get F_p=`r(p)', tag(model[(2)] F_p[1])
         
         
        // Layout
        collect style showbase off
        collect style cell, nformat(%5.2f)
        collect style cell border_block, border(right, pattern(nil))
        collect style cell result[_r_se], sformat("(%s)")
        collect style cell cell_type[item column-header], halign(center)
        collect style header result, level(hide)
        collect style column, extraspace(1)
        collect label levels F 1 "F-statistic" , modify
        collect label levels F_p 1 "p-value" , modify
        collect style cell F[1], border(top, pattern(single))
         
         
        // Now for the crucial part: The "trick" is to create result levels that combine
        // _r_b & F and _r_p & F_p, respectively...
         
        // Remap result[_r_b] and result[F] to new result[coeffs]
        collect remap result[_r_b] = result[coeffs]
        collect remap result[F] = result[coeffs]
         
        // Remap result[_r_p] and result[F_p] to new result[coeffs_p]
        collect remap result[_r_p] = result[coeffs_p]
        collect remap result[F_p] = result[coeffs_p]
        
        // Build stars based on result[coeffs_p] and attach to result[coeffs]
        collect stars coeffs_p 0.01 "***" 0.05 "**" 0.1 "*", attach(coeffs) shownote
        
         
        collect composite define Fstar = coeffs stars, replace delimiter("")
        collect addtags F[1], fortags(F_p[1])
         
        collect layout (colname#result[coeffs _r_se] ///
                        F#result[Fstar] ///
                        F_p#result[coeffs_p]) ///
                       (model)

        Best regards,
        Jürgen
        Last edited by Juergen Wiemers; 12 Jul 2024, 07:50.

        Comment


        • #5
          Here is a somewhat simpler solution that avoids the messy remapping of dimensions. The idea is to simply add auxiliary statistics and corresponding p-values to the (already existing) levels `result[_r_b]` and `result[_r_p]`

          Code:
          use https://www.stata-press.com/data/r17/nhanes2l
          
          // Collect stuff; add F-statistic and p-value to _r_b / _r_p, respectively
          // Tag F-statistic and p-value with new dim "aux"
          // Note: Same level aux[F] for both, F-value and p-stat!
          //       Otherwise, stars for F-statistic are not displayed...
          collect _r_b _r_se, tag(model[(1)]): regress bpsystol weight i.diabetes i.sex
          testparm i.diabetes
          collect get _r_b=`r(F)', tag(aux[F] model[(1)])
          collect get _r_p=`r(p)', tag(aux[F] model[(1)])
          
          collect _r_b _r_se, tag(model[(2)]): regress bpsystol weight diabetes##sex
          testparm i.diabetes i.diabetes#i.sex
          collect get _r_b=`r(F)', tag(aux[F] model[(2)])
          collect get _r_p=`r(p)', tag(aux[F] model[(2)])
          
           
          // Labels for F-stat
          collect label levels aux F "F-statistic", modify
          // ... new dim for p-value to be able to attach label
          collect addtags aux_p[F], fortags(aux[F]#result[_r_p])
          collect label levels aux_p F "p-value", modify
           
           
          // Stars spec
          collect stars _r_p 0.01 "***" 0.05 "**" 0.1 "*", attach(_r_b) shownote dimension
          
           
          // Layout
          collect style showbase off
          collect style cell, nformat(%5.2f)
          collect style cell border_block, border(right, pattern(nil))
          collect style cell result[_r_se], sformat("(%s)")
          collect style cell stars[label], halign(left)
          collect style header result, level(hide)
          collect style column, dups(center)
          
          collect layout (colname#result[_r_b _r_se] ///
                          aux[F]#result[_r_b] ///
                          aux_p[F]#result[_r_p]) ///
                         (model#stars)
          Last edited by Juergen Wiemers; 14 Jul 2024, 09:18.

          Comment

          Working...
          X