statsmodels.gam.smooth_basis.BSplines#

class statsmodels.gam.smooth_basis.BSplines(x, df, degree, include_intercept=False, constraints=None, variable_names=None, knot_kwds=None)[source]#

additive smooth components using B-Splines

This creates and holds the B-Spline basis function for several components.

Parameters:
xarray_like, 1-D or 2-D

underlying explanatory variable for smooth terms. If 2-dimensional, then observations should be in rows and explanatory variables in columns.

dfint or sequence of int

number of basis functions or degrees of freedom; should be equal in length to the number of columns of x; may be an integer if x has one column or is 1-D.

degreeint or sequence of int

degree(s) of the spline; the same length and type rules apply as to df

include_interceptbool, optional

If False, then the basis functions are transformed so that they do not include a constant. This avoids perfect collinearity if a constant or several components are included in the model.

constraintsNone, str, or ndarray, optional

Constraints are used to transform the basis functions to satisfy those constraints. constraints = ‘center’ applies a linear transform to remove the constant and center the basis functions.

variable_nameslist of str, optional

The names for the underlying explanatory variables, x used in for creating the column and parameter names for the basis functions. If x is a pandas object, then the names will be taken from it.

knot_kwdslist of dict, optional

option for the knot selection. By default knots are selected in the same way as in patsy, however the number of knots is independent of keeping or removing the constant. Interior knot selection is based on quantiles of the data and is the same in patsy and mgcv. Boundary points are at the limits of the data range. The available options used with get_knots_bsplines are

  • knots : array_like, optional interior knots

  • spacing : ‘quantile’ or ‘equal’

  • lower_bound : float, optional location of lower boundary knots, all boundary knots are at the same point

  • upper_bound : float, optional location of upper boundary knots, all boundary knots are at the same point

  • all_knots : array_like, optional If all knots are provided, then those will be taken as given and all other options will be ignored.

Attributes:
smootherslist of univariate smooth component instances

The individual smooth components making up the additive model.

basisndarray

Design matrix of spline basis columns for all components.

penalty_matriceslist of ndarray

Penalty matrices, one for each smooth term.

dim_basisint

Number of columns in the basis.

k_variablesint

Number of smooth components.

col_nameslist of str

Names created for the basis columns.

There are additional attributes about the specification of the splines
and some attributes mainly for internal use.

Methods

transform(x_new)

Create the spline basis for new observations

Notes

A constant in the spline basis function can be removed in two different ways. The first is by dropping one basis column and normalizing the remaining columns. This is obtained by the default include_intercept=False, constraints=None The second option is by using the centering transform which is a linear transformation of all basis functions. As a consequence of the transformation, the B-spline basis functions do not have locally bounded support anymore. This is obtained constraints='center'. In this case include_intercept will be automatically set to True to avoid dropping an additional column.

Methods

transform(x_new)

Create the spline basis for new observations