How to compute Studentized Residuals in Python?

前端 未结 4 500
青春惊慌失措
青春惊慌失措 2020-12-11 17:42

I\'ve tried searching for an answer to this problem but so far I haven\'t found any. I used statsmodel to implement an Ordinary Least Squares regression model on a mean-impu

4条回答
  •  失恋的感觉
    2020-12-11 18:13

    Use the OLSRresults.outlier_test() function to produce a dataset that contains the studentized residual for each observation.

    For example:

    #import necessary packages and functions
    import numpy as np
    import pandas as pd
    import statsmodels.api as sm
    from statsmodels.formula.api import ols
    
    #create dataset
    df = pd.DataFrame({'rating': [90, 85, 82, 88, 94, 90, 76, 75, 87, 86],
                       'points': [25, 20, 14, 16, 27, 20, 12, 15, 14, 19]})
    
    #fit simple linear regression model
    model = ols('rating ~ points', data=df).fit()
    
    #calculate studentized residuals
    stud_res = model.outlier_test()
    
    #display studentized residuals
    print(stud_res)
    
    student_resid    unadj_p     bonf(p)
    0   -0.486471   0.641494    1.000000
    1   -0.491937   0.637814    1.000000
    2    0.172006   0.868300    1.000000
    3    1.287711   0.238781    1.000000
    4    0.106923   0.917850    1.000000
    5    0.748842   0.478355    1.000000
    6   -0.968124   0.365234    1.000000
    7   -2.409911   0.046780    0.467801
    8    1.688046   0.135258    1.000000
    9   -0.014163   0.989095    1.000000

    This tutorial provides a full explanation: https://www.statology.org/studentized-residuals-in-python/

提交回复
热议问题