python中回归的残差标准误差

时间:2020-08-10 04:04:36

标签: python regression

有人知道获取回归的残差标准误差或回归的标准误差的命令或方法吗?

我使用以下命令获取系数和R平方,我想学习一种类似这样的命令来计算回归的标准误差:

#For the coefficients:
model = smf.OLS(y, X).fit()
print(model.params)

#For the R-squared:
model = smf.OLS(y, X).fit()
print(model.rsquared)

我将非常感谢能够帮助我解决此问题的人。

2 个答案:

答案 0 :(得分:0)

RSE是误差标准偏差的估计值。 您可以这样:

import math
import numpy as np


def RSE(y_true, y_predicted):
    """
    - y_true: Actual values
    - y_predicted: Predicted values
    """
    y_true = np.array(y_true)
    y_predicted = np.array(y_predicted)
    RSS = np.sum(np.square(y_true - y_predicted))

    rse = math.sqrt(RSS / (len(y_true) - 2))
    return rse


if __name__ == "__main__":
    y_true = [1, 2, 3]
    y_predicted = [2, 3, 4]
    print(RSE(y_true, y_predicted))

答案 1 :(得分:0)

适合 smf.ols

# Residual Standard Error of the model
np.sqrt(fitted_model.scale)