The computational companion
Scan it.
Run it.
Change it.
Book QR targets, runnable scripts, and selected R/Python examples—organised around the printed code boxes.
57 code boxes28 R targets29 Python targets
chapter06 / multiple_regression
fit <- lm(price ~ living_area + monthly_fee,
data = apartments)
fit = smf.ols(
"price ~ living_area + monthly_fee",
data=apartments
).fit()
Same model · two ecosystems
Book-to-browser bridge
Every QR opens
inspectable source.
The printed QR codes point to small, box-level files that mirror the code in the book. Use those for quick reference; use the full chapter scripts to run an example end to end.
Rdf <- read.csv("apartment_price_data.csv")
xbar <- mean(df$living_area)
ybar <- mean(df$price)
s_xy <- sum((df$living_area-xbar) *
(df$price-ybar)) / (n-1)
corr_builtin <- cor(df$living_area, df$price)
Pythondf = pd.read_csv("apartment_price_data.csv")
xbar = df["living_area"].mean()
ybar = df["price"].mean()
s_xy = ((df["living_area"]-xbar) *
(df["price"]-ybar)).sum() / (n-1)
corr_builtin = df["living_area"].corr(df["price"])
Rols_model <- lm(
price ~ living_area + monthly_fee +
new_production, data = df)
coeftest(ols_model,
vcov = vcovHC(ols_model, type = "HC0"))
Pythonols_model = smf.ols(
"price ~ living_area + monthly_fee + "
"new_production", data=df).fit()
robust = ols_model.get_robustcov_results(
cov_type="HC0", use_t=True)
Rdf$rv <- df$share_seats_left_last_election - .5
df$Z <- ifelse(df$rv >= 0, 1, 0)
df$Zrv <- df$Z * df$rv
df_h <- df[abs(df$rv) <= .05, ]
rd_fs <- lm(left_coalition_last_term ~
Z + rv + Zrv, data = df_h)
Pythondf["rv"] = df["share_seats_left_last_election"] - .5
df["Z"] = (df["rv"] >= 0).astype(int)
df["Zrv"] = df["Z"] * df["rv"]
df_h = df.loc[df["rv"].abs() <= .05]
rd_fs = smf.ols(
"left_coalition_last_term ~ Z + rv + Zrv",
data=df_h).fit()
02 · 6 boxesCovariation in data
Correlation, ranks, and simple regression.
03 · 4 boxesProbability & inference
Simulation, testing, and confidence intervals.
04 · 2 boxesPopulation correlation
Inference for a population correlation.
05 · 5 boxesSimple linear regression
Inference, robust errors, and simulation.
06 · 10 boxesMultiple regression
Partial regression, F-tests, and intervals.
07 · 2 boxesNonlinear functional form
Air pollution and flexible specifications.
08 · 6 boxesDependent errors
Clustering, multilevel models, and fixed effects.
09 · 4 boxesBinary outcomes
Logistic regression and predicted probabilities.
10 · 10 boxesPrediction
Cross-validation, regularization, and trees.
11 · 4 boxesNonparametric regression
Regressograms, kernels, and nonparametric RD.
12 · 2 boxesTime series
Preparation, models, and diagnostics.
13 · 2 boxesCausal analysis
Regression discontinuity and robust estimation.