I am trying to port this functionality into python
> x <- 0:10
> y <- x**2
> lm(y ~ ns(x,df=2))
Such as:
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
x = pd.DataFrame(np.arange(11))
y = x**2
formula="y ~ cr(x, df = 3)"
reg = smf.ols(formula,data=x).fit()
print(res.summary())
However with this python formulation, I cannot set df<3. Any suggestions how I can have a natural spline in python with two degrees of freedom, and use it in patsy as an R style equation?
These are clearly generating different bases: I'm not sure what the difference is, but the exploration below might help.
Note that
crmimics the basis construction frommgcv(see here; in addition to Simon Wood's book they are also discussed here), whilens()is a natural spline built on a B-spline basis. I believe thatsplines::bs()andpatsy.bswould match perfectly, but there is nopatsy.ns.