Skip to content

linearmodels/clustered-without-clusters

Pack Severity Fix Versions
linearmodels error none, edit by hand All versions

A clustered fit in linearmodels needs the intended groups. With no groups, models such as PanelOLS and IV2SLS put each observation in its own cluster, while IVGMM raises an error. Pass clusters= with the group labels, or use cluster_entity=True or cluster_time=True on a panel model that supports it.

result = PanelOLS(y, X, entity_effects=True).fit(cov_type="clustered")
result = PanelOLS(y, X, entity_effects=True).fit(cov_type="clustered", cluster_entity=True)

The rule reports fit(cov_type="clustered") with none of cluster_entity, cluster_time and clusters. Models that accept omitted groups put each observation in its own cluster, which gives the heteroskedasticity robust covariance. IVGMM, IVGMMCUE and IVJIVE require groups.

The rule reads cov_type when it is a string literal and skips calls that unpack arguments.

xpd has no automatic fix. Because the code does not show whether to cluster by entity or by time, and the groups for clusters= are not in the file. Make the change by hand.

On 7.1 and 7.0, PanelOLS with entity effects on the wage panel gave the following first standard errors:

fit First standard error
cov_type="clustered" 0.041301
cov_type="robust" 0.041301
cov_type="clustered", cluster_entity=True 0.049385

PooledOLS, RandomEffects and FirstDifferenceOLS gave equal pairs in the same way. So did OLS, IV2SLS, IVLIML, AbsorbingLS, SUR and IV3SLS, which take the groups as clusters=, such as 0.427716 both ways on IV2SLS. IVGMM, IVGMMCUE and IVJIVE raise TypeError instead, and the rule's message for them says so.

The rule reports 31 calls in 9 of the 950 public files, each a regression table whose standard errors are said to be clustered and are not. BetweenOLS is left out, because it refuses cluster_entity and its rows are the entities already.

For linearmodels PanelOLS, PooledOLS, RandomEffects and FirstDifferenceOLS, cov_type="clustered" with no cluster_entity, cluster_time or clusters puts each observation in a cluster of its own, which is the robust covariance. PanelOLS on the wage panel gave the standard error 0.0413, the same as cov_type="robust", against 0.0494 with cluster_entity=True. Add cluster_entity=True to cluster by entity.

See all rules in the linearmodels pack.