Announcement

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

  • Use collect stars command with table of t-tests

    Hi All,

    I have made a table of t-tests using the code below, which I borrowed from an example in the Stata manual.
    The example is here: https://www.stata.com/manuals/tablesexample4.pdf

    I would like to use the collect stars command to replace the last column, which currently shows p-values, with a column showing significance stars.
    The Stata help file indicates that the command should be something like:

    Code:
    collect stars _r_p 0.01 "**" 0.05 "*"
    I have tried many different versions of the collect stars command with _r_p replaced with item names from the example below (e.g., p Difference), but I can't seem to get it to work.
    Does anyone know how to do this? I would prefer to have the stars in a separate column rather than attached to something.

    Thanks,

    Jeremy

    Code:
    use https://www.stata-press.com/data/r18/nhanes2l
    collect create ex4
    quietly: collect r(N_1) r(mu_1) r(N_2) r(mu_2) r(p): by race, sort: ttest bpsystol, by(sex)
    collect remap result[N_1 mu_1] = Males
    collect remap result[N_2 mu_2] = Females
    collect remap result[p] = Difference
    collect style header Males Females Difference, title(name)
    collect layout (race) (Males Females Difference)
    collect label levels Males N_1 "N" mu_1 "Mean BP"
    collect label levels Females N_2 "N" mu_2 "Mean BP"
    collect label levels Difference p "p-value"
    collect style column, dups(center) width(equal)
    collect style cell, halign(center)
    collect style cell Males[mu_1] Females[mu_2] Difference[p], nformat(%5.2f)
    collect preview

  • #2
    collect stars works with items tagged with result, so you cannot remap result[p] to Difference[p]. Also, the command to attach stars in your example needs to use result p instead of _r_p, i.e.
    Code:
    collect stars p 0.01 "**" 0.05 "*"
    Here is how I changed the original example to replace the p-values with stars.
    Code:
    use https://www.stata-press.com/data/r18/nhanes2l
    collect create ex4
    quietly: collect r(N_1) r(mu_1) r(N_2) r(mu_2) r(p): by race, sort: ttest bpsystol, by(sex)
    collect remap result[N_1 mu_1] = Males
    collect remap result[N_2 mu_2] = Females
    collect style header Males Females, title(name)
    collect layout (race) (Males Females result[p])
    collect label levels Males N_1 "N" mu_1 "Mean BP"
    collect label levels Females N_2 "N" mu_2 "Mean BP"
    collect label levels result p "p-value", modify
    collect style column, dups(center) width(equal)
    collect style cell, halign(center)
    collect style cell Males[mu_1] Females[mu_2] result[p], nformat(%5.2f)
    collect preview
    
    * define stars for items tagged with -result[p]-
    collect stars p 0.01 "**" 0.05 "*"
    * notice that dim -result- now has level -stars-
    collect levels result
    * add stars to the layout
    collect layout (race) (Males Females result[p stars])
    * customize the column header for -result-
    collect style header result, title(label)
    collect label dim result "Difference", modify
    * remove p-values from table
    collect layout (race) (Males Females result[stars])
    
    Here is the resulting table.
    Code:
    --------------------------------------------------------------
          |         Males                Females        Difference
          |      N       Mean BP       N       Mean BP     stars
    ------+-------------------------------------------------------
    White |    4312      132.85      4753      128.53       **
    Black |     500      133.69       586      133.85
    Other |     103      130.67       97       126.72
    --------------------------------------------------------------

    Comment


    • #3
      Thank you Jeff! I really like the added functionality that the collect command offers, but I often find the code really difficult to write. I hope Stata will continue offering supplemental commands like etable and dtable that make the table easier to make. A ttable would be useful!

      Comment

      Working...
      X