What is A/B testing? Why do we do it?

Design-led companies (Apple, Google, Airbnb, etc.) frequently apply design thinking to design new products (Naiman 2020). A/B testing (also known as split testing or bucket testing) is “a method of comparing two versions of a webpage or app against each other to determine which one performs better.” (Optimizly, 2019).

How A/B testing works

Some people get randomly assigned to group A, some others to group B. Each group is exposed to a different treatment of some underlying variable. This variable could be the discount amount, ad copy, etc. This underlying variable is what gets “manipulated.” Marketing researchers or data scientists then observe some outcome(s) that might be affected by the manipulated variables. We then create a dummy variable for the treatment group and for the control group. ## To evaluate the effect of the 2 different treatments of (A and B), we run the following regression: Y = β0 + β1*XA + ϵ

The coefficient β1 can be interpreted as the additional effect that treatment A has on the outcome variable compared to treatment B. β0 can be interpreted as the average outcome, or the predicted value of Y, for treatment group B. Usually, if one of the treatments is considered a “control” group, the control group would be used as the baseline in the regression. i.e. the group that is left out and absorbed into the intercept β0.

What is dummy coding? Why do we need it?

Dummy coding is required for performing experimental research. Since we have dummy variables (i.e., a control/placebo group and a treatment group) in our model, the intercept has more meaning. Dummy coded variables have values of 0 for the reference/control/placebo group and 1 for the comparison/treatment group. Since the intercept is the expected mean value when X=0, it is the mean value only for the reference group (when all other X=0). Dummy coding is a way to make the categorical variable into a series of dichotomous variables (variables that can have a value of zero or one only). For more details, please see the UCLA statistics site (available at https://stats.idre.ucla.edu/spss/faq/coding-systems-for-categorical-variables-in-regression-analysis/).

Example - Analyzing the relationship between advertising exposures and product purchase

It is suggested that “the effect of advertising appears non-linear, with an optimum between two and three exposures per week (Tellis, 1987).” For our example on the relationship between advertising exposures and product purchase below, we will be testing the relationship between advertising and product purchase using regression analysis. Our null hypothesis (usually denoted as H0) is that there is no relationship between advertising exposures and product purchases using regression analysis. The alternative hypothesis (usually denoted as H1) is that there is a relationship between advertising exposures and product purchases. The hypothesis test can be represented by the following notation:

Null Hypothesis: H0: β1 = 0 Alternative Hypothesis: H1: β1 ≠ 0

First, we will be creating a new variable that has a value of one for each observation at that level and zeroes for all others. In our example using the variable (Ads), the first new variable (Ads1) will have a value of one for each observation in which the consumers are exposed to the 1st ads campaign and zero for all other observations. Likewise, we create Ads2 when the consumers are exposed to the 1st ads campaign, and 0 otherwise, and Ads3 is 1 when the consumers are exposed to the 3rd ads campaign, and 0 otherwise. The level of the categorical variable that is coded as zero in the new variables is the reference level or the level to which all of the other levels are compared. In our example, it is the reference level Ads0. Our objective is to see which ads campaign leads to more product sales.

Example 1 - A simple A/B test

You can also perform this analysis using Excel

#setwd("C:/Users/zxu3/Documents/R/ab_testing")
library(readr)
data <- read_csv("ab_testing.csv")
## Rows: 80 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): Ads, Purchase
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ls(data) # list the variables in the dataset
## [1] "Ads"      "Purchase"
head(data) #list the first 6 rows of the dataset
## # A tibble: 6 × 2
##     Ads Purchase
##   <dbl>    <dbl>
## 1     1      152
## 2     0       21
## 3     3       77
## 4     0       65
## 5     1      183
## 6     1       87
# creating the factor variable
data$Ads <- factor(data$Ads)
is.factor(data$Ads)
## [1] TRUE
# showing the first 15 rows of the variable "Ads"
data$Ads[1:15]
##  [1] 1 0 3 0 1 1 2 2 2 0 3 3 0 2 3
## Levels: 0 1 2 3
#now we do the regression analysis and examine the results
summary(lm(Purchase~Ads, data = data))
## 
## Call:
## lm(formula = Purchase ~ Ads, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -50.095 -27.891  -0.227  24.773  65.905 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   55.381      6.472   8.557 9.41e-13 ***
## Ads1          75.714      9.152   8.273 3.31e-12 ***
## Ads2          36.557      9.842   3.715 0.000386 ***
## Ads3          -2.654      9.048  -0.293 0.770096    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 29.66 on 76 degrees of freedom
## Multiple R-squared:  0.5624, Adjusted R-squared:  0.5452 
## F-statistic: 32.56 on 3 and 76 DF,  p-value: 1.216e-13

Example 2.1

# Load the required library
library(readr)

# Read in the dataset
display <- read_csv("ab_testing.csv")
## Rows: 80 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): Ads, Purchase
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# List the variables in the dataset
ls(display)
## [1] "Ads"      "Purchase"
# View the first few rows of the dataset
head(display)
## # A tibble: 6 × 2
##     Ads Purchase
##   <dbl>    <dbl>
## 1     1      152
## 2     0       21
## 3     3       77
## 4     0       65
## 5     1      183
## 6     1       87
# Create the factor variable for Ads
display$Ads <- factor(display$Ads)

# Check if 'Ads' is now a factor
is.factor(display$Ads)
## [1] TRUE
# Show the first 15 rows of the 'Ads' variable
display$Ads[1:15]
##  [1] 1 0 3 0 1 1 2 2 2 0 3 3 0 2 3
## Levels: 0 1 2 3
# Run the regression analysis for a predictor with 4 different levels
summary(lm(Purchase~Ads, data = display))
## 
## Call:
## lm(formula = Purchase ~ Ads, data = display)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -50.095 -27.891  -0.227  24.773  65.905 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   55.381      6.472   8.557 9.41e-13 ***
## Ads1          75.714      9.152   8.273 3.31e-12 ***
## Ads2          36.557      9.842   3.715 0.000386 ***
## Ads3          -2.654      9.048  -0.293 0.770096    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 29.66 on 76 degrees of freedom
## Multiple R-squared:  0.5624, Adjusted R-squared:  0.5452 
## F-statistic: 32.56 on 3 and 76 DF,  p-value: 1.216e-13

Example 2.2 -An A/B test with only one line of syntax (no dummy coding required)

ab_tesing

#Alternatively, you can also use the factor function within the lm function, saving the step of creating the factor variable first.
summary(lm(Purchase~ factor(Ads), data))
## 
## Call:
## lm(formula = Purchase ~ factor(Ads), data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -50.095 -27.891  -0.227  24.773  65.905 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    55.381      6.472   8.557 9.41e-13 ***
## factor(Ads)1   75.714      9.152   8.273 3.31e-12 ***
## factor(Ads)2   36.557      9.842   3.715 0.000386 ***
## factor(Ads)3   -2.654      9.048  -0.293 0.770096    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 29.66 on 76 degrees of freedom
## Multiple R-squared:  0.5624, Adjusted R-squared:  0.5452 
## F-statistic: 32.56 on 3 and 76 DF,  p-value: 1.216e-13

abtesting

# Load the necessary library
library(readr)

ab_data <- read_csv("abtesting.csv")
## Rows: 38 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): Ads, Purchase
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(lm(Purchase ~ factor(Ads), data = ab_data))
## 
## Call:
## lm(formula = Purchase ~ factor(Ads), data = ab_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -57.000 -23.250   3.071  22.643  51.000 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    95.429      6.441  14.816  < 2e-16 ***
## factor(Ads)1   41.571      9.630   4.317 0.000118 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 29.52 on 36 degrees of freedom
## Multiple R-squared:  0.3411, Adjusted R-squared:  0.3228 
## F-statistic: 18.64 on 1 and 36 DF,  p-value: 0.0001184

Interpretations - Is our campaign effective? Let’s show the significance of the independent variable.

Since the p-value for X is .00018, which is less than .05, we reject the null hypothesis in favor of the alternative hypothesis.

The coefficient for Ads1 in the regression output is 41.57, which indicates that the 1st Advertising campaign is more effective (relative to the group who did not receive any advertising exposure).

Now the estimates for β0 and β1 are 95.43 and 41.57, respectively, leading to a prediction of average sales of 95.43 for the control group (group A) and a prediction of average sales which is 95.43 + 41.57*1 = 137 for the treatment group or the group of consumers who were exposed to the advertising campaign.

A question you might want to ask - why do I show you this tutorial using both Excel and R?

You will also find exactly the same coefficients using the Regression Data Analysis Tool in Excel. However, Excel also can’t handle large datasets (hundreds of thousands of records (Gapintelligence, 2020). Additionally, if you would like to perform the analysis and document the whole process (e.g., objectives, methods, hypotheses, results, and discussions), then using Rstudio with RMarkdown is probably one of the best choices.

Part 2 - Individual Work - AB testing

Q1: Write a summary of at least one article listed in the Reference section:

https://www.npr.org/sections/shots-health-news/2025/02/04/nx-s1-5285413/cereal-sugar-kids-advertising-health

When reading the article “Families buy more sugary cereal if advertising targets kids, not adults,” I initially thought this might be a criticism of advertisers targeting children with animations and catchy music to sell items like cereal. However, there was more to it. After reading the article, it is now obvious that the article aims to show that cereal companies with added sugars attempted AB testing by pivoting their advertising campaigns toward adults but found that sales did not change. However, when children see cereal advertised, sales tend to increase. Adults are less impressionable, while children who see an animated cartoon, like Coco Puffs, on TV become vulnerable to the marketing campaign and demand the item from their parents.

Q2: Please perform a regression analysis using both Excel and R. Interpret your results (P value, R square, coefficients, etc.). For interpretation purposes, please make sure to refer to the reading (Reading - 3.1- 3.2 - Linear Regression (download the 2nd edition - free PDF book - Chapter 3) posted in Week 5. Assume that you work for this company. What are the marketing implications?

In this Linear regression analysis, I evaluated the hypothesis and discovered if it was found true or false. When conducting the linear regression, I used Excel and RStudio to find the solution as well as some dummy variables. The answers I got for both were nearly identical and had led me to the conclusion that the Null was statistically significant. With the p-value falling under 0.05 It was concluded that the hypothesis of “There is no relationship between advertising exposures and product purchases” was false. The marketing implications brought are that when a kid is exposed to an advertisement of cereal box, they are more likely to recall that ad and convince their parents to buy, companies should take the opportunity and market towards kids.

AB1 is 66.8 more than AB0

45% of the products purchesed can be explained by the advertising

Q3 Did we get the same results in Example 2.1 and Example 2.2? Which solution do you like better? Why?

I got the same result from both. Knowing now that 2.2 can create the same result with less effort than the 2.1 example makes me question the purpose of 2.1. If I had to choose, 2.2 would be better because it is faster and simpler.

Q4: Perform an AB test using the dataset ab_test.csv and explain your results. Please include both the Excel solution and the R Markdown solution.

So, I did use the other abtesing csv first, so my results here will be based on that instead.

On testing, I realized that both hypotheses were rejected and had p-values under 0.05.

As for the R-squared, it was lower at 34% and could be explained less by advertising.

AB1 is 41.57 more than AB0.