PanelOLS模型异方差测试报错:'PanelEffectsResults'对象无'resid'属性
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
Correct the residual call
Replaceest.residwithest.residsin your Breusch-Pagan test line. Your updated heteroscedasticity test code should look like this:diag.het_breuschpagan(est.resids, est.model.exog, retres=False)Double-check your workflow (optional but important)
I noticed you first split your data with scikit-learn'strain_test_splitand fit a regularLinearRegression, but then you fit aPanelOLSon 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 thePanelOLSusing onlyX_trainandY_traininstead of the fullXandY1. 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

