Problem
A marketing mix model (MMM) regresses weekly revenue on spend per media channel plus controls such as trend, seasonality, holidays and competitor sales. Ordinary least squares can return a negative coefficient for a paid channel, which reads as "advertising lowers sales". That contradicts the business assumption that spend never lowers revenue: saturation at most flattens the response. Such a model cannot guide budget moves, and some packages cannot bound the sign of a coefficient.
Example from the article: On the public Robyn demo dataset (208 weeks of revenue, five paid channels and a newsletter), OLS gives the search channel a coefficient of -0.0737 and an intercept of about -2.35 million.
Solution in brief
Put a lower bound of 0 on the media coefficients (and optionally the intercept) inside the estimator, rather than editing variables afterwards. The article shows two ways: SciPy curve_fit with a bounds argument, and ridge regression from the R package glmnet, called from Python through rpy2. Ridge also handles correlated channels and picks its penalty by cross-validation.
Questions this case answers
- How to fit a regression on correlated aggregate predictors so that selected coefficients cannot be negative, when the standard estimator has no sign option
- How to call a statistical routine that exists only in another language from the main analysis stack without rewriting it
- How to stop a marketing mix regression from returning negative coefficients for media channels?
- How to force the sign of regression coefficients when the statistics package has no such option?
Tools and methods
- SciPy
curve_fit— least-squares fit with bounds per parameter - statsmodels OLS — unconstrained baseline to compare the coefficients with
- glmnet via rpy2 — ridge with per-coefficient limits and cross-validated penalty
- Ridge regression — penalizes coefficient size against overfitting and collinearity
- Prophet — trend, seasonality and holiday components used as control variables
Google or a one-time email code. No password.