AB Testing With R Programming
Last Updated :
23 Oct, 2020
Split testing is another name of A/B testing and it’s a common or general methodology. It’s used online when one wants to test a new feature or a product. The main agenda over here is to design an experiment that gives repeatable results and robust to make an informed decision to launch it or not. Generally, this test includes a comparison of two web pages by representing variants A and B for them, as the number of visitors is similar the conversion rate given by the variant becomes better. Overall, it’s an experiment where two or more variations of the same web page are compared against together by showcasing them to real-time visitors, and through that determines which one performs better for a given goal. A/B testing is not only used or limited by web pages only, it can be used in emails, popups, sign-up forms, apps, and more. Let’s look into the example of a case study. So let’s implement AB testing in the R language.
Case Study
Let’s imagine we have results of A/B tests from two hotel booking websites, (Note: the data is not the real one ). First, we need to conduct a test analysis of the data; second, we need to draw conclusions from the data which we obtained from the first step, and in the final step, we make recommendations or suggestions to the product or management teams.
Data Set Summary
Download the data set from here.
- Variant A is from the control group which tells the existing features or products on a website.
- Variant B is from the experimental group to check the new version of a feature or product to see if users like it or if it increases the conversions(bookings).
- Converted is based on the data set given, there are two categories defined by logical value. It’s going to show true when the customer completes bookings and it’s going to show false when the customer visits the sites but not makes a booking.
Test Hypothesis
- Null Hypothesis: Both versions A and B have an equal probability of conversion or driving customer booking. In other words, there is no difference or no effect between A and B versions
- Alternative Hypothesis: Versions both A and B possess different probability of conversion or driving customer booking and there is a difference between A and B version. Version B is better than version A in driving customer bookings. PExp_B! = Pcont_A.
Analysis in R
1. Prepare the dataset and load the tidyverse library which contains the relevant packages used for the analysis.
R
library (tidyverse)
setwd (“~egot_\\Projects\\ABTest”)
ABTest <- read.csv ( "Website Results.csv" ,
header = TRUE )
save (ABTest, file = "~rda\\ABTest.rda" )
|
2. Let’s filter conversions for variants A & B and compute their corresponding conversion rates
R
conversion_subset_A <- ABTest %>%
filter (variant == "A" & converted == "TRUE" )
conversions_A <- nrow (conversion_subset_A)
visitors_A <- nrow (ABTest %>%
filter (variant == "A" ))
conv_rate_A <- (conversions_A/visitors_A)
print (conv_rate_A)
conversion_subset_B <- ABTest %>%
filter (variant == "B" & converted == "TRUE" )
conversions_B <- nrow (conversion_subset_B)
visitors_B <- nrow (ABTest %>%
filter (variant == "B" ))
conv_rate_B <- (conversions_B/visitors_B)
print (conv_rate_B)
|
Output:
0.02773925
0.05068493
3. Let’s compute the relative uplift using conversion rates A & B. The uplift is a percentage of the increase
R
uplift <- (conv_rate_B - conv_rate_A) / conv_rate_A * 100
uplift
|
Output:
82.72%
B is better than A by 83%. This is high enough to decide a winner.
4. Let’s compute the pooled probability, standard error, the margin of error, and difference in proportion (point estimate) for variants A & B
R
p_pool <- (conversions_A + conversions_B) / (visitors_A +
visitors_B)
print (p_pool)
SE_pool <- sqrt (p_pool * (1 - p_pool) * ((1 / visitors_A) +
(1 / visitors_B)))
print (SE_pool)
MOE <- SE_pool * qnorm (0.975)
print (MOE)
d_hat <- conv_rate_B - conv_rate_A
|
Output:
0.03928325
0.01020014
0.0199919
5. Let’s compute the z-score
R
z_score <- d_hat / SE_pool
print (z_score)
|
Output:
2.249546
6. Using this z-score, we can quickly determine the p-value via a look-up table, or using the code below:
R
p_value <- pnorm (q = -z_score,
mean = 0,
sd = 1) * 2
print (p_value)
|
Output:
0.02447777
7. Let’s compute the confidence interval for the pool
R
ci <- c (d_hat - MOE, d_hat + MOE)
ci
X_hat_A <- conversions_A / visitors_A
se_hat_A <- sqrt (X_hat_A * (1 - X_hat_A) / visitors_A)
ci_A <- c (X_hat_A - qnorm (0.975) * se_hat_A, X_hat_A
+ qnorm (0.975) * se_hat_A)
print (ci_A)
X_hat_B <- conversions_B / visitors_B
se_hat_B <- sqrt (X_hat_B * (1 - X_hat_B) / visitors_B)
ci_B <- c (X_hat_B - qnorm (0.975) * se_hat_B,
X_hat_B + qnorm (0.975) * se_hat_B)
print (ci_B)
|
Output:
0.002953777 0.042937584
0.01575201 0.03972649
0.03477269 0.06659717
8. Let’s visualize the results computed so far in a dataframe (table):
R
vis_result_pool <- data.frame (
metric = c (
'Estimated Difference' ,
'Relative Uplift(%)' ,
'pooled sample proportion' ,
'Standard Error of Difference' ,
'z_score' ,
'p-value' ,
'Margin of Error' ,
'CI-lower' ,
'CI-upper' ),
value = c (
conv_rate_B - conv_rate_A,
uplift,
p_pool,
SE_pool,
z_score,
p_value,
MOE,
ci_lower,
ci_upper
))
vis_result_pool
|
Output:
metric value
1 Estimated Difference 0.02294568
2 Relative Uplift(%) 82.71917808
3 pooled sample proportion 0.03928325
4 Standard Error of Difference 0.01020014
5 z_score 2.24954609
6 p-value 0.02447777
7 Margin of Error 0.01999190
8 CI-lower 0.00000000
9 CI-upper 0.04589136
Recommendation & Conclusions
- Variant A has 20 conversions and 721 hits whereas Variant B has 37 conversions and 730 hits.
- Relative uplift of 82.72% based on a variant A conversion rate is 2.77% and for B is 5.07%. Hence, variant B is better than A by 82.72%.
- For this analysis P-value computed was 0.02448. Hence, there is strong statistical significance in test results.
- From the above results that depict strong statistical significance. You should reject the null hypothesis and proceed with the launch.
- Therefore, Accept Variant B and you can roll it to the users for 100%.
If you want to know the full analysis and datasets details then please click on this Github link.
Limitations
It is one of the tools for conversion optimization and it’s not an independent solution and it’s not going to fix all the conversion issues of ours and it can’t fix the issues as you get with messy data and you need to perform more than just an A/B test to improve on conversions.
Similar Reads
How To Start Programming With R
R Programming Language is designed specifically for data analysis, visualization, and statistical modeling. Here, we'll walk through the basics of programming with R, from installation to writing our first lines of code, best practices, and much more. Table of Content 1. Installation2. Variables and
12 min read
Jobs related to R Programming
Strong open-source programming language R has grown to be a vital resource for statisticians, data scientists, and academics in a variety of fields. Its powerful features for data processing, statistical modeling, and visualization have created many R programming jobs for those who know how to use i
8 min read
Simulation Using R Programming
Simulation is a powerful technique in statistics and data analysis, used to model complex systems, understand random processes, and predict outcomes. In R, various packages and functions facilitate simulation studies. Introduction to Simulation in RSimulating scenarios is a powerful tool for making
5 min read
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This art
3 min read
R Programming for Data Science
R is an open-source programming language used statistical software and data analysis tools. It is an important tool for Data Science. It is highly popular and is the first choice of many statisticians and data scientists. R includes powerful tools for creating aesthetic and insightful visualizations
13 min read
Data Wrangling in R Programming - Working with Tibbles
R is a robust language used by Analysts, Data Scientists, and Business users to perform various tasks such as statistical analysis, visualizations, and developing statistical software in multiple fields.In R Programming Language Data Wrangling is a process of reimaging the raw data to a more structu
6 min read
Reading Files in R Programming
So far the operations using the R program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. So the two most common o
9 min read
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS. In this R Language tutorial, we will Learn R Programming La
15+ min read
Working with Databases in R Programming
Prerequisite: Database Connectivity with R Programming In R programming Language, a number of datasets are passed to the functions to visualize them using statistical computing. So, rather than creating datasets again and again in the console, we can pass those normalized datasets from relational da
4 min read
What is ODBC in R programming?
ODBC (Open Database Connectivity) in R programming is a standardized API that allows R to connect to and interact with a wide range of database management systems (DBMS). By using ODBC, R can perform database operations such as querying, updating, and retrieving data from various relational database
3 min read