获取标准化残差,cook和#x27;在Python中轻松地使用s值、hat值(杠杆)等?

获取标准化残差,cook和#x27;在Python中轻松地使用s值、hat值(杠杆)等?,python,scikit-learn,linear-regression,statsmodels,Python,Scikit Learn,Linear Regression,Statsmodels,我正在寻找拟合线性回归后的影响统计数据。在R中,我可以这样获得它们(例如): hatvalues(fitted_model) #hatvalues (leverage) cooks.distance(fitted_model) #Cook's D values rstandard(fitted_model) #standardized residuals rstudent(fitted_model) #studentized residuals #import statsmodels impo

我正在寻找拟合线性回归后的影响统计数据。在R中,我可以这样获得它们(例如):

hatvalues(fitted_model) #hatvalues (leverage)
cooks.distance(fitted_model) #Cook's D values
rstandard(fitted_model) #standardized residuals
rstudent(fitted_model) #studentized residuals
#import statsmodels
import statsmodels.api as sm

#Fit linear model to any dataset
model = sm.OLS(Y,X)
results = model.fit()

#Creating a dataframe that includes the studentized residuals
sm.regression.linear_model.OLSResults.outlier_test(results)
等等

在安装了这样的模型之后,在Python中使用statsmodels时,如何获得相同的统计数据:

hatvalues(fitted_model) #hatvalues (leverage)
cooks.distance(fitted_model) #Cook's D values
rstandard(fitted_model) #standardized residuals
rstudent(fitted_model) #studentized residuals
#import statsmodels
import statsmodels.api as sm

#Fit linear model to any dataset
model = sm.OLS(Y,X)
results = model.fit()

#Creating a dataframe that includes the studentized residuals
sm.regression.linear_model.OLSResults.outlier_test(results)
编辑:见下面的答案…

我在这里找到了它:


虽然接受的答案是正确的,但我发现在拟合模型后,将统计数据作为影响实例的实例属性单独访问(
statsmodels.regression.linear\u model.OLSResults.get\u influence
)会有所帮助。这使我不必为
摘要框架编制索引
,因为我只对其中一个统计数据感兴趣,而不是所有统计数据。因此,也许这有助于其他人:

import statsmodels.api as sm

#Fit linear model to any dataset
model = sm.OLS(Y,X)
results = model.fit()

#create instance of influence
influence = results.get_influence()

#leverage (hat values)
leverage = influence.hat_matrix_diag

#Cook's D values (and p-values) as tuple of arrays
cooks_d = influence.cooks_distance

#standardized residuals
standardized_residuals = influence.resid_studentized_internal

#studentized residuals
studentized_residuals = influence.resid_studentized_external