我使用statsmodel statsmodels.stats.outliers_influence.variance_inflation_factor对吗?

时间:2019-08-01 04:37:14

标签: python machine-learning regression

目前,我正在使用VIF检测多重共线性。但是,网上几乎没有示例可供参考,因此我尝试自己使用它。

cat_var = df[["BsmtExposure","MSZoning","Exterior1st","MSSubClass","GarageType","GarageFinish"]].apply(preprocessing.LabelEncoder().fit_transform)
dfX = df[["OverallQual","ExterQual","GrLivArea","1stFlrSF","GarageCars","BsmtQual","HeatingQC","YearBuilt"]]
data_categorical = dfX.join(cat_var)
sm_data_categorical = sm.add_constant(data_categorical)
vifDf = sm_data_categorical
vifDf = vifDf.drop(["OverallQual","YearBuilt"],axis=1)
feature = vifDf.columns
print(feature)
vif = [variance_inflation_factor(vifDf[feature].values,feature.get_loc(var)) for var in feature]
print(vif)

Output:
[139.09182494163923, 1.9269169697717614, 1.794083234373851, 1.828696948899336, 1.6357605533337554, 1.680843256052908, 1.4734276288799137, 1.2599932369972506, 1.0704636681342352, 1.1139451723386682, 1.2658662212832537, 1.4714527943918547, 1.2728931548738207]

我也使用过statsmodels.add_constant。

1 个答案:

答案 0 :(得分:0)

Aaron,有一个名为mcTest的R程序包,该程序包对变量进行多重线性诊断。有关实现的更多信息,请参见以下链接(http://rfaqs.com/mctest-r-package-detection-collinearity-among-regressors)。 希望对您有所帮助。