1

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?

193381
  • 121
  • 4

1 Answers1

3

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.

Tavin
  • 390
  • 2
  • 13