【问题标题】:Calculate studentized deletion residuals and outlier_test() for the predictions of the OLS为 OLS 的预测计算学生化删除残差和 outlier_test()
【发布时间】:2019-12-24 09:28:47
【问题描述】:

我正在使用 statmodel OLS 进行迭代异常值消除。我已经安装了模型。

ols_result = sm.OLS(y,X).fit()

然后我可以使用

ols_result.outlier_test(method="bonf")

我正在删除超过 bonferroni p > %10 的样本,其中厨师距离在每次迭代中也是最高的。直到我没有得到 bonf(p)>%10 的样本,然后我得到了原始样本的子集。

假设我有 400 个样本,删除异常值后我得到了 380 个样本。现在我想用 400 个样本再次找到学生化删除残差和 bonferroni,以回归适合 380 个样本。看看删除的异常值是否真的是异常值。

这就是问题的开始。我一直在寻找简单的方法来使用 statmodels OLS 模型来获取拟合值的残差和厨师距离,而不是自己编写这些函数。但.outlier_test().get_influence() 似乎适用于OLS Result 对象。

你们有什么简单的方法可以在没有太多代码的情况下完成这些测试吗?

提前致谢

【问题讨论】:

    标签: python statistics linear-regression


    【解决方案1】:

    经过对这种实用性的深入研究。我找不到使用 statmodels 库的简单方法。相反,我编写了自己的代码,也从 statmodels 源代码中获得了帮助。如果有人需要我在下面分享的代码

    def internally_studentized_residual(X,Y,y_hat):
        """
    
        Calculate studentized residuals internal
    
        Parameters
        ______________________________________________________________________
    
        X:            List
                      Variable x-axis
    
        Y:            List
                      Response variable of the X 
    
    
        y_hat:        List
                      Predictions of the response variable with the given X
    
        Returns
        ______________________________________________________________________
    
        Dataframe :   List
                      Studentized Residuals   
    
    
        """
    
    #     print(len(Y))
        X = np.array(X, dtype=float)
        Y = np.array(Y, dtype=float)
        y_hat = np.array(y_hat,dtype=float)
        mean_X = np.mean(X)
        mean_Y = np.mean(Y)
        n = len(X)
    
    #     print(X.shape,Y.shape,y_hat.shape)
    
        residuals = Y - y_hat
    
        X_inverse = np.linalg.pinv(X.reshape(-1,1))[0]
        h_ii = X_inverse.T * X
    
        Var_e = math.sqrt(sum((Y - y_hat) ** 2)/(n-2))
        SE_regression = Var_e/((1-h_ii) ** 0.5)
        studentized_residuals = residuals/SE_regression
        return studentized_residuals
    
    def deleted_studentized_residual(X,Y,y_hat):
        """
    
        Calculate studentized residuals external
    
        Parameters
        ______________________________________________________________________
    
        X:            List
                      Variable x-axis
    
        Y:            List
                      Response variable of the X 
    
    
        y_hat:        List
                      Predictions of the response variable with the given X
    
        Returns
        ______________________________________________________________________
    
        Dataframe :   List
                      Studentized Residuals External  
    
    
        """
        #formula from https://newonlinecourses.science.psu.edu/stat501/node/401/
        r = internally_studentized_residual(X,Y,y_hat)
        n = len(r)
        return [r_i*math.sqrt((n-2-1)/(n-2-r_i**2)) for r_i in r]
    
    
    def outlier_test(X,Y,y_hat,alpha=0.1):
        """
    
        outlier test for the points
    
        Parameters
        ______________________________________________________________________
    
        X:            List
                      Variable x-axis
    
        Y:            List
                      Response variable of the X 
    
    
        y_hat:        List
                      Predictions of the response variable with the given X
    
        alpha:        float
                      alpha value for multiple test
    
        Returns
        ______________________________________________________________________
    
        Dataframe :   studentized, unadjusted p values and benferroni multiple 
                      test dataframe  
    
    
        """
        resid = deleted_studentized_residual(X,Y,y_hat)
        df = len(X) - 1 
        p_vals = stats.t.sf(np.abs(resid),df) * 2
        bonf_test = multipletests(p_vals,alpha,method="bonf")
        df_result = pd.DataFrame()
        df_result.loc[:,"student_resid"] = resid
        df_result.loc[:,"unadj_p"] = p_vals
        df_result.loc[:,"bonf(p)"] = bonf_test[1]
        df_result.index = X.index
    
        return df_result
    
    

    这可能是一个有点脏的代码。我没有时间清洁并提高效率。它使用numpy 模块和scipy.stats

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 2021-02-23
      • 1970-01-01
      • 2017-06-03
      • 2020-10-22
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 2013-09-10
      相关资源
      最近更新 更多