1

I need to add a constrain to my linear regression below so that y_pred [i] >= y_pred [i+1]

from patsy import dmatrix
import statsmodels.api as sm

xknot = dmatrix("bs(x, knots=(0.0001,1,2,4,6,8,9.3), degree=3, include_intercept=False)", 
                        {"x": x},return_type='dataframe') 

fit = sm.GLM(y, xknot).fit() 

y_pred = fit1.predict(dmatrix("bs(x, knots=(0.0001,1,2,4,6,8,9.3), include_intercept=False)",
                             {"x": x}, return_type='dataframe')) ```
Fahad Ward
  • 11
  • 2
  • This is not supported by statsmodels, and I don't think there is an easy way to do this. Statsmodels does not support inequality constraints. Furthermore, it is difficult to impose monotonicity and shape constraints on splines. – Josef Mar 22 '20 at 17:15
  • Thank you @Josef, I'm trying to force coefficient (b) in y=bx to be something like this b[i]>= b[i+1] – Fahad Ward Mar 22 '20 at 18:38
  • If you create the model `mod = sm.GLM(y, xknot)`, then you could use the loglikelihood `mod.loglike(params)` with scipy optimizers directly to see whether any of the constrained optimizers can solve the problem. If you figure out a way, then reporting this in a statsmodels issue could be used for extending statsmodels. – Josef Mar 22 '20 at 20:16

0 Answers0