If you want cubic b-splines you can do this:
#!/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. Depending on your data you may wish to use cr()
for natural cubic splines or cc()
for cyclic cubic splines. See http://patsy.readthedocs.io/en/latest/spline-regression.html for more details.