31

Python で LOWESS 回帰の信頼区間を計算するにはどうすればよいですか? これらを、次のコードで作成された LOESS プロットに影付きの領域として追加したいと思います (statsmodels 以外のパッケージも問題ありません)。

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()

ウェブブログSerious Statsから、以下に信頼区間を含むプロットの例を追加しました(Rのggplotを使用して作成されています)。

ここに画像の説明を入力

4

3 に答える 3

4

私のプロジェクトでは、時系列モデリングの間隔を作成する必要があり、手順をより効率的にするために、tsmoothieを作成しました: 時系列の平滑化と異常値検出のための Python ライブラリをベクトル化した方法です。

間隔を計算する可能性とともに、さまざまな平滑化アルゴリズムを提供します。

の場合LowessSmoother

import numpy as np
import matplotlib.pyplot as plt
from tsmoothie.smoother import *
from tsmoothie.utils_func import sim_randomwalk

# generate 10 randomwalks of length 200
np.random.seed(33)
data = sim_randomwalk(n_series=10, timesteps=200, 
                      process_noise=10, measure_noise=30)

# operate smoothing
smoother = LowessSmoother(smooth_fraction=0.1, iterations=1)
smoother.smooth(data)

# generate intervals
low, up = smoother.get_intervals('prediction_interval', confidence=0.05)

# plot the first smoothed timeseries with intervals
plt.figure(figsize=(11,6))
plt.plot(smoother.smooth_data[0], linewidth=3, color='blue')
plt.plot(smoother.data[0], '.k')
plt.fill_between(range(len(smoother.data[0])), low[0], up[0], alpha=0.3)

ここに画像の説明を入力

また、tsmoothie は複数の時系列の平滑化をベクトル化された方法で実行できることも指摘します。これが誰かを助けることを願っています

于 2020-08-24T11:48:36.840 に答える