Cross-validation with fixed effects¶
With more than one fixed effect, cv_fixed_effects chooses how the fixed
effects enter the cross-validation:
fit = lasso_stream("y ~ x1 + x2 + i(occupation) | worker + firm", data) # "full_sample"
fit = lasso_stream("y ~ x1 + x2 + i(occupation) | worker + firm", data,
cv_fixed_effects="refit")
"full_sample" (the default) partials the fixed effects out once, from all
the rows, and cross-validates the residuals. It needs one pass over the data.
Each held-out row is then residualized with fixed effects that its own outcome
helped estimate.
"refit" estimates the fixed effects again on each fold’s training rows,
so no held-out outcome reaches its own residual. It is exact, and takes about
as many fits as there are folds.
With one fixed effect, the default is already exact when the folds are its
groups, which they are unless fold_by= says otherwise: a group’s mean comes
from its own rows, all in one fold.
Which to use¶
For choosing the penalty, use the default. On simulated panels the two choose the same penalty, or one a step away on the path, and the fits predict equally well:
lambda_min was the same in 70% of replications, and otherwise almost always one step of the path away (about 10% in the penalty);
the prediction error of the coefficients at lambda_min was the same at the median, and within 7% either way in 90% of replications;
where lambda_1se differed (mainly with many small worker groups of uneven size), the default’s choice predicted better, by 20 to 30%.
For the cross-validated error itself, as an estimate of out-of-sample error,
use "refit" when cells are small. The default’s error is off by about 1%
when the groups of the largest fixed effect are large, but with small groups in
every dimension, as in a matched employer-employee panel with few observations
per worker and per firm, it was 8 to 13% too low, and with small worker groups
of uneven size and large firms 5% too high.
"refit" took 8 to 15 times as long as the default.
The simulations¶
benchmarks/fe_cv_study.py: 40,000 rows, 30 covariates of which 5 matter,
worker and firm effects (and, in half the scenarios, year effects), 10
replications of each of 32 scenarios:
worker groups |
200 of about 200 rows, or 8,000 of about 5 |
firms |
100 of about 400 rows, or 2,000 of about 20 |
sizes |
uniform, or power-law (a few large groups, many small ones) |
covariates |
nearly independent of the fixed effects, or with large worker and firm components |
The folds were the default, groups of the worker effect. The prediction error compared is (b - beta)’ S (b - beta), with beta the true coefficients and S the covariates’ covariance after the fixed effects are partialled out.