2015-01-04 65 views
1

我正在使用polyfit将我的数据拟合成一行。该线的方程式形式为y = mx + b。我正在尝试检索斜率上的误差和y截距上的误差。这是我的代码:检索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截距的标准偏差。我怎样才能做到这一点?

回答

5

如果可以使用最小二乘拟合,可以计算出斜率,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) 

功能是由菲利普PA费尔南德斯写的,最初发布here