The tutorial explains how to fit generalized linear models (GLMs) in Python using the Statsmodels library, focusing on the formula‑based API. It walks through importing Statsmodels, specifying a GLM with the glm class, and fitting it via the fit method. The core of model definition is the R‑style formula, where the response variable is expressed as a function of explanatory variables, with syntax for categorical variables (C()), intercept handling (add_constant or -1), interaction terms (semicolon vs multiplication), and inline transformations. The family argument selects the distribution (e.g., Gaussian, Binomial, Poisson) and its default link function, though alternative links can be supplied. After fitting, the summary method provides model statistics such as log‑likelihood, deviance, and detailed coefficient tables (estimates, standard errors, z‑scores, p‑values, and confidence intervals). Users can extract just the coefficients via params and obtain confidence intervals with confint, adjusting the confidence level via the alpha argument. For prediction, a test dataset containing all model variables is supplied to the predict method; if omitted, the original training data are used. The tutorial emphasizes practical steps for model specification, fitting, result interpretation, and generating predictions.
Chapters
Now that you understand the building blocks of GLMs, it is time to learn how to fit a GLM in Python. The starting point is the Stats Models library which is used for statistical and econometric analysis. We import the library using statsmodels API from 050 version. The formula based entry is supported which we can import as follows, or we can import the GLM function directly via statsmodels Formula API. To fit a model we first need to describe the model using the model class glm. Then the method fit is used to fit the model. Very detailed results of the model fit can be analyzed via the summary method and finally we can compute predictions using the predict method. There are two ways to describe the model using formulas or arrays. If you're familiar with R language then you will appreciate the ability to fit a GLM using the R style formulas. The Stats models uses the PATSI package to convert formulas and data to the matrices which are then used in model fitting. Note that if you are using the error based method, the intercept is not included. By default you can add it using the addconstant function. For this course we will use the formula based method. The main arguments are formula, data and family. The formula is at the heart of the modeling function where the response or output is modeled as a function of the explanatory variables or the inputs. Each explanatory variable is specified and separated with a plus sign. Note that the formula needs to be enclosed in quotation marks. There are different ways we can represent explanatory variables in the model. Categorical variables are enclosed with capital C. Removing the intercept is done with -1. The interaction terms are written in two ways depending on the need where the semicolon applies to only the interaction term, whereas the multiplication symbol will also, in addition to the interaction term, add individual variables. We will see how this works in chapter four. Lastly, we can also add transformations of the variables directly in the formula family. Distributions are in the family's namespace. Here we list only three which we will use in this course. The default link function is denoted in parentheses, but you could choose other link functions available for each distribution. However, if you choose to use a non default LINQ function you would have to specify it directly. To view the results of the model fit. We use the summary method which provides the main information on model fit such as the model description, model statistics such as log likelihood and deviance and and estimated model parameters with their corresponding statistics. The estimated parameters are given by Coeff with their standard error z scores, P values and 95% confidence intervals. To only view the regression coefficients, we can use Param's given model fit. Similarly, the confidence intervals for the parameter estimates can be obtained by calling confint. The default is 95%, which you can change. Using the alpha argument. With calls argument, you can specify which confidence intervals to return. When doing predictive modeling, your final goal is to compute and assess predictions given the fitted model and test data. The first step is to specify the test data, which should contain all the variables you have included in the final model. Note that if you don't specify test data, the function uses data with which the model was fit. Final predictions are computed with predict. Now let's try the.