简体   繁体   English

在Python中,如何拟合最小分位数b样条回归线?

[英]In Python, how do you fit the minimum quantile b-spline regression line?

You can find the minimum quantile regression line fit like this: 您可以找到最小分位数回归线拟合度,如下所示:

import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg

mod = smf.quantreg('y ~ x', data)
res = mod.fit(q = 0.000001)

But what if you want to find the minimum b-spline regression fit line? 但是,如果您想找到最小的b样条回归拟合线怎么办?

If you want cubic b-splines you can do this: 如果要三次B样条曲线,可以执行以下操作:

#!/usr/bin/env python3

import matplotlib.pyplot as plt
import numpy as np
import statsmodels.formula.api as smf


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

mod = smf.quantreg('y ~ bs(x, df=9)', dict(x=x, y=y))
res = mod.fit(q=0.000001)
print(res.summary())

plt.plot(x, y, '.')
plt.plot(x, res.predict(), 'r')
plt.show()

You will need to play with the degrees of freedom ( df parameter) or specify the knots parameter instead. 您将需要使用自由度( df参数)或改为指定knots参数。 Depending on your data you may wish to use cr() for natural cubic splines or cc() for cyclic cubic splines. 根据您的数据,您可能希望对自然三次样条使用cr()或对于循环三次样条使用cc() See http://patsy.readthedocs.io/en/latest/spline-regression.html for more details. 有关更多详细信息,请参见http://patsy.readthedocs.io/en/latest/spline-regression.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM