如何使用scipy计算线性回归的斜率误差?

问题描述 投票:0回答:1

为了计算线性回归的斜率误差(使用scipy),我想用

slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
sd_slope = (len(x)*std_err**2)/(len(x)*(x**2).sum()-x.sum()**2)

. 这个公式来自维基百科。为什么会有这样的错误?

python numpy scipy linear-regression
1个回答
0
投票

最后你想计算的是估计斜率的方差,即斜率std误差的平方。结果是 std_err 所回 .linregress 已经是斜率的标准误差。

但如果你还想手动计算,你需要将其替换为 std_err 在你的等式中 s (残差标准差的估计值),即为 sqrt(SSE / (n-2)) 哪儿 n == len(x),样本量。所以,在代码中,

# Get the predicted values
yhat = intercept + slope * x

# Get SSE i.e. sum of squared errors (or RSS: residual sum of squares)
SSE = np.sum((y - yhat)**2)

# Calculate the "s" the estimate of standard deviation of residuals
s = np.sqrt(SSE / (n-2))

# Now your equation (it will give variance)
your_eq = (n*s**2) / (n*(x**2).sum() - x.sum()**2)

# Square root of the above value gives the std error
sd_slope = np.sqrt(your_eq)

# You can see that it is equal to (within precision) std_err of scipy
assert np.isclose(sd_slope, std_err)
© www.soinside.com 2019 - 2024. All rights reserved.