Statistical analysis of marketing promotion data using Python, one-way ANOVA, linear regression, and Tukey's HSD post hoc testing to determine whether sales differ significantly across TV promotion budget categories.
This project was completed as part of the Google Advanced Data Analytics Professional Certificate โ Course 4: The Power of Statistics.
Marketing teams often need to determine whether differences in sales performance between different promotional strategies are statistically meaningful or simply due to random variation.
In this project, historical marketing promotion data is analyzed to investigate whether TV promotion budget categories are associated with statistically significant differences in sales.
The analysis follows a complete statistical workflow:
- Exploratory data analysis
- Visualization of sales distributions
- Data cleaning
- Categorical linear regression modeling
- Model assumption checking
- One-way ANOVA
- Tukey's HSD post hoc analysis
- Interpretation of statistical results
- Communication of findings to non-technical stakeholders
The primary question investigated is:
Is there a statistically significant difference in sales among Low, Medium, and High TV promotion budget categories?
Stakeholders want to understand whether different levels of TV promotion investment are associated with meaningful differences in sales.
The marketing data contains information about:
- TV promotion budget category
- Radio promotion budget
- Social media promotion budget
- Influencer size
- Sales
The main categorical variables considered were:
- TV: Low, Medium, High
- Influencer: Nano, Micro, Macro, Mega
Exploratory analysis indicated that TV promotion category had a much stronger relationship with sales than influencer size. Therefore, TV was selected as the categorical independent variable for the statistical analysis.
The analysis uses the marketing_sales_data.csv dataset.
Each row represents an independent marketing promotion.
| Feature | Description | Type |
|---|---|---|
TV |
TV promotion budget category | Categorical |
Radio |
Radio promotion budget | Numerical |
Social Media |
Social media promotion budget | Numerical |
Influencer |
Influencer size category | Categorical |
Sales |
Sales generated by the promotion | Numerical |
- Low
- Medium
- High
- Nano
- Micro
- Macro
- Mega
Before statistical modeling, rows containing missing values were removed.
After cleaning, the dataset contained 569 observations.
The project was implemented in Python using:
- Python
- Pandas โ data manipulation and cleaning
- NumPy โ numerical operations
- Matplotlib โ visualization
- Seaborn โ statistical visualization
- SciPy โ statistical utilities
- Statsmodels โ linear regression and ANOVA
- Jupyter Notebook โ interactive analysis
- Exploratory Data Analysis (EDA)
- Ordinary Least Squares (OLS) regression
- One-way ANOVA
- Tukey's HSD post hoc test
- Residual analysis
- Q-Q plot analysis
- Homoscedasticity analysis
hypothesis-testing-with-python/
โ
โโโ Hypothesis testing with Python.ipynb
โโโ marketing_sales_data.csv
โโโ README.md
โโโ .gitignore
The complete Jupyter Notebook containing:
- Data exploration
- Visualizations
- Data cleaning
- OLS regression
- Regression diagnostics
- One-way ANOVA
- Tukey's HSD test
- Statistical interpretation
- Stakeholder recommendations
The dataset used for the analysis.
Documentation explaining the project, methodology, results, and conclusions.
A boxplot was used to examine the distribution of sales across TV promotion categories.
sns.boxplot(
data=data,
x='TV',
y='Sales',
palette='plasma'
)The visualization showed considerable variation in sales across the three TV categories.
The general pattern indicated:
High TV > Medium TV > Low TV
in terms of average sales.
This suggested that TV promotion category could be an important predictor of sales and motivated further statistical testing.
A second boxplot was used to investigate sales across influencer categories.
sns.boxplot(
data=data,
x='Influencer',
y='Sales',
palette='cividis'
)The influencer categories showed some variation in sales, but the relationship appeared substantially weaker than the relationship observed for TV promotion categories.
Therefore, TV was selected as the primary categorical independent variable.
The dataset contained missing values.
Rows containing missing observations were removed using:
data = data.dropna(axis=0)The dataset was then checked for remaining missing values:
data.isna().sum(axis=0)The result confirmed that all variables contained zero missing values.
A categorical Ordinary Least Squares regression model was created to estimate sales based on TV promotion category.
The model formula was:
ols_formula = 'Sales ~ C(TV)'The model was created and fitted using statsmodels:
OLS = ols(
formula=ols_formula,
data=data
)
model = OLS.fit()Using C(TV) tells statsmodels to treat TV as a categorical variable rather than a continuous numerical variable.
The fitted model produced the following key results:
| Metric | Result |
|---|---|
| Observations | 569 |
| R-squared | 0.874 |
| Adjusted R-squared | 0.874 |
| F-statistic | 1971.46 |
| F-test p-value | 8.81 ร 10โปยฒโตโถ |
The model's Rยฒ of 0.874 indicates that approximately 87.4% of the variation in sales is explained by TV promotion category in this dataset.
This represents a strong relationship between TV promotion category and sales.
The model uses High TV promotion as the reference category.
Therefore:
High = reference category
The estimated coefficients were:
| TV Category | Coefficient | Interpretation |
|---|---|---|
| High | Reference | Baseline |
| Medium | -101.506 | Approximately $101.51M lower sales than High |
| Low | -208.813 | Approximately $208.81M lower sales than High |
The estimated difference between Low and High TV promotion categories was:
โ$208.81 million
with a 95% confidence interval of approximately:
[$โ216.64M, $โ200.99M]
The estimated difference was:
โ$101.51 million
with a 95% confidence interval of approximately:
[$โ109.32M, $โ93.69M]
The coefficients were statistically significant at the 0.05 significance level.
Before interpreting the regression results, the model assumptions were evaluated.
Because the independent variable is categorical rather than continuous, the traditional linearity assumption between a continuous predictor and response is not directly applicable in the same way.
Each row represents an independent marketing promotion.
Therefore, the observations are treated as independent.
Residuals were examined using:
- Histogram
- Normal Q-Q plot
The Q-Q plot showed some deviation from the theoretical normal distribution, indicating reasonable concern regarding the normality assumption.
However, for the purposes of this analysis, the normality assumption was treated as sufficiently acceptable to continue.
A residuals-versus-fitted-values plot was used to examine whether residual variance remained approximately constant.
The residuals appeared reasonably distributed around zero without a strong systematic pattern.
Therefore, the constant variance assumption was considered acceptable.
A one-way ANOVA was performed to determine whether mean sales differed significantly across TV promotion categories.
sm.stats.anova_lm(
model,
typ=2
)The ANOVA produced:
| Source | Sum of Squares | df | F | p-value |
|---|---|---|---|---|
| C(TV) | 4,052,692 | 2 | 1971.46 | 8.81 ร 10โปยฒโตโถ |
| Residual | 581,759 | 566 | โ | โ |
There is no statistically significant difference in mean sales among the Low, Medium, and High TV promotion groups.
In mathematical notation:
Hโ: ฮผLow = ฮผMedium = ฮผHigh
There is a statistically significant difference in mean sales among at least one pair of TV promotion groups.
Hโ: At least one group mean differs
The ANOVA produced an F-statistic of approximately:
1971.46
with a p-value of:
8.81 ร 10โปยฒโตโถ
Since:
p-value < 0.05
the null hypothesis is rejected.
There is strong statistical evidence that mean sales differ significantly among the TV promotion categories.
However, ANOVA alone does not tell us exactly which groups differ from one another.
Therefore, a post hoc test was required.
Tukey's Honestly Significant Difference (HSD) test was performed to compare every pair of TV promotion categories while controlling the family-wise error rate.
tukey_oneway = pairwise_tukeyhsd(
endog=data['Sales'],
groups=data['TV']
)
tukey_oneway.summary()The results were:
| Group 1 | Group 2 | Mean Difference | Adjusted p-value | Lower CI | Upper CI | Significant |
|---|---|---|---|---|---|---|
| High | Low | -208.81 | 0.001 | -216.64 | -200.99 | Yes |
| High | Medium | -101.51 | 0.001 | -109.32 | -93.69 | Yes |
| Low | Medium | 107.31 | 0.001 | 99.71 | 114.91 | Yes |
All three pairwise comparisons were statistically significant.
The estimated difference in mean sales was approximately:
$208.81 million
The confidence interval was approximately:
$200.99M to $216.64M
This indicates significantly higher sales for High TV promotion compared with Low TV promotion.
The estimated difference in mean sales was approximately:
$101.51 million
The confidence interval was approximately:
$93.69M to $109.32M
High TV promotion was associated with significantly higher sales than Medium TV promotion.
The estimated difference in mean sales was approximately:
$107.31 million
The confidence interval was approximately:
$99.71M to $114.91M
Medium TV promotion was associated with significantly higher sales than Low TV promotion.
The results consistently show the following ordering:
High TV promotion
โ
Highest Sales
Medium TV promotion
โ
Intermediate Sales
Low TV promotion
โ
Lowest Sales
Importantly, every pair of TV promotion categories was statistically significantly different.
The statistical analysis provides strong evidence that TV promotion budget category is associated with differences in sales.
The analysis suggests:
- High TV promotion budgets are associated with the highest sales.
- Medium TV promotion budgets generate significantly more sales than Low TV promotion budgets.
- High TV promotion budgets generate significantly more sales than both Medium and Low TV promotion budgets.
- The relationship is statistically significant according to both the one-way ANOVA and Tukey HSD tests.
- TV promotion category alone explains approximately 87.4% of the observed variation in sales in this dataset.
Based on this historical dataset, increasing TV promotion investment appears to be strongly associated with higher sales.
However, the results should not automatically be interpreted as proof that increasing TV spending causes the increase in sales. The analysis identifies a strong statistical association within the available observational data.
Before making major budget allocation decisions, the business should consider additional factors such as:
- Radio spending
- Social media spending
- Influencer size
- Geographic location
- Seasonality
- Product type
- Campaign duration
- Customer demographics
- Other marketing activities
A more comprehensive multivariable model could help isolate the effect of TV promotion while accounting for these additional factors.
Although the categorical TV model produced a strong Rยฒ, it could be improved.
Instead of grouping TV spending into:
Low
Medium
High
the analysis could use the actual TV promotion budget as a continuous variable.
This would preserve more information from the original dataset.
A multivariable regression model could include:
TV
Radio
Social Media
Influencer
This would allow the analysis to investigate the relationship between sales and multiple marketing channels simultaneously.
The effectiveness of one marketing channel may depend on another.
For example:
TV ร Social Media
TV ร Influencer
Radio ร Social Media
Interaction terms could therefore provide additional insights.
Future models could incorporate:
- Campaign location
- Season
- Time of year
- Product category
- Campaign duration
- Customer segment
These variables may explain additional variation in sales.
git clone https://github.com/Koheinn/hypothesis-testing-with-python.gitcd hypothesis-testing-with-pythonpip install pandas numpy matplotlib seaborn scipy statsmodels jupyterAlternatively, create a virtual environment:
python -m venv .venvActivate it on Windows:
.venv\Scripts\activateThen install the required libraries:
pip install pandas numpy matplotlib seaborn scipy statsmodels jupyterjupyter notebookOpen:
Hypothesis testing with Python.ipynb
Run the notebook cells sequentially.
This project demonstrates practical understanding of:
- Exploratory Data Analysis
- Categorical variables
- Boxplots
- Linear regression
- Ordinary Least Squares (OLS)
- Regression residuals
- Q-Q plots
- Normality
- Homoscedasticity
- Independent observations
- One-way ANOVA
- Null and alternative hypotheses
- F-statistics
- p-values
- Statistical significance
- Confidence intervals
- Tukey's HSD
- Multiple comparison correction
- Statistical communication
- Business interpretation of statistical results
Program: Google Advanced Data Analytics Professional Certificate
Course: Course 4 โ The Power of Statistics
Activity: Hypothesis Testing with Python
The project demonstrates the application of statistical inference techniques to a practical marketing analytics problem.
Dataset:
Saragih, H. S. โ Dummy Marketing and Sales Data
Source: Kaggle
The dataset is used for educational and analytical purposes as part of the course activity.
Heinn Htet Zan
Mid-Level Full Stack Software Developer | Computer Science | Data & AI Enthusiast
- GitHub: https://github.com/Koheinn
- LinkedIn: https://www.linkedin.com/in/heinn-htet-zan-040794291/
- Portfolio: https://hhzportfolio.netlify.app
The analysis found statistically significant differences in sales across all TV promotion budget categories. High TV promotion was associated with the highest sales, followed by Medium and then Low TV promotion. One-way ANOVA confirmed an overall significant difference, while Tukey's HSD confirmed that every pair of TV categories differed significantly.
This project demonstrates how statistical testing can transform marketing data into evidence-based insights that can support business decision-making.