Python中LOWESS的置信区间

问题描述 投票:23回答:2

如何计算Python中LOWESS回归的置信区间?我想将这些作为阴影区域添加到使用以下代码创建的LOESS图中(除了statsmodel之外的其他包也很好)。

import numpy as np
import pylab as plt
import statsmodels.api as sm

x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.2
lowess = sm.nonparametric.lowess(y, x, frac=0.1)

plt.plot(x, y, '+')
plt.plot(lowess[:, 0], lowess[:, 1])
plt.show()

我在webblog Serious Stats中添加了一个带有置信区间的示例图(它是使用R中的ggplot创建的)。

python statsmodels loess
2个回答
13
投票

LOESS没有标准错误的明确概念。在这种情况下,它并不意味着什么。从那以后,你坚持使用蛮力方法。

引导您的数据。您将使用LOESS曲线拟合自举数据。请参阅本页中间部分,以了解您的工作情况。 http://statweb.stanford.edu/~susan/courses/s208/node20.html

enter image description here

获得大量不同的LOESS曲线后,您可以找到顶部和底部的第X个百分位数。

enter image description here


3
投票

这是一个非常古老的问题,但它是第一个出现在谷歌搜索上的问题之一。您可以使用scikit-misc中的loess()函数执行此操作。这是一个例子(我试图保留你原来的变量名称,但我稍微提高了噪音以使其更加明显)

import numpy as np
import pylab as plt
from skmisc.loess import loess

x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.4

l = loess(x,y)
l.fit()
pred = l.predict(x, stderror=True)
conf = pred.confidence()

lowess = pred.values
ll = conf.lower
ul = conf.upper

plt.plot(x, y, '+')
plt.plot(x, lowess)
plt.fill_between(x,ll,ul,alpha=.33)
plt.show()

结果:

loess smooth with CI

© www.soinside.com 2019 - 2024. All rights reserved.