View on GitHub

Stata Examples Regressions and Tables

Reusable Stata Code from Various Projects

This is a work-in-progress website of sample Stata files, produced by Fan. Materials gathered from various projects in which STATA code is used. The goal of this repository is to make it easier to find/re-use codes produced for various projects.

STATA files are linked below by section. Various functions are stored in corresponding .do files. To use the files, clone the repository. Some files have examples/instructions created using Jupyter notebooks or STATA translator and are shown as HTML and PDF files. See here for Github set up.

From Fan’s other repositories: For dynamic borrowing and savings problems, see Dynamic Asset Repository; For example R code, see R Panel Data Code, for example Matlab code, see Matlab Example Code; For intro econ with Matlab, see Intro Mathematics for Economists, and for intro stat with R, see Intro Statistics for Undergraduates. See here for all of Fan’s public repositories.

1. Regressions

1.1 All Purpose Regression Tool

1. All Purpose N Columns M Panels Regression Structure
• Code: DO | PDF Gen Tables Log | HTML Gen Tables Log
• Output: PDF Table | HTML Table | DOC Table
• A regression table has these ingredients: (1) regression method (2) LHS (3) RHS (to keep) (4) RHS (controls not to show in table) (5) conditions (6) regression options (7) row and column title and footnotes labeling
• Specify regression table column and panel specific ingredients for all, none, any row or column subsets freely
• Versatile structure that can test large sets of regression specifications

1.2 Various Examples

1. Discrete Interactions: DO | HTML | PDF
• Regression with interacted discrete regressors
• core: regress + esttab

2. Generate Table

2.1 Multiple Regression Panels

1. Multiple Panels: DO | Gen Table Log | PDF Table | TEX Table
• Three panels, different regressors for each panel, different conditionings each column.
• core: regres + esttab

2.2 Multiple Regression Panels with Interactions

1. Continuous and Discrete Interactions: DO | Gen Table Log | PDF Table | TEX Table
• Three panels, different regressors for each panel, different conditionings each column.
• core: regres + esttab

2.3 Cross Tabulation

1. Continuous and Discrete Interactions: DO | Gen Table Log | PDF Table | TEX Table
• Three row categories, two interacting column categories, statistics for multiple variables
• core: regres + estpost tabstat

3. Dataset Wrangling

3.1 Generate and Replace

1. Generate Re-group Categorical Variable: DO | HTML | PDF
• Change or Reduce the number of categories for a categorical variable.
• 3 methods: a. recode; b. egen cut; c. inlist/inrange.
• Semi-automated loop: reset relabel regroup many variables with some time saving.
• core: recode turn (min/35 = 1 “Turn <35”) … (46/max = 5 “Turn > 45”) (else =. ), gen(turn_m5); egen var = cut(turn), at(31(3)52) label; if inrange(turn, 31, 35), if inlist(turn, 46, 48, 51)
2. Within-Group Fill and Replace Values: DO | HTML | PDF
• Fill missing values in group by single nonmissing value in group.
• core: bys trunk (var_one_val_in_group): gen var_test_fill = var_one_val_in_group
3. Labels: DO | HTML | PDF
• label a variable, label the values of a discrete variable
• output as one string (1) var label (2) var value labels (3) obs each value of categorical.
• core: labelbook + local st : variable label foreign + local st : label foreign_lab 0

3.2 Summary

1. Multiple Variables Jointly Nonmissing: DO | HTML | PDF
• Find rows where there are no missing values for any variables in a list of variables and where certain conditionings are satisfied
• core: egen valid = rownonmiss(\$svr_list) if \$scd_bse \$scd_one \$scd_two

3.3 Random

1. Drop Random Subset of Data: DO | HTML | PDF
• Drop random subset of data for different variables
• core: round((_n/_N) x it_drop_frac) == round(it_drop_frac x uniform())

4. Programming

4.1 Basic

1. Local, Global and Scalar: DO | HTML | PDF
• local, global and scalar as boolean controls
• core: local bl_includereg1 = 1, if (`bl_includereg1’); global bl_includereg3 = 0, if (\$bl_includereg3); scalar bl_includereg5 = 0, if (bl_includereg5);
2. Loops: DO | HTML | PDF
• local, global and scalar as boolean controls
• core: #delimit; global ls “vara varb”; #delimit cr; foreach svr_outcome in \$ls {}
3. String: DO | HTML | PDF
• string delimit define, string labeling, string replace quotes, pound, etc.
• core: #delimit; global ls “vara varb”; subinstr()

4.2 Matrix

1. Define and Slice Matrix: DO | HTML | PDF
• matrix definition and slicing, get column and row names, replace matrix subset.
• core: matrix + rownumb/colnumb + matrix mt_bl_estd = J(it_rowcnt, it_colcnt, bl_fillval) + mat_a[1…, colnumb(mat_a, “reg1”)] + …

5. Support

5.1 Logging

1. LOG2HTML and Translator: DO | Translator PDF | SMCL Log PDF
• translator to export console buffer to PDF, or to export log file to pdf; log2html to convert to html.
• core: log2html + translate @Results     