Lesson 1
Panel Data Models: Fixed and Random Effects
Panel data follow the same individuals or firms over time, so each variable can vary both between individuals and within individuals over time. The estimators covered are pooled OLS, between, first-difference, fixed effects (within or dummy-variable) and random effects. They differ in how they treat the unobserved individual-specific effect a_i, and the Hausman test is used to choose between fixed and random effects.
Types of variables and variation
- Time-varying regressors x_it change across both individuals and time (e.g. annual income, monthly food spending).
- Time-invariant regressors have x_it = x_i for every t (e.g. gender, race).
- Individual-invariant regressors have x_it = x_t for every i (e.g. a time trend, the national unemployment rate).
- Overall variation is x_it - x̄. Between variation is x̄_i - x̄. Within variation is x_it - x̄_i, which removes each individual's level.
Pooled OLS, between and first-difference estimators
- Pooled OLS: y_it = b0 + b1 x1_it + b2 x2_it + u_it is estimated on all n*T observations and ignores the panel structure.
- Between estimator: regresses the individual means ȳ_i on x̄_i, uses only n observations and is rarely used.
- Model with an individual effect: y_it = b0 + b1 x_it + a_i + u_it, where a_i captures unobserved factors specific to each individual.
- a_i may be correlated with the regressors (e.g. ability and education). Leaving it in the error term then biases the estimates.
- First difference: regress Δy_it on Δx_it with n(T-1) observations. a_i cancels out, but time-invariant variables drop out as well.
Fixed effects
- Within transformation: subtract each individual's time mean from every variable (time-demeaning), so a_i drops out. Then estimate by OLS.
- The individual effects can be recovered afterwards: a_i = ȳ_i - b·x̄_i.
- The dummy-variable regression includes a dummy for each individual (dropping one as the reference). It gives the same slope coefficients as the within estimator.
- Time-invariant variables cannot be included because they are perfectly collinear with the fixed effects.
- The within R-squared explains only within variation, while the dummy-variable R-squared is much higher because the dummies absorb between variation.
- With T = 2, fixed effects and first differences give identical estimates. With T > 2, first differences can be better if the errors are serially correlated.
Random effects and model choice
- Random effects assume a_i is uncorrelated with the regressors. The composite error a_i + u_it is then serially correlated within each individual.
- Pooled OLS is still consistent in this case but inefficient. Random effects uses a quasi-demeaning transformation: y_it - θ ȳ_i.
- θ = 1 - sqrt(σu² / (σu² + T σa²)).
- θ close to 0 gives roughly pooled OLS (a_i unimportant). θ close to 1 gives roughly fixed effects (a_i important).
- Random effects can include time-invariant variables.
- Hausman test: use it to choose between fixed effects and random effects.
Key terms
- Panel data
- Repeated observations on the same units over several time periods.
- Individual-specific effect a_i
- An unobserved, time-constant factor that shifts an individual's outcome.
- Within transformation
- Subtracting each individual's time average from every variable.
- First-difference estimator
- OLS on period-to-period changes, which removes a_i.
- Random effects
- An estimator that assumes a_i is uncorrelated with the regressors and uses quasi-demeaning by θ.
- Hausman test
- A test used to choose between the fixed effects and random effects estimators.
Lesson 2
Instrumental Variables and 2SLS
When a regressor such as education is correlated with the error term, OLS is biased. Instrumental variables (IV) solve this by using only the variation in the endogenous regressor that is driven by an instrument, which must be relevant and must affect the outcome only through the regressor. Two-stage least squares (2SLS) puts this into practice.
The endogeneity problem
- Wage equation: log(wage) = b0 + b1 educ + b2 exper + b3 exper² + e.
- Ability, persistence, health and social networks affect wages and are also correlated with schooling.
- Because these factors are unobserved, education is endogenous and the OLS estimate of b1 is biased.
- IV corrects for this without needing to measure the confounders.
What makes a good instrument
- Relevance: the instrument Z must be correlated with the endogenous regressor.
- Exclusion (validity): Z affects the dependent variable only through the regressors, so Z must be independent of the error term.
- Distance to the nearest college is relevant. It is valid only if labor markets near colleges are not otherwise different.
- Quarter of birth affects schooling through compulsory schooling laws but is only weakly correlated with education. Bound, Jaeger and Baker found first-stage F statistics of 2.4 and 1.87.
Two-stage least squares
- Stage 1: regress educ on Z and the exogenous regressors, then compute fitted values educ-hat.
- educ-hat is a linear combination of exogenous variables, so it is uncorrelated with the error term.
- Stage 2: regress log(wage) on educ-hat and the other regressors. This gives a consistent estimate of b1.
- Card (1993) used college proximity as the instrument: growing up near a college added about 0.3 years of schooling, and the IV return to schooling was about 13.2% per year.
- Running the two stages manually gives the correct point estimates but wrong standard errors. Use a built-in IV command (e.g. Stata's ivregress).
Cautions
- Weak instruments: the first-stage F test on the instruments should be at least 10, preferably 12 or more.
- Several instruments can be used together if each one is valid.
- IV estimates a local average treatment effect (LATE): the effect for the people whose treatment is changed by the instrument.
- Validity is hard to test. With more than one instrument, overidentification tests (Sargan, Hausman) check whether the instruments give similar estimates.
Key terms
- Endogenous regressor
- A regressor that is correlated with the error term.
- Instrument
- A variable correlated with the endogenous regressor that affects the outcome only through the regressors.
- 2SLS
- An estimator that regresses the endogenous variable on the instruments, then uses the fitted values in the main regression.
- Weak instrument
- An instrument with low first-stage correlation (F below 10), which makes IV estimates unreliable.
- LATE
- The average treatment effect for the subgroup whose treatment is shifted by the instrument.
Lesson 3
Difference-in-Differences Estimation
Difference-in-differences (DiD) estimates a program's causal effect by comparing the before-after change in a treated group with the before-after change in an untreated control group. It relies on the parallel trends assumption: without the program, both groups would have changed by the same amount. DiD can be computed from four group means or, equivalently, as the interaction coefficient in a regression.
Intuition and setup
- Used when a program was not randomized and outcomes are observed for a treated and a control group, both before and after implementation.
- Example: free school lunch in Sao Paulo (treated) from 2009; Rio (control); math test scores in 2008 and 2010.
- D1 = change over time in treated group = program effect + time trend (e.g., a soccer tournament in 2008 affecting scores).
- D2 = change over time in control group = estimate of the trend.
- DiD = D1 - D2.
- Equivalently: D3 = treated minus control in post period (effect + area differences); D4 = treated minus control in pre period (area differences); DiD = D3 - D4. Both versions are algebraically identical.
When to use DiD vs alternatives
- Random assignment: simple treatment-control difference is unbiased; DiD may still improve precision.
- Simple before-after difference only works if nothing else changed over time - rarely reasonable.
- If assignment depends only on observables, multiple regression with controls works.
- DiD helps when assignment and other group differences are unknown.
- Worked example: Sao Paulo scores rose 70, Rio rose 40, so DiD = 70 - 40 = 30 points.
Regression form
- Y = b0 + b1*Post + b2*Treat + b3*(Post x Treat) + error.
- E[Y | control, pre] = b0; E[Y | treated, post] = b0 + b1 + b2 + b3.
- Control change over time = b1; treated change = b1 + b3; DiD = b3, the interaction coefficient.
- Works with aggregate data, repeated cross-sections, or panel (longitudinal) data; individual data allows more flexible models.
- Reasons to use regression: automatic standard errors; can add controls X (e.g., socioeconomic status) for differing trends; controls reduce error variance and shrink standard errors.
Validity: parallel trends
- Pre-existing level differences between groups are fine (e.g., Sao Paulo chosen because kids were worse off).
- Invalid if trends unrelated to the program differ, e.g., Rio gets its own lunch program at the same time.
- If either D2-as-trend or D4-as-stable-gap assumption seems doubtful, the DiD estimate is suspect.
Key terms
- Difference-in-differences
- Change over time in the treated group minus change over time in the control group.
- Parallel trends assumption
- Absent treatment, treated and control groups would have followed the same trend over time.
- Interaction term
- Product Post x Treat in the DiD regression; its coefficient is the DiD estimate.
- Repeated cross-section
- Separate samples drawn in each period and group.
- Longitudinal (panel) data
- Data observing the same individuals in both periods.
Lesson 4
Machine Learning Basics for Economists
Machine learning develops algorithms that learn from data and generalize to unseen data. It splits into supervised learning (regression and classification with labeled data) and unsupervised learning (finding structure such as clusters or lower-dimensional representations without labels). Key algorithms range from linear regression to ensembles and neural networks.
Supervised vs unsupervised
- Supervised: features (inputs) predict a target with known labels in training data.
- Regression predicts a continuous target (house price); classification predicts a discrete class (spam/not spam).
- Unsupervised: no labels; find structure, e.g., clustering emails into unspecified groups.
Core supervised algorithms
- Linear regression: fits a line minimizing sum of squared errors; basis for many advanced methods.
- Logistic regression: fits a sigmoid to give class probabilities.
- KNN: non-parametric; predicts average/majority of K nearest neighbors. Small K overfits, very large K underfits; K is a hyperparameter tuned e.g. by cross-validation.
- SVM: finds decision boundary (hyperplane) with maximum margin; support vectors lie on margin edge; kernels (linear, polynomial, RBF, sigmoid) give nonlinear boundaries via implicit feature engineering; strong in high dimensions.
- Naive Bayes: uses Bayes' theorem assuming independent features; fast, good for text/spam.
Trees, ensembles and neural networks
- Decision tree: series of yes/no splits aiming for pure leaves.
- Bagging: train models on bootstrap samples; random forest = many trees voting, with random feature exclusion to decorrelate trees.
- Boosting: models trained in sequence, each fixing previous errors (AdaBoost, gradient boosting, XGBoost); often more accurate but more overfitting-prone and slower.
- Neural networks: hidden layers learn hidden features automatically; many layers = deep learning.
Unsupervised methods
- K-means: pick K random centers, assign points to nearest, recompute centers, repeat until stable.
- Other clustering: hierarchical, DBSCAN (no K needed, arbitrary shapes).
- Dimensionality reduction: PCA finds orthogonal directions of maximum variance; drop low-variance components.
Key terms
- Feature
- Input/independent variable used for prediction.
- Label
- Known true value of the target in training data.
- Hyperparameter
- Setting chosen before training, like K in KNN or K-means.
- Overfitting
- Fitting training data too closely so the model generalizes poorly.
- Ensemble
- Combining many simple models into a stronger one (bagging, boosting).
- Principal component
- Direction capturing maximal remaining variance in the data.
