← All Economics manuals

Economics · Econometrics Economics

Econometrics Economics

From probability and least squares to causal identification, time series and modern prediction: a complete study path with explanations, worked examples, formulas, tables and graphs.

19chapters
202topics

18. Applied Econometrics with R

A reproducible workflow from raw data to defensible regression tables

Reproducibility and the analysis pipeline

Applied econometrics is a computational discipline. Reliable results require more than a single successful regression command: data origins, transformations, missing-value decisions, sample construction, model formulas and the chosen standard-error estimator must be documented. A reproducible script should start from the same raw input files, perform all cleaning transformations explicitly and recreate estimates, diagnostic plots and final tables without manual edits.

R is an open-source programming environment for data analysis. The familiar workflow uses a script editor such as RStudio, CSV or other data imports, objects holding data frames and models, and packages for inference or visualisation. The basic modelling command lm estimates OLS. The formula y ~ x1 + x2 includes an intercept by default; y ~ x1 + x2 - 1 removes it. Syntax should never be mistaken for identification: the model formula encodes variables, not whether their coefficients have causal interpretations.

A minimal reproducible project

The following script uses an explicitly synthetic sample. It is intended to demonstrate software behaviour, not to emulate the coefficients of a particular empirical textbook dataset. Because the random seed is fixed, running the code on a compatible installation should reproduce the same generated observations.

set.seed(2026)
n <- 500
education <- round(pmin(20, pmax(8, rnorm(n, 13, 2))))
experience <- runif(n, 0, 30)
ability <- rnorm(n)
logwage <- 1.6 + 0.07 * education + 0.02 * experience +
           0.12 * ability + rnorm(n, sd = 0.30)
dat <- data.frame(education, experience, ability, logwage)
summary(dat)
plot(dat$education, dat$logwage, pch = 19, cex = .5,
     xlab = "Years of education", ylab = "Log wages")

In this data-generating process, the structural conditional education coefficient is 0.07 when ability and experience are included. Altering the simulation so ability also influences education would create omitted-variable bias in an education-only regression. Simulated examples are useful because the researcher knows the truth and can assess estimation behaviour rather than guessing it from the fitted output.

Simple versus multiple regression

m1 <- lm(logwage ~ education, data = dat)
m2 <- lm(logwage ~ education + experience + ability, data = dat)
summary(m1)
summary(m2)
confint(m2)

Compare the education coefficients, the estimated residual scale and the confidence intervals. The discrepancy between simple and multiple slopes reveals how adjustment changes conditional comparisons; it is not automatically a proof of the direction or magnitude of omitted confounding in an unrelated observational dataset. Next modify the simulation so that education depends on ability and evaluate how the estimates change across repeated samples.

Transformations and interactions

m3 <- lm(logwage ~ education + experience + I(experience^2), data = dat)
m4 <- lm(logwage ~ education * ability + experience, data = dat)
summary(m3)
summary(m4)
# Marginal effect of experience at a selected experience level:
b <- coef(m3)
me_exp_10 <- b["experience"] + 2 * b["I(experience^2)"] * 10
print(me_exp_10)

R's I() operator treats a mathematical expression as a single transformed predictor, while x1 * x2 expands to the main effects and interaction (x1 + x2 + x1:x2). A marginal-effect estimate is a function of several coefficients; calculate its uncertainty with the model covariance matrix rather than treating the components as independent.

Robust and clustered inference

library(lmtest)
library(sandwich)
coeftest(m2, vcov. = vcovHC(m2, type = "HC3"))
# For genuine clustered data with independent clusters:
# coeftest(m2, vcov. = vcovCL(m2, cluster = dat$cluster_id))

The HC3 estimate changes the estimated standard errors and test statistics, not the OLS coefficients. Cluster-based methods require an actual cluster identifier and a sampling structure that supports cluster-asymptotic inference. Inventing a cluster ID simply to obtain different standard errors would have no statistical justification.

General linear restrictions

The car package provides convenient Wald tests under the supplied covariance matrix. When testing several restrictions jointly with heteroskedasticity, it is important to request a robust covariance matrix rather than silently relying on the classical regression ANOVA table.

library(car)
linearHypothesis(m2, c("education = 0", "experience = 0"),
                 vcov. = vcovHC(m2, type = "HC3"))

Instrumental variables

For a real IV analysis, articulate why the excluded instrument affects the endogenous regressor and why it cannot affect the outcome through other channels. R formula notation in ivreg() separates the structural regression from the instrument set with |. The instrument set contains the excluded instruments and all included exogenous controls.

library(AER)
# Illustrative schema: z is an externally justified instrument for d
iv_fit <- ivreg(y ~ d + income + age | z + income + age,
                data = empirical_data)
summary(iv_fit, diagnostics = TRUE)
coeftest(iv_fit, vcov. = vcovHC(iv_fit, type = "HC1"))

This second template is intentionally not executable until the analyst supplies a real data frame with justified variable definitions. Running a regression without a credible instrument does not create a meaningful causal estimate.

Panel models

The fixest package provides concise estimation of unit and time fixed effects. A vertical bar inside feols separates regressors from fixed-effect dimensions. Clustering by unit allows flexible residual dependence within each unit, under the corresponding assumptions.

library(fixest)
model_fe <- feols(outcome ~ policy + control1 | region + year,
                  vcov = ~ region, data = panel_dat)
etable(model_fe)

If treatment changes by region and year, interpret the coefficient in light of the assignment mechanism and whether an ordinary FE parameter represents the desired causal estimand. When the research question is DID with staggered treatment, dedicated estimators may be needed rather than simply adding more fixed effects.

Model diagnostics and plots

par(mfrow = c(2, 2))
plot(m2)
par(mfrow = c(1, 1))
# Save a reproducible data summary:
capture.output(summary(dat), file = "descriptive_summary.txt")

The base R diagnostic suite includes residual-versus-fitted, Q–Q, scale-location and leverage-related plots. A residual–fitted plot may reveal nonlinearities and heteroskedasticity. Normal Q–Q departures deserve attention for exact small-sample inference but do not automatically invalidate a large-sample robust regression. Influential observations need substantive inspection instead of automatic deletion.

Reporting numerical results without overclaiming

A good empirical table names the dependent variable, the estimation method, standard errors (including their clustering level), observation count, fixed effects, important controls, and estimation sample. Accompany each major coefficient with a correctly unit-labelled interpretation and a discussion of causal identification. For interaction and log models, convert coefficients into marginal effects on meaningful scales. Specify the population and time period, and state where data or methods restrict generalisability.

StepEssential actionDiagnostic question
DataInspect, clean, transformAre units and missing values understood?
DesignDefine estimand and controlsIs variation plausibly exogenous?
EstimateChoose OLS, IV, FE or otherDoes method target the stated parameter?
InferChoose robust or clustered covarianceAre observations statistically independent?
ValidateCheck specification and sensitivityCould assumptions fail?
CommunicateReport coefficient, uncertainty and scopeWhat can the result genuinely establish?

A clean estimation sample and a reproducible prediction pipeline

Before comparing specifications, define the observation key and verify that it is unique. A firm-year panel should have one intended record per firm-year; a many-to-many merge can silently multiply observations and distort both coefficients and standard errors. Record the units, date range, outcome definition, missingness by variable and every sample restriction. A coefficient can change after adding a control because the model now uses a different sample, so compare nested specifications on a common sample when isolating the role of adjustment.

For prediction, divide data into training, validation and test portions according to the deployment setting. Fit imputation rules, scaling, feature selection and learned transformations using training data only, then apply the fitted rules to held-out data. Preserve training factor levels, contrasts, column names and column order. Repeated units should remain together when the goal is prediction for unseen units, and future dates must stay out of earlier forecasting folds. Save the random seed, package versions, model formula, preprocessing objects and final sample count. These records make a result reproducible; they do not replace a defensible identification argument.

CheckWhy it matters
Unique observation keysPrevents accidental multiplication of rows after joins
Common estimation sampleSeparates changes in controls from changes in observations
Training-only preprocessingPrevents information from held-out outcomes or covariates leaking into fit
Matching model-matrix columnsPreserves the meaning and order of trained coefficients
Saved seed and package versionsMakes stochastic and software-dependent steps traceable
Torna all’inizio ↑

Ripetizioni personalizzate · Da 15€/h

Un dubbio da chiarire?
Studiamolo insieme.

Offro lezioni personalizzate per studenti delle superiori e dell’università: spiegazioni chiare, esercizi guidati e un metodo costruito sulle tue esigenze. Scopri le materie e le tariffe, oppure prenota una lezione per lavorare sul tuo programma.