Announcement

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

  • Expxport codebook descriptives to latex

    Hello, I have a very simple query that unfortunately I am unable to resolve.

    I am trying to export my descriptive statistics into a latex table. I can easily generate the descriptions with the codebook and sum command, but I am having trouble understanding how to export them.

    Does anyone knows how I can do this??

    here is a replication of the commands.

    Code:
    sysuse auto, clear
    
    codebook make price mpg foreign,compact
    sum make price mpg foreign
    In sum, I would like a latex table that replicates the codebook table, but I would also include the standard deviation in the summary statistic

    thanks a lot for your help

  • #2
    Hi Tom,
    You may want to take a look at asdocx from SSC. To export to tex
    Code:
     asdocx codebook, save(mytab.tex) replace    
     asdocx sum, stat(sd) save(mysd.tex) replace
    Then, with just a little coding in latex, you can add add the standard deviation to the codebook table.
    I forgot to mention that there is a price barrier for asdocx - see https://fintechprofessor.com/asdocx/pricing/
    Best,
    Matt
    Last edited by Matthew Alexander; 27 Dec 2021, 06:24.

    Comment


    • #3
      Thanks I knew about the command, but as you mentioned there is a price barrier. I wondered whether there was free option.

      Comment


      • #4
        Hi Tom,
        Try frmttable from SSC.
        If you are able to create a matrix of codebook values, then you can easily add an SD column and then export to latex.
        At most, I suspect you would need to write a loop over each variable/summary statistic.
        Hope this helps,
        Matt

        Comment


        • #5
          Another alternative - particularly if you want to export string variable labels , which cannot be done using Stata matrices - is to to use the - file - command.
          Personally, I have never used it. But see the suggestions by Martin Buis and Kyle Lippold here https://www.statalist.org/forums/for...rix-of-strings

          Comment


          • #6
            Code:
            cls
            clear all
            sysuse auto
            loc v make price mpg foreign
            loc s count mean sd min max
            loc t qui table(var),
            foreach st of local s {
                foreach vr of local v {
                    loc t "`t' statistic(`st' `vr')"
                }
            }
            `t'
            collect label levels result 1 "N" 2 "Mean" 3 "SD" 4 "Min" 5 "Max", replace
            collect export descstats.tex, tableonly replace

            Comment


            • #7
              An alternative, using Baum and Azevedo's -outtable- from SSC:

              Code:
              tabstat make price mpg foreign, save stat(N mean sd min max) col(stat)
              mat xp = r(StatTotal)'
              outtable using test, mat(xp) replace center nobox format(%5.0f %10.3f) label
              producing test.tex as a fragment.

              Comment


              • #8
                No LaTeX output yet, but it is easy enough to modify codebook, compact for additional statistics:

                Code:
                [
                
                . codebook2 make price mpg foreign
                
                Variable   Obs Unique      Mean        SD     p50   Min    Max  Label
                --------------------------------------------------------------------------------------------------
                make        74     74         .         .       .     .      .  Make and model
                price       74     74  6165.257  2949.496  5006.5  3291  15906  Price
                mpg         74     21   21.2973  5.785503      20    12     41  Mileage (mpg)
                foreign     74      2  .2972973  .4601885       0     0      1  Car origin
                --------------------------------------------------------------------------------------------------

                Comment


                • #9
                  Code:
                  version 17
                  
                  clear all
                  
                  /*  make html/tex codebook with info from describe and summarize
                  
                      1) save describe and
                      2) merge with codebook
                      3) collect summarize add stat from 1 and 2.
                      4) collect layout
                      5) collect export
                  */
                  
                  sysuse auto  
                  
                  tempfile describe
                  
                  frame copy default describe
                  
                  frame describe {
                          
                  describe , replace
                  rename name variable
                  save `describe'
                  
                  } // END frame describe  
                  
                  tempfile cb
                  
                  qui {
                      
                      log using "`cb'" , text replace name(tmp)
                      noi codebook, compact
                      log close tmp
                  }
                  
                  frames create cb
                  
                  frames cb {
                  
                  import delim using "`cb'", clear delimiters(" ", collapse) varnames(1)
                  drop if mi(obs)
                  
                  rename (v#)(labwords#)
                  
                  foreach v of varlist labwords* { // fix splitted varlabels
                      
                      replace label = label + " " + `v'
                      drop `v'
                  }
                  
                  compress
                  merge 1:1 variable using "`describe'" , assert(match) nogen
                  sort position
                  
                  foreach stat in unique obs type format {
                      
                      capture confirm string variable `stat'
                  
                      if ( _rc == 7 ) {    
                          
                          mata : st_local("cb_`stat'", invtokens(strofreal(st_data(.,"`stat'")',"%12.0f")))
                      }
                  
                      else {
                          
                          mata : st_local("cb_`stat'", invtokens(st_sdata(.,"`stat'")'))
                      }
                  }
                  
                  list // codebook + describe
                  
                  } // END frame cb
                  
                  collect clear
                  
                  local i = 0
                  
                  qui foreach v of varlist * { // summarize all vars
                      
                      local ++i
                          
                       su `v'
                      
                       collect get ///
                          r() ///
                          name="`v'" ///
                          label="`:var lab `v''" ///
                          obs="`:word `i' of `cb_obs''" ///
                          unique="`:word `i' of `cb_unique''" ///
                          type="`:word `i' of `cb_type''"  ///
                          format="`:word `i' of `cb_format''"
                  }
                  
                  collect layout (cmdset) (result[name label type format obs unique N mean sd min max])
                  
                  collect export test.html , replace  // or test.tex
                  Code:
                  . collect layout (cmdset) (result[name label type format obs unique N mean sd min max])
                  
                  Collection: default
                        Rows: cmdset
                     Columns: result[name label type format obs unique N mean sd min max]
                     Table 1: 12 x 11
                  
                  ------------------------------------------------------------------------------------------------
                     |         name                  label  type format obs unique  N     mean       sd  min   max
                  ---+--------------------------------------------------------------------------------------------
                  1  |         make         Make and model str18  %-18s  74     74  0                            
                  2  |        price                  Price   int %8.0gc  74     74 74 6165.257 2949.496 3291 15906
                  3  |          mpg          Mileage (mpg)   int  %8.0g  74     21 74  21.2973 5.785503   12    41
                  4  |        rep78     Repair record 1978   int  %8.0g  69      5 69 3.405797 .9899323    1     5
                  5  |     headroom         Headroom (in.) float  %6.1f  74      8 74 2.993243 .8459948  1.5     5
                  6  |        trunk  Trunk space (cu. ft.)   int  %8.0g  74     18 74 13.75676 4.277404    5    23
                  7  |       weight          Weight (lbs.)   int %8.0gc  74     64 74 3019.459 777.1936 1760  4840
                  8  |       length           Length (in.)   int  %8.0g  74     47 74 187.9324 22.26634  142   233
                  9  |         turn      Turn circle (ft.)   int  %8.0g  74     18 74 39.64865 4.399354   31    51
                  10 | displacement Displacement (cu. in.)   int  %8.0g  74     31 74 197.2973 91.83722   79   425
                  11 |   gear_ratio             Gear ratio float  %6.2f  74     36 74 3.014865 .4562871 2.19  3.89
                  12 |      foreign             Car origin  byte  %8.0g  74      2 74 .2972973 .4601885    0     1
                  ------------------------------------------------------------------------------------------------
                  Last edited by Bjarte Aagnes; 28 Dec 2021, 16:46.

                  Comment

                  Working...
                  X