stata

esttab: Order _cons after indicate()


When using the user-command esttab, I would like to place the estimates of the constant after the row containing the text for the indicate() variables (YES).

sysuse auto
eststo m1: regress price weight length headroom i.foreign
esttab m1, indicate("International FE = *.foreign") order(length weight *.foreign headroom _cons)

I have tried to include different versions of my indicate variables in the order function (.e.g *.foreign, "International FE"), but none seem to work.

Crossposted @Statalist


Solution

  • Given the stylistic conventions, this is an odd thing to do with the table layout, so it is likely impossible without forking estout/esttab, which hews to those conventions.

    However, if you are willing to save the table as a text file, you could write a sed script that moves the line starting with _cons and the line below to before the first hline. You could probably do the same thing using Python from within Stata.

    Here's an example that works with the macOS version of sed. Here is the bash script:

    #!/bin/bash
    
    # This script modifies a text file by cutting a block of lines starting with '_cons',
    # the line above it, and the following two lines. It then inserts this block above the third
    # occurrence of a line that consists solely of hyphens (e.g., '--------------------').
    # A blank line is also inserted before the cut block.
    
    # How to use the script:
    # 1. Save the script to a file, e.g., 'process_file.sh'.
    # 2. Make the script executable by running: chmod +x process_file.sh
    # 3. Run the script by providing the filename of the text file to modify:
    #    ./process_file.sh table.txt
    #    Replace 'table.txt' with the actual filename you want to process.
    
    # Ensure a filename is provided as an argument
    if [ $# -eq 0 ]; then
      echo "Usage: $0 filename"
      exit 1
    fi
    
    # Assign the first argument (filename) to the variable 'filename'
    filename="$1"
    
    # Create a temporary file to store the cut lines and assign its name to 'tempfile'
    tempfile=$(mktemp)
    
    # Use sed to find the line starting with '_cons', capture it along with the following two lines,
    # and store them in the temporary file
    sed -n '/^_cons/{N;N;p;}' "$filename" > "$tempfile"
    
    # Remove the '_cons' line and the next two lines from the original file
    sed -i '' '/^_cons/{N;N;d;}' "$filename"
    
    # Find the line number of the third occurrence of a line containing only hyphens (---)
    # The grep command finds all such lines, then sed selects the third one, and cut extracts the line number
    line_number=$(grep -n '^[-]\{3,\}$' "$filename" | sed -n '3p' | cut -d: -f1)
    
    # Use the 'ed' text editor in silent mode to edit the file:
    # - Move to the line number found above
    # - Insert the contents of the temporary file one line above the third hyphen line
    # - Insert a blank line before the pasted content
    # - Write the changes and quit the editor
    ed -s "$filename" <<EOF
    $line_number
    -1r $tempfile
    -2i
    
    .
    w
    q
    EOF
    
    # Remove the temporary file as it's no longer needed
    rm "$tempfile"
    
    # Print a message indicating the file has been successfully modified
    echo "File $filename has been modified."
    

    Here's how you might use it in Stata:

    . sysuse auto
    (1978 automobile data)
    
    . eststo m1: regress price weight length headroom i.foreign
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(4, 69)        =     22.21
           Model |   357434897         4  89358724.3   Prob > F        =    0.0000
        Residual |   277630499        69  4023630.42   R-squared       =    0.5628
    -------------+----------------------------------   Adj R-squared   =    0.5375
           Total |   635065396        73  8699525.97   Root MSE        =    2005.9
    
    ------------------------------------------------------------------------------
           price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
          weight |   5.749148   .9514243     6.04   0.000     3.851108    7.647187
          length |  -81.11971   33.27376    -2.44   0.017     -147.499   -14.74036
        headroom |  -481.1805   324.0927    -1.48   0.142    -1127.728    165.3667
                 |
         foreign |
        Foreign  |   3570.379   633.9009     5.63   0.000     2305.781    4834.976
           _cons |   4429.788   3720.404     1.19   0.238    -2992.214    11851.79
    ------------------------------------------------------------------------------
    
    . esttab m1 using table.txt, indicate("International FE = *.foreign") order(length weight *.foreign headr
    > oom _cons) replace
    (output written to table.txt)
    
    . shell ./process_files.sh table.txt
    
    ----------------------------
    File table.txt has been modified.
    
    . type table.txt
    ----------------------------
                          (1)   
                        price   
    ----------------------------
    length             -81.12*  
                      (-2.44)   
    
    weight              5.749***
                       (6.04)   
    
    headroom           -481.2   
                      (-1.48)   
    
    Internatio~E          Yes   
    
    _cons              4429.8   
                       (1.19)   
    
    ----------------------------
    N                      74   
    ----------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001