For a simple linear regression, you can calculate studentized residuals using following
define mean of X and Y as :
mean_X = sum(X) / len(X)
mean_Y = sum(Y) / len(Y)
Now you have to estimate coefficients beta_0 and beta_1
beta1 = sum([(X[i] - mean_X)*(Y[i] - mean_Y) for i in range(len(X))]) / sum([(X[i] - mean_X)**2 for i in range(len(X))])
beta0 = mean_Y - beta1 * mean_X
Now you need to find fitted values, by using this
y_hat = [beta0 + beta1*X[i] for i in range(len(X))]
Now compute Residuals, which is Y - Y_hat
residuals = [Y[i] - y_hat[i] for i in range(len(Y))]
We need to find H
matrix which is
where X
is the matrix of our independent variables.
To find leverage, we have to take the diagonal elements of H
matrix, in the following way:
leverage = numpy.diagonal(H)
Find Standard Error if regression as
Var_e = sum([(Y[i] - y_hat[i])**2 for i in range(len(Y)) ]) / (len(Y) -2)
SE_regression = math.sqrt(Var_e*[(1-leverage[i]) for i in range len(leverage)])
Now you can compute Studentized Residuals
studentized_residuals = [residuals[i]/SE_regression for i in range(len(residuals))]
Note that we have two types of studentized residuals. One is Internally Studentized Residuals and second is Externally Studentized Residuals
My solution finds Internally Studentized Residuals.
I made corrections in my calculation. For externally studentized residuals, refer @kkawabat's answer