Announcement

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

  • adding additional header line to collect table

    The following is a silly example but shows, I think, the point I want to make and uses the auto data:

    Code:
    collect clear
    sysuse auto, clear
    
    regress price c.weight
    
    collect get var = "linear", tags(row[1])
    collect get var = "p-value", tags(row[2])
    collect get var = "quad", tags(row[3])
    collect get var = "p-value2", tags(row[4])
    
    collect get eff1 = el(r(table),1,1), tags(row[1])
    collect get eff1 = el(r(table),4,1), tags(row[2])
    
    regress price c.weight##c.weight
    
    collect get eff2 = el(r(table),1,1), tags(row[1])
    collect get eff2 = el(r(table),4,1), tags(row[2])
    collect get eff2 = el(r(table),1,2), tags(row[3])
    collect get eff2 = el(r(table),4,2), tags(row[4])
    
    regress price weight mpg
    
    collect get eff3 = el(r(table),1,1), tags(row[1])
    collect get eff3 = el(r(table),4,1), tags(row[2])
    
    regress price c.weight##c.weight mpg
    
    collect get eff4 = el(r(table),1,1), tags(row[1])
    collect get eff4 = el(r(table),4,1), tags(row[2])
    collect get eff4 = el(r(table),1,2), tags(row[3])
    collect get eff4 = el(r(table),4,2), tags(row[4])
    
    collect layout (row) (result) 
    *collect style header row, level(hide)
    collect style cell result[var],border(right) 
    collect label levels result var "Model"
    collect label levels result eff1 "Linear"
    collect label levels result eff2 "Quad"
    collect label levels result eff3 "Linear"
    collect label levels result eff4 "Quad"
    collect style cell result[var],smcl(text)
    collect style cell result, basestyle nformat(%6.3f)
    noi collect preview
    running this code produces the following result:

    Code:
    . run "/Users/rich/Documents/Stata/price.do"
    
    ------------------------------------------
      |    Model | Linear   Quad Linear   Quad
    --+----------+----------------------------
    1 |   linear |  2.044 -7.273  1.747 -9.040
    2 |  p-value |  0.000  0.009  0.008  0.003
    3 |     quad |         0.002         0.002
    4 | p-value2 |         0.001         0.000
    ------------------------------------------
    I would like to add, above the "Linear Quad" pairs a new line that says, e.g., "w/out mpg" over the first pair and "w mpg" over the second pair - how can I do this

  • #2
    Here is one way you can add "super" column headers.
    Code:
    * add tag dimension to group your results
    collect addtags hasmpg[no], fortags(result[eff1 eff2])
    collect addtags hasmpg[yes], fortags(result[eff3 eff4])
    * label the levels as you like
    collect label levels hasmpg no "w/out mpg" yes "w mpg"
    * add new group dimension to the layout
    collect layout (row) (hasmpg#result)
    
    * center the repeating "super" column headers
    collect style column, dups(center)
    collect preview
    Here is the first table.
    Code:
    -------------------------------------
      | w/out mpg w/out mpg  w mpg  w mpg
      |    Linear      Quad Linear   Quad
    --+----------------------------------
    1 |     2.044    -7.273  1.747 -9.040
    2 |     0.000     0.009  0.008  0.003
    3 |               0.002         0.002
    4 |               0.001         0.000
    -------------------------------------
    Here is the second table.
    Code:
    -------------------------------
      |   w/out mpg       w mpg    
      | Linear   Quad Linear   Quad
    --+----------------------------
    1 |  2.044 -7.273  1.747 -9.040
    2 |  0.000  0.009  0.008  0.003
    3 |         0.002         0.002
    4 |         0.001         0.000
    -------------------------------

    Comment


    • #3
      Jeff Pitblado (StataCorp) - thank you very much!

      Comment


      • #4
        Jeff Pitblado (StataCorp) - I see a problem and it can be seen by comparing my output in #1 to yours in #2 - the column headed "Model" in my output is not shown in your output and this appears to be due entirely to the inclusion of your code; for example, the first output below includes your code while the second output below comments out your code - but I want to include the column headed "Model" as well as the "super" header - how can I have both?

        Code:
        . run "/Users/rich/Documents/Stata/price.do"
        
        -------------------------------
          |   w/out mpg       w mpg    
          | Linear   Quad Linear   Quad
        --+----------------------------
        1 |  2.044 -7.273  1.747 -9.040
        2 |  0.000  0.009  0.008  0.003
        3 |         0.002         0.002
        4 |         0.001         0.000
        -------------------------------
        
        . run "/Users/rich/Documents/Stata/price.do"
        
        ------------------------------------------
          |    Model | Linear   Quad Linear   Quad
        --+----------+----------------------------
        1 |   linear |  2.044 -7.273  1.747 -9.040
        2 |  p-value |  0.000  0.009  0.008  0.003
        3 |     quad |         0.002         0.002
        4 | p-value2 |         0.001         0.000
        ------------------------------------------

        Comment


        • #5
          Sorry, I was so focused on the column super headers that I missing that result had a var level.

          Add the following lines above my original code block
          Code:
          * need place-holder level for the Model column
          collect addtags hasmpg[Model], fortags(result[var])
          collect style cell hasmpg[Model], border(right)
          * use braille blank (u2800) to force an empty column header cell
          collect label levels hasmpg Model "⠀"
          Here is the first table.
          Code:
          ------------------------------------------------
            |        ⠀ | w/out mpg w/out mpg  w mpg  w mpg
            |    Model |    Linear      Quad Linear   Quad
          --+----------+----------------------------------
          1 |   linear |     2.044    -7.273  1.747 -9.040
          2 |  p-value |     0.000     0.009  0.008  0.003
          3 |     quad |               0.002         0.002
          4 | p-value2 |               0.001         0.000
          ------------------------------------------------
          Here is the second table.
          Code:
          ------------------------------------------
            |        ⠀ |   w/out mpg       w mpg
            |    Model | Linear   Quad Linear   Quad
          --+----------+----------------------------
          1 |   linear |  2.044 -7.273  1.747 -9.040
          2 |  p-value |  0.000  0.009  0.008  0.003
          3 |     quad |         0.002         0.002
          4 | p-value2 |         0.001         0.000
          ------------------------------------------

          Comment


          • #6
            that works - thank you again Jeff

            Comment

            Working...
            X