Sunday, June 26, 2022

Impact of Advertisement on Sales using Adstock in R Part 1

Linear Model using Adstock in R Part 1


Introduction

In the previous blog on basics of Adstockhttps://www.aimlmadeeasy.com/2022/06/adstock-analysis-in-r.html,we learnt about how to apply the decay effect of adstock using logistic function.In this blog, we will build a regression model and estimate a relationship between sales and advertisement. For our analysis, we will be using the cheese data set from bayesm library.

To understand implementation in R, we will look at how we can apply adstock transformation for a simple case

package.name<-c("dplyr","data.table","stats","bayesm","ggplot2")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}


Step 1: Creating a Simple Illustration

Importing the cheese dataset

data("cheese")
test.df<-cheese
head(test.df)
              RETAILER VOLUME       DISP    PRICE
1  LOS ANGELES - LUCKY  21374 0.16200000 2.578460
2 LOS ANGELES - RALPHS   6427 0.12411257 3.727867
3   LOS ANGELES - VONS  17302 0.10200000 2.711421
4   CHICAGO - DOMINICK  13561 0.02759109 2.651206
5      CHICAGO - JEWEL  42774 0.09061273 1.986674
6       CHICAGO - OMNI   4498 0.00000000 2.386616

The attributes are as follows:

  • RETAILER:List of Retailers
  • VOLUME:Number of units sold
  • DISP:A Measure of advertising display activity
  • PRICE:Unit Price in Dollars


For each retailer, the data captures weekly sales of cheese. Here the dependent variable \(y_t\) is the units sold or VOLUME. DISP is the \(x1_t\) and PRICE is \(x2_t\). For sake of explanation, lets look at regression model for only one of the retailers such as ATLANTA - KROGER CO.

Step 2: Defining Adstock Function

Lets define the adstock function using filter function from the stats library

adstockTransform <- function(x){
  stats::filter( 1/(1+exp(-2*x)), 0.25, method = "recursive")
}


  • First part of the argument is an expression in x.It is normally a time series
  • Filter coefficient for Auto regressive (AR) or Moving Average Term
  • Method = “recursive” indicates that an auto regressive (AR) term is used


Step 3: Applying Adstock Transformation

Since this a time series data, we need to also check for stationarity.For our case, lets assume that the weekly time series is stationary in nature. With this assumption, we can apply linear model.

test.df2<-test.df%>%
  filter(RETAILER=="ATLANTA - KROGER CO")%>%
  mutate(Volume_Log=log(VOLUME),
         Price_Log=log(PRICE))%>%
  mutate(Disp_Adstock=adstockTransform(DISP))%>%
  select(-DISP,-VOLUME,-PRICE)

head(test.df2)
               RETAILER Volume_Log Price_Log Disp_Adstock
27  ATLANTA - KROGER CO   8.318498 1.0623441    0.5118027
114 ATLANTA - KROGER CO   8.444838 1.0880211    0.6279507
201 ATLANTA - KROGER CO   8.316300 1.0798555    0.6724189
288 ATLANTA - KROGER CO   8.996652 0.8667424    0.6809895
375 ATLANTA - KROGER CO   9.106867 0.8791677    0.7032240
462 ATLANTA - KROGER CO   9.103200 0.8521204    0.7997878


Step 4: Applying Linear Model

fit <- lm(Volume_Log  ~ Disp_Adstock + Price_Log, data=test.df2)
summary(fit)

Call:
lm(formula = Volume_Log ~ Disp_Adstock + Price_Log, data = test.df2)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.25070 -0.07254 -0.01011  0.05973  0.33558 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)    9.4147     0.3564  26.417  < 2e-16 ***
Disp_Adstock   1.2312     0.4110   2.996  0.00402 ** 
Price_Log     -1.7167     0.1208 -14.211  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.115 on 58 degrees of freedom
Multiple R-squared:  0.8436,    Adjusted R-squared:  0.8382 
F-statistic: 156.4 on 2 and 58 DF,  p-value: < 2.2e-16


We can see that the regression model is significant with an \(R^2\) of = 0.83. Effect of Price as well as Adstock variable is significant.Since this is log-log model of Sales on Price, we can see that the elasticity is -1.71 which makes sense as increasing the price reduces the units sold.

My Channel

Web Scraping Tutorial 4- Getting the busy information data from Popular time page from Google

Popular Times Popular Times In this blog we will try to scrape the ...