线性回归的标准差/误差
我有:
t = [0.0, 3.0, 5.0, 7.2, 10.0, 13.0, 15.0, 20.0, 25.0, 30.0, 35.0]
U = [12.5, 10.0, 7.6, 6.0, 4.4, 3.1, 2.5, 1.5, 1.0, 0.5, 0.3]
U_0 = 12.5
y = []
for number in U:
y.append(math.log(number/U_0, math.e))
(m, b) = np.polyfit(t, y, 1)
yp = np.polyval([m, b], t)
plt.plot(t, yp)
plt.show()
通过这样做,我得到了线性回归的结果,其中 m=-0.1071
和 b=0.0347
。
我该如何计算 m
值的偏差或误差呢?
我想要的格式是 m = -0.1071*(1+ 正负误差)
这里的
m
是k
,而b
是n
,在公式y=kx+n
中。
2 个回答
6
你可以使用 scipy.stats.linregress
这个工具:
m, b, r_value, p_value, std_err = stats.linregress(t, yp)
线性回归的质量是通过 r_value
这个相关系数来衡量的,当 r_value = 1.0
时,说明有完美的相关性。
需要注意的是,std_err
是估算出的斜率的标准误差,而不是来自线性回归的误差。
10
import numpy as np
import pandas as pd
import statsmodels.api as sm
import math
U = [12.5, 10.0, 7.6, 6.0, 4.4, 3.1, 2.5, 1.5, 1.0, 0.5, 0.3]
U_0 = 12.5
y = []
for number in U:
y.append(math.log(number/U_0, math.e))
y = np.array(y)
t = np.array([0.0, 3.0, 5.0, 7.2, 10.0, 13.0, 15.0, 20.0, 25.0, 30.0, 35.0])
t = sm.add_constant(t, prepend=False)
model = sm.OLS(y,t)
result = model.fit()
result.summary()