pygam#

Module: pygam#

Helper functions for GAMs#

This module contains functions used for the GAM lab of ISLP.

Functions#

ISLP.pygam.anova(*models, scale=None, useF=True)#

Compute an ANOVA table for a sequence of GAM models.

Parameters:
modelsGAMs

Sequence of fitted GAM models.

scalefloat

Estimate of noise level, defaults to None.

useFbool

If True use an F distribution for p-value computation based on degrees of freedom of largest model. Otherwise, use chi-squared.

Returns:
anova_table: pd.DataFrame

Notes

Implicitly assumes models are nested, fit on the same (X,y) with the same sample weights.

ISLP.pygam.approx_lam(X, term, df, W=None, CUTOFF=1000000000000.0)#

For a given term, try to find multiplier of penalty to achieve a specified degrees of freedom.

Parameters:
Xarray-like of shape (n_samples, n_features)

Input dataset

termTerm

Term for which we which to scale penalty

dffloat

Desired degrees of freedom

Warray-like (optional)

Diagonal weight matrix.

CUTOFFfloat

Search for solution in interval [0,CUTOFF].

Returns:
lamarray-like

Rescaled lam values.

Notes

The term must be part of a GAM that has already been fit.

ISLP.pygam.degrees_of_freedom(X, term, lam=None, W=None)#

For a given term, try to find multiplier of penalty to achieve a specified degrees of freedom.

Parameters:
Xarray-like of shape (n_samples, n_features)

Input dataset

termTerm

Term for which we which to scale penalty.

lamarray-like (optional)

Values at which to approximate degrees of freedom. If None, defaults to term.lam

Warray-like (optional)

Diagonal weight matrix.

Returns:
dffloat

Degrees of freedom as computed by the trace of the smoother matrix.

Notes

The term must be part of a GAM that has already been fit.

ISLP.pygam.plot(gam, term_idx, quantiles=[0.025, 0.975], ax=None, levels=None, partial_kwargs={'c': 'b', 'linewidth': 4}, err_kwargs={'c': 'r', 'linewidth': 4, 'ls': '--'}, bar_kwargs={'capsize': 10})#

Plot the fitted function of a term in a GAM model.

Parameters:
gamGAM

A fitted GAM model.

term_idxint

Which term in the GAM to plot?

quantiles[float, float], default=[0.025, 0.0975]

Which quantiles for pointwise confidence bands?

axmatplotlib axes, optional
levelssequence

For categorical features, which indices to include in plot. Defaults to all levels.

partial_kwargsdict

Keyword arguments for partial dependence plot for continuous variables.

err_kwargsdict

Keyword arguments for pointwise confidence bands for continuous variables.

bar_kwargsdict

Keyword arguments for barplot for for categorical variables.

Returns:
axmatplotlib axes

Axes with partial dependence plot added.