Fixed effects

Panel and grouped data often have a level that belongs to the group (a worker, a firm, a school) and that the features do not explain. regression_forest and causal_forest take such groups after a | in the formula, and remove their effects with hdfe-stream:

import forest_stream as fs

# the conditional mean of y given x1 and x2, net of a firm effect
fit = fs.regression_forest("y ~ x1 + x2 | firm", "panel.parquet")

# several fixed effects, and an interaction
fs.regression_forest("y ~ x1 + x2 | worker + firm^year", "panel.parquet")

# the effect of w on y by x1 and x2, with worker and firm effects removed
fs.causal_forest("y ~ w | x1 + x2 | worker + firm", "panel.parquet")

The model behind a regression forest with fixed effects is additive:

y = g(x) + (the group effects) + error

and the forest estimates g. Its predictions are therefore net of the group effects: they say how y moves with the features, not what a particular group’s level is. Prediction needs only the feature columns (fixed_effects="group_means" is the exception).

Choosing a method

fixed_effects= picks how the group effects are handled in a regression forest.

fixed_effects=

What it does

What it assumes

Cost

"backfit" (default)

Alternates a forest and the group effects until the effects settle

The additive model

Several forests and several hdfe-stream fits

"outcome_within"

Subtracts each group’s mean of the outcome once, then fits the forest

The features are unrelated to the groups

One hdfe-stream fit and one forest

"group_means"

Leaves the outcome alone and adds each group’s mean of each feature as extra features

None about the groups, but it estimates something else (see below)

One forest, on more features

Use "backfit" unless you know the features are unrelated to the groups or you need the speed. The features usually are related to the groups: workers with a higher effect tend to have different jobs, and firms with a higher effect tend to hire different workers. "outcome_within" is exact when they are not and estimates the wrong function when they are, and more data does not help. "group_means" is the fastest, and answers a different question.

For a causal forest the same choice applies to the nuisance forests, which are described below.

Backfitting

Each round does the following.

  1. Fit a forest to y less the current group effects.

  2. Take the forest’s out-of-bag predictions: for each row, the prediction of the trees that did not use it. This keeps the effects from absorbing the forest’s overfit.

  3. Re-estimate the group effects by regressing y less those predictions on the fixed effects (hdfe-stream).

The first round starts from the group means of y. Rounds repeat until the effects stop changing. The final forest, with all of num_trees, is fitted to y less the final effects. The rounds use backfit_num_trees trees each (default a quarter of num_trees, at least 100) and the same seed, so one round differs from the next only through the outcome it is fitted to.

By default the rounds after the first do not grow new trees. They keep the first round’s splits and estimate each leaf’s value again on the updated residual, one pass over the data per batch of trees. In our simulations the estimates, their standard errors and the coverage of their intervals match growing every round’s trees afresh, at roughly half to two-thirds of the time. backfit_warm_start=False grows new trees every round.

When it stops

After each round the package measures how much the effects changed: the root mean square change over rows. It compares that with the typical standard error of the effects, which is sigma * sqrt(rank / rows), where sigma is the standard deviation of what is left of y after the forest and the effects, and rank is the number of parameters in the fixed effects (hdfe-stream reports it). With one fixed effect this is sigma * sqrt(groups / rows), because each effect is a mean of its group’s rows.

Backfitting stops when the change is below backfit_tolerance times that standard error. The default is 0.1: the effects have stopped moving by more than a tenth of their own statistical precision, which cannot matter to anything estimated from them. A tighter tolerance buys nothing a user could see and costs rounds. This is why the tolerance is in standard errors and not in units of the outcome: the outcome’s scale says nothing about how precisely a group’s effect is estimated, which depends on how many rows the group has and how noisy the outcome is.

max_iterations (default 100) is the most rounds. If it is reached first, a FixedEffectsWarning says how large the last change was. Slow convergence is typical when the features are close to functions of the groups, so that the forest and the effects can trade variation with each other. More rounds, a larger tolerance, or a different method are then the options; the diagnostics below show which.

Each round’s forest differs from the last one in its sampling noise, so the changes shrink and then level off at a floor instead of reaching zero. A tolerance below that floor can never be met. So the rounds also end, and count as converged, when the changes have stopped shrinking while they are within a few times the tolerance. More trees per round (backfit_num_trees) lowers the floor.

If hdfe-stream does not report the rank, the scale is instead the noise that reseeding alone makes in the effects, which the package measures once: it fits a second forest to the same outcome with another seed and looks at how far apart the two sets of effects are. The tolerance then means a fraction of that noise.

What it reports

fit.backfit is a dictionary:

Key

Meaning

iterations

Rounds run

converged

Whether the stopping rule was met

stopped

"tolerance", "noise_floor" or "max_iterations"

changes

The change after each round, in units of scale (compare with backfit_tolerance)

changes_abs

The same, as a root mean square, in the outcome’s units

scale, scale_kind

The unit of changes: "standard_error", or "seed_noise" when that is the fallback

sigma

The standard deviation of the final residuals

noise

The change that reseeding alone makes, when it was measured

seconds, forest_seconds, effects_seconds

Time in all, in forests, and in hdfe-stream fits

A healthy run shows changes falling round by round to below the tolerance. Look at the list when a fit is slow or warns: a list that falls slowly and steadily can mean that the features nearly determine the groups, and a list that stalls above the tolerance can mean too few trees per round.

To follow the rounds as they run, pass verbose=True.

What it does not give you

The variance that predict returns is the forest’s, and it treats the estimated group effects as known. With many rows per group the error in the effects is small next to the forest’s, and the variance is a good guide. With few rows per group the effects carry more error than the variance allows for, and the intervals will tend to be somewhat too narrow; a loose backfit_tolerance adds to this, so keep the default when groups are small.

Groups of one row

A group with a single row has an effect that fits that row’s outcome exactly. By default (fixef_rm="none") the forest keeps these rows and gives each its own effect, so predictions with include_fixed_effects=True work for them. With fixef_rm="singleton" the rows are left out of the effect estimates, as hdfe-stream’s regressions do by default: the effects come only from groups with two or more rows. A left-out row gets no group effect, so when you predict it counts as a group not seen at fit (see new_groups). The number left out is in fit.backfit["singletons_dropped"].

Outcome within

fixed_effects="outcome_within" subtracts each group’s mean of the outcome and fits the forest to what remains. It is one hdfe-stream fit and one forest.

It is the right answer when the features are unrelated to the groups, and only then. The group mean of the outcome includes the average of g(x) over the group’s rows, so the forest ends up estimating g(x) less a quantity that depends on x whenever the features differ across groups.

To check, the fit reports in fit.between_group_share the share of each feature’s variance that lies between groups (the R-squared of the feature on the fixed effects), and warns when any exceeds 10 percent. A feature that is mostly constant within groups, such as a firm’s size in a worker-firm panel, will trigger it.

Group means as features

fixed_effects="group_means" leaves the outcome as it is. For each fixed effect it adds, for each feature, the mean of that feature within the group. The forest can then learn the part of the group effect that those means explain. It is a single forest, with p features plus p per fixed effect.

It estimates something different from the other two. The forest’s prediction is the mean of y given the features and their group means, which includes the part of the group effect that those means explain. The part they do not explain stays in the error. It is not g(x) net of the group effects, and the two are equal only when the group means explain none of the effect.

  • Sampling. The rows of a group share a leftover effect, so they are sampled together. cluster is by default the fixed effect, when there is exactly one; with several you must say which to use.

  • Prediction. The group means come from the data the forest was fitted on, so predict needs the fixed-effect columns as well as the features, and every group must have been in that data. A row from a new group raises an error.

  • Importance. variable_importance lists the mean features too, named <feature>__mean_by_<fixed effect>.

Causal forests

A causal forest removes the outcome’s and the treatment’s dependence on the features and the fixed effects before it looks for how the treatment’s effect varies. There are two ways, chosen by nuisance=:

  • "forest" (the default) estimates each one’s conditional mean with an out-of-bag regression forest, and subtracts it. With fixed effects in the formula, each of the two forests is fitted with the method in fixed_effects=, "backfit" or "outcome_within", and the same stopping rule. fit.backfit then has an entry for "y" and one for "w". nuisance_num_trees sets the trees in each.

  • "linear" subtracts the fixed effects and a linear function of the features, in one hdfe-stream fit. It is much faster, and adequate when the outcome and the treatment are close to linear in the features.

"group_means" is not available for a causal forest. Use nuisance="linear" where speed matters.

Whichever is chosen, the causal forest’s own splits look at the features only. The fixed effects enter through the nuisance step.

Memory and disk

Backfitting keeps a few vectors of one number per row on disk (the outcome, the effects, the forest’s predictions, and the current residual) and holds the out-of-bag predictions of one round in memory, 16 bytes per row. profile=True includes them in its estimate. The binned copy of the data is built once and shared by all the rounds.