\setlength{\pdfpagewidth}{11in}
\setlength{\pdfpageheight}{8.5in}
\documentclass[landscape]{slides}
\usepackage{graphicx}
\begin{document}

\begin{slide}

{\bf Introduction to Stata}

{\em Christopher F Baum

Faculty Micro Resource Center

Academic Technology Services, Boston College 

August 2004}

{\tt baum@bc.edu}

\texttt{http://fmwww.bc.edu/GStat/docs/StataIntro.pdf}

{\em Thanks to Petia Petrova and Nicholas J.\ Cox for their comments on this draft.}

\end{slide}


\begin{slide}

What is {\it Stata}? And why should you care?

Stata is a full-featured statistical programming language for Windows, Macintosh, UNIX, and Linux. It can be considered a ``stat package,'' like SAS, SPSS, RATS, or eViews. Stata has traditionally been a command-line-driven package that operates in a graphical (windowed) environment. The latest release---Stata version 8.2 (October 2003)---introduced a graphical user interface (GUI) for
command entry. Stata may also be used in a command-line environment on a shared system (e.g. UNIX) if you do not have a graphical interface to that system. The number of variables is limited to 2,047 in standard (Intercooled) Stata, but can be much larger in Stata/SE. The number of observations is limited only by memory.

\end{slide}

\begin{slide}

{\bf Portability}

Stata is eminently portable, and its developers committed to cross-platform compatibility. Stata runs the same way on Windows, Macintosh, UNIX, and Linux systems. The only platform-specific aspects of using Stata are those related to native operating system commands: e.g. is that file 
\begin{verbatim}
C:\Stata\StataData\myfile.dta 
or 
/u/baum/statadata/myfile.dta
\end{verbatim}
And--perhaps unique among statistical packages--Stata's binary data files may be freely copied from one platform to any other, or even accessed over the Internet from any machine that runs Stata.


\end{slide}



\begin{slide}

{\bf Strengths of Stata: Data Manipulation}

Stata is advertised as having three major strengths: data manipulation, statistics, and graphics. 
Stata is an excellent tool for data manipulation: moving data from external sources into the program, cleaning it up, generating new variables, generating summary data sets, merging data sets and checking for merge errors, collapsing cross--section time--series data on either of its dimensions, reshaping data sets from ``long'' to ``wide''... In this context, Stata is an excellent program for answering ad hoc questions about any aspect of the data. 


\end{slide}

\begin{slide}

{\bf Strengths of Stata: Statistics}

In terms of statistics, Stata provides all of the standard univariate, bivariate and multivariate statistical tools, from descriptive statistics and t-tests through one-, two- and N-way ANOVA, regression, principal components, and the like. It has a very powerful set of techniques for the analysis of limited dependent variables: logit, probit, ordered logit and probit, multinomial logit, and the like. Stata's regression capabilities are full-featured, including regression diagnostics, prediction, robust estimation of standard errors, instrumental variables / two-stage least squares, seemingly unrelated regressions, vector autoregressions and error correction models, etc. 

\end{slide}

\begin{slide}

{\bf Specialized Statistical Techniques}

Stata's breadth and depth really shines in terms of its specialized statistical 
capabilities. These include environments for time--series econometrics (ARCH, ARIMA, VAR, VEC), model simulation and bootstrapping, maximum likelihood estimation, and nonlinear least squares. Families of commands provide the leading techniques utilized in each of several categories: the ``\texttt{xt}'' commands for cross-section/time-series or panel (longitudinal) data; the ``\texttt{svy}'' commands for the handling of survey data with complex sampling designs; and the ``\texttt{st}'' commands for the handling of survival-time data with duration models. 


\end{slide}

\begin{slide}

{\bf Strengths of Stata: Graphics}

Stata graphics have been extensively improved and
enhanced in version 8. They are excellent tools for exploratory data analysis, and can produce high--quality 2-D publication-quality graphics in several dozen different forms. Every aspect of graphics may be programmed and customized, and new graph types and graph ``schemes'' are being continuously developed. The programmability of graphics implies that a number of similar graphs may be generated without any ``pointing and clicking'' to alter aspects of the graphs. Stata does not have 3-D graphics capabilities, but those are under development in the new graphics system. 

\end{slide}

\begin{slide}

{\bf Cost, Availability and Support}

And last (but certainly not least) Stata is very inexpensive! Their GradPlan program makes the full version of Stata version 8 software available to BC faculty and students for \$89.00 (one--year license for students) or \$129.00 (perpetual license for faculty) with various options for purchasing documentation. A quite thorough set of documentation is available for \$129.00 (a 4-volume reference manual and user's guide). The ``Small Stata'' version is available to students for \$39.00 for a one--year license; it will handle a limited number of observations and variables, but contains all of Stata's commands (not recommended for Ph.D.\ students). GradPlan orders are made direct to Stata, with delivery from on--campus inventory.

\end{slide}

\begin{slide}

Stata is very well supported by telephone and email technical support, as well as the more informal support provided by other users on \textbf{StataList}, the Stata listserv. The manuals are useful--particularly the User's Guide--but full details of the command syntax are available online, and in hypertext form in the GUI environment. The command \texttt{findit} \textit{keyword} can also be used to locate Stata materials, including descriptions of built--in commands, Stata FAQs, and hundreds of user--written routines.

\end{slide}

\begin{slide}

{\bf But why should I type commands?}

But before we discuss the specifics to back up these claims, let's consider a meta-issue: why would you want to learn how to use a command-line-driven package? Isn't that ever so 20th century?

Stata may be used in an interactive mode, and those learning the package may wish to make use of the version 8 menu system. But when you execute a command from a pull--down menu, it records the command that you could have typed in the Review window, and thus you may learn that with experience you could type that command (or modify it and resubmit it) more quickly than by use of the menus.

Let us consider a couple of reasons why a command-line-driven package makes for 
an effective and efficient research strategy.

\end{slide}

\begin{slide}

{\bf Reproducibility}

First, the important issue of reproducibility. If you are conducting 
scientific research, you must be able to reproduce your results. Ideally, 
anyone with your programs and data should be able to do so without your 
assistance. If you cannot produce such reproducible research findings, it can 
be argued that you are not following the scientific method, nor is your work conforming to ethical standards of research.

A thorough discussion of this issue is covered in our webpage,
{\tt http://fmwww.bc.edu/GStat/docs/pointclick.html}.

\end{slide}

\begin{slide}

In a computer program where all actions are point and click, such as a 
spreadsheet, who can say how you arrived at a certain set of results? Unless 
every step of your transformations of the data can be retraced, how can you 
find exactly how the sample you are employing differs from the raw data? A 
command-driven program is capable of this level of reproducibility, and one 
could argue that we owe it to our students to instill this level of rigor in 
their research practices.

Reproducibility also makes it very easy to perform an alternate analysis of a particular model. What would happen if we added this interaction, or introduced this additional variable, or decided to handle zero values as missing? Even if many steps have been taken since the basic model was specified, it is easy to go back and produce a variation on the analysis if all the work is represented by a series of programs.

\end{slide}

\begin{slide}

Stata makes this reproducibility very easy through a log facility, the 
ability to generate a command log (containing only the commands you have entered), and a ``do-file editor'' which allows you to easily enter, execute and save ``do-files'': sequences of commands, or program fragments. There is also an elaborate hypertext-based help browser, providing complete access to commands' descriptions and examples of syntax. Each of these components appears in a separate window on the screen in the GUI version of Stata.

\end{slide}

\begin{slide}
 
{\bf Extensibility}
 
Another clear advantage of the command-line driven environment is its interaction with the continual expansion of Stata's capabilities. A command, to Stata, is a verb instructing the program to perform some action.

Commands may be ``built in'' commands--those elements so frequently used that they have been coded into the ``Stata kernel.'' A relatively small fraction of the total number of official Stata commands are built in, but they are used very heavily.

\end{slide}

\begin{slide}

The vast majority of Stata commands are written in Stata's own programming language--the ``ado-file'' language. If a command is not built in to the Stata kernel, Stata searches for it along the ``adopath''. Like the \texttt{PATH} in Unix, Linux or DOS, the \texttt{adopath} indicates the several directories in which an ado-file might be located. This implies that the ``official'' Stata commands are not limited to those coded into the kernel. If Stata's developers tomorrow wrote a command named ``concatenate'', they would make two files available on their web site: \texttt{concatenate.ado} (the ado-file code) and \texttt{concatenate.hlp} (the associated help file). Both are straight ASCII text: these files should be produced in a text editor, not a word processing program.

\end{slide}

\begin{slide}

{\bf Update facility}

One of the great strengths of Stata versions 6, 7 and 8 is that they may be updated over the Internet. Stata is actually a web browser, so it may contact Stata's web server and enquire whether there are more recent versions of either Stata's executable (the kernel) or the ado-files. The kernel is updated relatively infrequently--once a month at most--but the ado-files may be modified every ten days or so. This enables Stata's developers to distribute bug fixes, enhancements to existing commands, and even entirely new commands during the lifetime of a given release. Updates during the life of the version you own are free; you need only have a licensed copy of Stata and access to the Internet (which may be by proxy server) to check for and, if desired, download the updates.

\end{slide}

\begin{slide}

{\bf User extensibility: the Stata Journal}

The importance of this program design goes far beyond the limits of official Stata. Since the adopath includes both Stata directories and other directories on your hard disk (or on a server's filesystem), you may acquire new Stata commands from a number of web sites. The {\it Stata Journal} (SJ), a quarterly refereed journal, is the primary method for distributing user contributions. Between 1991 and 2001, the {\it Stata Technical Bulletin} played this role, and a complete set of issues of the STB Reprints are available in O'Neill Library.

\end{slide}

\begin{slide}

The SJ is a subscription publication (and available at O'Neill Library: see Quest), but the ado- and hlp-files may be freely downloaded from Stata's web site. The Stata command \texttt{help} accesses help on all installed commands; the Stata command \texttt{findit} will locate commands that have been documented in the STB and the SJ, and with one click you may install them in your version of Stata. Help for these commands will then be available in your own Stata.

\end{slide}

\begin{slide}

{\bf User extensibility: the SSC archive}

But this is only the beginning. Stata users worldwide participate in the \textbf{StataList} listserv, and when a user has written and documented a new general-purpose command to extend Stata functionality, they announce it on the \texttt{StataList} listserv (to which you may freely subscribe: see Stata's web site). Since September 1997, all items posted to \texttt{StataList} (over 600) have been placed in the Boston College Statistical Software Components (SSC) Archive in \textbf{RePEc}, available from IDEAS ({\tt http://ideas.repec.org}) and EconPapers ({\tt http://econpapers.repec.org}).

\end{slide}

\begin{slide}
Any component in the SSC archive may be readily inspected with a web browser, using IDEAS' or EconPapers' search functions, and if desired you may install it with one command from the archive from within Stata. For instance, if you know there is a module in the archive named ``omninorm,'' you could use {\tt ssc install omninorm} to install it. Anything in the archive can be accessed via Stata's {\tt ssc} command: thus {\tt ssc describe omninorm} will locate this module, and make it possible to install it with one click. 

Windows users should not attempt to download the materials from a web browser; it won't work.

\end{slide}

\begin{slide}

The importance of all this is that Stata is {\bf infinitely extensible}. Any ado-file on your {\tt adopath} is a full-fledged Stata command. Stata's capabilities thus extend far beyond the official, supported features described in the Stata manual to a vast array of additional tools. Since the current directory is on the {\tt adopath}, if I create an ado--file {\bf hello.ado}:

\begin{verbatim}
program define hello
display "hello from Stata!"
end 
exit
\end{verbatim}

Stata will now respond to the command {\tt hello}. It's that easy.

\end{slide}

\begin{slide}

{\bf Command syntax}

Let us consider the form of Stata commands. One of Stata's great strengths, compared with many statistical packages, is that its command syntax follows strict rules: in grammatical terms, there are no irregular verbs. This implies that when you have learned the way a few key commands work, you will be able to use many more without extensive study of the manual or even on-line help. The {\tt search} command will allow you to find the command you need by entering one or more keywords, even if you do not know the command's name.

\end{slide}

\begin{slide}

The fundamental syntax of all Stata commands follows a template. Not all elements of the template are used by all commands, and some elements are only valid for certain commands. But where an element appears, it will appear in the same place, following the same grammar. 

Like Unix or Linux, Stata is case sensitive. Commands must be given in lower case. For best results, keep all variable names in lower case to avoid confusion.

Following the examples in the {\it Getting Started with Stata...} manual, we will make use of {\tt auto.dta}, a dataset of 74 automobiles' characteristics.

\end{slide}

\begin{slide}

The general syntax is:

\begin{verbatim}
[by varlist:] cmdname [varlist] [=exp] [if exp] [in range]
                      [weight] [using spec] [,options]
\end{verbatim}

where elements in square brackets are optional for some commands. In some cases, only the {\tt cmdname} itself is required:

{\tt desc} without arguments gives a description of the current contents of memory (including the identifier and timestamp of the current dataset), while {\tt summ} without arguments provides summary statistics for all (numeric) variables. Both may be given with a {\tt varlist} specifying the variables to be considered.

What are the other elements?

\end{slide}
\begin{slide}

{\bf The varlist}

{\it varlist} is a list of one or more variables on which the command is to operate: the subject(s) of the verb. Stata works on the concept of a single set of variables currently defined and contained in memory, each of which has a name. As {\tt desc} will show you, each variable has a data type (various sorts of integers and reals, and string variables of a specified maximum length). The varlist specifies which of the defined variables are to be used in the command.

The order of variables in the dataset matters, since you can use hyphenated lists to include all variables between first and last. (The {\tt order} and {\tt move} commands can alter the order of variables.) You can also use ``wildcards'' to refer to all variables with a certain prefix. If you have variables pop60, pop70, pop80, pop90, you can refer to them in a varlist as {\tt pop*} or {\tt pop?0}.

\end{slide}
\begin{slide}

{\bf exp clause}

The {\it exp} clause is used in commands such as {\tt generate} and {\tt replace} where an algebraic expression is used to produce a new (or updated) variable. In algebraic expressions, the operators ==, \&, $\vert$ and ! are used as equal, AND, OR and NOT, respectively. The $\bigwedge$ operator is used to denote exponentiation. The + operator is overloaded to denote concatenation of character strings.

\end{slide}
\begin{slide}

{\bf if and in clauses}

Stata differs from several common programs in that Stata commands will automatically apply to all observations currently defined. You need not write explicit loops over the observations. You can, but it is usually bad programming practice to do so. Of course you may want not to refer to all observations, but to pick out those that satisfy some criterion. This is the purpose of the {\it if exp} and {\it in range} clauses. For instance, we might:

\begin{verbatim}
sort price
list make price in 1/5
\end{verbatim}

to determine the five cheapest cars in auto.dta. The 1/5 is a {\it numlist}: in this case, a list of observation numbers. $\ell$ is the last observation, thus {\it list make price in -5/$\ell$} will list the five most expensive cars in auto.dta.

\end{slide}
\begin{slide}

Even more commonly, you may employ the {\it if exp} clause. This restricts the set of observations to those for which the ``exp'', a Boolean expression, evaluates to true. Stata's missing value codes are greater than the largest positive number, so that the last command would avoid listing cars for which the price is missing. 

{\tt list make price if foreign==1}

lists only foreign cars, and 

{\tt list make price if price $>$ 10000 \& price $< $. }

lists only expensive cars (in 1978 prices!) Note the double equal in the {\it exp}. A single equal sign, as in the C language, is used for assignment; double equal in comparison.  

\end{slide}
\begin{slide}

{\bf the using clause}

Some commands access files--reading data from external files, or writing to files. These commands contain a {\it using} clause, in which the filename appears. If a file is being written, you must specify the ``replace'' option to overwrite an existing file of that name.

\end{slide}
\begin{slide}

{\bf Stata binary files}

Stata's own binary file format, the {\bf .dta} file, is cross-platform compatible, even between machines with different byte orderings (low-endian and high-endian). A .dta file may be moved from one computer to another using {\bf ftp} (in binary transfer mode). 

\end{slide}
\begin{slide}

To bring the contents of an existing Stata file into memory, the command:

{\tt use} {\it file} [,{\tt clear}]

is employed ({\tt clear} will empty the current contents of memory). You must make sufficient memory available to Stata to load the entire file, since Stata's speed is largely derived from holding the entire data set in memory. Consult {\it Getting Started...} for details on adjusting the memory allocation on your computer, since it differs by operating system. 

\end{slide}
\begin{slide}

Reading and writing binary (.dta) files is much faster than dealing with text (ASCII) files (with the {\tt insheet} or {\tt infile} commands), and permits variable labels, value labels, and other characteristics of the file to be saved along with the file. To write a Stata binary file, the command

{\tt save} {\it file} [,{\tt replace}]

is employed. The {\tt compress} command can be used to economize on the disk space (and memory) required to store variables.

\end{slide}
\begin{slide}

{\bf Transportability}

Stata binary files may be easily transformed into SPSS or SAS files with the third-party application Stat/Transfer. Stat/Transfer is available for Windows and Mac OS X systems as well as on various Unix systems on campus. Personal copies of Stat/Transfer version 7 (which handles Stata version 8 datafiles) are available at a discounted academic rate of \$52.00 through the Stata GradPlan.

Stat/Transfer can also transfer SAS, SPSS and many other file formats into Stata format, without loss of variable labels, value labels, and the like. It is a very useful tool.

\end{slide}
\begin{slide}

{\bf Accessing data over the Web}

The amazing thing about ``use filename'' is that it is by no means limited to the files on your hard disk. Since Stata is a web browser,

{\tt webuse klein} 

or 

{\tt use http://fmwww.bc.edu/ec-p/data/Wooldridge/crime1.dta }

will read these datasets into Stata's memory over the web. 

\end{slide}
\begin{slide}

The {\tt type} command can display any text file, whether on your hard disk or over the Web; thus

\begin{verbatim}
type http://fmwww.bc.edu/ec-p/data/Wooldridge/crime1.des
\end{verbatim}

will display the codebook for this file, and 

\begin{verbatim}
copy http://fmwww.bc.edu/ec-p/data/Wooldridge/crime1.des 
     crime.codebook
\end{verbatim}

will make a copy of the codebook on your own hard disk.

\end{slide}
\begin{slide}

When you have {\tt use}d a dataset over the Web, you have loaded it into memory in your desktop Stata. You cannot save it to the Web, but can save the data to your own hard disk. The advantages of this feature for instructional and collaborative research should be clear. Students may be given a URL from which their assigned data are to be accessed; it matters not whether they are using Stata for Windows, Macintosh, Linux, or UNIX. 

\end{slide}
\begin{slide}

{\bf The options clause}

Many commands make use of options (such as {\tt clear} on {\tt use}, or {\tt replace} on {\tt save}). All options are given following a single comma, and may be given in any order. Options, like commands, may generally be abbreviated (with the notable exception of {\tt replace}).

\end{slide}
\begin{slide}

{\bf The by prefix}

The last standard element of the command syntax (other than {\it weights}, which we will not discuss here) is a very powerful tool: the {\it by} prefix. When a command is prefixed with a bylist, it is performed repeatedly for each element of the variable or variables in that list, each of which must be categorical. For instance,

{\tt by foreign: summ price}

will provide descriptive statistics for both foreign and domestic cars. If the data are not already sorted by the bylist variables, the prefix {\tt bysort}  should be used. 

\end{slide}
\begin{slide}

The option {\it ,total} will add the overall summary. What about a classification with several levels, or a combination of values?

\begin{verbatim}
bysort rep78: summ price

bysort rep78 foreign: summ price
\end{verbatim}

This is a very handy tool, which often replaces explicit loops that must be used in other programs to achieve the same end.

\end{slide}
\begin{slide}

The by prefix should not be confused with the by {\it option} available on some commands, which allows for specification of a grouping variable: for instance

{\tt ttest price, by(foreign)}

will run a t-test for the difference of sample means across domestic and foreign cars. 

\end{slide}
\begin{slide}

Another useful aspect of {\it by} is the way in which it modifies the meanings of the observation number symbol. Usually {\it \_n} refers to the current observation number, which varies from 1 to {\it \_N}, the maximum defined observation. Under a bylist, {\it \_n} refers to the observation within the bylist, and {\it \_N} to the total number of observations for that category. This is often useful in creating new variables. 

For instance, if you have individual data with a family identifier, these commands might be useful: 

\begin{verbatim}
sort famid age
by famid: gen siblings = _N
by famid: gen birthorder = _N - _n +1
\end{verbatim}

\end{slide}
\begin{slide}

{\bf Missing values}

Missing value codes in Stata appear as the dot (.) in printed output (and a string missing value code as well: ``'', the null string). It takes on the largest possible positive value, so in the presence of missing data you do not want to say

{\tt generate hiprice = (price $>$ 10000)}, but rather

{\tt generate hiprice = (price $>$ 10000 \& price <.)}

which then generates a ``dummy variable'' for high-priced cars (for which price data are complete, with prices ``less than missing''). 

Stata version 8 allows for multiple missing value codes ({\tt .a, .b, .c, \dots, .z}).
\end{slide}
\begin{slide}

{\bf Display formats}

Each variable may have its own default display format. This does not alter the contents of the variable, but affects how it is displayed. For instance, {\tt \%9.2f} would display a two-decimal-place real number. The command

{\tt format varname \%9.2f}

will save that format as the default format of the variable, and 

{\tt format date \%tm}

will format a Stata date variable into a monthly format (e.g., {\tt 1998m10}).

\end{slide}
\begin{slide}

{\bf Variable labels}

Each variable may have its own variable label. The variable label is a character string (maximum 80 characters) which describes the variable, associated with the variable via

{\tt label variable varname "text"}

Variable labels, where defined, will be used to identify the variable in printed output, space permitting.

\end{slide}
\begin{slide}

{\bf Value labels}

Value labels associate numeric values with character strings. They exist separately from variables, so that the same mapping of numerics to their definitions can be defined once and applied to a set of variables (e.g. 1=very satisfied...5=not satisfied may be applied to all responses to questions about consumer satisfaction). Value labels are saved in the dataset.

{\tt label define sexlbl 0 male 1 female}

{\tt label values sex sexlbl}

If value labels are defined, they will be displayed in printed output instead of the numeric values.

\end{slide}
\begin{slide}

{\bf Generating new variables}

The command {\tt generate} is used to produce new variables in the dataset, whereas {\tt replace} must be used to revise an existing variable (and {\tt replace} must be spelled out). The syntax just demonstrated is often useful if you are trying to generate indicator variables, or dummies, since it combines a {\tt generate} and {\tt replace} in a single command.

A full set of functions are available for use in the {\tt generate} command, including the standard mathematical functions, recode functions, string functions, date and time functions, and specialized functions ({\tt help functions} for details). Note that {\tt generate}'s {\tt sum()} function is a running sum.

\end{slide}
\begin{slide}

The {\tt D., L.}, and {\tt F.} operators may be used under a timeseries calendar (including in the context of panel data) to specify first differences, lags, and leads, respectively. These operators understand missing data, and numlists: e.g. {\tt L(1/4).x} is the first through fourth lags of {\tt x}, while {\tt L2D.x} is the second lag of the first difference of the {\tt x} variable.

Stata also contains a full-featured matrix language, with one limitation: matrices may not be larger than 800 rows or columns in standard Stata. This does not prevent the computation of matrix expressions on much larger datasets, since commands like {\tt matrix accum} can form expressions over a larger dimension. 

The matrix language allows any estimation results to be stored in matrices and manipulated, and supports the programming of many estimators. Matrix functions include the singular value decomposition and eigensystem of a symmetric matrix.

\end{slide}
\begin{slide}

 Additionally, Stata is not limited to using the set of defined functions. The {\tt egen} ({\it e}xtended {\it gen}erate) command makes use of functions written in the Stata ado-file language, so that {\it \_gzap.ado} would define the extended generate function {\tt zap()}. This would then be invoked as

{\tt egen newvar = zap(oldvar)}

which would do whatever {\tt zap} does on the contents of oldvar, creating the new variable newvar.

A number of {\tt egen} functions provide row--wise operations: row sum, row average, row standard deviation, etc.

\end{slide}
\begin{slide}

{\bf Estimation commands}

All estimation commands share the same syntax. Multiple equation estimation commands use a list of equations,  rather than a {\it varlist}, where equations are defined in parenthesized {\it varlist}s. Most estimation commands allow the use of various kinds of weights. 

Estimation commands display confidence intervals for the coefficients, and tests of the most common hypotheses. More complex hypotheses may be analyzed with the {\tt test} and {\tt lincom} commands; for nonlinear hypothesis, {\tt testnl} and {\tt nlcom} may be applied, making use of the delta method. 

Robust (Huber/White) estimates of the covariance matrix are available for almost all estimation commands by employing the {\tt robust} option. 

\end{slide}
\begin{slide}
Predicted values and residuals may be obtained after any estimation command with the {\tt predict} command. For nonlinear estimators, {\tt predict} will produce other statistics as well (e.g. the log of the odds ratio from logistic regression). The {\tt mfx} command may be used to generate marginal effects, including elasticities and semi--elasticities, for any estimation command.

All estimation commands ``leave behind'' results of estimation  in the {\tt e()} array, where they may be inspected with {\tt ereturn list}. Any item here, including scalars such as $R^2$ and {\it RMSE}, the coefficient vector, and the estimated variance--covariance matrix, may be saved for use in later calculations.

\end{slide}
\begin{slide}

{\bf An overview of useful commands}

Let us now describe several categories of commands, and the facilities Stata provides for data manipulation, statistics, and graphics. Some data manipulation commands you should learn:

\begin{verbatim}
help      online help on a specific command
findit    online references on a keyword or topic
ssc       access routines from the SSC Archive
log       logging to an external file 
generate  create a new variable
replace   modify an existing variable
sort      change the sort order of the dataset
compress  economize on space used by variables
\end{verbatim}

\end{slide}
\begin{slide}

{\bf Data manipulation commands:}

\begin{verbatim}
append    combine datasets by stacking
merge     merge datasets (one-to-one or match merge)
encode    generate numeric variable from categorical variable
recode    recode categorical variable
destring  convert string variables to numeric
tab       abbreviation for tabulate: 1- and 2-way tables
table     tables of summary statistics

foreach   loop over elements of a list, performing a block of code
forvalues loop over a numlist, performing a block of code
while     loop with a counter over a block of code
local     define or modify a local macro (scalar variable)
\end{verbatim}

\end{slide}
\begin{slide}

{\bf Data manipulation commands:}

\begin{verbatim}
use       load a Stata data set
save      write the contents of memory to a Stata data set
insheet   load a text file in tab- or comma-delimited format
infile    load a text file in space-delimited format
          or as defined in a dictionary 
outfile   write a text file in space- or comma-delimited format
outsheet  write a text file in tab- or comma-delimited format
clear     clear memory
drop      drop certain variables and/or observations
keep      keep only certain variables and/or observations
rename    rename variable
renvars   rename a set of variables
contract  make a dataset of frequencies
collapse  make a dataset of summary statistics

\end{verbatim}

\end{slide}
\begin{slide}

{\bf Data manipulation commands:}

\begin{verbatim}
pwd       print the working directory
cd        change the working directory
tsset     define the time indicator for timeseries or panel data

quietly   do not show the results of a command
update query  see if Stata is up to date
exit      exit the program (,clear if dataset is not saved)
\end{verbatim}

\end{slide}
\begin{slide}

{\bf Useful statistical commands:}

\begin{verbatim}
summarize  descriptive statistics
correlate  correlation matrices
ttest      perform 1-, 2-sample and paired t-tests
anova      1-, 2-, n-way analysis of variance
regress    least squares regression
predict    generate fitted values, residuals, etc.
test       test linear hypotheses on parameters
lincom     linear combinations of parameters
cnsreg     regression with linear constraints
testnl     test nonlinear hypothesis on parameters    
ivreg      instrumental variables regression
prais      regression with AR(1) errors

\end{verbatim}

\end{slide}
\begin{slide}

{\bf Useful statistical commands:}

\begin{verbatim}
sureg      seemingly unrelated regressions
reg3       three-stage least squares
probit     binomial probit estimation
logit      binomial logit estimation
tobit      Tobit regression
cnreg      censored normal regression
mfx        marginal effects after nonlinear estimation
glm        generalized linear models
heckman    Heckman's selection model
oprobit    ordered probit estimation
ologit     ordered logit estimation
qreg       quantile regression, including median regression
\end{verbatim}
\end{slide}
\begin{slide}

{\bf Useful statistical commands:}

``xt'' commands for panel data estimation, such as

\begin{verbatim}
xtreg,fe   fixed effects estimator
xtreg,re   random effects estimator
xtgls      panel-data models using generalized least squares
xtlogit    panel-data logit models
xtprobit   panel-data probit models
xtpois     panel-data Poisson regression
xtgee      panel-data models using generalized estimating equations
xtabond    Arellano-Bond dynamic panel data estmator
\end{verbatim}

``{\tt svy}'' commands for the handling of complex survey data, 
including stratification and PSUs

\end{slide}

\begin{slide}

{\bf Useful statistical commands:}

Timeseries data commands, such as

\begin{verbatim}
arima      Box-Jenkins models, regressions with ARMA errors
arch       models of autoregressive conditional heteroskedasticity
dfgls      unit root tests
corrgram   correlogram estimation
var        vector autoregressions (basic and structural)
irf        impulse response functions, variance decompositions
vec        vector error--correction models (cointegration)
\end{verbatim}

Nonlinear estimation commands, such as

\begin{verbatim}
bstrap     bootstrap sampling
nl         nonlinear least squares
ml         maximum likelihood estimation with user-specified LLF
\end{verbatim}
\end{slide}
\begin{slide}

{\bf Graphics:}

{\tt twoway}      produces a variety of graphs, depending on options listed

{\tt histogram rep78}       histogram of this categorical variable

{\tt twoway scatter price mpg}   a Y vs X scatterplot

{\tt twoway line price mpg}   a Y vs X line plot

{\tt tsline GDP}   a Y vs time time--series plot

{\tt twoway area price mpg}   an Y vs X area plot

{\tt twoway rline price mpg}   a Y vs X range plot (hi-lo) with lines

The command {\tt twoway} may be omitted in most cases.
\end{slide}
\begin{slide}

The overwhelming flexibility of Stata graphics allows any of these plot types (including many more that are available) to be easily combined on the same graph.  For instance, using the auto.dta dataset,

{\tt  twoway (scatter price mpg) (lfit price mpg)} 

 will generate a scatterplot, overlaid with the linear regression fit, and
 
{\tt   twoway (lfitci price mpg) (scatter price mpg) }
 
 will do the same with the confidence interval displayed. 
 
 \end{slide}
\begin{slide}
\includegraphics[width=9in]{intro1}
 \end{slide}
\begin{slide}
\includegraphics[width=9in]{intro2}
 \end{slide}
\begin{slide}
 
 A nonparametric fit of a bivariate
 relationship can be readily overlaid on a graph via
 
 {\tt  twoway  (lowess price mpg) (scatter price mpg)}
 
 Twoway graphs may also represent mathematical functions, without explicit data:
 
 {\tt twoway (function y=log(x)*sin(x)) (function y=x*cos(x))}
 
\end{slide}


\begin{slide}
\includegraphics[width=9in]{intro3}
 \end{slide}
\begin{slide}
\includegraphics[width=9in]{intro4}
 \end{slide}
 \begin{slide}
 
Graphs may also be readily combined into a single graphic for presentation. For instance,

{\tt twoway (scatter price mpg) (lfit price mpg), name(auto1)}

{\tt gen gpm = 1/mpg}

{\tt label var gpm "Gallons per mile"}
 
 {\tt  twoway  (lowess price gpm) (scatter price gpm), name(auto2)}

{\tt graph combine auto1 auto2,  saving(myauto, replace) /// }

{\tt ti("Some exploratory aspects of auto.dta") }

where the ``///'' is a continuation of the line.

\end{slide}
\begin{slide}
\includegraphics[width=9in]{intro5}
 \end{slide}

\begin{slide}

Now let us walk though some analysis of nontrivial Stata datasets. A list of over 100 datasets suitable for instructional use is available on the economics web pages as

{\tt http://fmwww.bc.edu/ec-p/data/ecfindata.html\#teach} 

Let's consider the data Zvi Griliches used in his 1976 article on the wages of young men ({\it Journal of Political Economy}, 84, S69-S85). These are cross-sectional data on 758 individuals collected over several survey years.

{\tt do http://fmwww.bc.edu/ec-p/software/stata/stataintro1 }

\end{slide}
\begin{slide}

\begin{verbatim}
* StataIntro: cross-section example 
log using intro1, replace
use http://fmwww.bc.edu/ec-p/data/hayashi/griliches76.dta
describe
summarize     
label define ur 0 rural 1 urban
label values smsa ur
tab smsa
tab mrt smsa, chi2
ttest med,by(smsa)
anova lw mrt smsa
anova lw mrt smsa mrt*smsa
anova,regress
regress lw tenure kww smsa
predict lweps,resid
scatter lweps kww
bysort year: regress lw tenure kww smsa
graph matrix iq kww age s expr lw, msize(tiny)
gen medrural = med*(smsa==0)
gen medurban = med*(smsa==1)
regress lw tenure kww medurban medrural
test medurban=medrural
log close 
\end{verbatim}
\end{slide}

\begin{slide}
The following example reads some daily Dow-Jones Averages
data, graphs daily returns, then performs Dickey-Fuller tests
for unit roots on the DJIA, its log, and its returns (log price relatives), and
on their first differences.
AR(3) models are then estimated on the series, and the
Box--Pierce portmanteau test
is then performed on the residuals.

In this example, we make use of ``local macros'' (with values {\tt `v'}), which enable us to perform the same
operations on several named variables without having to write out the commands for each
variable. This facility may be used with {\tt varlists} of any length, and makes it very easy
to generate parallel analyses, produce graphs, etc. for an arbitrary set of variables or time periods.

{\tt do http://fmwww.bc.edu/ec-p/software/stata/stataintro2 }
\end{slide}

\begin{slide}
\begin{verbatim}
* StataIntro: time-series example 
log using intro2,replace
use http://fmwww.bc.edu/ec-p/data/micro/ddjia.dta
desc 
summ
tsset
tsline ret
foreach v of varlist djia ldjia ret {
      dfgls  `v', maxlag(12)
      dfgls D.`v', maxlag(12)
      regress `v'  L(1/3).`v', robust
      predict eps_`v',resid
      wntestq eps_`v'
      }
log close
\end{verbatim}
\end{slide}


\end{document}
