【发布时间】:2021-08-17 08:24:08
【问题描述】:
我正在开发一个时间序列数据框,例如:
df = pd.DataFrame({'year':[ '1990','1991','1992','1993','1994','1995','1996','1997','1998','1999','2000'],
'count':[96,184,148,154,160,149,124,274,322,301,300]})
我有兴趣使用Theil-Sen's slope estimator 和Scipy 中的函数找到回归线
https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.mstats.theilslopes.html
如下codesn-p:
from scipy import stats
x = df['year'].astype(float)
y = df['count']
res = stats.theilslopes(y, x, 0.90)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'b.')
ax.plot(x, res[1] + res[0] * x, 'r-')
plt.ylabel('count')
plt.xlabel('year')
plt.show()
我可以找到 Theil-Sen's regression line,但是在添加可视化 confidence interval 的代码时,可视化有些不现实。
ax.plot(x, res[1] + res[2] * x, 'r--')
ax.plot(x, res[1] + res[3] * x, 'r--')
有没有办法,我可以用theil-sen's slope estimator和confidence interval得到一个可视化,如下图:
我们将非常感谢您的帮助。 谢谢!
【问题讨论】:
标签: python pandas scipy time-series stat