Sunday, 23 April 2023

Find spline knots by variable in python

When fitting a linear GAM model in python imposing n_splines=5, a piecewise-linear function is fitted:

import statsmodels.api as sm
from pygam import LinearGAM

data = sm.datasets.get_rdataset('mtcars').data

Y = data['mpg']
X = data.drop("mpg",axis=1)

model = LinearGAM(spline_order=1,n_splines=5).fit(X, Y)

By using .coef from fitted model, the coefficientes for every splines can be recovered for further analysis:

model.coef_

However, how can we obtain the sections of each of the 5 splines for each variable?

As an example, for cyl variable we would fit the following splines:

enter image description here

The 5 sections are determined by the knots, so, in the plot we would see the variable limits for the computed betas. (i.e.:4-5,5-6,6-7,7-8).

The only thing I find kn the docu: model.edge_knots: array-like of floats of length 2. The minimum and maximum domain of the spline function. Which corresponds for cyl to [4,8].



from Find spline knots by variable in python

No comments:

Post a Comment