← Back to list

Step by step on how to perform Mixed Effect Modelling using Python

Introduction

siddharth ranjan · 2023-03-19 16:01 · 43 claps · 3.9 min read
#machine-learning #regression #mixed-effects #data-science
Open on Medium ↗
Wiki topics: ML · Machine Learning EDU · Education & Learning 🔬 · Science · General

Step by step on how to perform Mixed Effect Modelling using Python

Introduction

Mixed Effect Modelling is a statistical approach used to model relationships between variables when there is clustering or grouping in the data. This technique is particularly useful when analyzing data with nested or hierarchical structures, where there are multiple observations at different levels of grouping.

In this article, we will explore how to use Python for mixed effect modelling in the retail vertical.

Why Use Mixed Effect Modelling?

In retail, there are often multiple levels of grouping within the data. For example, sales data may be collected at the store level, but there may be multiple stores in different geographic regions, each with their own unique characteristics. Mixed effect modelling can help us account for these nested structures and estimate the impact of various factors on sales at each level.

Mixed effect modelling is also useful when dealing with longitudinal data, where we have multiple observations for each subject over time. This approach can help us identify trends and patterns in the data, while accounting for the correlation between observations for the same subject.

Step-by-Step Approach

1- Data Preparation

First, we need to prepare our data for analysis. In this example, we will be using a dataset containing sales data for a retail chain with multiple stores in different regions. Our dataset contains information on sales, promotions, store location, and other factors.

We will begin by importing the necessary libraries and loading the data into a pandas dataframe:

import pandas as pd
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf

data = pd.read_csv('sales_data.csv')

2- Model Specification

Next, we need to specify our mixed effect model. This involves selecting the variables we want to include in the model, as well as specifying the random effects.

model = smf.mixedlm('sales ~ promotion + region + (1|store)', data=data)

In this example, we are modeling sales as a function of promotion and region, with a random effect for each store.

Why Store is selected as random effect?

In the mixed effect modelling example for the retail vertical, we used the “store” variable as a random effect in the model. This is because the retail chain has multiple stores with their own unique characteristics, and we want to account for the clustering of data at the store level.

However, there may be other logical groups that we could have selected depending on the specific research question and data structure. For example, if the retail chain operates in multiple cities or countries, we could use “city” or “country” as a random effect to account for the differences in sales patterns across different locations.

Similarly, if the retail chain has multiple product categories, we could use “product category” as a random effect to account for the variation in sales patterns across different categories. The choice of the logical group to use as a random effect depends on the research question and the structure of the data.

3- Model Fitting

Once we have specified our model, we can fit it to our data using the fit() function.

result = model.fit()

4- Model Evaluation

Finally, we can evaluate our model and examine the results. We can use the summary() function to view a summary of the model output, including coefficients, standard errors, and p-values.

print(result.summary())

Interpreting the results of a mixed effect model can be a bit tricky, but there are a few key pieces of information that we can use to understand the impact of different factors on the outcome variable (in this case, sales). Here are some guidelines for interpreting the results:

  1. Coefficients: The coefficients in the model output represent the estimated effect of each predictor variable on the outcome variable. For example, if the coefficient for promotion is 0.5, this means that a one-unit increase in promotion is associated with a 0.5-unit increase in sales, all else being equal.
  2. Standard errors: The standard errors in the model output represent the variability in the estimated coefficients. A larger standard error indicates more uncertainty in the estimated effect of the predictor variable.
  3. P-values: The p-values in the model output indicate the statistical significance of each predictor variable. A p-value less than 0.05 indicates that the estimated effect of the predictor variable is statistically significant at the 5% level, meaning that it is unlikely to have occurred by chance.
  4. Random effects: The random effects in the model output represent the variability in the intercept (the value of the outcome variable when all predictor variables are zero) across different levels of the grouping variable. For example, if we have a random effect for “store”, this means that the intercept may vary across different stores, reflecting the fact that some stores may have higher or lower sales than others.

In the example of the retail vertical, we used the “store” variable as a random effect in the model. The coefficient estimate for the store variable represents the variation in sales across different stores, after controlling for the effects of promotion and region. A positive coefficient indicates that the store has higher sales than average, while a negative coefficient indicates lower sales than average.

In addition to interpreting the individual coefficients, it is also important to examine the overall fit of the model. This can be done by looking at measures such as the R-squared value or the Akaike information criterion (AIC), which indicate how well the model fits.

Conclusion

Mixed effect modelling is a powerful statistical technique that can help businesses in the retail vertical optimize their marketing spend and identify the most effective channels for driving sales

We can also look at the random effects to see how the intercept varies across different levels of the grouping variable. Overall, the goal is to identify the factors that have the greatest impact on sales, while accounting for the clustering of data at the group level.


메타데이터
post_id
90ccc23c2c6b
slug
mixed-effect-modelling-90ccc23c2c6b
url
https://medium.com/@sidd1/mixed-effect-modelling-90ccc23c2c6b
canonical_url
https://medium.com/@sidd1/mixed-effect-modelling-90ccc23c2c6b
author_url
https://medium.com/@sidd1
status
ok
fetched_at
2026-07-23 16:45:57