You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PanelOLS模型异方差测试报错:'PanelEffectsResults'对象无'resid'属性

Fixing the 'PanelEffectsResults' object has no attribute 'resid' Error

Hey there, let's break down what's going on here and fix that error quickly.

The core issue here is a simple naming mix-up: when you use linearmodels.PanelOLS, the fitted results object (est) stores residuals in the resids attribute (plural, with an 's'), not resid (singular). That's why you're getting the "no attribute 'resid'" error.

Step-by-Step Fix

  1. Correct the residual call
    Replace est.resid with est.resids in your Breusch-Pagan test line. Your updated heteroscedasticity test code should look like this:

    diag.het_breuschpagan(est.resids, est.model.exog, retres=False)
    
  2. Double-check your workflow (optional but important)
    I noticed you first split your data with scikit-learn's train_test_split and fit a regular LinearRegression, but then you fit a PanelOLS on the entire original dataset (not the training set). If your goal is to test heteroscedasticity on the model trained with your training data, you should refit the PanelOLS using only X_train and Y_train instead of the full X and Y1. Here's how that adjusted workflow might look:

    # Training and testing set split
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y1, test_size=0.20, random_state=8)
    
    # Add constant to training data (for PanelOLS)
    X_train_const = sm.add_constant(X_train)
    
    # Fit PanelOLS on training data
    model = PanelOLS(Y_train, X_train_const, entity_effects=True)
    est = model.fit()
    
    # Run Breusch-Pagan test with correct residual attribute
    diag.het_breuschpagan(est.resids, est.model.exog, retres=False)
    

This should resolve the attribute error and align your workflow if you intended to test the training-set model's heteroscedasticity.

内容的提问来源于stack exchange,提问作者Julian Lo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:46:59