Announcement

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

  • Exporting output of "margins... , pwcompare(groups)" to a LaTeX table

    Dear all,

    how can I export the output of a margins call including pwcompare(groups) to a LaTeX table?
    Specifically, this command saves strings into "r(group#)" which would need to be contained in the table, also.

    I tried saving the "r(group#)"'s into a matrix (as recommended here: http://repec.org/bocode/e/estout/adv...ml#advanced906).
    But this did not work, as matrices only allow numeric values.

    I tried using the option ", post", which seemingly does not save the results of "r(group#)" into some "e(group#)" that I could call with esttab.

    A reproducible example:

    Code:
    sysuse auto
    reg i.rep78##i.foreign, r
    margins i.rep78#i.foreign, pwcompare(groups)
    How now to save the last output including the strings from the column "unadjusted groups" into a publication-style TeX-table?

    Thanks so much,

    Max



  • #2
    If you have Stata 17 or newer you can use collect to build custom
    tables and export them to LaTeX.

    Here is how you can do this using the above example.
    Code:
    sysuse auto
    
    * you left out the depvar in your example, so I chose mpg :)
    regress mpg rep78##foreign, vce(robust)
    
    * for this model you can use -pwcompare- directly, but I will keep with
    * your use of -margins, pwcompare()-
    collect: margins rep78#foreign, pwcompare(groups)
    
    * associate each groups# result with its predictive margin
    local stripe : colname r(table)
    local k : list sizeof stripe
    forval i = 1/`k' {
        gettoken name stripe : stripe
        collect addtag colname[`name'], fortags(result[groups`i'])
        local glist `glist' groups`i'
    }
    * put all the groups# results in a composite result so we can
    * arrange them in a single column
    collect composite define groups = `glist'
    
    * label the groups
    collect label levels result groups "Unadjusted groups", modify
    
    * turn off the factor variable binder
    collect style row stack, nobinder
    
    * arrange the results of interest
    collect layout (colname) (result[_r_b _r_se groups])
    
    * export to LaTeX
    collect export groups.tex, replace
    Here is the resulting table.
    Code:
    --------------------------------------------------------------------------
                                    | Coefficient Std. error Unadjusted groups
    --------------------------------+-----------------------------------------
    Repair record 1978 # Car origin |
      1 # Domestic                  |          21   2.256139                AB
      1 # Foreign                   |           .          .
      2 # Domestic                  |      19.125   1.321943                 A
      2 # Foreign                   |           .          .
      3 # Domestic                  |          19   .8206174                 A
      3 # Foreign                   |    23.33333   1.261739                 B
      4 # Domestic                  |    18.44444   1.532706                 A
      4 # Foreign                   |    24.88889   .9068465                 B
      5 # Domestic                  |          32   1.504093                 C
      5 # Foreign                   |    26.33333   3.131019                BC
    --------------------------------------------------------------------------
    I used pdflatex to produce a PDF from groups.tex, here is a screenshot of the table from Preview app on my Mac.


    Click image for larger version

Name:	groups.png
Views:	1
Size:	89.1 KB
ID:	1747292

    Comment


    • #3
      Dear Jeff,
      thanks so much for the reply, this is much appreciated. The output looks exactly how I envisioned it. I'll try to adapt to my actual project and hope this works. (With a dependent variable, of course )
      Best

      Comment

      Working...
      X