Usefull Stata Command

subject Type Homework Help
subject Pages 50
subject Words 29552
subject School N/A
subject Course N/A

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
Kenneth L. Simons, 29-Aug-16
1
Useful Stata Commands (for Stata versions 13 & 14)
Kenneth L. Simons
– This document is updated continually. For the latest version, open it from the course disk space. –
This document briefly summarizes Stata commands useful in ECON-4570 Econometrics and ECON-
6570 Advanced Econometrics.
This presumes a basic working knowledge of how to open Stata, use the menus, use the data editor, and
use the do-file editor. We will cover these topics in early Stata sessions in class. If you miss the
sessions, you might ask a fellow student to show you through basic usage of Stata, and get the
recommended text about Stata for the course and use it to practice with Stata.
More replete information is available in Lawrence C. Hamilton’s Statistics with Stata, Christopher F.
Baum’s An Introduction to Modern Econometrics Using Stata, and A. Colin Cameron and Pravin K.
Trivedi’s Microeconometrics using Stata. See: http://www.stata.com/bookstore/books-on-stata/ .
Readers on the Internet: I apologize but I cannot generally answer Stata questions. Useful places to
direct Stata questions are: (1) built-in help and manuals (see Stata’s Help menu), (2) your friends and
colleagues, (3) Stata’s technical support staff (you will need your serial number), (4) Statalist
(http://www.stata.com/statalist/) (but check the Statalist archives before asking a question there).
Most commands work the same in Stata versions 12, 11, 10, and 9.
Throughout, estimation commands specify robust standard errors (Eicker-Huber-White heteroskedastic-consistent standard
errors). This does not imply that robust rather than conventional estimates of Var[b|X] should always be used, nor that they
are sufficient. Other estimators shown here include Davidson and MacKinnon’s improved small-sample robust estimators for
OLS, cluster-robust estimators useful when errors may be arbitrarily correlated within groups (one application is across time
for an individual), and the Newey-West estimator to allow for time series correlation of errors. Selected GLS estimators are
listed as well. Hopefully the constant presence of “vce(robust)” in estimation commands will make readers sensitive to the
need to account for heteroskedasticity and other properties of errors typical in real data and models.
Kenneth L. Simons, 29-Aug-16
2
Contents
Preliminaries for RPI Dot.CIO Labs ........................................................................................................... 5!
A. Loading Data .......................................................................................................................................... 5!
A1. Memory in Stata Version 11 or Earlier ............................................................................................ 5!
B. Variable Lists, If-Statements, and Options ............................................................................................ 6!
C. Lowercase and Uppercase Letters .......................................................................................................... 6!
D. Review Window, and Abbreviating Command Names ......................................................................... 6!
E. Viewing and Summarizing Data ............................................................................................................ 6!
E1. Just Looking ..................................................................................................................................... 7!
E2. Mean, Variance, Number of Non-missing Observations, Minimum, Maximum, Etc. .................... 7!
E3. Tabulations, Histograms, Density Function Estimates ..................................................................... 7!
E4. Scatter Plots and Other Plots ............................................................................................................ 7!
E5. Correlations and Covariances ........................................................................................................... 8!
F. Generating and Changing Variables ....................................................................................................... 8!
F1. Generating Variables ........................................................................................................................ 8!
F2. Missing Data ..................................................................................................................................... 8!
F3. True-False Variables ......................................................................................................................... 9!
F4. Random Numbers ........................................................................................................................... 10!
F5. Replacing Values of Variables ....................................................................................................... 10!
F6. Getting Rid of Variables ................................................................................................................. 10!
F7. If-then-else Formulas ...................................................................................................................... 11!
F8. Quick Calculations .......................................................................................................................... 11!
F9. More ................................................................................................................................................ 11!
G. Means: Hypothesis Tests and Confidence Intervals ............................................................................ 11!
G1. Confidence Intervals ...................................................................................................................... 11!
G2. Hypothesis Tests ............................................................................................................................ 12!
H. OLS Regression (and WLS and GLS) ................................................................................................. 12!
H1. Variable Lists with Automated Category Dummies and Interactions ........................................... 12!
H2. Improved Robust Standard Errors in Finite Samples ..................................................................... 13!
H3. Weighted Least Squares ................................................................................................................. 13!
H4. Feasible Generalized Least Squares ............................................................................................... 14!
I. Post-Estimation Commands .................................................................................................................. 14!
I1. Fitted Values, Residuals, and Related Plots .................................................................................... 14!
I2. Confidence Intervals and Hypothesis Tests ..................................................................................... 14!
I3. Nonlinear Hypothesis Tests ............................................................................................................. 15!
I4. Computing Estimated Expected Values for the Dependent Variable .............................................. 15!
I5. Displaying Adjusted R2 and Other Estimation Results ................................................................... 16!
I6. Plotting Any Mathematical Function .............................................................................................. 16!
I7. Influence Statistics ........................................................................................................................... 17!
I8. Functional Form Test ....................................................................................................................... 17!
I9. Heteroskedasticity Tests .................................................................................................................. 17!
I10. Serial Correlation Tests ................................................................................................................. 18!
I11. Variance Inflation Factors ............................................................................................................. 18!
I12. Marginal Effects ............................................................................................................................ 18!
J. Tables of Regression Results ................................................................................................................ 19!
Kenneth L. Simons, 29-Aug-16
3
J0. Copying and Pasting from Stata to a Word Processor or Spreadsheet Program ............................. 19!
J1. Tables of Regression Results Using Stata’s Built-In Commands ................................................... 19!
J2. Tables of Regression Results Using Add-On Commands ............................................................... 20!
J2a. Installing or Accessing the Add-On Commands ....................................................................... 20!
J2b. Storing Results and Making Tables ........................................................................................... 21!
J2c. Near-Publication-Quality Tables ............................................................................................... 21!
J2d. Understanding the Table Command’s Options ......................................................................... 22!
J2e. Saving Tables as Files ............................................................................................................... 22!
J2f. Wide Tables ............................................................................................................................... 23!
J2g. Storing Additional Results ........................................................................................................ 23!
J2h. Clearing Stored Results ............................................................................................................. 23!
J2i. More Options and Related Commands ...................................................................................... 23!
J3. Tabulations and General Tables Using Add-On Commands .......................................................... 23!
K. Data Types, When 3.3 3.3, and Missing Values ............................................................................... 24!
L. Results Returned after Commands ....................................................................................................... 24!
M. Do-Files and Programs ........................................................................................................................ 24!
N. Monte-Carlo Simulations ..................................................................................................................... 26!
O. Doing Things Once for Each Group .................................................................................................... 26!
P. Generating Variables for Time-Series and Panel Data ......................................................................... 27!
P1. Creating a Time Variable ................................................................................................................ 27!
P1a. Time Variable that Starts from a First Time and Increases by 1 at Each Observation ............. 27!
P1b. Time Variable from a Date String ............................................................................................ 28!
P1c. Time Variable from Multiple (e.g., Year and Month) Variables .............................................. 28!
P1d. Time Variable Representation in Stata ..................................................................................... 29!
P2. Telling Stata You Have Time Series or Panel Data ....................................................................... 29!
P3. Lags, Forward Leads, and Differences ........................................................................................... 29!
P4. Generating Means and Other Statistics by Individual, Year, or Group .......................................... 30!
Q. Panel Data Statistical Methods ............................................................................................................ 30!
Q1. Fixed Effects – Using Dummy Variables ...................................................................................... 30!
Q2. Fixed Effects – De-Meaning .......................................................................................................... 31!
Q3. Other Panel Data Estimators .......................................................................................................... 31!
Q4. Time-Series Plots for Multiple Individuals .................................................................................... 32!
R. Probit and Logit Models ....................................................................................................................... 32!
R1. Interpreting Coefficients in Probit and Logit Models .................................................................... 32!
S. Other Models for Limited Dependent Variables .................................................................................. 34!
S1. Censored and Truncated Regressions with Normally Distributed Errors ...................................... 35!
S2. Count Data Models ......................................................................................................................... 35!
S3. Survival Models (a.k.a. Hazard Models, Duration Models, Failure Time Models) ....................... 35!
T. Instrumental Variables Regression ....................................................................................................... 36!
T1. GMM Instrumental Variables Regression ...................................................................................... 37!
T2. Other Instrumental Variables Models ............................................................................................ 38!
U. Time Series Models ............................................................................................................................. 38!
U1. Autocorrelations ............................................................................................................................. 38!
U2. Autoregressions (AR) and Autoregressive Distributed Lag (ADL) Models ................................. 38!
U3. Information Criteria for Lag Length Selection .............................................................................. 39!
U4. Augmented Dickey Fuller Tests for Unit Roots ............................................................................ 39!
Kenneth L. Simons, 29-Aug-16
4
U5. Forecasting ..................................................................................................................................... 39!
U6. Break Tests ..................................................................................................................................... 40!
U6a. Breaks at Known Times ........................................................................................................... 40!
U6b. Breaks at Unknown Times ....................................................................................................... 41!
U7. Newey-West Heteroskedastic-and-Autocorrelation-Consistent Standard Errors .......................... 42!
U8. Dynamic Multipliers and Cumulative Dynamic Multipliers ......................................................... 42!
V. System Estimation Commands ............................................................................................................ 42!
V1. GMM System Estimators ............................................................................................................... 43!
V2. Three-Stage Least Squares ............................................................................................................. 43!
V3. Seemingly Unrelated Regression ................................................................................................... 43!
V4. Multivariate Regression ................................................................................................................. 44!
W. Flexible Nonlinear Estimation Methods ............................................................................................. 44!
W1. Nonlinear Least Squares ............................................................................................................... 44!
W2. Generalized Method of Moments Estimation for Custom Models ............................................... 44!
W3. Maximum Likelihood Estimation for Custom Models ................................................................. 44!
X. Data Manipulation Tricks .................................................................................................................... 45!
X1. Combining Datasets: Adding Rows ............................................................................................... 45!
X2. Combining Datasets: Adding Columns.......................................................................................... 45!
X3. Reshaping Data .............................................................................................................................. 48!
X4. Converting Between Strings and Numbers .................................................................................... 48!
X5. Labels ............................................................................................................................................. 49!
X6. Notes .............................................................................................................................................. 50!
X7. More Useful Commands ................................................................................................................ 50!
Kenneth L. Simons, 29-Aug-16
Useful Stata (Version 14) Commands
Preliminaries for RPI Dot.CIO Labs
RPI computer labs with Stata include, as of Fall 2016: Sage 4510, the VCC Lobby (all Windows PCs),
and hopefully now all Dot CIO labs.
To access the Stata program, use the Q-drive. Look under My Computer and open the disk drive Q:,
probably labeled as “Common Drive (Q:)”, then double-click on the program icon that you see. You
must start Stata this way – it does not work to double-click on a saved Stata file, because Windows in
the labs is not set up to know Stata is installed or even which saved files are Stata files.
To access the course disk space, go to: \\hass11.win.rpi.edu\classes\ECON-4570-6560. If you are
logged into the WIN domain you will go right to it. If you are logged in locally on your machine or into
anther domain you will be prompted for credentials. Use:
username: win\"rcsid"
password: "rcspassword"
substituting your RCS username for "rcsid" and your RCS password for "rcspassword". Once entered
correctly the folder should open up.
To access your personal RCS disk space from DotCIO computers, find the icon on the desktop
labeled “RPI AFS Files,” double-click on it, and enter your username and password. Your personal disk
space will be attached probably as drive H. (Public RCS materials may be attached perhaps as drive P.)
Save Stata do-files to your personal disk space or a memory stick. For handy use when logging in, you
may put the web address to attach the course disk space in a file on your personal disk space (e.g., drive

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.