Professional Documents
Culture Documents
AN INTRODUCTION TO STATA
ECMT1020
What is Stata?
1
5/08/2016
Strengths of Stata
The capabilities of Stata are not limited to the features above. Stata codes can
be shared, reused and made into extensions for specialized routines
Strengths of Stata
• Data manipulation: moving data from external sources into the program,
cleaning it up, generating new variables, generating summary data sets,
merging data sets, reshaping data sets from “long” to “wide”
• Statistics: Stata provides all of the standard descriptive statistics and t-tests
through most sophisticated regressions. This includes regression diagnostics,
prediction, robust estimation of standard errors, instrumental variables and two-
stage least squares, seemingly unrelated regressions, vector autoregressions
and error correction models, logit, probit, ordered logit and probit, multinomial
logit etc.
• Graphics: These are excellent tools for exploratory data analysis, and can
produce high-quality graphics in many different forms. Every aspect of graphics
may be programmed and customized
2
5/08/2016
Updating Stata
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 to check for and, if desired, download the updates
Launch Stata
or
or
3
5/08/2016
window
Review
Variable
window
Output window
Output window
Command window
You can also access most of the commands by selecting items from
Stata’s menus to open dialogs that build Stata command
4
5/08/2016
The manual are included with the software as PDFs, and can be accessed
from the Help menu.
Useful links:
• http://www.stata.com/support/
• http://www.stata.com/links/resources.html
• http://www.ats.ucla.edu/stat/stata/
• http://data.princeton.edu/stata/default.html
• http://saproject.psc.isr.umich.edu/content/lesson_modules.html
Command Description
Help command Display the description and the syntax of the
e.g. help regress Stata command (when you already know the
name of the command)
Search topic Find the Stata command you are looking for
(when you don’t know the specific name)
5
5/08/2016
Stata Viewer
The Viewer is where you can see help information, view or print log, check
and install the latest official updates to Stata. You can Open it from the
main Stata menu: Window - Viewer
6
5/08/2016
Reproducibility
E.g. This allows you to keep track of what would happen if we added an
interaction, introduced an additional variable, or decided to handle zero
values as missing.
7
5/08/2016
1. Open a do file
3. The working directory displayed at the bottom left hand corner of the
main window is your default directory. Any files you save without
specifying a directory will be saved here
Stata Do file
To use the Stata do file editor, open the Windows menu in Stata and
go down to the “Do-File Editor”.
8
5/08/2016
The Stata log file records the commands you entered and the corresponding
outputs for the current session
To record the log file as plain text (so it can read with any word processor or
editor) use the command: log using filename, text
Keep in mind that you need to close a log file when you want to stop recording
the Stata output. You can do this typing log close
To open a log file, you can either use the open log button, or enter the Stata
command in the command window log using c:directory\filename.log
You can temporarily close a log file typing log off and then starting it again
typing log on
In some cases you may get the message “no room to add more
observations or no room to add more variables”. This is because not
enough memory has been assigned to STATA
9
5/08/2016
Describe: If you are using a data set created by someone else, you
may want to know what is in the data set.
Type describe using filename, for basic content of the data file
Type describe to get a description of the data in memory
Tabulate: this command is very useful to produce one and two way
tables, including frequencies (single and cumulated) and percentages
Type tabulate var1 for one way tables for var1, or
tabulate var1 var2 for two way tables
10
5/08/2016
Equal: ==
Not equal: != or ~=
And: &
Or: |
Not: !
11
5/08/2016
12
5/08/2016
Transformation in Stata
Command/Syntax Task
^2 Square
^3 Cube
sqrt(var) Square root
log(var) Logarithm
1/var Reciprocal
abs(var) Absolute value
max (x1,….xn) Maximum of x1,…xn
gen varlag=var[_n-1] Taking lags
norm(x) Cumulative standard normal distribution
normden(x) Standard normal density
normden(x,m,s) Normal density with mean m and sd s
invnorm(p) Inverse cumulative standard normal distribution
13
5/08/2016
Command/Syntax Task
regress y x1 x2 x3 Run linear regression of dependent variable y on independent
variables x1, x2 and x3. By default, the constant is included
rreg Robust regression
logit Logit analysis
probit Probit analysis
glm Generalized linear model
predict Calculate prediction or residuals after estimations
test Test linear hypothesis on parameters
mfx Marginal effects
ivreg Instrumental variable regression
qreg Quantile regression
Command/Syntax Task
tobit One and two limit Tobit model
14
5/08/2016
Test commands
Command/Syntax Task
ttest T test on equality of means
ttest varname [if Test of the hypothesis that varname has the same
exp], by (groupvar) mean within the two groups defined by the dummy variable groupvar
test Test linear hypothesis on the parameter
lrtest Likelihood ratio test after estimation
lincom Test non linear hypothesis on the parameter
dfgls DF Unit root test
wntestq Portmanteau (q) test for white noise
pperron Phillips-Perron unit root test
dfuller Augmented Dickey Fuller
dwstat Durbin Watson d statistic
durbina Durbin’s alternative test for serial correlation
bgodfrey Breusch-Godfrey test for higher order serial correlation
Vargranger Granger causality tests after var/svar
archlm Engle’s LM test for the presence of autoregressive condit. heteroskedsticity
15