\documentclass{article}
\usepackage{graphicx}
\usepackage{stata}
\usepackage{Statweave}
\begin{document}

\section{Example of a StatWeave document}

\subsection{Some Standard Features}

\subsubsection{Some simple Stata input and output}

Here is a regression example using the obligatory \texttt{auto} dataset, using good physics

\begin{Winput}
. clear
. sysuse auto
. // making the physics right
. gen gp100m = 100/mpg
. regress gp100m weight displacement gear_ratio foreign
\end{Winput}
\begin{Woutput}
(1978 Automobile Data)

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(4, 69)        =     56.84
       Model |  91.7374232         4  22.9343558   Prob > F        =    0.0000
    Residual |  27.8388375        69  .403461414   R-squared       =    0.7672
-------------+----------------------------------   Adj R-squared   =    0.7537
       Total |  119.576261        73  1.63803097   Root MSE        =    .63519

------------------------------------------------------------------------------
      gp100m |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |   .0014428    .000216     6.68   0.000     .0010118    .0018737
displacement |   .0012388   .0021161     0.59   0.560    -.0029828    .0054603
  gear_ratio |  -.2037991   .3258603    -0.63   0.534    -.8538726    .4462744
     foreign |    .733736   .2301493     3.19   0.002     .2746007    1.192871
       _cons |   .8147969   1.239181     0.66   0.513    -1.657301    3.286895
------------------------------------------------------------------------------
\end{Woutput}

\subsubsection{Including a result in the body of a sentence.}

This is useful for including results in sentences in a paper.

StatWeave can use inline expansions. As an example, we can see that the 
coefficient for the foreign variable is 0.734. 
To put something inline, think of it as being fed to a \texttt{display} command.

\subsubsection{Including a graph.}

Here is an example scatterplot command:
\begin{Winput}
. twoway (scatter gp100m weight if !foreign) ///
    (scatter gp100m weight if foreign), ///
    legend(order(1 "US" 2 "Non-US"))
\end{Winput}
Here is the graph.
\begin{center}
\includegraphics{scatter}
\end{center}
Need to work around two corners a little, but not too awful.

\subsubsection{Hiding commands and showing only results}

It can be useful to hide commands and show just their output. Here, for 
example, is the output from a summarize command:

\begin{Woutput}
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      gp100m |         74     5.01928    1.279856   2.439024   8.333333
      weight |         74    3019.459    777.1936       1760       4840
displacement |         74    197.2973    91.83722         79        425
  gear_ratio |         74    3.014865    .4562871       2.19       3.89
\end{Woutput}

Showing just commands works similarly.

Showing just commands or output is done line by line.

\subsubsection{Splitting input and output.}

StatWeave is special because it can directly split input and output from 
Stata commands. This is worthwhile in teaching documents, for the most part:

This command:
\begin{Winput}
. summarize gp100m
\end{Winput}
will give summary statistics about the gp100m variable
\begin{Woutput}
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      gp100m |         74     5.01928    1.279856   2.439024   8.333333
\end{Woutput}


The downside of StatWeave splitting output is that it puts all commands 
before all output for every codeblock. This is very unStata-like.

\begin{Winput}
. summarize weight
. regress gp100m weight
\end{Winput}
\begin{Woutput}
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      weight |         74    3019.459    777.1936       1760       4840

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(1, 72)        =    194.71
       Model |  87.2964969         1  87.2964969   Prob > F        =    0.0000
    Residual |  32.2797639        72  .448330054   R-squared       =    0.7300
-------------+----------------------------------   Adj R-squared   =    0.7263
       Total |  119.576261        73  1.63803097   Root MSE        =    .66957

------------------------------------------------------------------------------
      gp100m |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |    .001407   .0001008    13.95   0.000      .001206    .0016081
       _cons |   .7707669   .3142571     2.45   0.017     .1443069    1.397227
------------------------------------------------------------------------------
\end{Woutput}

This behavior came because StatWeave was originally SASWeave, but was expanded
to handle Stata, SAS, and R.

\subsection{Some other features to check}

\subsubsection{Showing Mata code and output}

Here is some Mata code. 

\begin{Winput}
. mata
: X = (76, 53, 48 \ 53, 88, 46 \ 48, 46, 63)
: Xi = invsym(X)
\end{Winput}
\begin{Woutput}
------------------------------------------------- mata (type end to exit) ------
\end{Woutput}

It is useful to see the output from checking that Xi is really the inverse of X.

\begin{Winput}
: Xi*X
: end
\end{Winput}
\begin{Woutput}
                  1              2              3
    +----------------------------------------------+
  1 |             1   -1.11022e-16   -1.11022e-16  |
  2 |  -1.11022e-16              1              0  |
  3 |             0              0              1  |
    +----------------------------------------------+

--------------------------------------------------------------------------------
\end{Woutput}

\end{document}
