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.

df{int, array_like[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.

degree{int, array_like[int]}

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

include_interceptbool

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.

constraints{None, str, array}

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_names{list[str], None}

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_kwdsNone or list of dict

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 use with get_knots_bsplines are

  • knots : None or array interior knots

  • spacing : ‘quantile’ or ‘equal’

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

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

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

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.

Attributes:
smootherslist of univariate smooth component instances
basisdesign matrix, array of spline bases columns for all components
penalty_matriceslist of penalty matrices, one for each smooth term
dim_basisnumber of columns in the basis
k_variablesnumber of smooth components
col_namescreated names 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