├── Advertising Adstock Transformation with Maximum Period Decay.R ├── Advertising Adstock Transformation with Maximum Period Decay.SAS ├── Advertising Adstock Transformation.R ├── Advertising Adstock Transformation.SAS └── README.md /Advertising Adstock Transformation with Maximum Period Decay.R: -------------------------------------------------------------------------------- 1 | # Program Name: Adstock Transformationwith Maximum Period Decay 2 | # Written By : Gabriel Mohanna 3 | # Date Created: July 11, 2014 4 | # Narrative : An adstock transformation with Maximum Period Decay 5 | # 6 | # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 7 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code is Poetry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 8 | # /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 9 | 10 | # Define Adstock Rate 11 | adstock_rate <- 0.50 12 | max_memory <- 2 13 | 14 | # Create Data 15 | advertising <- c(117.913, 120.112, 125.828, 115.354, 177.090, 141.647, 137.892, 0.000, 0.000, 0.000, 0.000, 16 | 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 158.511, 109.385, 91.084, 79.253, 102.706, 17 | 78.494, 135.114, 114.549, 87.337, 107.829, 125.020, 82.956, 60.813, 83.149, 0.000, 0.000, 18 | 0.000, 0.000, 0.000, 0.000, 129.515, 105.486, 111.494, 107.099, 0.000, 0.000, 0.000, 19 | 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000) 20 | 21 | # Calculate Advertising Adstock 22 | learn_rates <- rep(adstock_rate, max_memory+1) ^ c(0:max_memory) 23 | adstocked_advertising <- filter(c(rep(0, max_memory), advertising), learn_rates, method="convolution") 24 | adstocked_advertising <- adstocked_advertising[!is.na(adstocked_advertising)] 25 | 26 | # Graph Data 27 | plot(seq(1,length(advertising)), advertising, type="h", 28 | xlab="Time (Usually in Weeks)", ylab="Advertising", 29 | ylim=c(0, max(c(advertising, adstocked_advertising))), 30 | frame.plot=FALSE) 31 | lines(adstocked_advertising) 32 | -------------------------------------------------------------------------------- /Advertising Adstock Transformation with Maximum Period Decay.SAS: -------------------------------------------------------------------------------- 1 | /* Macro Name : Advertising Adstock with Maximum Period Decay 2 | * Written By : Gabriel Mohanna 3 | * Date Created: Feb 21, 2014 4 | * Narrative : Create Adstock transformation with Maximum Period Decay and compare to simple adstock transformation. 5 | * 6 | * \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 7 | * <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code is Poetry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 8 | * /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 9 | * 10 | * ***********************************************************************************************************************/ 11 | 12 | ** Define Adstock Rate; 13 | %let adstock = 0.50; 14 | 15 | ** Create Data; 16 | data advertising; 17 | format Week 8. advertising 8.3; 18 | 19 | Week = _N_; 20 | input advertising @@; 21 | 22 | datalines; 23 | 117.913 120.112 125.828 115.354 177.090 141.647 137.892 0.000 0.000 0.000 0.000 24 | 0.000 0.000 0.000 0.000 0.000 0.000 158.511 109.385 91.084 79.253 102.706 25 | 78.494 135.114 114.549 87.337 107.829 125.020 82.956 60.813 83.149 0.000 0.000 26 | 0.000 0.000 0.000 0.000 129.515 105.486 111.494 107.099 0.000 0.000 0.000 27 | 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 28 | ; 29 | run; 30 | 31 | ** Calculate Advertising Adstock with 50% rate and 2 weeks period decay; 32 | data Advertising; 33 | set Advertising; 34 | 35 | format advertising_adstock advertising_adstock_w_max_Decay 8.2; 36 | 37 | if _N_ = 1 then do; 38 | advertising_adstock = advertising; 39 | retain advertising_adstock; 40 | end; 41 | else do; 42 | advertising_adstock = sum(advertising, advertising_adstock * 0.5); 43 | end; 44 | 45 | advertising_adstock_w_max_Decay = sum(0.5**0 * advertising, 0.5**1 * lag1(advertising), 0.5**2 * lag2(advertising), 0); 46 | run; 47 | 48 | 49 | ** Graph Data; 50 | ods graphics on; 51 | ods html; 52 | proc sgplot data=Advertising; 53 | vbar week / response=advertising; 54 | vline week / response=advertising_adstock; 55 | vline week / response=advertising_adstock_w_max_Decay; 56 | run; 57 | -------------------------------------------------------------------------------- /Advertising Adstock Transformation.R: -------------------------------------------------------------------------------- 1 | # Program Name: Adstock Transformation 2 | # Written By : Gabriel Mohanna 3 | # Date Created: Feb 23, 2014 4 | # Narrative : A simple advertising adstock transformation. 5 | # 6 | # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 7 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code is Poetry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 8 | # /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 9 | 10 | # Define Adstock Rate 11 | adstock_rate = 0.50 12 | 13 | # Create Data 14 | advertising = c(117.913, 120.112, 125.828, 115.354, 177.090, 141.647, 137.892, 0.000, 0.000, 0.000, 0.000, 15 | 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 158.511, 109.385, 91.084, 79.253, 102.706, 16 | 78.494, 135.114, 114.549, 87.337, 107.829, 125.020, 82.956, 60.813, 83.149, 0.000, 0.000, 17 | 0.000, 0.000, 0.000, 0.000, 129.515, 105.486, 111.494, 107.099, 0.000, 0.000, 0.000, 18 | 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000) 19 | 20 | # Calculate Advertising Adstock 21 | # Credit: http://stackoverflow.com/questions/14372880/simple-examples-of-filter-function-recursive-option-specifically 22 | adstocked_advertising = filter(x=advertising, filter=adstock_rate, method="recursive") 23 | 24 | # Alternative Method Using Loops Proposed by Linh Tran 25 | adstocked_advertising = numeric(length(advertising)) 26 | adstocked_advertising[1] = advertising[1] 27 | for(i in 2:length(advertising)){ 28 | adstocked_advertising[i] = advertising[i] + adstock_rate * adstocked_advertising[i-1] 29 | } 30 | 31 | # Graph Data 32 | plot(seq(1,length(advertising)), advertising, type="h", 33 | xlab="Time (Usually in Weeks)", ylab="Advertising", 34 | ylim=c(0, max(c(advertising, adstocked_advertising))), 35 | frame.plot=FALSE) 36 | lines(adstocked_advertising) 37 | -------------------------------------------------------------------------------- /Advertising Adstock Transformation.SAS: -------------------------------------------------------------------------------- 1 | /* Macro Name : Adstock Transformation 2 | * Written By : Gabriel Mohanna 3 | * Date Created: Feb 6, 2014 4 | * Narrative : A simple advertising adstock transformation. 5 | * 6 | * \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 7 | * <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code is Poetry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 8 | * /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 9 | * 10 | * ***********************************************************************************************************************/ 11 | 12 | ** Define Adstock Rate; 13 | %let adstock = 0.50; 14 | 15 | ** Create Data; 16 | data advertising; 17 | format Week 8. advertising 8.3; 18 | 19 | Week = _N_; 20 | input advertising @@; 21 | 22 | datalines; 23 | 117.913 120.112 125.828 115.354 177.090 141.647 137.892 0.000 0.000 0.000 0.000 24 | 0.000 0.000 0.000 0.000 0.000 0.000 158.511 109.385 91.084 79.253 102.706 25 | 78.494 135.114 114.549 87.337 107.829 125.020 82.956 60.813 83.149 0.000 0.000 26 | 0.000 0.000 0.000 0.000 129.515 105.486 111.494 107.099 0.000 0.000 0.000 27 | 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 28 | ; 29 | run; 30 | 31 | ** Calculate Adstock; 32 | data Advertising; 33 | set Advertising; 34 | 35 | format advertising_adstock 8.2; 36 | 37 | if _N_ = 1 then do; 38 | advertising_adstock = advertising; 39 | retain advertising_adstock; 40 | end; 41 | else do; 42 | advertising_adstock = sum(advertising, advertising_adstock*&adstock.); 43 | end; 44 | run; 45 | 46 | 47 | ** Graph Data; 48 | ods graphics on; 49 | ods html; 50 | proc sgplot data=Advertising; 51 | vbar week / response=advertising; 52 | vline week / response=advertising_adstock; 53 | run; 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BlogFiles 2 | ========= 3 | 4 | Wordpress Blog Files 5 | --------------------------------------------------------------------------------