# Partial predictions

In climate economics and in other settings, we often would like to estimate a **response function**, or the outcome as a function of some covariate, i.e., \(y = f(T)\). Most of the time, \(T\) stands for temperature. Figure 3 in Carleton and Hsiang (2016) documents a bunch of different response functions from the literature.

These models, however, usually include lots of controls, including fixed effects. Consider the following (fairly generic) estimating equation:

\[y_{it} = f(T; \beta) + \phi_i + \varepsilon_{it}\]
This model includes unit fixed effects (\(\phi_i\) and \(\phi_t\)}). We include these usually it’s only reasonable to interpret the relationship between \(y\) and \(f(T)\) as causal **if** we have accounted for potential confounding factors in the cross-sectional and time-series relationships between climate and the outcome. Put another way, it wouldn’t make much sense to attribute the difference between the GDP per capita of Switzerland and the GDP per capita of Mexico to the differences in climate alone, so instead we control for those countries’ average characteristics using fixed effects and estimate the causal relationship using only **residual variation** in temperature and the outcome. That is to say, we zoom in on years when Switzerland was, say, exceptionally warm and compare its GDP per capita to years when it was exceptionally cold, and then do the same for Mexico.

Computationally, this could mean including a large set of fixed effects, so estimating these models via ordinary least-squares (OLS) using dummy variables is a nonstarter for settings with thousands of fixed effects, computationally speaking. Instead, we use programs such as `fixest`

(there are many others, but `fixest`

is the best one I know of, right now), which use fancy mathematical tricks to alleviate the computational burden.

One of the most intuitive ways to consider the relationships we estimate, particularly when \(f(T)\) is non-linear, is to just plot it, with standard errors included. That’s where we run into some issues. Unlike the base R function `lm`

, `fixest`

does not have a native prediction function that can supply standard errors. This is for a good reason: strictly speaking, predicting the outcome \(y\) **requires** that we observe the variance-covariance matrix (call it the “vcov”) for all of the terms, including the fixed effects. But one of the side effects of the mathematical tricks we use is that we can no longer obtain the full vcov.

So, we’re basically sunk if we want to plot \(\hat{y}\) with standard errors included. But what if we’re just interested in the relationship between \(T\) and \(y\), holding all the other controls constant? I.e., what if we just want to plot the response function \(f(T)\)? In that case, and assuming a linear-in-parameters model^{1}, we only need the vcov for the coefficients related to the variable we’re allowing to change, in this case \(T\).

To see how this works, consider that a response function is really just the expected value of the outcome across a range of \(T\) (say, 0 to 40) minus \(\hat{y}\) at a single value of \(T\) (say, 20 C). That is, for T = 40, the expected value of

\[E[y_{it}|T=40] - E[y_{it}|T = 20] = f(T=40; \hat{\beta}) + \phi_i - f(T=20; \hat{\beta}) - \phi_i = f(T=40; \hat{\beta}) - f(T=20; \hat{\beta})\]

As you can see, the fixed effects drop out, so for this linear-in-parameters model at least we don’t need the entire vcov to generate standard errors for each point on the response curve. So far as I know, though, there’s no easy way to run this kind of prediction without a bit of wrangling. Typically, I’ve used `survey::svycontrast`

to do this sort of thing (see here), but it always felt a bit fiddly. So the following code lets us do this kind of partial prediction easily, by just passing the variables we want to predict over.

```
library(fixest)
library(ggplot2)
library(cowplot)
N <- 100
df <- data.frame(x1 = rnorm(N), x2 = rnorm(N))
df$y <- 1 + 3 * df$x1 - 2 * df$x2 + rnorm(N, 0, 0.5)
fit <- feols(y ~ x1 + x2, data = df)
predict_partial <- function(fit, newdata) {
# Compute XB for Xs provided in newdata
# Note: Will not work for variables created via on the fly via formula (e.g., x:y, I(x^2), x^y)
X <- as.matrix(newdata[, names(newdata) %in% names(coef(fit))])
# Get indices of variables that we'll use from the fit
i <- which(names(coef(fit)) %in% colnames(X))
B <- coef(fit)[i]
sigma <- vcov(fit)[i, i]
# Compute
est <- as.vector(X %*% B)
se <- apply(X, MARGIN = 1, FUN = get_se, sigma = sigma)
# There is maybe a smarter matrix algebra way to do this?
list(est = est, se = se)
}
get_se <- function(r, sigma) {
# Compute linear combination, helper function for predict_partial
# Given numeric vector r (the constants) and vcov sigma (the ), compute SE
r <- matrix(r, nrow = 1)
sqrt(r %*% sigma %*% t(r))
}
newdata <- data.frame(x1 = seq(-3, 3, 1))
newdata$x2 <- newdata$x^2
plot_df <- cbind(newdata, as.data.frame(predict_partial(fit, newdata)))
ggplot(plot_df, aes(x = x1, y = est)) +
geom_line() +
geom_line(aes(y = est - se * 1.96), linetype = "dashed") +
geom_line(aes(y = est + se * 1.96), linetype = "dashed") +
theme_cowplot()
```

This bears more explanation, but I’m out of time for the day.

I’m not actually sure we need to assume this, but someone smarter than me can work out the math of what we could do in the non-linear-in-parameters case.↩︎