Skip to content

get_prediction intervals ignore df_resid_inference with cluster-robust use_t=True #10743

Closed

Description

@bhaskargurram-ai

With cov_type="cluster" and use_t=True, conf_int, t_test and pvalues use the t distribution with n_groups - 1 degrees of freedom (df_resid_inference), but get_prediction uses df_resid. The confidence and prediction intervals from summary_frame() / conf_int() are therefore too narrow. This affects OLS/WLS/GLS, GLM (which=None, "mean", "linear") and discrete models.

import numpy as np, pandas as pd
import statsmodels.formula.api as smf

rng = np.random.default_rng(0)
G = 10
df = pd.DataFrame({"g": np.repeat(np.arange(G), 20)})
df["x"] = rng.normal(size=len(df)) + rng.normal(size=G)[df.g]
df["y"] = 1 + 0.5 * df.x + rng.normal(size=G)[df.g] + rng.normal(size=len(df))

res = smf.ols("y ~ x", df).fit(cov_type="cluster", cov_kwds={"groups": df.g}, use_t=True)
print(res.get_prediction(pd.DataFrame({"x": [1.0]})).conf_int())
print(res.t_test([1, 1]).conf_int())  # same linear combination

Output on 0.15.0 and on main (27e7475):

[[1.01182206 3.28075021]]   # get_prediction, t(198)
[[0.84491023 3.44766204]]   # t_test, t(9)

Expected: both give [0.8449, 3.4477], i.e. x0 @ params ± t(0.975, G - 1) * se. The standard error is the same in both (0.5753), only the quantile differs (t with 198 vs 9 df). I checked the expected interval with a hand-coded cluster sandwich and scipy.stats.t.ppf(0.975, 9).

In a small simulation (G = 10 clusters of 20, 2000 replications) the nominal 95% interval for E[y | x = 1] covered the true value 89.0% of the time with the current code and 93.4% with n_groups - 1 degrees of freedom.

Cause: regression/_prediction.get_prediction and get_prediction_glm, get_prediction_linear and get_prediction_monotonic in base/_prediction_inference.py pass df=self.df_resid. The results classes use getattr(self, "df_resid_inference", self.df_resid) for pvalues, conf_int and t_test (#1903 fixed this for pvalues).

Versions: statsmodels 0.15.0 (PyPI) and main 27e7475, numpy 2.4.6, scipy 1.17.1, pandas 3.1.1, Python 3.11.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions