Compared with unit-by-time effects, grouping leaves variation with which to estimate slopes. Compared with additive unit effects, it allows type-specific time paths. The trade-off is approximation error and uncertainty about group membership. Increasing the number of groups can reduce approximation bias while leaving little within-group variation.
Trex constructs unit-level features, smooths them across nearest neighbors, clusters the smoothed features, and fits a linear model absorbing group-by-time indicators. The default feature construction stacks the panel history of covariates and, unless disabled, outcomes. Alternatively supply purpose-built unit-level classification features. This is a particular KNN-smoothed implementation, not every estimator covered by the grouped-FE literature.
Worked example: an auxiliary type measurement
The simulation includes a noisy auxiliary measurement of each unit’s latent time path. This makes classification informative by construction; it is not evidence that arbitrary empirical clustering will recover meaningful types.
classification_unit_ids explicitly aligns auxiliary rows with panel unit IDs. It avoids relying on accidental input order. If omitted, follow the ordering contract in the API documentation. The panel feature builder expects a complete unit-time grid; duplicate cells are averaged and missing cells are rejected, rather than imputed silently.
Figure 1: Estimated clusters summarized by their auxiliary measurement paths. Cluster numbers are arbitrary labels.
Group sizes: [13, 40, 7]
Design decisions and inference
The neighbor count controls smoothing, while the number of clusters controls the discreteness approximation. Standardizing embeddings changes what Euclidean closeness means. Large-scale searches are chunked; optional flash-kmeans acceleration is not required by the example and may have additional hardware/dependency requirements.
Using outcomes to form groups is not automatically invalid, but inference must account for estimated classification and the relevant asymptotic regime. The final absorbed-regression covariance is conditional on the estimated grouping; it is not a general post-clustering uncertainty guarantee. For an evaluation study, keep validation outcomes out of classification and compare against simpler additive FE and prespecified groups.
predict(X) returns the slope component, not arbitrary new-unit group effects. Group labels can permute across runs without changing the partition; compare co-membership or fitted paths rather than label integers.