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
# load the library
library(tidyverse)
# set up your own directory
setwd(“~egot_\\Projects\\ABTest”)
# Using read.csv base import function
ABTest <- read.csv("Website Results.csv",
header = TRUE)
# save in your own directory
save(ABTest, file = "~rda\\ABTest.rda")
2. Let’s filter conversions for variants A & B and compute their corresponding conversion rates
R
# Let's filter out conversions for variant_A
conversion_subset_A <- ABTest %>%
filter(variant == "A" & converted == "TRUE")
# Total Number of Conversions for variant_A
conversions_A <- nrow(conversion_subset_A)
# Number of Visitors for variant_A
visitors_A <- nrow(ABTest %>%
filter(variant == "A"))
# Conversion_rate_A
conv_rate_A <- (conversions_A/visitors_A)
print(conv_rate_A) # 0.02773925
# Let's take a subset of conversions for variant_B
conversion_subset_B <- ABTest %>%
filter(variant == "B" & converted == "TRUE")
# Number of Conversions for variant_B
conversions_B <- nrow(conversion_subset_B)
# Number of Visitors for variant_B
visitors_B <- nrow(ABTest %>%
filter(variant == "B"))
# Conversion_rate_B
conv_rate_B <- (conversions_B/visitors_B)
print(conv_rate_B) # 0.05068493
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 # 82.72%
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
# Pooled sample proportion for variants A & B
p_pool <- (conversions_A + conversions_B) / (visitors_A +
visitors_B)
print(p_pool) # 0.03928325
# Let's compute Standard error for variants A & B (SE_pool)
SE_pool <- sqrt(p_pool * (1 - p_pool) * ((1 / visitors_A) +
(1 / visitors_B)))
print(SE_pool) # 0.01020014
# Let's compute the margin of error for the pool
MOE <- SE_pool * qnorm(0.975)
print(MOE) # 0.0199919
# Point Estimate or Difference in proportion
d_hat <- conv_rate_B - conv_rate_A
Output:
0.03928325
0.01020014
0.0199919
5. Let’s compute the z-score
R
# Compute the Z-score so we
# can determine the p-value
z_score <- d_hat / SE_pool
print(z_score) # 2.249546
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
# Let's compute p_value
# using the z_score value
p_value <- pnorm(q = -z_score,
mean = 0,
sd = 1) * 2
print(p_value) # 0.02447777
Output:
0.02447777
7. Let’s compute the confidence interval for the pool
R
# Let's compute Confidence interval for the
# pool using pre-calculated results
ci <- c(d_hat - MOE, d_hat + MOE)
ci # 0.002953777 0.042937584
# Using same steps as already shown,
# let's compute the confidence
# interval for variants A separately
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) # 0.01575201 0.03972649
# Using same steps as already shown,
# let's compute the confidence
# interval for variants B separately
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) # 0.03477269 0.06659717
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
Hypothesis Testing in R Programming
A hypothesis is made by the researchers about the data collected for any experiment or data set. A hypothesis is an assumption made by the researchers that are not mandatory true. In simple words, a hypothesis is a decision taken by the researchers based on the data of the population collected. Hypo
6 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
Writing to Files in R Programming
R programming Language is one of the very powerful languages specially used for data analytics in various fields. Analysis of data means reading and writing data from various files like excel, CSV, text files, etc. Today we will be dealing with various ways of writing data to different types of file
2 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
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
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
Regression and its Types in R Programming
Regression analysis is a statistical tool to estimate the relationship between two or more variables. There is always one response variable and one or more predictor variables. Regression analysis is widely used to fit the data accordingly and further, predicting the data for forecasting. It helps b
5 min read