|
| 1 | +import pandas as pd |
| 2 | +from causalnlp import CausalInferenceModel |
| 3 | +from lightgbm import LGBMRegressor |
| 4 | + |
| 5 | +# Load your data with columns for seasonality, marketing_spend, website_traffic, and sales |
| 6 | +df = pd.read_csv('marketing_sales_daily_data.csv') |
| 7 | + |
| 8 | +# 1. Impact of seasonality on marketing spend |
| 9 | +model_seasonality_marketing = CausalInferenceModel( |
| 10 | + df, |
| 11 | + method='t-learner', |
| 12 | + treatment_col='is_high_season', # Binary treatment (0/1) |
| 13 | + outcome_col='marketing_spend', |
| 14 | + include_cols=['month', 'weekday'] |
| 15 | +) |
| 16 | +model_seasonality_marketing.fit() |
| 17 | + |
| 18 | +# 2. Impact of marketing spend on website traffic |
| 19 | +model_marketing_traffic = CausalInferenceModel( |
| 20 | + df, |
| 21 | + method='t-learner', |
| 22 | + treatment_col='high_marketing_spend', # Binary treatment (0/1) |
| 23 | + outcome_col='website_traffic', |
| 24 | + include_cols=['month', 'weekday', 'is_high_season'] |
| 25 | +) |
| 26 | +model_marketing_traffic.fit() |
| 27 | + |
| 28 | +# 3. Impact of website traffic on sales |
| 29 | +model_traffic_sales = CausalInferenceModel( |
| 30 | + df, |
| 31 | + method='t-learner', |
| 32 | + treatment_col='high_website_traffic', # Binary treatment (0/1) |
| 33 | + outcome_col='sales', |
| 34 | + include_cols=['month', 'weekday', 'is_high_season', 'high_marketing_spend'] |
| 35 | +) |
| 36 | +model_traffic_sales.fit() |
| 37 | + |
| 38 | +# Average Treatment Effect (ATE) |
| 39 | +seasonality_marketing_effect = model_seasonality_marketing.estimate_ate() |
| 40 | +print(f"Effect of high season on marketing spend: {seasonality_marketing_effect['ate']}") |
| 41 | + |
| 42 | +# Conditional Average Treatment Effect (CATE) |
| 43 | +holiday_effect = model_seasonality_marketing.estimate_ate(df['month'].isin([11, 12])) |
| 44 | +print(f"Effect of high season during holidays: {holiday_effect['ate']}") |
| 45 | + |
| 46 | +model_with_text = CausalInferenceModel( |
| 47 | + df, |
| 48 | + method='t-learner', |
| 49 | + treatment_col='high_marketing_spend', |
| 50 | + outcome_col='sales', |
| 51 | + text_col='campaign_description', # Text data as a controlled-for variable |
| 52 | + include_cols=['month', 'seasonality'] |
| 53 | +) |
| 54 | +model_with_text.fit() |
| 55 | + |
| 56 | +# Interpret the model to see feature importance |
| 57 | +feature_importance = model_with_text.interpret(plot=False) |
| 58 | +print(feature_importance) # Show top 10 features |
0 commit comments