Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Spline features

The modelling tools in modelmatrix allow for construction of spline functions of features.

Force rebuild

Index(['Sales', 'CompPrice', 'Income', 'Advertising', 'Population', 'Price', 'ShelveLoc', 'Age', 'Education', 'Urban', 'US'], dtype='str')

Let’s make a term representing a cubic spline for Population. We’ll use knots based on the deciles.

array([ 58.9, 110.4, 160. , 218.6, 272. , 317.8, 366. , 412.2, 467. ])

The object bs_pop does not refer to any data yet, it must be included in a ModelSpec object and fit using the fit method.

Compare to R

We can compare our polynomials to a similar function in R

Error importing in API mode: ModuleNotFoundError("No module named '_rinterface_cffi_api'")
Trying to import in ABI mode.
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
Cell In[6], line 1
----> 1 get_ipython().run_line_magic('load_ext', 'rpy2.ipython')

File /opt/hostedtoolcache/Python/3.12.15/x64/lib/python3.12/importlib/__init__.py:90, in import_module(name, package)
     88             break
     89         level += 1
---> 90 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1387, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1360, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:1334, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:950, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:999, in _LoaderBasics.exec_module(self, module)

File <frozen importlib._bootstrap>:488, in _call_with_frames_removed(f, *args, **kwds)

File /opt/hostedtoolcache/Python/3.12.15/x64/lib/python3.12/site-packages/rpy2/ipython/__init__.py:1
----> 1 from . import rmagic
      3 load_ipython_extension = rmagic.load_ipython_extension

File /opt/hostedtoolcache/Python/3.12.15/x64/lib/python3.12/site-packages/rpy2/ipython/rmagic.py:59
     56 import textwrap
     57 import typing
---> 59 import rpy2.rinterface_lib.callbacks
     61 import rpy2.rinterface as ri
     63 import rpy2.rinterface_lib.openrlib

File /opt/hostedtoolcache/Python/3.12.15/x64/lib/python3.12/site-packages/rpy2/rinterface_lib/callbacks.py:15
     13 from rpy2.rinterface_lib import openrlib
     14 from rpy2.rinterface_lib import ffi_proxy
---> 15 from rpy2.rinterface_lib import conversion
     17 logger = logging.getLogger(__name__)
     19 _CCHAR_ENCODING = sys.getdefaultencoding()

File /opt/hostedtoolcache/Python/3.12.15/x64/lib/python3.12/site-packages/rpy2/rinterface_lib/conversion.py:13
     11 from typing import Union
     12 from rpy2.rinterface_lib import openrlib
---> 13 from rpy2.rinterface_lib import _rinterface_capi as _rinterface
     15 ffi = openrlib.ffi
     17 _R_RPY2_MAP = {}  # type: Dict[int, Type]

File /opt/hostedtoolcache/Python/3.12.15/x64/lib/python3.12/site-packages/rpy2/rinterface_lib/_rinterface_capi.py:288
    284             return res
    287 if FFI_MODE is ffi_proxy.InterfaceType.ABI:
--> 288     _getvar_wrap = openrlib.rlib.R_getVar
    289 elif FFI_MODE is ffi_proxy.InterfaceType.API:
    290     _getvar_wrap = _getvar

error: symbol 'R_getVar' not found in library '/usr/lib/R/lib/libR.so': /usr/lib/R/lib/libR.so: undefined symbol: R_getVar

We’ll recompute these features using bs in R. The default knot selection of the modelmatrix and R versions are slightly different so we just fix the set of internal knots.

Underlying model

As for poly, the computation of the B-splines is done by a special sklearn transformer.

Natural splines

Natural cubic splines are also implemented.

Intercept

Looking at py_features we see it contains columns: [Population**i for i in range(1, 4)]. That is, it doesn’t contain an intercept, the order 0 term. This can be include with intercept=True. This means that the column space includes an intercept, though there is no specific column labeled as intercept.