0% found this document useful (0 votes)
15 views24 pages

Market Mix Modeling Implementation Using Python _ by Akanksha Anand (Ak) _ Medium

The document discusses the implementation of Market Mix Modeling (MMM) using Python, focusing on optimizing advertising strategies based on sales data from various media channels. It covers data preprocessing, exploratory data analysis, and the application of linear regression to evaluate the impact of TV, radio, and newspaper advertising on sales. The results indicate a high explanatory power of the model, suggesting effective predictions of sales based on the advertising channels used.

Uploaded by

Liu Xiaotong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views24 pages

Market Mix Modeling Implementation Using Python _ by Akanksha Anand (Ak) _ Medium

The document discusses the implementation of Market Mix Modeling (MMM) using Python, focusing on optimizing advertising strategies based on sales data from various media channels. It covers data preprocessing, exploratory data analysis, and the application of linear regression to evaluate the impact of TV, radio, and newspaper advertising on sales. The results indicate a high explanatory power of the model, suggesting effective predictions of sales based on the advertising channels used.

Uploaded by

Liu Xiaotong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Get unlimited access to the best of Medium for less than $1/week. Become a member

Market Mix Modeling


Implementation using Python
Akanksha Anand (Ak) · Follow
6 min read · Jan 25, 2024

121 2

Time to determine the advertising channel that contributes the highest share
in profitability.

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 1/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Photo by Jon Tyson on Unsplash


https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 2/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Incorporating Python into Market Mix Modeling unlocks a powerful toolset


for data-driven decisions, empowering businesses to optimize strategies and
thrive in the dynamic landscape of marketing analytics.

Diving into the intricate world of Market Mix Modeling, we’ve dissected the
essential components in our previous discussion. Now, brace yourself as we
leap headfirst into the exciting realm of implementation. Don’t worry if
you’re still catching up on the basics — I’ve got you covered with a quick 10-
minute primer in these two reads:

Market Mix Modeling — Basics


A solution to cookie restrictions for Marketing Analysts
medium.com

MMM — A deeper dive


Let’s double tap into Market Mix Modeling variables, technique, use
cases and more
medium.com

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 3/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Dataset
I got the dataset from Kaggle. This dataset has 200 sales records for mediums
such as TV, radio and newspaper. I am aware that the dataset is very small.
For now, let’s step ahead with this. I am looking for a more robust dataset to
be able to perform various operations and share the same with you. If you
happen to have a sales dataset that is publicly available with huge records,
please share that in the chat and we can explore and perform MMM on that.

Importing Libraries
You know the drill here, importing the libraries that we are going to use to
perform transformation on the dataset going ahead.

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error as mae
from sklearn import metrics

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 4/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Don’t get scared seeing so many libraries, we are going to use them and
perform magic over the dataset. Trust me, the results received will make it
all worth it.

Data Preprocessing
Once we load the dataset, we try and check the size of the dataset, any null
values present and the datatypes of the column. Let’s take a glimpse of it.

Dataset: Advertisement

You can see that the first column ‘Unnamed’ has information about the
column number which is of no use to us. Let’s drop the column as we
proceed further.

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 5/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

df.drop(df.columns[0], axis=1, inplace=True)

Exploratory Data Analysis


It’s time to perform Exploratory Data Analysis and try to read between the
lines what the dataset is trying to say.

1. Correlation Matrix: Table that shows the correlation values for each pair-
relationship

plt.figure(figsize=(10,5))
sns.heatmap(df.corr(),
annot=True,
linewidths=.5,
center=0,
cbar=False,
cmap='RdBu_r')
plt.show()

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 6/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Correlation matrix

From the above correlation matrix, we can state that Sales and TV have a
strong correlation(0.78), with Sales and Radio showing a medium
correlation(0.58) whereas Sales and Newspapers are highly
uncorrelated(0.23).

2. Feature Importance: Feature importance enables the assessment of the


significance of each input variable in predicting the output variable. A
feature is deemed important when shuffling its values increases model error,

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 7/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

indicating that the model heavily relied on the feature for making
predictions.

X = df.loc[:, df.columns != 'sales']


y = df['sales']

# Building Random Forest model


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.25, random_
model = RandomForestRegressor(random_state=1)
model.fit(X_train, y_train)
pred = model.predict(X_test)
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(25).plot(kind='barh',figsize=(10,10))

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 8/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Feature Importance

This aligns with what we interpreted from the Correlation matrix. TV >>>
Radio >>> Newspaper

3. Pair Plots: A pair plot offers a straightforward way to visually explore the
connections between different variables. It’s akin to a correlation matrix, but
https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 9/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

instead of displaying correlations, it presents a graphical representation for


each pair of variables. Now, let’s delve into the code to generate our pair plot.

sns.pairplot(df)

Pair-plot (upper half)

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 10/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Pair-plot (lower half)

From this plot, we can state that there is observable coherence between our
pair plot and the initial correlation matrix. It indicates a robust positive
correlation between TV and sales, a comparatively weaker correlation for
radio, and an even lesser correlation for newspapers.

We can also see that the newspaper is right-skewed, let’s try and un-skew it
using Box-Cox transformation

Box-Cox Transformation

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 11/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

fig = plt.figure()
ax1 = fig.add_subplot(211)
x = df['newspaper']
prob = stats.probplot(x, dist=stats.norm, plot=ax1)
ax1.set_xlabel('')
ax1.set_title('Probplot against normal distribution')
#Using boxcox to transform the data so it’s closest to normal distribution
ax2 = fig.add_subplot(212)
df['newspaper'], _ = stats.boxcox(x)
prob = stats.probplot(df['newspaper'], dist=stats.norm, plot=ax2)
print("")
ax2.set_title('Probplot after Box-Cox transformation')

plt.show()

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 12/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 13/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

After the Box-cox transformation, we can see that the ‘Newspaper’ column
data is no more skewed.

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 14/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

After Box-Cox Transformation


https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 15/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Now that we have finished transforming data and performed exploratory


analysis, it is time to build the model.

Linear Regression
Linear regression is a statistical method that models the relationship
between a dependent variable and one or more independent variables by
fitting a linear equation to the observed data. It aims to find the best-fitting
straight line (or hyperplane in higher dimensions) that minimizes the sum of
squared differences between the actual and predicted values.

X = df[["TV","radio",'newspaper']]
y = df.sales
xtrain,xtest,ytrain,ytest = train_test_split(X,y,test_size=0.3)

#variable for Linear Regression model


model = LinearRegression()
model.fit(xtrain,ytrain)

result = pd.DataFrame()
result['xtest - tv'] = xtest['TV'].copy()
result['xtest - radio'] = xtest['radio'].copy()
result['ytest'] = ytest.copy()
result['ypred'] = yPred.copy()

print('MAE : ', metrics.mean_absolute_error(ytest,yPred))


print('MSE : ', metrics.mean_squared_error(ytest,yPred))

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 16/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium
print('RMSE : ', np.sqrt(metrics.mean_squared_error(ytest,yPred)))
print('R-Squared : ', (metrics.r2_score(ytest,yPred))*100)

Model Evaluation
After running the model, we received the following metrics:

MAE : 1.1979141139820386

MSE : 2.5366854894583866

RMSE : 1.592697551156021

R-Squared : 90.99264003736845

An R² value of 90.99% indicates that the model provides a very high level of
explanatory power, suggesting that the chosen independent variable(s)(in
this case, T.V., radio and newspaper) are effective in predicting the variation
observed in the dependent variable (sales of the product). This is a strong
indication that the model is a good fit for the data, and the explained
variability is significantly high.

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 17/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Coefficients
We can also extract model coefficients from the model to see what it looks
like:

cdf = pd.DataFrame(model.coef_, X.columns, columns=['Coefficients'])


print(cdf)

Model Coefficients

From above, we can build the equation —

Sales = 0.045 * T.V. + 0.179* radio + 0.0317 *


newspaper

The lower coefficient of T.V. might raise questions and it’s possible for a
variable with a high correlation to have a lower coefficient in a linear
regression equation, and this scenario can occur due to multicollinearity.
https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 18/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

You can find the entire code here: Kaggle

We’ll cover more on that in the next blog. Till then, stay tuned!

If you liked my blog, give this a clap and follow me! You can also connect with me
on LinkedIn. I’m excited to receive your thoughts and appreciate any feedback you
can share. Your insights are invaluable in shaping my upcoming content. Keep an
eye out for more exciting blogs, and thank you for being part of this voyage!

Marketing Market Mix Modeling Python Data Science Analytics

Written by Akanksha Anand (Ak) Follow

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 19/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

249 Followers

Data @CIAI, Marketing Media Analytics for Life Science and Healthcare

More from Akanksha Anand (Ak)

Akanksha Anand (Ak) Akanksha Anand (Ak) in Towards AI

Python Implementation of Markov Causal Inference Python


Chain Attribution Model Implementation
Touching upon each element of Markov Chain Mastering Causal Inference in Python
using Python

Jan 16 19 2 Feb 18 239 1

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 20/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Akanksha Anand (Ak) Akanksha Anand (Ak)

Market Mix Modeling — Basics Attribution Modeling


A solution to cookie restrictions for Marketing Diving deeper in Marketing Analytics along
Analysts with implementation

Jan 18 106 1 Jan 9 155

See all from Akanksha Anand (Ak)

Recommended from Medium

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 21/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Luís Roque in Towards Data Science Iacopo Amicabile

MMM: Bayesian Framework for Surfing the Pareto frontline in the


Marketing Mix Modeling and ROAS context of Robyn MMM
Bayesian framework to model media Don’t settle for Robyn one-pager, explore
channels performance, Return on Ad Spend… further possible MMM solutions.

Jun 6 433 5 Oct 4 1

Lists

Predictive Modeling w/ Coding & Development


Python 11 stories · 887 saves
20 stories · 1635 saves

Practical Guides to Machine MODERN MARKETING


Learning 195 stories · 909 saves
10 stories · 2004 saves

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 22/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

Chris Yan Anup Paudel

An End-to-End E-commerce Data-Driven Marketing Strategies:


Customer Segmentation Model… How to Use Analytics to Boost Yo…
Customer segmentation is a powerful Photo by Firmbee.com on Unsplash
strategy for understanding and targeting…

Jul 11 2 Jul 3 98

Linh V Nguyen in Operations Research Bit Namish Saxena in GenAIUs

Using Data Science for Optimizing Building Autonomous AI Agents


Marketing Budget and Attribution with CrewAI

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 23/24
11/6/24, 4:08 AM Market Mix Modeling Implementation using Python | by Akanksha Anand (Ak) | Medium

We have 4 different display advertising


campaigns (with Markov Chain and Linear… Sep 24 12

Jun 22 291

See more recommendations

Help Status About Careers Press Blog Privacy Terms Text to speech Teams

https://medium.com/@akanksha.etc302/market-mix-modeling-implementation-using-python-dcf1a4377b86 24/24

You might also like