Generalized Additive Models GAM in the StatsModels library

In this recipe, we explain what are generalized additive models in the StatsModels library

Recipe Objective - Generalized Additive Models (GAM) in the StatsModels library?

The generalized additive model allows a penalty estimation of the smooth terms of the generalized linear model.

For more related projects -

https://www.dezyre.com/projects/data-science-projects/deep-learning-projects
https://www.dezyre.com/projects/data-science-projects/neural-network-projects

Example:

# Importing libraries
import statsmodels.api as sm
from statsmodels.gam.api import GLMGam, BSplines

# Importing heart dataset from statsmodels in the form of pandas dataframe
data = sm.datasets.heart.load_pandas()

# Storing data
X = data.data

# Using BSplines for smoother
smoother = BSplines(X[['age','censors']], df=[10, 5], degree=[3, 3])

# Fit and summarize GLMGam model
model = GLMGam.from_formula("survival ~ age + censors", data=X, smoother=smoother)
model = model.fit()

# Model summary
model.summary()

Output - 
Generalized Linear Model Regression Results
Dep. Variable:	survival	No. Observations:	69
Model:	GLMGam	Df Residuals:	58.00
Model Family:	Gaussian	Df Model:	10.00
Link Function:	identity	Scale:	1.7473e+05
Method:	PIRLS	Log-Likelihood:	-508.37
Date:	Mon, 22 Nov 2021	Deviance:	1.0134e+07
Time:	17:48:25	Pearson chi2:	1.01e+07
No. Iterations:	3		
Covariance Type:	nonrobust		
coef	std err	z	P>|z|	[0.025	0.975]
Intercept	550.7731	626.940	0.879	0.380	-678.006	1779.552
age	3.1801	11.221	0.283	0.777	-18.812	25.172
censors	-455.7141	120.386	-3.785	0.000	-691.666	-219.763
age_s0	-877.3193	795.380	-1.103	0.270	-2436.235	681.597
age_s1	468.7773	590.502	0.794	0.427	-688.586	1626.141
age_s2	59.9559	348.030	0.172	0.863	-622.170	742.082
age_s3	-31.0203	280.428	-0.111	0.912	-580.649	518.609
age_s4	124.3547	243.360	0.511	0.609	-352.623	601.332
age_s5	-203.0540	273.073	-0.744	0.457	-738.267	332.159
age_s6	-70.7493	564.662	-0.125	0.900	-1177.466	1035.967
age_s7	127.8988	747.057	0.171	0.864	-1336.307	1592.104
age_s8	-281.4048	389.188	-0.723	0.470	-1044.200	481.390
censors_s0	0	0	nan	nan	0	0
censors_s1	0	0	nan	nan	0	0
censors_s2	0	0	nan	nan	0	0
censors_s3	0	0	nan	nan	0	0

In this way, we can use Generalized Additive Models (GAM) in the StatsModels library.

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.