Announcement

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

  • Format regression coefficients using file write

    I am outputting non-standard tables to TeX and using file write following this example: https://github.com/worldbank/stata-t...o/filewrite.do

    Here is an MWE with the good old car dataset. I loop over two outcomes as the issue occurs only with multiple columns

    Code:
    sysuse auto.dta
    
    global outcomes price mpg
    global X weight length
    
    local n_outcomes : word count $outcomes
    forval i = 1/`n_outcomes' {
        
        local Y `: word `i' of $outcomes'
        local outcome_`i' = "`Y'"
        
        reg `Y' $X
        mat result = r(table)
        
        scalar b_`i' = result[1,1]
        scalar SE_`i' = result[1,2]
        scalar p_`i' = result[1,4]
    
        local sig_`i' = ""
    
        if p_`i' <= 0.1 {
        local sig_`i' = "*"
        }
    
        if p_`i' <= 0.05 {
        local sig_`i' = "**"
        }
    
        if p_`i' <= 0.01 {
        local sig_`i' = "***"
        }
    }
    
    
    * Table
    capture file close sampleTable
    file open sampleTable using "sample.tex", write replace
    file write sampleTable ///
        "\begin{tabular}{lc}"                                                        _n ///
        "\hline \hline \\ [-1.8ex]"                                                    _n ///
        " & `outcome_1' & `outcome_2' \\"                                            _n ///
        "\hline \\ [-1.8ex]"                                                        _n ///
            " &" %9.3f (b_1) "`sig_1' & " %9.3f (b_2) "`sig_2' \\"                     _n ///
            " & ("%9.3f (SE_1) ") & (" %9.3f (SE_2) ") \\"                            _n ///
        "\hline\hline"                                                                _n ///
        "\end{tabular}"
    file close sampleTable

    The problem is that the formatting part %9.3f adds a space in the final .tex file (see below) which is annoying if I have to change that manually. Any suggestion how I can change this within the do-file?

    Code:
    \begin{tabular}{lc}
    \hline \hline \\ [-1.8ex]
    & price & mpg \\
    \hline \\ [-1.8ex]
    & 4.699*** & -0.004*** \\
    & ( -97.960) & ( -0.080) \\
    \hline\hline
    
    \end{tabular}

  • #2
    You are allowing for 9 digits before the decimal point, which apparently you do not need. Either specify %4.3f or specify a string format (outputting the coefficients as strings).

    Code:
    di "(`:di %9.3f 1.28239893')"
    di "(`:di %4.3f 1.28239893')"
    Res.:

    Code:
    . di "(`:di %9.3f 1.28239893')"
    (    1.282)
    
    . di "(`:di %4.3f 1.28239893')"
    (1.282)
    Last edited by Andrew Musau; 09 Dec 2021, 03:59.

    Comment


    • #3
      Thank you for the explanation! The solution is however not ideal. I wouldn't want to have to anticipate how many digits my estimates will have. Is there any other way?

      Comment


      • #4
        As I stated in #2, you can output as a string. Pass it through the -string()- function.

        Code:
        di "(`:di %15.3f 1.28239893')"
        di "(`:di %15.3f 1212.28239893')"
        di "(`:di %15.3f 777777777.28293')"
        di "(`=string(1.28239893,"%15.3f")')"
        di "(`=string(1212.28239893,"%15.3f")')"
        di "(`=string(777777777.28293,"%15.3f")')"
        Res.:

        Code:
        
        . di "(`:di %15.3f 1.28239893')"
        (          1.282)
        
        . 
        . di "(`:di %15.3f 1212.28239893')"
        (       1212.282)
        
        . 
        . di "(`:di %15.3f 777777777.28293')"
        (  777777777.283)
        
        . 
        . di "(`=string(1.28239893,"%15.3f")')"
        (1.282)
        
        . 
        . di "(`=string(1212.28239893,"%15.3f")')"
        (1212.282)
        
        . 
        . di "(`=string(777777777.28293,"%15.3f")')"
        (777777777.283)
        Last edited by Andrew Musau; 09 Dec 2021, 04:47.

        Comment


        • #5
          strange, when I included that in the MWE I still got the gap.

          Comment


          • #6
            Can you present the full code?

            Comment


            • #7
              You are right. I have no idea why we need so many quotation marks(?), but this code following your suggestion

              Code:
              cap erase "sample.tex"
              
              sysuse auto.dta
              
              global outcomes price mpg
              global X weight length
              
              local n_outcomes : word count $outcomes
              forval i = 1/`n_outcomes' {
                  
                  local Y `: word `i' of $outcomes'
                  local outcome_`i' = "`Y'"
                  
                  reg `Y' $X
                  mat result = r(table)
                  
                  scalar b_`i' = result[1,1]
                  scalar SE_`i' = result[1,2]
                  scalar p_`i' = result[1,4]
              
                  local sig_`i' = ""
              
                  if p_`i' <= 0.1 {
                  local sig_`i' = "*"
                  }
              
                  if p_`i' <= 0.05 {
                  local sig_`i' = "**"
                  }
              
                  if p_`i' <= 0.01 {
                  local sig_`i' = "***"
                  }
              }
              
              
              * Table
              capture file close sampleTable
              file open sampleTable using "sample.tex", write replace
              file write sampleTable ///
                  "\begin{tabular}{lc}"                                                                                                _n ///
                  "\hline \hline \\ [-1.8ex]"                                                                                          _n ///
                  " & `outcome_1' & `outcome_2' \\"                                                                         _n ///
                  "\hline \\ [-1.8ex]"                                                                                                    _n ///
                      " &" %9.3f (b_1) "`sig_1' & " %9.3f (b_2) "`sig_2' \\"                                           _n ///
                      " & (" "`=string(SE_1, "%15.3f")'" ") & ("  "`=string(SE_2, "%15.3f")'" ") \\"          _n ///
                  "\hline\hline"                                                                                                            _n ///
                  "\end{tabular}"
              file close sampleTable
              works to remove the space. Here is the tex document.

              Code:
              \begin{tabular}{lc}
              \hline \hline \\ [-1.8ex]
              & price & mpg \\
              \hline \\ [-1.8ex]
              & 4.699 & -0.004 \\
              & (-97.960) & (-0.080) \\
              \hline\hline
              \end{tabular}
              Thank you for your help!

              Comment


              • #8
                Deleted

                Comment

                Working...
                X