Announcement

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

  • Split tables displaying summary and correlations created by putdocx at specific column and display the rest on the next page.

    I have created the following summary table containing the mean, sd, min and max as well as the correlations for a range of variables in Stata. I have used putdocx to export the results to Word.

    Code:
    putdocx begin
    putdocx sectionbreak, landscape
    putdocx paragraph, style(Heading2)
    putdocx text ("Table 1: Correlation table for 1970-2019")
    pwcorr GDPppp popsize electricityaccess GDPgrowth unemploymentrate inflation fdi trade grosssavings privatesectorcredit capitalformation, sig
    matrix sig = vech(r(sig))'
    table (result coleq) (colname), statistic(freq) statistic(mean GDPppp popsize electricityaccess GDPgrowth unemploymentrate inflation fdi trade grosssavings privatesectorcredit capitalformation) statistic(sd GDPppp popsize electricityaccess GDPgrowth unemploymentrate inflation fdi trade grosssavings privatesectorcredit capitalformation) statistic(min GDPppp popsize electricityaccess GDPgrowth unemploymentrate inflation fdi trade grosssavings privatesectorcredit capitalformation) statistic(max GDPppp popsize electricityaccess GDPgrowth unemploymentrate inflation fdi trade grosssavings privatesectorcredit capitalformation) command(corr=(vech(r(C))'): pwcorr  GDPppp popsize electricityaccess GDPgrowth unemploymentrate inflation fdi trade grosssavings privatesectorcredit capitalformation) nformat(%5.2f mean sd)
    collect style cell result[corr], maximum(0.99, label(" - ")) nformat(%6.2f) halign(center)
    local stripe : colfullname sig
    foreach el of local stripe {
      if sig[1,"`el'"] < 0.05 {
        tokenize "`el'", parse(":")
        collect style cell result[corr]#coleq[`1']#colname[`3'], ///
          font(, bold) smcl(error)
      }
    }
    collect stars sig 0.01 "***" 0.05 "**" 0.1 "*", attach(corr) shownote
    collect layout (colname) (result[freq mean sd min max] coleq#result[corr])
    collect style cell result, nformat(%8.2g) halign(center)
    collect style header result[corr], level(hide)
    collect label levels result sd "SD", modify
    collect label levels result min "Min.", modify
    collect label levels result max "Max.", modify
    collect label levels colname GDPppp "1. GDP" popsize "2. Population size" electricityaccess "3. Electricity access" GDPgrowth "4. GDP Growth" unemploymentrate "5. Unemployment rate" inflation "6. Inflation" fdi "7. Foreign direct investment" trade "8. Trade" grosssavings "9. Gross savings" privatesectorcredit "10. Private sector credit" capitalformation "11. Capital formation", modify
    collect label levels coleq GDPppp "1. GDP" popsize "2. Population size" electricityaccess "3. Electricity access" GDPgrowth "4. GDP Growth" unemploymentrate "5. Unemployment rate" inflation "6. Inflation" fdi "7. Foreign direct investment" trade "8. Trade" grosssavings "9. Gross savings" privatesectorcredit "10. Private sector credit" capitalformation "11. Capital formation", modify
    collect preview
    putdocx collect
    putdocx save Correlation, replace
    In Word, I am manually splitting the Table at the column Unemployment rate vertically to move the rest of the correlation table to the next page, add the first column again, so that the correlation table is readable when fit onto one page. Is there a way to automize this in putdocx? I have read the manual, but I only found that headerrow(1) option.
    __________________________________________________ __________

    Cheers, Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    It sounds like one way to go about this would be to programatically split your table at an arbitrary column. Here is an example that looks a bit like what you are doing. I create a new table dimension and tag each value so that it appears in the correct table. Hopefully this get's you started.

    Code:
    clear
    set obs 1000
    collect clear
    
    gen a = runiform()
    gen b = runiform()
    gen c = runiform()
    gen x = runiform()
    gen y = runiform()
    gen z = runiform()
    
    table (result coleq) (colname), statistic(freq) statistic(mean a b c x y z) ///
                                    statistic(sd a b c x y z) ///
                                    statistic(min a b c x y z) ///
                                    statistic(max a b c x y z) ///
                                 command(corr=(vech(r(C))'): pwcorr a b c x y z) ///
                                    nformat(%5.2f mean sd)
                                    
    collect style cell result[corr], maximum(0.99, label(" - ")) ///
                                     nformat(%6.2f) halign(center)
    
    collect addtags table[t1], fortags(result[freq mean sd min max] ///
                                       coleq[a b c]#result[corr])
    collect addtags table[t2], fortags(coleq[x y z]#result[corr])
    
    collect layout (colname) (result[freq mean sd min max] coleq#result[corr]) (table)
    Code:
    . collect layout (colname) (result[freq mean sd min max] coleq#result[corr]) (table)
    
    Collection: Table
          Rows: colname
       Columns: result[freq mean sd min max] coleq#result[corr]
        Tables: table
       Table 1: 6 x 7
       Table 2: 3 x 3
    
    table = t1
    --------------------------------------------------------------------------------------
      |  Mean   Standard deviation   Minimum value   Maximum value       a       b       c
      |                                                               corr    corr    corr
    --+-----------------------------------------------------------------------------------
    a |  0.51                 0.29        .0023002        .9990784     -                  
    b |  0.51                 0.28        .0055892         .999611    0.04     -          
    c |  0.49                 0.29        .0014939        .9998347    0.01    0.02     -  
    x |  0.50                 0.30        .0005608        .9999558    0.02   -0.02    0.00
    y |  0.51                 0.28        .0003897        .9987042    0.01   -0.03    0.03
    z |  0.51                 0.28        .0011517        .9994027   -0.04   -0.02   -0.02
    --------------------------------------------------------------------------------------
    
    table = t2
    ------------------------
      |      x      y      z
      |   corr   corr   corr
    --+---------------------
    x |    -                
    y |   0.03     -        
    z |  -0.04   0.03     - 
    ------------------------

    Comment


    • #3
      Daniel has helpfully shown a technique.

      May I suggest instead that you reconsider how you have your table laid out? Having a table split vertically, that is, having unique columns on different pages makes for awkward reading.

      You could instead produce two different tables, such as one with the typical summary statistics and a second treble with just the correlation matrix. This is the cleanest and simplest approach.

      Another alternative could be to reshape the table to have variables across the columns (if not too many to fit on a page) then list out summary statistics and correlations below.

      Comment


      • #4
        Thank you Daniel Schaefer, that worked like a charm!
        Leonardo Guizzetti, I agree with you. But sometimes, it's not entirely up to you. I have seen cases where the table was split in the past, so I wanted to know how to make it possible.
        __________________________________________________ __________

        Cheers, Felix
        Stata Version: MP 18.0
        OS: Windows 11

        Comment

        Working...
        X