Market Mix Modeling Implementation Using Python _ by Akanksha Anand (Ak) _ Medium
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
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
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:
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
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).
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.
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
sns.pairplot(df)
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
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
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)
result = pd.DataFrame()
result['xtest - tv'] = xtest['TV'].copy()
result['xtest - radio'] = xtest['radio'].copy()
result['ytest'] = ytest.copy()
result['ypred'] = yPred.copy()
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:
Model Coefficients
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
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!
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
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
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
Lists
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
Jul 11 2 Jul 3 98
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
Jun 22 291
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