Announcement

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

  • Add horizontal line to table in latex using esttab

    Hello,

    This involves the use of the SSC available command esttab (available via SSC install estout).

    I have added some scalars to a table of regression coefficients, and then the observations after that (via the obslast option). I would like to add a horizontal line after the scalars (before the observations) in my latex table.

    I can figure out how to add the line before or after a variable name via varlabels(, blist(var1 "\hline"))

    The example here: http://repec.org/bocode/e/estout/estpost.html under " Post a one-way frequency table (tabulate)" adds a horizontal line after Total - which is not a variable per se in the table.

    How can add one before "Observations" in a regression table? That is, is there a way to refer to the obversations line where var1 is in the varlabels command above?

    Here is a working example with sysuse auto:

    sysuse auto, clear

    eststo: reg price mpg trunk

    estadd scalar sum=_b[mpg]+_b[trunk]

    esttab using test.tex, booktabs scalars("sum Sum") obslast varlabels(, blist(N "\hline")) replace


    I want a horizontal line between "Sum" and "N" in the table. Here the varlabels option isn't actually doing anything....

    This is pretty in the weeds but if anyone could help it would stop my hourlong obsession with trying to figure this out

    Thanks,
    Amanda

  • #2
    I dont know esttab, but you might edit the tex file using -filefilter-, Note the \W (Windows EOL):
    Code:
    filefilter test.tex test2.tex , from("\BS(N\BS)") to("\BSmidrule\W\BS(N\BS)")
    Then, test2.tex have:
    Code:
    \midrule
    Sum         &      -176.6         &      -176.6         \\
    \midrule
    \(N\)       &          74         &          74         \\
    \bottomrule

    Comment


    • #3
      Amazing, this definitely worked. Thanks. Would be great if anyone still had a way to do it directly in esttab but this is nice and now I've learned about filefilter for doing any other crazy stuff I want to do!

      Comment


      • #4
        As far as I can tell, there is no restriction on what you can include as a scalar in -esttab- and therefore, nothing stops you from being explicit

        Code:
        esttab using test.tex, booktabs scalars("sum Sum" "k \hline") obslast replace
        Note that the "trick" here is that I define a non-existent scalar "k" whose display entry is the horizontal line.

        Comment


        • #5
          Hi Andrew Musau,

          I am trying to apply what you mentioned here. Is this also possible without scalars?

          I would like to introduce a line separating my lincom results from the rest in my regression output but cannot figure out where to insert the "k \hline".

          Would greatly appreciate it if you have any tips for this.



          Code:
          estimates clear
          local c = 1
          foreach depvar in Too_Cheap Too_Exp {
              eststo e`c': qui reg `depvar' i.Soc##i.Env Attitude 
              lincom _b[1.Soc] + _b[1.Soc#1.Env]
              local r`c' = r(estimate)
              local s`c' = r(se)
              local p`c' = r(p)
              estimates restore e`c'
              lincom _b[1.Env] + _b[1.Soc#1.Env]
              local z`c' = r(estimate)
              local x`c' = r(se)
              local y`c' = r(p)
              estadd local pr "`= cond(`p`c''<0.01,"`:di %5.3f `=`r`c''''***", cond(`p`c''<0.05,"`:di %5.3f `=`r`c''''**", cond(`p`c''<0.1,"`:di %5.3f `=`r`c''''*",  "`:di %5.3f `=`r`c''''")))'"
              estadd local sr "(`:di %5.3f `=`s`c'''')"
              estadd local zr "`= cond(`y`c''<0.01,"`:di %5.3f `=`z`c''''***", cond(`y`c''<0.05,"`:di %5.3f `=`z`c''''**", cond(`y`c''<0.1,"`:di %5.3f `=`z`c''''*",  "`:di %5.3f `=`z`c''''")))'"
              estadd local xr "(`:di %5.3f `=`x`c'''')"
          local c = `c'+1
          }
          esttab e1 e2, se append keep(1.Soc 1.Env 1.Soc#1.Env Attitude) b(2)             ///
          star(* 0.10 ** 0.05 *** 0.01) stats( pr sr zr xr r2 F N)                                  ///
          coef(1.Soc "Social" 1.Env "Environmental" 1.Soc#1.Env "Interaction" Attitude "Attitude")
          Click image for larger version

Name:	Screen Shot 2023-02-22 at 15.38.43.png
Views:	1
Size:	33.6 KB
ID:	1702917

          Comment


          • #6
            Same principle as in #4.

            Code:
            esttab e1 e2, se append keep(1.Soc 1.Env 1.Soc#1.Env Attitude) b(2) ///
            star(* 0.10 ** 0.05 *** 0.01) stats(pr sr zr xr k r2 F N, labels(pr " " zr " " \hline "R-squared" "F-statistic" "Observations" )) ///
            coef(1.Soc "Social" 1.Env "Environmental" 1.Soc#1.Env "Interaction" Attitude "Attitude")

            Comment


            • #7
              Hello Andrew Musau I would like to get your suggestion : I want to add Village FE row under midrule in the follow code, how to do it
              Code:
              est clear 
              eststo: regress  credit_score  $dmo i.vill_name, vce(cluster vill_name)
              
              esttab using "$cb\reg_predit_credit_score.tex", replace /// 
              keep($dmo)  stats(N r2 , fmt(0 3)  labels("Observations" "\(R^{2}\)")) ///
               b(3) se(3) mtitle("Credit score") label star(* 0.10 ** 0.05 *** 0.01) /// 
               booktabs nonotes

              Comment


              • #8
                Code:
                est clear
                eststo: regress credit_score $dmo i.vill_name, vce(cluster vill_name)
                estadd local VE "Yes"
                
                esttab using "$cb\reg_predit_credit_score.tex", replace ///
                keep($dmo) stats(N r2 VE, fmt(0 3) labels("Observations" "\(R^{2}\)" "Village Effects")) ///
                b(3) se(3) mtitle("Credit score") label star(* 0.10 ** 0.05 *** 0.01) ///
                booktabs nonotes

                Comment


                • #9
                  Thanks, Andrew Musau , I would like to request your help or suggestions to this post:https://www.statalist.org/forums/for...96#post1744196

                  Comment


                  • #10
                    Originally posted by Thein Zaw View Post
                    Thanks, Andrew Musau , I would like to request your help or suggestions to this post:https://www.statalist.org/forums/for...96#post1744196
                    I've already replied to your question in that thread. There's no need to tag me in a different thread. Firstly, tagging a specific person reduces the likelihood of others responding to your question. Secondly, I check the forum almost daily, so if I see your question and I'm willing and able to respond, I'll do so.

                    Comment

                    Working...
                    X