Access standardized residuals, cook's values, hatvalues (leverage) etc. easily in Python?

前端 未结 2 2089
闹比i
闹比i 2021-02-08 20:47

I am looking for influence statistics after fitting a linear regression. In R I can obtain them (e.g.) like this:

hatvalues(fitted_model) #hatvalues (leverage)
c         


        
相关标签:
2条回答
  • 2021-02-08 21:31

    Although the accepted answer is correct, I found it helpful to separately access the statistics as instance attributes of an influence instance (statsmodels.regression.linear_model.OLSResults.get_influence) after I fit my model. This saved me from having to index the summary_frame as I was only interested in one of the statistics and not all of them. So maybe this helps somebody else:

    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
    
    0 讨论(0)
  • 2021-02-08 21:51

    I found it here:

    http://www.statsmodels.org/dev/generated/statsmodels.stats.outliers_influence.OLSInfluence.summary_frame.html

    OLSInfluence.summary_frame()
    
    0 讨论(0)
提交回复
热议问题