python - 检索 y 轴截距的标准差

标签 python statistics linear-regression

我正在使用 polyfit 将我的数据拟合成一条直线。直线方程的形式为 y = mx + b。我正在尝试检索斜率上的误差和 y 轴截距上的误差。这是我的代码:

fit, res, _, _, _ = np.polyfit(X,Y,1, full = True)

此方法返回残差。但我不想要残差。所以这是我使用的另一种方法:

slope, intercept, r_value, p_value, std_err = stats.linregress(X,Y)

我知道 std_err 返回斜坡上的错误。我仍然需要得到 y 轴截距的标准差。我该怎么做?

最佳答案

如果可以使用最小二乘法拟合,则可以使用以下函数计算斜率、y 截距、相关系数、斜率的标准差和 y 截距的标准差:

import numpy as np

def lsqfity(X, Y):
    """
    Calculate a "MODEL-1" least squares fit.

    The line is fit by MINIMIZING the residuals in Y only.

    The equation of the line is:     Y = my * X + by.

    Equations are from Bevington & Robinson (1992)
    Data Reduction and Error Analysis for the Physical Sciences, 2nd Ed."
    pp: 104, 108-109, 199.

    Data are input and output as follows:

    my, by, ry, smy, sby = lsqfity(X,Y)
    X     =    x data (vector)
    Y     =    y data (vector)
    my    =    slope
    by    =    y-intercept
    ry    =    correlation coefficient
    smy   =    standard deviation of the slope
    sby   =    standard deviation of the y-intercept

    """

    X, Y = map(np.asanyarray, (X, Y))

    # Determine the size of the vector.
    n = len(X)

    # Calculate the sums.

    Sx = np.sum(X)
    Sy = np.sum(Y)
    Sx2 = np.sum(X ** 2)
    Sxy = np.sum(X * Y)
    Sy2 = np.sum(Y ** 2)

    # Calculate re-used expressions.
    num = n * Sxy - Sx * Sy
    den = n * Sx2 - Sx ** 2

    # Calculate my, by, ry, s2, smy and sby.
    my = num / den
    by = (Sx2 * Sy - Sx * Sxy) / den
    ry = num / (np.sqrt(den) * np.sqrt(n * Sy2 - Sy ** 2))

    diff = Y - by - my * X

    s2 = np.sum(diff * diff) / (n - 2)
    smy = np.sqrt(n * s2 / den)
    sby = np.sqrt(Sx2 * s2 / den)

    return my, by, ry, smy, sby    

print lsqfity([0,2,4,6,8],[0,3,6,9,12])

输出:

(1, 0, 1.0, 0.0, 2.4494897427831779)

该函数由 Filipe P. A. Fernandes 编写,最初发布于 here .

关于python - 检索 y 轴截距的标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764220/

相关文章:

python - 在 Elementtree 中找不到元素

python - 如何通过标记现有数据帧的内容来创建新数据帧?

python - 在 python 中替换正则表达式

python-2.7 - Python(Scipy): Finding the scale parameter (standard deviation) of a gaussian distribution

machine-learning - 变分推理、变分贝叶斯和变分 EM 之间有什么关系?

python - 10折交叉验证python单变量回归

r - 如何使用 lm() 修复此无效类型错误?

python - 大数据的sklearn线性回归

python - Docker Selenium : selenium. common.exceptions.WebDriverException : Message: Service chromedriver unexpectedly exited. 状态代码为:127

sql-server-2008 - 如何判断上次访问 sql 表的时间