Skip to content

Latest commit

ย 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Hypothesis Testing with Python: Marketing Promotion Analysis

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.


๐Ÿ“Œ Project Overview

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:

  1. Exploratory data analysis
  2. Visualization of sales distributions
  3. Data cleaning
  4. Categorical linear regression modeling
  5. Model assumption checking
  6. One-way ANOVA
  7. Tukey's HSD post hoc analysis
  8. Interpretation of statistical results
  9. 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?


๐ŸŽฏ Business Problem

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.


๐Ÿ—‚๏ธ Dataset

The analysis uses the marketing_sales_data.csv dataset.

Each row represents an independent marketing promotion.

Features

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

TV Categories

  • Low
  • Medium
  • High

Influencer Categories

  • Nano
  • Micro
  • Macro
  • Mega

Before statistical modeling, rows containing missing values were removed.

After cleaning, the dataset contained 569 observations.


๐Ÿ› ๏ธ Technologies & Libraries

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

Statistical Methods

  • 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

๐Ÿ“ Repository Structure

hypothesis-testing-with-python/
โ”‚
โ”œโ”€โ”€ Hypothesis testing with Python.ipynb
โ”œโ”€โ”€ marketing_sales_data.csv
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ .gitignore

Files

Hypothesis testing with Python.ipynb

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

marketing_sales_data.csv

The dataset used for the analysis.

README.md

Documentation explaining the project, methodology, results, and conclusions.


๐Ÿ”Ž Exploratory Data Analysis

Sales vs. TV Promotion

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.


Sales vs. Influencer Size

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.


๐Ÿงน Data Cleaning

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.


๐Ÿ“ˆ Linear Regression Model

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.


๐Ÿ“Š Regression Results

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.


๐Ÿ“ Interpreting the Regression Coefficients

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

Low vs. 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]

Medium vs. High

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.


๐Ÿงช Model Assumption Checks

Before interpreting the regression results, the model assumptions were evaluated.

1. Linearity

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.


2. Independence

Each row represents an independent marketing promotion.

Therefore, the observations are treated as independent.


3. Normality of Residuals

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.


4. Constant Variance

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.


๐Ÿงฎ One-Way ANOVA

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 โ€” โ€”

๐Ÿง  Hypotheses

Null Hypothesis โ€” Hโ‚€

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

Alternative Hypothesis โ€” Hโ‚

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

โœ… ANOVA Conclusion

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.

Conclusion

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 HSD Post Hoc Test

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.


๐Ÿ“Œ Tukey HSD Interpretation

High vs. Low

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.


High vs. Medium

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.


Medium vs. Low

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.


๐Ÿ“ˆ Overall Statistical Finding

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.


๐Ÿ’ผ Business Interpretation

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.

Stakeholder Recommendation

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.


๐Ÿš€ Potential Model Improvements

Although the categorical TV model produced a strong Rยฒ, it could be improved.

1. Use Actual TV Budget

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.


2. Include Additional Predictors

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.


3. Add Interaction Effects

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.


4. Consider Additional Business Variables

Future models could incorporate:

  • Campaign location
  • Season
  • Time of year
  • Product category
  • Campaign duration
  • Customer segment

These variables may explain additional variation in sales.


๐Ÿงฐ How to Run the Project

1. Clone the repository

git clone https://github.com/Koheinn/hypothesis-testing-with-python.git
cd hypothesis-testing-with-python

2. Install dependencies

pip install pandas numpy matplotlib seaborn scipy statsmodels jupyter

Alternatively, create a virtual environment:

python -m venv .venv

Activate it on Windows:

.venv\Scripts\activate

Then install the required libraries:

pip install pandas numpy matplotlib seaborn scipy statsmodels jupyter

3. Launch Jupyter Notebook

jupyter notebook

Open:

Hypothesis testing with Python.ipynb

Run the notebook cells sequentially.


๐Ÿ“š Key Statistical Concepts Demonstrated

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

๐ŸŽ“ Course Context

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.


๐Ÿ“– Reference

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.


๐Ÿ‘จโ€๐Ÿ’ป Author

Heinn Htet Zan

Mid-Level Full Stack Software Developer | Computer Science | Data & AI Enthusiast

Profiles


โญ Key Takeaway

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.

About

Statistical hypothesis testing with Python using one-way ANOVA and Tukey HSD to analyze differences in marketing sales across TV promotion categories

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages