Incorporating Stata into reproducible documents

Hua Peng@StataCorp

2017 Stata Conference in Baltimore

Why reproducible documents?

Eliminate manual steps such as hand-editing documents

  • mix formatted text and Stata output
  • include inline Stata results
  • embed Stata graphs
  • produce tables containing the output from selected Stata commands

Commands in Stata 15

  • dyndoc - convert dynamic Markdown documents to web pages
  • putdocx - create Word documents
  • putpdf - create PDF files

A dyndoc example

Produce a blog article from a dynamic Markdown document

Quick review of dynamic tags

dd_do for a block of Stata code

<<dd_do>>
sysuse auto
regress weight displacement
<</dd_do>>

. sysuse auto
(1978 Automobile Data)

. regress weight displacement

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(1, 72)        =    289.52
       Model |  35312313.3         1  35312313.3   Prob > F        =    0.0000
    Residual |  8781865.08        72  121970.348   R-squared       =    0.8008
-------------+----------------------------------   Adj R-squared   =    0.7981
       Total |  44094178.4        73  604029.841   Root MSE        =    349.24

------------------------------------------------------------------------------
      weight |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
displacement |   7.573261   .4450891    17.02   0.000     6.685992     8.46053
       _cons |   1525.276   96.74555    15.77   0.000     1332.417    1718.134
------------------------------------------------------------------------------

Attributes change a tag's behavior

<<dd_do:quietly>>
matrix define eb = e(b)
<</dd_do>>

dd_display for inline Stata results

  • For every unit increase in displacement, a <<dd_display:%9.4f eb[1,1]>> unit increase in weight is predicted.
  • For every unit increase in displacement, a 7.5733 unit increase in weight is predicted.

dd_graph

<<dd_do:quietly>>
scatter weight displacement, mcolor(%30)
<</dd_do>>
<<dd_graph>>

putdocx

Produce a .docx document from a do-file

  • full range of control for text formatting
  • flexible table output

Output tables in Markdown

Produce Markdown tables from commands, including

  • all estimation commands using _coef_table
  • table
  • estimates table

dyntext

Process dynamic tags in any text files, for example, LaTeX and JavaScript

LaTeX file

Produce a .pdf file from a LaTeX file with dynamic tags. It requires pdflatex and Stata command sjlog

dyntext extex.md, sav(extex.tex) replace
!pdflatex extex.tex

JavaScript

The following code generates a scatter plot between mpg and weight

sysuse auto, clear
dyndoc gchart1.txt mpg weight, replace

Use pandoc instead of Stata's markdown

Produce reveal.js slildes deck use pandoc. It requires pandoc and user written Stata commands dynpandoc and pandocmarkdown.

dynpandoc birds17.md, sav(birds17.html) replace to(revealjs)    /// 
  pargs(-s --template=revealjs.html --standalone                /// 
  --section-divs --variable theme="stata")

putpdf

Produce a .pdf document from a do-file

TODO

  • cached code block
  • better LaTeX support
  • Markdown support in the do-file editor

THE END